美文网首页
Color the fence

Color the fence

作者: Gitfan | 来源:发表于2017-03-02 22:09 被阅读0次

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

    相关文章

      网友评论

          本文标题:Color the fence

          本文链接:https://www.haomeiwen.com/subject/oggkgttx.html