“胡萝卜的梦”----SRM 401 DIV 2 250points 题目和自己的答案

Problem Statement
John works at a company called "FIELD-Tech", and today, he was so tired after work that he fell asleep as soon as he got home. Unfortunately, even in his sleep, he was unable to forget about his work. In one dream, he was asked to help a carrot producing company deal with the following question: how many carrots grow on a line segment connecting two given carrots? The endpoints of the segment (i.e., the two given carrots) should not be included. It's a rather strange question, and to make it even stranger, the company's representatives (guys who have carrots instead of heads) said that all the carrots grow on an infinite plane, and there is exactly one carrot at each point with integer coordinates. You must help tired John deal with this problem. The coordinates of the two carrots are (x1, y1) and (x2, y2). Return the number of carrots that lie strictly on the line segment connecting these carrots.

Definition
Class: DreamingAboutCarrots
Method: carrotsBetweenCarrots
Parameters: int, int, int, int
Returns: int
Method signature: int carrotsBetweenCarrots(int x1, int y1, int x2, int y2) (be sure your method is public)

Constraints
- x1, y1, x2, and y2 will each be between 0 and 50, inclusive.
- (x1, y1) and (x2, y2) will represent different points.



public class DreamingAboutCarrots {

    public double tempX;
    public double tempY;
    public int totalNumber;

    public double funcTrans(double x1, double y1, double x2, double y2,
            double tempX) {

        double result = y1 + ((y2 - y1) / (x2 - x1)) * (tempX - x1);

        return result;
    }

    public int carrotsBetweenCarrots(int x1, int y1, int x2, int y2) {

        if (x1 < x2) {

            tempX = x1 + 1;
            while (tempX < x2) {
                tempY = funcTrans(x1, y1, x2, y2, tempX);
                if ((tempY == Math.ceil(tempY)) || (tempY == Math.floor(tempY))) {
                    totalNumber++;
                    System.out.println("(" + tempX + "," + tempY + ")");
                } else {
                }
                tempX++;
            }
        }

        else if (x1 > x2) {
            tempX = x2 + 1;
            if (tempX == x2) {
                totalNumber--;
            }
            while (tempX < x1) {
                tempY = funcTrans(x1, y1, x2, y2, tempX);
                if ((tempY == Math.ceil(tempY)) || (tempY == Math.floor(tempY))) {
                    totalNumber++;
                    System.out.println("(" + tempX + "," + tempY + ")");
                } else {
                }
                tempX++;
            }
        }

        else {
            tempX = x1;
            if (y1 < y2) {
                tempY = y1 + 1;
                while (tempY < y2) {
                    totalNumber++;
                    System.out.println("(" + tempX + "," + tempY + ")");
                    tempY++;
                }
            } else if (y1 > y2) {
                tempY = y2 + 1;
                while (tempY < y1) {
                    totalNumber++;                   
                    System.out.println("(" + tempX + "," + tempY + ")");
                    tempY++;
                }
            } else {
                totalNumber = 0;
            }
        }

        return totalNumber;

    }

    public static void main(String[] args) {
        DreamingAboutCarrots temp = new DreamingAboutCarrots();
        System.out.println("Total Number is :"
                + temp.carrotsBetweenCarrots(0, 0, 42, 36));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值