nyoj 791——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
题意:给你一个v升油漆,给出数字1--9对应的油漆花费,让你用那么多油漆画出最大的数。
解题思路;/*
这题的贪心思想比较明显。我们要让能得到数尽量大,那么就要保证让位数尽量长,在位数
尽量长的基础上,让高位数尽量大,这是我们的贪心策略。我们知道,除以最小的数,可以
让位数最长,然而最长可以不仅仅是由最小的数得到。如:v=5 a={2,3,24,32,31,14,15,7,9},
虽然可以得到最长长度是2,且由除以2可得到结果11,但是最优的结果却是21。所以只要我们能
保证我们得到的高位数字比选择最小的数所得高位数字大并且位数相等即可。
*/1234567891011121314151617181920212223242526272829#include<bits/stdc++.h>
using
namespace
std;
const
int
INF=1e9;
int
digit[20];
int
main(){
int
n,i,v,j,k,minv,ai;
while
(
scanf
(
"%d"
,&v)!=EOF){
minv=INF;
for
(i=1;i<=9;i++){
scanf
(
"%d"
,&digit[i]);
minv=minv>digit[i]?digit[i]:minv;
}
if
(minv>v){
printf
(
"-1\n"
);
continue
;
}
for
(i=v/minv;i>=1;i--){
//目前的染料如果染最小花费那个数字可以染多少位
for
(j=9;j>=1;j--){
//从大到小遍历数字
if
(v>=digit[j]&&(v-digit[j])/minv+1>=i){
//目前的染料能染较大的数字且保证跟染最小那个数字可染的位数相同
//贪心选择染大的数字
printf
(
"%d"
,j);
v-=digit[j];
}
}
}
printf
(
"\n"
);
}
return
0;
}
学学学 练练练 刷刷刷
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步