首页 > 代码库 > HDU 3348 coins

HDU 3348 coins

该题意:给你一个价格,还有面值分别为1,5,10,50,100(单位:毛)纸币的数量,要你用最少数量的纸币和最多数量的凑出这个价格,输出最少和最多的数量。

最少的数量要用贪心的思想,优先取面值尽量大 的纸币来凑这个价格。

而最多的数量网上许多算法提供的方法多是用较小的纸币去补较大的纸币,这里提供另一种想法:因为我们要求的是花的最多数量纸币,所以就是要保证手上的纸币数量最少!!这样想的话问题就比较简单了,就转化为最少数量问题了。假设手上总共有p毛,而价格为q毛,我们用手上最少的数量的纸币去凑(p-q)毛,然后再用总数量减去该最少数量即可。

 

                Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
                        Total Submission(s) : 4   Accepted Submission(s) : 3

                     Font: Times New Roman | Verdana | Georgia

                            Font Size: ← →

Problem Description

In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  

Input

The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.

Output

For each case, output the least money you need to take, if it’s impossible, just output -1.

Sample Input

16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6

Sample Output

1

 

 

代码比较简陋,跟大家分享想法为主:

 

 

#include "stdio.h"
#include "string.h"

int b[10]={0,1,5,10,50,100};
int main()
{
    int t;
    int p,r;
    int a[10],c[10],e[10];
    int i,j,k,sum;

    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        scanf("%d",&p);
        r=p;
        for(i=1;i<=5;i++)
        {
            scanf("%d",&a[i]);
            sum=sum+b[i]*a[i];
        }
        for(i=5;i>0;i--)
        {
            if(r/b[i]<a[i])
            {
                c[i]=r/b[i];
                r=r-b[i]*c[i];
            }
            else
            {
                c[i]=a[i];
                r=r-c[i]*b[i];
            }
        }
        if(r!=0)
        {
            printf("-1 -1\n");
        }else
        {
            k=sum-p;
            for(i=5;i>0;i--)
            {
                if(k/b[i]<a[i])
                {
                    e[i]=k/b[i];
                    k=k-b[i]*e[i];
                }
                else
                {
                    e[i]=a[i];
                    k=k-e[i]*b[i];
                }
            }
            if(k==0)
            {
                printf("%d %d\n",c[1]+c[2]+c[3]+c[4]+c[5],(a[1]+a[2]+a[3]+a[4]+a[5]-(e[1]+e[2]+e[3]+e[4]+e[5])));
            }
            
        }    

    }
}