nyoj 791 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
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. 样例输入
-
55 4 3 2 1 2 3 4 529 11 1 12 5 8 9 10 6
样例输出
-
5555533
贪心算法,输出的那一部分是难点。。。。 代码: #include<stdio.h> #include<string.h> int main(){ int i,j,k,t; int a[10009]; int n; int min; while(~scanf("%d",&n)){ min=0x3f3f3f3f; for(i=0;i<9;i++){ scanf("%d",&a[i]); if(min>a[i]) min=a[i]; } if(n<a[i]){ printf("-1\n");//判断,如果不要符合条件的话直接退出 continue; } for(i=n/min;i>=0;i--){ for(j=8;j>=0;j--){ if(n>=a[j]&&(n-a[j])/min>=i){//从最大的数开始涂,如果满足大于等于i就执行。 n-=a[j]; printf("%d",j+1); break; } } } printf("\n"); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理