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));
}
}
分享到:
相关推荐
Driver HASP SRM emulator (x86)
Control of SRM Motor using Fuzzy logic
不错的VMware SRM资料,可以看看
开关磁阻电机仿真程序,基于ARM的开关磁阻电机的仿真模型
VMworld 2009 - TA17:End-to-End Disaster Recovery with Automated SRM Failback
G-SRM-SP-布局与策略.pptx
G-SRM-SLM-生命周 期-01培训手册.pptx G-SRM-SP-供应商考核.pptx G-SRM-SP-布局与策略.pptx G-SRM-SP-综合绩效.pptx G-SRM-SR-供货比例-01培训手册.pptx G-SRM-SR-合同管理-01培训手册_.ppt G-SRM系统供应商绩效及...
topcoder的数学类算法题目。一个整数被称为k-smooth当且仅当它的最大素因子不大于k,给定N和K,计算出1 - N中有多少个整数是k-smooth。1 , 1 <= K <= 1000.
G-SRM-SP-布局与策略.zip
omron系列CPM1/CPM1A/CPM2A/CPM2C/SRM1(-V2) PLC编程手册pdf,omron系列CPM1/CPM1A/CPM2A/CPM2C/SRM1(-V2) PLC编程手册
SRM2Multi dumper for hsap
ta_1lab_theory-of-algorithms:2 semestr srm 1实验室
HASP按照文件解决H0033错误,Unable to access HASP SRM Run-time Environment? (H0033)
ERP项目实施方法论--调研阶段--SRM调研提纲
exam srm sample questions These questions and solutions are representative of the types of questions that might be asked of candidates sitting for Exam SRM. These questions are intended to represent ...
Hasp SRM 32bit usb virtual driver source code.
组织可以采购所有类型的产品,如直接和间接材料,服务,这可以与 SAP ERP 模块和其他非 SAP 后端系统集成,用于会计和计划。 SAP SRM 允许您优化采购流程,与供应商有效合作,获得长期利益,并执行预测,采购周期...
云计算 虚拟化 VMware SRM 安全相关 技术文档 参考手册 官方文档
SAP-SRM系统概览介绍
VMware SRM 系统管理员手册 技术手册 云计算 虚拟化必备