http://codeforces.com/contest/349/problem/B
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has.
Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digitd requiresad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.
Help Igor find the maximum number he can write on the fence.
Input
The first line contains a positive integer v(0 ≤ v ≤ 106). The second line contains nine positive integersa1, a2, ..., a9(1 ≤ ai ≤ 105).
Output
Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1.
Sample test(s)Input
55 4 3 2 1 2 3 4 5
Output
55555
Input
29 11 1 12 5 8 9 10 6
Output
33
Input
01 1 1 1 1 1 1 1 1
Output
-1
思路:貪心即可
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main(){ int a[10],sum,minn,k,i,j,cnt,r; while(~scanf("%d",&sum)) { minn = 1000000005; k = 0; for(i = 1; i<=9; i++) { scanf("%d",&a[i]); if(a[i]<minn)//找出花費最小的顏料數 { minn = a[i]; k = i; } else if(a[i] == minn && k<i)//花費與最少的相等,自然取數字大的 k = i; } if(sum<minn)//最少的花費都大於總顏料,自然不行 { printf("-1\n"); continue; } cnt = sum/minn;//最小花費能得到的數字長度 r = sum%minn; if(!r)//若總顏料能整出最小花費,全部輸出這個數一定是最大 { for(i = 1; i<=cnt; i++) printf("%d",k); printf("\n"); continue; } while(sum>0)//總顏料還沒用完,找出與最小花費長度相等的最大數 { int x = 0; for(i = 1; i<=9; i++) { int s = sum-a[i];//減去這個數位花費 if(s<0)//這個數字會使顏料用完,換下一個顏料 continue; if(s/minn == cnt-1 && x<i)//減去該數位花費之後,剩下的除以最小的長度是原來長度-1,必定是最符合的,在所有最符合的狀況中找到最大的 x = i; } if(x) { sum-=a[x];//放了一個數字,總花費減少 cnt--;//長度減一 printf("%d",x);//輸出這個數字 } } printf("\n"); } return 0;}