您的位置:首页 > 其它

CodeForces 546A A Contest #1(A. Soldier and Bananas)

2015-07-17 20:57 281 查看
Asoldierwantstobuywbananasintheshop.Hehastopaykdollarsforthefirstbanana,2kdollarsforthesecondoneandsoon(inotherwords,hehastopayi·kdollarsforthei-thbanana).Hehasndollars.Howmanydollarsdoeshehavetoborrowfromhisfriendsoldiertobuywbananas?InputThefirstlinecontainsthreepositiveintegersk, n, w(1  ≤  k, w  ≤  1000,0 ≤ n ≤ 109),thecostofthefirstbanana,initialnumberofdollarsthesoldierhasandnumberofbananashewants.OutputOutputoneinteger—theamountofdollarsthatthesoldiermustborrowfromhisfriend.Ifhedoesn'thavetoborrowmoney,output0.SampleInputInput3174Output13这个题目是意思是有ndollar,thefirstbananacostkdollars,第i个就需costk*i,问买w个bananas是否需要借钱;借钱需要多少?分析:首先计算w个bananas需要多少money,在与n比较。#include<iostream>#include<cstdio>usingnamespacestd;intmain(){intk,n,w,b;inti;while(scanf("%d%d%d",&k,&n,&w)==3){intsum=0;for(i=1;i<=w;i++)sum+=i*k;//计算w个bananas所需的money
 b=sum-n;//比较总需费与n的大小,最后输出b的结果
if(b==0||b<0)
printf("0\n");
else
printf("%d\n",b);
}return0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: