CodeForces 716A Crazy Computer

时间:2023-03-09 05:44:55
CodeForces 716A Crazy Computer

题目链接:http://codeforces.com/problemset/problem/716/A

题目大意:

  输入 n c, 第二行 n 个整数,c表示时间间隔 秒。

  每个整数代表是第几秒。如果本次和下一秒间隔>c 则之前的计数次数清0,否则每次输入一个数,计数++;

  问最后剩几个数。

举例:

input:

6 5
1 3 8 14 19 20

output:

 3

解析:

1 3 8 此时剩 3 个数,因为每次输入距离下次输入时间间隔都<5。下一次是 14 距离上一次 间隔为14-8>5 ,计数清0。

下一次 19-14<5 保留,下一次 20-19<5 保留,故此时剩下 14 19 20 。所有输出 3.

解题思路:

  先输入一个数,然后for剩下的数,如果下一个数与前一个是查大于 c 则cut=0,否则cut++;最后输出 cut+1.为什么+1 读者自己思考,可参考举例解析。

AC Code:

 #include<bits/stdc++.h>
using namespace std;
int main()
{
int n,c;
while(scanf("%d%d",&n,&c)!=EOF)
{
int i;
int x,x1,cut=;
scanf("%d",&x);
for(i=; i<n; i++)
{
scanf("%d",&x1);
if((x1-x)>c)
cut=;
else cut++;
x=x1;
}
printf("%d\n",cut+);
}
return ;
}