关注
import java.util.Scanner;
public class Main {
/**
* 蚂蚱跳跃次数
*
* @param desPoint
* @return
*/
public static int jumpCount(int desPoint) {
int currentJumpPosition = 0;// 当前跳跃的位置
int jumpStep = 0;// 跳跃步进
int jumpCount = 0;// 跳跃次数
while (currentJumpPosition != desPoint) {// 循环,往一个方向跳跃
if (desPoint > 0)
jumpStep += 1; // 正方向
else {
jumpStep += -1;// 反方向
}
jumpCount++;
currentJumpPosition += jumpStep;// 加上步长
// 超过了
if ((desPoint > 0 && currentJumpPosition >
desPoint)
|| (currentJumpPosition < desPoint && desPoint
< 0)) {
jumpCount--;// 减一
currentJumpPosition -= jumpStep;// 减去步进
jumpStep -= 1;// 减一
boolean isForward = false;// 往后
if (desPoint < 0) {
isForward = true; // 往前
}
while (currentJumpPosition != desPoint) {// 来回跳跃,直到达到终点
jumpCount++;
if (isForward) {// 往前跳
if (jumpStep > 0) {
jumpStep = jumpStep + 1;
} else {
jumpStep = -jumpStep + 1;
}
} else { // 往后跳
if (jumpStep > 0) {
jumpStep = -jumpStep - 1;
} else {
jumpStep = jumpStep - 1;
}
}
isForward = !isForward;// 取反
currentJumpPosition += jumpStep;// 加上步长
}
}
}
return jumpCount;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int desPoint = scanner.nextInt();// 目标位置
System.out.println(jumpCount(desPoint));
}
}
}
AC 17%
查看原帖
点赞 2
相关推荐
点赞 评论 收藏
分享
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
等闲_:感觉咱们双非的同学都有一个共性,想证明双非也是能进大厂的,我之前所有的标签都喜欢带着双非,仿佛这样可以像别人证明自己的实力,现在我却不再想证明双非到底能不能进大厂,我的生活的所有者是我自己,享受生活,接受结果 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 春招什么时候投? #
5100次浏览 76人参与
# 春节提前走,你用什么理由请假? #
4952次浏览 107人参与
# 春节前,你还在投简历吗? #
7181次浏览 94人参与
# 实习到现在,你最困惑的一个问题 #
1908次浏览 56人参与
# 牛客AI体验站 #
13352次浏览 253人参与
# 牛友的春节生活 #
1659次浏览 56人参与
# 备战春招/暑实,现在应该做什么? #
1662次浏览 53人参与
# 从夯到拉,锐评职场mentor #
1559次浏览 31人参与
# 聊聊Agent开发 #
14458次浏览 363人参与
# 距离春招还有一个月,你现在是什么开局? #
2930次浏览 53人参与
# 推荐一个值得做的AI项目 #
3938次浏览 125人参与
# 暑期实习什么时候投? #
3579次浏览 84人参与
# 实习想申请秋招offer,能不能argue薪资 #
218565次浏览 1171人参与
# 腾讯工作体验 #
566922次浏览 3700人参与
# 哪些瞬间让你真切感受到了工作的乐趣 #
24443次浏览 105人参与
# 通信硬件2024笔试面试经验 #
269161次浏览 2053人参与
# 实习必须要去大厂吗? #
188502次浏览 1767人参与
# 正在春招的你,也参与了去年秋招吗? #
349915次浏览 2590人参与
# 双非本科的出路是什么? #
208704次浏览 1566人参与
# 最难的技术面是哪家公司? #
65407次浏览 971人参与
