您的位置:首页 > Web前端

Color the fence

2015-04-11 21:27 309 查看

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
一开始我没想到会每个位置都贪心一下,以为只要贪心第一次就可以了,结果WA了20多次。后来上网看看别人的代码之后发现每位都要,最后就AC了。所以必须要多想一下,别太快打代码。
我的代码:
#include <cstdio>using namespace std;int main(){int n,i,j,k,mod,t,min,p,q;int a[10];while (~scanf("%d",&n)){min=99999999;for (i=1;i<=9;++i){scanf("%d",&a[i]);if (a[i]<=min){min=a[i];//一个数字消耗的最小油漆量k=i;//数字}}if (n<min){printf("-1\n");continue;}t=n/min;//最小的符合位数mod=n%min;if (!mod){while (t--){printf("%d",k);}printf("\n");continue;}p=min;for (i=1;i<=t;++i){q=0;for (j=9;j>=1;--j){if (a[j]>=p&&n>=a[j]&&(n-a[j])/min==t-i&&j>q){q=j;break;}}n-=a[q];printf("%d",q);}printf("\n");}return 0;}
网上的代码:
(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;}
(2)
#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>using namespace std;int main(){int n;int s[11];while(scanf("%d",&n)!=EOF){	int min= 0x3f3f3f3f;//设它为最大值;for(int i=1;i<10;i++){int num;cin>>num;if(num<min){min=num;}s[i]=num;}for(int i=n/min-1;i>=0;i--)//从最大的可以染的颜色开始{for(int j=9;j>=1;j--){if(n-s[j]>=0&&((n-s[j])/min>=i))//判断消耗之后剩余的可否染当前的数字;{cout<<j;n-=s[j];break;}}}cout<<endl;}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++