hdu1288

来源:互联网 发布:80w八实万是网络传销吗 编辑:程序博客网 时间:2024/05/18 00:34

Hat's Tea

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1887    Accepted Submission(s): 416


Problem Description
Hat is a member of PG Studio. Hat codes a lot and so he often buys tea at tea vending machine. But the tea vending machine just eat coins and spit out tea, if you feed the machine more coins than the tea’s price and the machine will not spit out your change.
Your program will be given numbers and types of coins Hat has and the tea price. The tea vending machine accepts coins of values 1, 5, 10 RMB (Jiao). The program should output which coins Hat has to use paying the tea so that he uses as many coins as possible.
 

Input
Each line of the input contains four integer numbers separated by a single space describing one situation to solve. The first integer on the line N, , is the tea price in Jiao. Next four integers , , are the numbers of YiJiao (1 Jiao.RMB), WuJiao (5 Jiao.RMB), and ShiJiao (10 Jiao.RMB) in Hat's valet. The last line of the input contains four zeros and no output should be generated for it.
 

Output
For each situation, your program should output one line containing the string " T1 YiJiao, T2 WuJiao, and T3 ShiJiao ", where T1, T2, T3 are the numbers of coins of appropriate values Hat should use to pay the tea while using as many coins as possible. If Hat does not have enough coins to pay the tea exactly, your program should output "Hat cannot buy tea.”.
 

Sample Input
6653 226 72 352 578 5 127 9510 0 0 0
 

Sample Output
Hat cannot buy tea.3 YiJiao, 115 WuJiao, and 0 ShiJiao
 题目大意:
用1角,五角,十角的硬币买茶叶,要求拼凑的钱正好等于茶叶的价钱,尽可能的多用硬币。
解题思路:
1,先判断总钱数是否够买茶叶,不够直接pass。
2,用总钱数n取余5,a1=n%5,判断最少用的一角的个数,如果一角的个数小于a1,不符合 题意,pass;
3,用两层for循环,判断用十角和五角的具体个数,详见代码:
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int flag;
int cnt_i,cnt_j;
int n,a,b,c;
void eg(int a,int b,int c,int n)
{
    for(int i=0;i<=c;i++)
    {
        for(int j=0;j<=b;j++)
        {
            int t=n-i*10-j*5;
            if(t<0)
                return ;
            else if(t<=a)
            {
                cnt_i=i;
                cnt_j=j;
                flag=1;
                return ;
            }
        }
    }
}
int main()
{

    while(scanf("%d%d%d%d",&n,&a,&b,&c))
    {
        if(a==0&&b==0&&c==0&&n==0)
        break;
        if(n>a+b*5+c*10)
        {
            printf("Hat cannot buy tea.\n");
            continue;
        }
        if(a>=n)
        {
            // printf("1");
            printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",n,0,0);

            continue;
        }
        if(a<n%5)
        {
            printf("Hat cannot buy tea.\n");
            continue;
        }
         flag=0;
         eg(a,b,c,n);
         if(flag)
         {
             //printf("2");
              printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",n-cnt_i*10-cnt_j*5,cnt_j,cnt_i);
         }
         else
            printf("Hat cannot buy tea.\n");
    }
    return 0;
}

原创粉丝点击