7-46 爬动的蠕虫 (15分)

7-46 爬动的蠕虫 (15分)

#include<cstdio>
int main()
{
	int n,u,d;
	int sum=0;
	int t=0;
	scanf("%d%d%d",&n,&u,&d);
	while(1)
	{
		sum=sum+u;
		t++;
		if(sum>=n)break;
		else{
			sum-=d;
			t++;
		}
	}
	printf("t=%d\n",t);
	return 0;
} 

思路:

首先输入三个数字,利用for循环,然后进行判断,判断所行距离是否正好或者大于井长,判断一次时间自增一次,如果次数达到了2的倍数,需要减小D米,最后判断结束输出结果即可。

#include<cstdio>
int main()
{
	int n,u,d;
	int sum=0;
	int t=0;
	scanf("%d%d%d",&n,&u,&d);
	while(sum<n)
	{
		t++;
		if(t%2==1)
		{
			sum+=u;
		}
		else{
			sum-=d;
		}
	}
	printf("t=%d\n",t);
	return 0;
} 
发布了61 篇原创文章 · 获赞 0 · 访问量 557

猜你喜欢

转载自blog.csdn.net/qq_38054511/article/details/104082691