zoukankan      html  css  js  c++  java
  • 2015 HUAS Provincial Select Contest #1~A

    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根香蕉,第一根k元,第二根2k元,以此类推,第i根i*k元,这个人手中有n元,则他买w根香蕉需要向朋友借多少元钱。解这个题目首先应注意的香蕉的总钱数在计算之前要赋值0,并且在计算完成后要判断总钱数与他自己所拥有的钱哪个比较大,再用大的减去小的得出结果。
    程序代码:
    #include<iostream> using namespace std; int main() { int k,n,w; cin>>k>>n>>w; int sum=0; for(int i=1;i<=w;i++) { sum=sum+i*k; } if(sum>n) cout<<sum-n<<endl; else cout<<"0"<<endl; return 0; }
  • 相关阅读:
    编码
    浏览器翻页
    验证码识别
    时间
    phantomjs配置
    产品
    java范型的理解
    使用JDBC连接数据库
    垃圾回收机制
    java的内存区域 && java内存模型
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4654858.html
Copyright © 2011-2022 走看看