Color the fence

Color the fence
時間限制:1000 ms | 內(nèi)存限制: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

主要思路:位數(shù)越長,數(shù)字越大。在保證最位數(shù)不變的情況下,盡量取值大的數(shù)字。

#include<cstdio>
#include<algorithm>
using namespace std;
int main(){

    int num[10],m;
    while(~scanf("%d",&m))
    {
      for(int i=1;i<=9;i++)
      {
          scanf("%d",num+i);
      }
      int s=99999999;
      for(int i=1;i<=9;i++)
      {
            if(num[i]<s) s=num[i];
      }
      if(s>m) {
        printf("-1\n");
        continue;
      }
      int Min=m/s,dig=1;
      for(int i=9;i>=1;)
      {
          if(m<0) break;
          else if((m-num[i]>=0)&&((m-num[i])/s+dig>=Min))
          {
              m-=num[i];
              dig++;
              printf("%d",i);
          }
          else i--;
      }
      printf("\n");
   }

}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容