1. 程式人生 > >找錢(貪心,比較簡單)

找錢(貪心,比較簡單)

找錢

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 55   Accepted Submission(s) : 24

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

港港和直接是一對好朋(ji)友,他們經常一起購物。
(金額有1,5,10,20,50,100這幾種)。

有一天,他們去了超市買了一些東西,一共要支付x元(1<=x<=1000),他們付給收銀員y元(x<=y<=2000)。

因為港港和直接都很懶,他們希望收銀員找他們的錢張數最少,請問收銀員最少找給他們幾張錢?

Input

輸入有多組測試用例,每組測試用例輸入兩個整數x,y,(1<=x<=1000),(x<=y<=2000),分別表示要支付的金額和實際付給收銀員的金額

Output

對於每組測試用例,輸出一個數字,表示收銀員最少需要找他們幾張錢。

Sample Input

23 50

Sample Output

4

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[6]={100,50,20,10,5,1};
 4 int main()
 5 {
 6
int x,y; 7 while(~scanf("%d %d",&x,&y)) 8 { 9 int temp=y-x; 10 int ans=0; 11 for(int i=0;i<6;i++) 12 { 13 if(temp>=a[i]) 14 { 15 int te=temp/a[i]; 16 ans+=te; 17 temp=temp-te*a[i];
18 } 19 } 20 printf("%d\n",ans); 21 } 22 return 0; 23 }