You wish to buy video games from the famous online video game store Mist.
Usually, all games are sold at the same price,p dollars. However, they are planning to have the seasonal Halloween Sale next month in which you can buy games at a cheaper price. Specifically, the first game will cost p dollars, and every subsequent game will cost d dollars less than the previous one. This continues until the cost becomes less than or equal to m dollars, after which every game will cost m dollars.
How many games can you buy during the Halloween Sale?
Example:
Input: p=20,d=3,m=6,s=80
Output: 6
Approach
C++
#include <bits/stdc++.h>using namespace std;int howManyGames(int p, int d, int m, int s){int sum = 0, cnt = 0;while (sum < s){sum += p;if (p - d > m)p = p - d;elsep = m;if (sum <= s)cnt++;}return cnt;}int main(){int p = 20, d = 3;int m = 6, s = 80;cout << howManyGames(p, d, m, s);return 0;}
No comments:
Post a Comment