HDU3348(贪心求硬币数

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int
num[6],sum[6];
int
mi(int num[],int a[],int count1)
{

    int
sum=0;
    for
(int i=5;i>1;i--)
    {

        if
(count1>a[i]*num[i])
        {

            sum=sum+num[i];
            count1=count1-a[i]*num[i];
        }

        else

        {

            int
temp=count1/a[i];
            sum=sum+temp;
            count1=count1-temp*a[i];
        }
    }

    if
(count1>num[1])
        return
-1;
    else
        return
sum+count1;
}

int
ma(int sum[],int num[],int a[],int count1)
{

    int
sum1=0;
    for
(int i=5;i>1;i--)
    {

        if
(count1>sum[i-1])
        {

            int
t=(count1-sum[i-1])/a[i];
            if
((count1-sum[i-1])%a[i]>0)
                t++;
            sum1=sum1+t;
            count1=count1-t*a[i];
        }

}
    if
(count1>num[1])
        return
-1;
    else
        return
sum1+count1;
}

void
pd(int sum[],int num[],int a[],int count1)
{

    int
tmin=mi(num,a,count1);
    if
(tmin==-1)
        printf("-1 -1\n");
    else

    {

        int
tmax=ma(sum,num,a,count1);
        if
(tmax==-1)
            printf("-1 -1\n");
        else

            printf("%d %d\n",tmin,tmax);
    }
}

int
main()
{

    int
t;
  int
  a[6]={0,1,5,10,50,100};
    scanf("%d",&t);
    while
(t--)
    {

int count1;
        scanf("%d",&count1);
        for
(int i=1;i<=5;i++)
        {

            scanf("%d",&num[i]);
        }

        for
(int i=1;i<=5;i++)
            sum[i]=sum[i-1]+a[i]*num[i];

if(sum[5]<count1)
            printf("-1 -1\n");
        else

            pd(sum,num,a,count1);
    }

    return
0;
}

coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1100    Accepted Submission(s): 316

Problem Description

"Yakexi, this is the best age!" Dong MW works hard and get high pay, he has many 1 Jiao and 5 Jiao banknotes(纸币), some day he went to a bank and changes part of his money into 1 Yuan, 5 Yuan, 10 Yuan.(1 Yuan = 10 Jiao)
"Thanks to the best age, I can buy many things!" Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn‘t like to get the change, that is, he will give the bookseller exactly P Jiao.

Input

T(T<=100) in the first line, indicating the case number.
T lines with 6 integers each:
P a1 a5 a10 a50 a100
ai means number of i-Jiao banknotes.
All integers are smaller than 1000000.

Output

Two integers A,B for each case, A is the fewest number of banknotes to buy the book exactly, and B is the largest number to buy exactly.If Dong MW can‘t buy the book with no change, output "-1 -1".

Sample Input

3 33 6 6 6 6 6 10 10 10 10 10 10 11 0 1 20 20 20

Sample Output

6 9 1 10 -1 -1

Author

madfrog

代码

时间: 2024-10-23 11:08:48

HDU3348(贪心求硬币数的相关文章

算法笔记——硬币找零之最少硬币数

题目来源:NYOJ995 问题描述: 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从银行拿回最少的硬币数,并保证能用这些硬币发工资. 我们应该注意到,人民币的硬币系统是 100,50,20,10,5,2,1,0.5,0.2,0.1,0.05, 0.02,0.01 元,采用这些硬币我们可以对任何一个工资数用贪心算法求出其最少硬币数. 但不幸的是: 我们可能没有这样一种好的硬币系统, 因此用贪心算法不能求出最少的硬币数,甚至有些金钱总数还

POJ2407---Relatives(求单个数的欧拉函数)

Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are several

【贪心】取数游戏

问题 N: [贪心]取数游戏 时间限制: 1 Sec  内存限制: 64 MB提交: 51  解决: 31[提交][状态][讨论版] 题目描述 给出2n(n≤100)个自然数(小于等于30000).将这2n个自然数排成一列,游戏双方A和B从中取数,只允许从两端取数.A先取,然后双方轮流取数.取完时,谁取得数字总和最大为取胜方:若双方和相等,属B胜.试问A方是否有必胜策略? 输入 共2行,第1行一个整数n:第2行有2*n个自然数. 输出 只有1行,若A有必胜策略,则输出“YES”,否则输出“NO”

ZOJ3574(归并排序求逆数对)

Under Attack II Time Limit: 5 Seconds      Memory Limit: 65536 KB Because of the sucessfully calculation in Under Attack I, Doctor is awarded with Courage Cross and promoted to lieutenant. But the war seems to end in never, now Doctor has a new order

求两数最小公倍数

输入代码: /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:sum123.cpp * 作 者:林海云 * 完成日期:2015年1月10日 * 版 本 号:v2.0 * * 问题描述:求两数的最小公倍数 * 输入描述:两个整数 * 程序输出:最小公倍数 */ #include<iostream> using namespace std; int gcd(int x,int y); int gad(int x,int

两种方法求丑数

我们把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 方法1 : 暴力破解,逐个判断 代码: <pre name="code" class="cpp">#include <iostream> #include <vector> using namespace std; //判断是否是丑数 bool isUgly(int index){ while(index % 2

求Fibonacci数的三种方法和时间复杂度解析

题目: 定义Fibonacci数列如下: f(0)=1 f(1)=1 f(n)=f(n-1)+f(n-2), n>=2 输入n,用最快的方法求该数列的第n项. 解答一: 直接用公式写递归函数.很简单,很低效,就不写了.时间复杂度T(N) = T(N-1) + T(N-2); 也是f(n)本身,2^(n/2)<f(n)<2^n. 解答二: 用循环求,也很直接,效率很高了,时间复杂度是O(n). int f(int n) { if(n <= 1) return 1; int f0=1,

编程题:求两数之和

#include<stdio.h>       /*包含输入输出头文件*/ main()                            /*定义主函数*/ {  int a,b,sum;                /*定义整数变量a.b.sum*/ a=123;                        /*给a赋值*/ b=456;                        /*给b赋值*/ sum=a+b;                  /*令sum=a+b*/ p

【贪心】删数问题

[贪心]删数问题 题目描述 输入一个高精度的正整数n(≤240位),去掉其中任意s个数字后,剩下的数字按原左右次序组成一个新的正整数.编程对给定的n和s,寻找一种方案,使得剩下的数字组成的新数最小. 输入 第1行:一个正整数n: 第2行:s(s<n的位数). 输出 最后剩下的最小数. 样例输入 175438 4 样例输出 13分析:结论是删去第一个递减的数,如果没有则删去最后一个数:代码: #include <iostream> #include <cstdio> #incl