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. 样例输入
-
5
-
5 4 3 2 1 2 3 4 5
-
2
-
9 11 1 12 5 8 9 10 6
样例输出
-
55555
-
33
-
这道贪心真的很有意思,调试了好久在做出来。代码中有注释。
-
#include<stdio.h> #include<algorithm> using namespace std; struct st { int l,num; }data[20]; int cmp(st x,st y) { if(x.num!=y.num) return x.num<y.num; return x.l>y.l; } int main() { int i,n,k,ans; while(scanf("%d",&n)!=EOF) { for(i=1;i<=9;i++) { scanf("%d",&data[i].num); data[i].l=i; } sort(data+1,data+10,cmp); //如果最少需要油漆的数字小于总油漆数,那么不可能有数字输出 。 if(data[1].num>n) { printf("-1\n"); continue; } //如果总油漆数能整除最小油漆数(即位数最多),输出该数即为最大值。 if(n%data[1].num==0) { for(int j=1;j<=n/data[1].num;j++) { printf("%d",data[1].l); } printf("\n"); continue; } k=n/data[1].num; int ans=1; /* 一共是k位数字,每次保证将满足情况的最大的输出即为最大值。 必须保证剩余位数+已经输出的位数=k */ for(i=1;i<=k;i++) { int max=data[1].l; int num=data[1].num; for(int j=9;j>1;j--) { //从中找到最大的满足情况的数字输出~ if(data[j].l>max&&((n-data[j].num)/data[1].num+ans==k)&&(n-data[j].num>0)) { max=data[j].l; num=data[j].num; } } printf("%d",max); n-=num; ans++; } printf("\n"); } }
-
There are multiple test cases.