`
zhubin215130
  • 浏览: 140002 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • JAVA
 
阅读更多
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));
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics