CodeForces 546A Soldier and Bananas

来源:互联网 发布:广联达软件配置要求 编辑:程序博客网 时间:2024/05/17 07:31

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

实现代码

#include<iostream>using namespace std;#define num 1010int doll[num];int main(){    int k,w;    long long n;    int sum=0;    cin>>k>>n>>w;    for(int i=1;i<=w;i++)        doll[i]=i*k;    for(int i=1;i<=w;i++)        sum=sum+doll[i];    if(sum<=n)        cout<<"0"<<endl;    if(sum>n)        cout<<sum-n<<endl;    return 0;}

题解:
一个soldier去商店买香蕉
k:香蕉初始单价
n:最初的钱
w:购买香蕉的数量
sum:购买w香蕉所需的总费用
注:香蕉的单价为k*i (i=1;i++)

第一个for求出不同购买数量的单价
第二个for求出购买的费用
即可得到所需要向朋友借的钱

0 0