CodeForces 546A-Soldier and Bananas

题意:

  有n dollar,the first banana cost  k dollars,第i个就需cost k*i,问买w个bananas是否需要借钱;借钱需要多少?

分析:首先计算w个bananas需要多少money,在与n比较。



代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <fstream>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <cstdlib>
 8 #include <algorithm>
 9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16 
17 using namespace std;
18 
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24 
25 int main()
26 {
27     int k, w;
28     long long n;
29 
30     while(scanf("%d%lld%d", &k, &n, &w)==3)
31     {
32         int i;
33         LL tot = 0;
34         for(i = 1; i <= w; i++ )
35             tot += i*k; //计算w个bananas所需的money
36         if(n >= tot)
37             printf("0
");
38         else
39             printf("%lld
", tot - n);  //比较总需费与n的大小,最后输出它们的差值
40     }
41 
42     return 0;
43 }


 



 
 
原文地址:https://www.cnblogs.com/xl1164191281/p/4655660.html