码迷,mamicode.com
首页 > 其他好文 > 详细

ACM士兵买香蕉,问他需要向他的朋友借多少钱

时间:2015-07-17 21:02:10      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

Description

A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i·k dollars for the i-th banana).

He has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas?

Input

The first line contains three positive integers k, n, w (1  ≤  k, w  ≤  1000, 0 ≤ n ≤ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants.

Output

Output one integer — the amount of dollars that the soldier must borrow from his friend. If he doesn‘t have to borrow money, output 0.

Sample Input

Input

3 17 4

Output

13

解题思路:首先确定循环结束条件是士兵买完香蕉,给定一个w表示士兵要买的香蕉数,每循环一次w就减少一个,直到被减少到0时就结束循环。用s表示士兵要给的钱数,开始时给s赋值为0,每循环一次s就被更新一次,且每次s都在原来的基础上加上一个k*w.

最后将士兵所带的钱与士兵所要给出的做一个比较,如果士兵所带的钱是足够的,他就不需要向他的朋友借钱(即输出0);否则士兵就需要向他的朋友借钱,这时输出他需要向他朋友借的钱数。

 
 
程序代码:
#include <iostream>
using namespace std;
int main ()
{
      int k,n,w,s=0,x=0;
      cin>>k>>n>>w;
      while(w!=0)
      {
            s=s+k*w;
            w--;
      }
      if(s>n)
      {
            x=s-n;
          cout<<x<<endl;
      }
      else 
            cout<<x<<endl;
      
      return 0;
}

 

ACM士兵买香蕉,问他需要向他的朋友借多少钱

标签:

原文地址:http://www.cnblogs.com/xinxiangqing/p/4655500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!