PAT基础编程题目-7-17 爬动的蠕虫
PAT基础编程题目-7-17 爬动的蠕虫
题目详情
解答
C语言版
#include<stdio.h>
int main() {
int n, u, d, time = 0, position = 0;
scanf("%d %d %d", &n, &u, &d);
while (1)
{
if (n - position > u) // 剩下的距离需要超过一分钟的时间爬
{
// time++;
// position += u;
// time++;
// position -= d;
time += 2;
position = position + u - d;
}
else {
// 剩下的距离不足一分钟爬
time++;
break;
}
}
printf("%d", time);
return 0;
}
C++版
#include<iostream>
using namespace std;
int main() {
int n, u, d, time = 0, position = 0;
cin >> n >> u >> d;
while (1)
{
if (n - position > u) // 剩下的距离需要超过一分钟的时间爬
{
// time++;
// position += u;
// time++;
// position -= d;
time += 2;
position = position + u - d;
}
else {
// 剩下的距离不足一分钟爬
time++;
break;
}
}
cout << time;
return 0;
}
Java版
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int n = 0, u = 0, d = 0, time = 0, position = 0;
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext()) {
n = scanner.nextInt();
u = scanner.nextInt();
d = scanner.nextInt();
}
scanner.close();
while (true)
{
if (n - position > u) // 剩下的距离需要超过一分钟的时间爬
{
// time++;
// position += u;
// time++;
// position -= d;
time += 2;
position = position + u - d;
}
else {
// 剩下的距离不足一分钟爬
time++;
break;
}
}
System.out.println(time);
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!