“胡萝卜的梦”----SRM 401 DIV 2 250points 题目和自己的答案
来源:互联网 发布:天刀陆小凤捏脸数据 编辑:程序博客网 时间:2024/04/29 20:37
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));
}
}
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));
}
}
- “胡萝卜的梦”----SRM 401 DIV 2 250points 题目和自己的答案
- SRM 697 Div 2: 250分题目和500分题目代码
- 【汇总】贴一下TopCoder SRM 303 DIV I 500分题的题目和...
- TopCoder 250 points 4-SRM 145 DIV 2 128.72/250 51.49%
- TopCoder 250 points 6-SRM 146 DIV 2 162.67/250 65.07%
- TopCoder 250 points 8-SRM 147 DIV 2 176.36/250 70.54%
- TopCoder 250 points 10-SRM 148 DIV 2 167.20/250 66.88%
- TopCoder 250 points 12-SRM 149 DIV 2 103.92/250 41.57%
- TopCoder 250 points 14-SRM 150 DIV 2 86.25/250 34.50%
- TopCoder 250 points 16-SRM 151 DIV 2 97.63/250 39.05%
- TopCoder 250 points 18-SRM 152 DIV 2 167.05/250 66.82%
- TopCoder 250 points 20-SRM 153 DIV 2 216.58/250 86.63%
- TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%
- TopCoder 300 points 24-SRM 155 DIV 2 75/250 30%
- SRM 397 DIV 2 [250]
- SRM 145 DIV 2 250
- 胡萝卜的故事 开篇
- TopCoder 250 points 3-SRM 145 DIV 1 84.71/250 33.88%
- Android:sqllite使用
- dojo异步编程之dojo/promise/all模块(dojo/DeferredList替代者)
- Android接受邮件
- 从内核开始分析完成端口模型
- spring scala
- “胡萝卜的梦”----SRM 401 DIV 2 250points 题目和自己的答案
- Rightmost Digit
- npKq2OaScript.rc(208): error RC2135: file not found: npKq2OaScript.tlb
- xhtml的相关内容
- Linux下Apache 2.2, MySQL 5.0的安装与配置
- Ubuntu下设置root帐号的新密码
- #Sam有话说#产品寄托情感
- Linux系统中rar、unrar命令安装和使用详解
- Link Aggregation and LACP with Open vSwitch