Color the fence

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<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");
   }

}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容