hdu Hat's Tea

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1288

去买茶,需要正好的钱才行,另外花的钱的个数最多  其实是一个简单的贪心问题,小的多取一点,多的少取一点。反着想,算出超出部分数量。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <ctype.h>
 7 #include <iomanip>
 8 #include <queue>
 9 #include <map>
10 #include <stdlib.h>
11 using namespace std;
12 
13 int main()
14 {
15     int i,j,k,t,m,x,y,z,n;
16     while(cin>>m>>x>>y>>z && m+x+y+z){
17         int sum=0;
18         sum=x+y*5+z*10;
19         n=m;
20         if(sum<m)
21             cout<<"Hat cannot buy tea."<<endl;
22         else{
23             m=sum-m;    //反着计算
24             int z1=m/10;
25             if(z1<z)
26                 m=m-z1*10,z=z-z1;
27             else
28                 m=m-z*10,z=0;
29             int y1=m/5;
30             if(y1<y)
31                 m=m-y1*5,y=y-y1;
32             else
33                 m=m-y*5,y=0;
34             int x1=m;
35             if(x1<x)
36                 m=m-x1,x=x-x1;
37             else
38                 m=m-x,x=0;
39             if(m!=0)
40                 printf("Hat cannot buy tea.
");
41             else
42                 printf("%d YiJiao, %d WuJiao, and %d ShiJiao
",x,y,z);
43         }
44     }
45 }
原文地址:https://www.cnblogs.com/wangmengmeng/p/4876954.html