A. Optimal Currency Exchange
Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has nn rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is dd rubles, and one euro costs ee rubles.
Recall that there exist the following dollar bills: 11, 22, 55, 1010, 2020, 5050, 100100, and the following euro bills — 55, 1010, 2020, 5050, 100100, 200200 (note that, in this problem we do not consider the 500500 euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange.
Help him — write a program that given integers nn, ee and dd, finds the minimum number of rubles Andrew can get after buying dollar and euro bills.
The first line of the input contains one integer nn (1≤n≤1081≤n≤108) — the initial sum in rubles Andrew has.
The second line of the input contains one integer dd (30≤d≤10030≤d≤100) — the price of one dollar in rubles.
The third line of the input contains integer ee (30≤e≤10030≤e≤100) — the price of one euro in rubles.
Output one integer — the minimum number of rubles Andrew can have after buying dollar and euro bills optimally.
100 60 70
40
410 55 70
5
600 60 70
0
In the first example, we can buy just 11 dollar because there is no 11 euro bill.
In the second example, optimal exchange is to buy 55 euro and 11 dollar.
In the third example, optimal exchange is to buy 1010 dollars in one bill.
题意:用卢比换美元和欧元,要使剩下的卢比数量最少
题解:给定美元和欧元的汇率分别是 d 和 e ,d是1的倍数,e是5的倍数,枚举x( x<=n ),输出min(ans,x%d+(n-x)%e)即可
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int n,d,e,ans=99999999; cin>>n>>d>>e; for(int i=0;i<=n;i++) ans=min(ans,i%d+(n-i)%e); cout<<ans<<endl; return 0; }