nyoj Color the fence
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.
- 样例输入
-
5 5 4 3 2 1 2 3 4 5 2 9 11 1 12 5 8 9 10 6
- 样例输出
-
55555 33
#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<min)
{
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)
{
n-=a[j];
printf("%d",j+1);
break;
}
}
}
printf("\n");
}
return 0;
}分析:
涂料一定, 肯定涂出的数字越多,数字就越大。所以我们就可以在涂出数字最多的情况下,枚举每一位的可以数字取最大(从9开始枚举找打第一个符合既是答案)。
枚举的符合条件就是当前的涂料可以涂此数字并且涂完此数字不影响涂的总数字的个数(即涂的总数字的个数不会减小)。
scanf()函数返回成功赋值的数据项数,出错时则返回EOF(-1)
也就是说scanf返回值的取值范围是大于等于-1的整数
只有返回值为EOF时 其取反的的值 即while循环的判断条件才为0 才能结束循环
其它输入情况下(无论是否输入成功) while循环的判断条件为非0 即为真
楼主给出的程序是很不严谨的 一但输入的值为字母符号之类的
scanf赋值不成功把读到的内容又返回到stdin的缓冲区
假设这个被吐回的值为t
由于scanf返回的值不是EOF而是其它非负整数
其取反得到的值使while又进入到下一次循环
scanf又从stdin缓冲区里读到了原先吐回的t
往返如此成了死循环……
楼主的代码要想执行成功只有这样操作
输入个int类型的值后再回车
接着可多次如上操作 想结束输入时
再人为制造个EOF(ctrl+z/d)
再回车使while循环条件为假结束循环
这时n的取值为最后一次成功读取到的int型数值
nyoj Color the fence的更多相关文章
- nyoj 791——Color the fence——————【贪心】
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- ACM Color the fence
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- NYOJ-791 Color the fence (贪心)
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- 349B - Color the Fence
Color the Fence Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- Codeforces 349B - Color the Fence
349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...
- Codeforces D. Color the Fence(贪心)
题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- B. Color the Fence
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
随机推荐
- 【learning】一般图最大匹配——带花树
问题描述 对于一个图\(G(V,E)\),当点对集\(S\)满足任意\((u,v)\in S\),均有\(u,v\in V,(u,v)\in E\),且\(S\)中没有点重复出现,我们称\(S\) ...
- 未来已来,腾讯AI计算网络
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:由鹅厂网事发表在云+社区 "鹅厂网事"由深圳市腾讯计算机系统有限公司技术工程事业群网络平台部运营,我们希望与业界各位志同道合的伙伴交流 ...
- 快递查询 C#
//电商ID private string EBusinessID = "1257164"; //电商加密私钥,快递鸟提供,注意保管,不要泄漏 private string App ...
- Linux设备驱动框架设计
引子 Linux操作系统的一大优势就是支持数以万计的芯片设备,大大小小的芯片厂商工程师都在积极地向Linux kernel提交设备驱动代码.能让这个目标得以实现,这背后隐藏着一个看不见的技术优势:Li ...
- 原生js写的flybird小游戏
游戏地址:http://zangzhihong.jusukeji.com/flybird/index.html html部分 <!DOCTYPE html> <!-- This ...
- 为什么会有可恶的腾讯电脑管家&怎么干掉它-电脑开机出现腾讯电脑管家-无法卸载腾讯电脑管家
为什么会有可恶的腾讯电脑管家 怎么干掉它 电脑开机出现腾讯电脑管家 无法卸载腾讯电脑管家 某天,趁空闲,升级了一些常用软件,其中就升了新版电脑qq,晚上玩完电脑后关机睡觉,第二天早上开机.出现了图一: ...
- cmd 命令大全
1.windows 系统定时关机 定时关机:shutdown -s -t 300 at 18:30 shutdown -s 取消定时:shutdown -a 注意:300为秒数,在windows co ...
- Java使用对象流读取文件的问题
把对象进行持久化(把对象存到本地)要用到对象流进行处理,在用对象流处理过程中,在写对象和读对象时候遇到了很多问题,分享一下. 我们处理对象数据的时候不可能只处理一个对象,在系统运行的时候,可能产生的对 ...
- protobuf的下载、编译和使用
一 背景 工作中临时需要使用 protobuf v3.0.2 做消息格式转换,折腾了很久才搞定,这里特意做一个记录. 二 准备工作 全程使用的电脑为公司的win7 64位旗舰版,已经预先安装VS201 ...
- spring Boot+spring Cloud实现微服务详细教程第一篇
前些天项目组的大佬跟我聊,说项目组想从之前的架构上剥离出来公用的模块做微服务的开发,恰好去年的5/6月份在上家公司学习了国内开源的dubbo+zookeeper实现的微服务的架构.自己平时对微服务的设 ...