Color the fence

描述

Tom has fallen in love with Mary. Now Tom wants to show his love and write a number on the fence opposite to 

Marys house. Tom thinks that the larger the numbers is, the more chance to win Marys 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 doesnt like zero.Thats why Tom wont 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


这道题是要求找最大的数,有九个数(1-9),

每个数字都有需要的油漆数,给你一个总油漆,

让你求出能组成的最大数字。

贪心:

首先考虑需要选出个数最多的。

选个数最多的时候注意选油漆最少的(开始一直错在这里,

还是太年轻,一直想选个数最多且数字是最大的,个数最多是没

错,但是后面数字大,高位的数字就不能很大了,所以只要考虑高位),

因为可以通过剩余的油漆数算出高位最大为多少。


特殊数据对了,程序就没有很多问题了。


#include <stdio.h>
#define INF 0x7fffffff
const int maxn = 10;
int a[maxn];
int main ( )
{
    int V, cnt, pos, min;
    while ( ~ scanf ( "%d", &V ) )
    {
        min = INF;
        for ( int i = 1; i < 10; i ++ )
        {
            scanf ( "%d", &a[i] );
            if ( min > a[i] )
            //开始贪心错了,虽然是找个数最多的,但是数字尽量小好一些,因为那样可以根据剩余的数字继续找比他大的数字
            {
                min = a[i];
                pos = i;
            }
        }
        if ( min > V )  //一个都没有的情况
        {
            printf ( "-1\n" );
            continue ;
        }
        cnt = V/a[pos]; //个数
        int m = V-a[pos]*cnt;   //剩余的个数
        while ( m > 0 && cnt > 0 )
        {
            int t = pos, v = m+a[pos];
            cnt --;
            for ( int i = pos+1; i < 10; i ++ )
                if ( a[i] <= v )    //比a[pos]+m(剩余的个数)小中的最大数
                    t = i;
            printf ( "%d", t );
            if ( t == pos ) //数字没变时就退出
                break ;
            m = v-a[t]; //余的个数
        }
        for ( int i = 1; i <= cnt; i ++ )
            printf ( "%d", pos );
        printf ( "\n" );
    }
    return 0;
}
/*
5
2 3 5 5 5 5 5 5 5
11
3 4 12 12 12 12 12 12 12 12
11
4 5 6 6 6 6 6 6 6
*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值