C - Turn for MEGA URAL - 1787

题目
A traffic light at the turn for the “MEGA” shopping center from the Novomoskovskiy highway works in such a way that k cars are able to take a turn in one minute. At weekends all the residents of the city drive to the mall to take a shopping, which results in a huge traffic jam at the turn. Administration of the mall ordered to install a camera at the nearby bridge, which is able to calculate the number of cars approaching this turn from the city. The observation started n minutes ago. You should use the data from the camera to determine the number of cars currently standing in the traffic jam.
Input
The first line contains integers k and n (1 ≤ k, n ≤ 100), which are the number of cars that can take a turn to “MEGA” in one minute and the number of minutes passed from the beginning of observation. The second line contains space-separated integers a1, …, an (0 ≤ ai ≤ 100), where ai is the number of cars that approached the turn during the i-th minute. The observation started at morning, when there were no cars at the turn.
Output
Output the number of cars currently standing in the traffic jam.
Example
input output
5 3
6 7 2
0
5 3
20 0 0
5
正确AC

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int k,n;
	cin>>k>>n;
	int i;
	int a[500]; 
	for(i=0;i<n;i++)
	{
	cin>>a[i];
	} 
	int sum=0;
	for(i=0;i<n;i++)
	{
		sum=sum+a[i]-k;
		if(sum<=0) sum=0;
	}
	cout<<sum;
	return 0;
} 

我的WA

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
 int n,m;
 cin>>n>>m;
 int i;
 int a[500];
 for(i=0;i<m;i++)
 {
 	cin>>a[i];
 }
 int sum=0;
 for(i=0;i<m;i++)
 {
 	sum=sum+a[i];
 }
 int flag;
if(sum-m*n<=0)
flag=0;
 else
 flag=sum-m*n;
 
 cout<<flag<<endl;

	return 0;
}

没有考虑到
k=5 n=3
3 7 5
这种有顺序的,不能全都sum加起来,得一个一个判断!!!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值