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方案不仅涵盖了从本地高可用性到异地容灾的全面保护,还通过其独特的技术和工具,如VMware Converter和Snapmirror技术,大幅提升了容灾效率和数据安全性。对于希望构建稳健、高效容灾策略的企业...
网络资源分享,可以获取hasp hl以及最新的srm加密狗数据,懂的人可以下载使用!
SRM的工作流程包括将LUNs分组,如LUN Group 1、LUN Group 2和LUN Group 3,它们分别承载着不同应用,如VMFS1、VMFS2和VMFS3。这些应用可以是Exchange、SQL Server、SAP等关键业务系统。通过创建不同的恢复计划,例如...
开关磁阻电机仿真程序,基于ARM的开关磁阻电机的仿真模型
此PPTX文件详细介绍了G-SRM的实施过程和关键业务流程,旨在帮助参与者理解并应用G-SRM策略。 首先,【方案概览】部分概述了G-SRM全球模板,它涵盖了供应商的考核与绩效、规划与策略、寻源与价格、供货比例、供应商...
标题 "Unable to access HASP SRM Run-time Environment? (H0033)" 提到的问题是用户在尝试运行一个依赖于HASP SRM(Sentinel Runtime Environment)的应用程序时遇到了错误代码H0033。这个错误通常表示系统无法正确...
【G-SRM-SP-布局与策略】是一个与企业供应链管理(SCM)相关的主题,尤其是聚焦于供应商关系管理(Supplier Relationship Management, SRM)的布局与策略方面。SRM是企业管理与其供应商之间关系的一种方法,旨在提高...
根据给定的文件信息,我们将深入探讨“SRM-MDM Catalog Setup – Ready Reference”这一主题,专注于SAP NetWeaver MDM系统中的SRM-MDM目录设置过程。这份文档不仅适用于SAP SRM(Supplier Relationship Management...
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.
1. **vCenter服务器**:作为VMware环境的核心管理平台,vCenter负责集中管理和监控虚拟机及相关资源。 2. **SRM服务器**:运行SRM软件的物理或虚拟服务器,操作系统需为Server 2008 x64。 3. **支持SRM的数据库**:...
本指南旨在为MDM/SRM顾问在配置SAP SRM(包括MDM-SRM WebDynpro配置UI和SRM-EBP)以及MDM系统时提供参考、启动文档或故障排除帮助。文章主要内容通过实践操作和实例展示,使读者能够更好地理解配置过程中的关键步骤...
SRM2Multi dumper for hsap
组织可以采购所有类型的产品,如直接和间接材料,服务,这可以与 SAP ERP 模块和其他非 SAP 后端系统集成,用于会计和计划。 SAP SRM 允许您优化采购流程,与供应商有效合作,获得长期利益,并执行预测,采购周期...
然而,SRM的双凸极结构和开关式控制方式使得其电磁特性高度非线性和强耦合性,导致转矩脉动的问题。传统的控制方法基于转矩分配函数(TSF),但该方法存在一些缺陷,例如参数难以获得,数学关系复杂等。 本文提出的...
本篇文档是一份由OMRON公司生产的CPM1/CPM1A/CPM2A/CPM2C/SRM1(-V2)系列可编程逻辑控制器(PLC)的编程手册。OMRON是一个知名的日本工业自动化控制设备制造商,其产品广泛应用于各种自动化系统和生产线上。这本手册...
标题中的“4phase-86srm.zip”指的是一个压缩包文件,包含了关于四相86式开关磁阻电机(Switched Reluctance Motor, SRM)的相关模型和可能的模拟数据。SRM是一种特殊的电动机类型,其工作原理基于电磁感应,通过...