南阳791 Color the fence

来源:互联网 发布:全球领先的物流软件 编辑:程序博客网 时间:2024/05/16 15:27

题目:

Color the fence

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

Tom has fallen in love with Mary. Now Tom wants to show his love and write a number on the fence opposite to 

Mary’s house. Tom thinks that the larger the numbers is, the more chance to win Mary’s heart he has.

Unfortunately, Tom could only get V liters paint. He did the math and concluded that digit i requires ai liters paint. 

Besides,Tom heard that Mary doesn’t like zero.That’s why Tom won’t use them in his number.

Help Tom find the maximum number he can write on the fence.

输入
There are multiple test cases.
Each case the first line contains a nonnegative integer V(0≤V≤10^6).
The second line contains nine positive integers a1,a2,……,a9(1≤ai≤10^5).
输出
Printf the maximum number Tom can write on the fence. If he has too little paint for any digit, print -1.
样例输入
55 4 3 2 1 2 3 4 529 11 1 12 5 8 9 10 6
样例输出
5555533
题目大意:有v升油漆想要涂出一个数字,给出数字1-9所需油漆,求用v升油漆可涂的最大数字
此题为贪心,基本思路为:找出所需油漆最小的数字(所需油漆相同取较大数字)d,所需油漆为min_v,v/min_v为数字的最大位数(max_bit),到这告一段落,假想求出的最大数就为max_bit个d组成,如输入:5 5 4 3 2 1 2 3 4 5能知道输出为:5个5——>55555。现在看另一个例子:5 2 2 4 5 4 5 4 3 5若果按刚才的想法输出为:2个2——>22但是事实为:82,接下来思考,在保证位数最大时,怎么得到最大值。要想数字大,那么首位尽可能大(首先考虑完位数问题了),如果v整除了min_v就没有改动的余地了,如果有余数leave,那么这个最大数还有改动的空间,从最大位开始贪心,能把最高位改动(前提可改的数大于d)就变动。变动后输出可改数值,最大位数max_bit-1;
程序:
#include<stdio.h>int main(){    int v,a[11],d,min_v,max_bit,leave;    while(~scanf("%d%d",&v,&a[1])){         min_v=a[1];d=1;        for(int i=2;i<10;i++) //找出升数最小数值;        {            scanf("%d",&a[i]);            if(a[i]<=min_v){                min_v=a[i];                d=i;            }        }        if(min_v>v){  //若最小值大于v,无解,输出-1;            printf("-1\n");            continue;        }        max_bit=v/min_v;  //求最大位数;        leave=v%min_v;   //求出在位数最大的前提下,最大余数;        for(int i;leave>0&&max_bit;){   //贪心;使高位数字尽可能大;确保最大位数非负;            for(i=9;i>d&&max_bit;i--){                if(leave>=(a[i]-a[d])){                     printf("%d",i);                    max_bit=max_bit-1;  //输出数字后,最大位数-1;                    leave-=(a[i]-a[d]);                    break;                }            }            if(i==d) break;        }        for(int i=0;i<max_bit;i++) printf("%d",d); //输出剩下的位数;        printf("\n");    }    return 0;}


0 0
原创粉丝点击