当前位置: 代码迷 >> 综合 >> Soldier and Bananas
  详细解决方案

Soldier and Bananas

热度:23   发布时间:2023-11-22 14:50:23.0

文章目录

  • 一、Soldier and Bananas
  • 总结


一、Soldier and Bananas

本题链接:Soldier and Bananas

题目
A. Soldier and Bananas
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples

input
3 17 4

output
13

本博客给出本题截图

在这里插入图片描述

题意:买第i根香蕉需要支付i * k的价格,(k为单价)问买w根香蕉需要跟别人借多少钱,注意不是必须借钱,自己的钱足够的情况下不需要借钱。

AC代码

#include <iostream>using namespace std;int main()
{
    int k, n, w;cin >> k >> n >> w;int cnt = 0;for (int i = 1; i <= w; i ++ )cnt += k * i;if (cnt > n) cout << cnt - n << endl;else cout << 0 << endl;return 0;
}

总结

integer 整数
initial 最初的
amount 数量

水题,不解释