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

#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的更多相关文章

  1. nyoj 791——Color the fence——————【贪心】

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  2. ACM Color the fence

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  3. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  4. NYOJ-791 Color the fence (贪心)

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  5. 349B - Color the Fence

    Color the Fence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  6. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  7. Codeforces D. Color the Fence(贪心)

    题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. B. Color the Fence

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. codeforces B. Color the Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...

随机推荐

  1. 禁ping以及清理系统多余账号说明

    查出多余的:grep -v “nologin$“ /etc/passwd

  2. 环境变量配置为jdk8,显示的java版本为jdk7

    经查找发现是jdk版本的问题,我系统环境变量配置的是jdk7,可是这个war包需要在jdk8的环境下运行.于是我就手动将环境变量的jdk7换成jdk8,结果发现依然还是会出现以上问题.于是我打开cmd ...

  3. 图文详解AO打印(标准模式)

    一.概述   AO打印是英文Active-Online Print的简称,也称主动在线打印.打印前支持AO通讯协议的AO打印机(购买地址>>)首先通过普通网络与C-Lodop服务保持在线链 ...

  4. 记一次结合PHP多进程和socket.io解决问题的经历

    公司是做棋牌游戏的.前段时间接到一个后台人工鉴定并处理通牌作弊玩家的需求,其中需要根据几个玩家的游戏ID查询并计算他们在某段时间内彼此之间玩牌输赢次数和输赢总额. 牌局数据是存储在日志中心的,他们把牌 ...

  5. 洛谷 P2401 不等数列

    其实有两种方法来解这道题# 第一种:找规律(非正经) 一看,这玩意像是个杨辉三角,还左右对称呢 因为新插入一个数$n$,有$n+1$个位置可以选,所以总数就乘$n+1$,对应的$f[n+1][i]$也 ...

  6. Liveness 探测 - 每天5分钟玩转 Docker 容器技术(143)

    Liveness 探测让用户可以自定义判断容器是否健康的条件.如果探测失败,Kubernetes 就会重启容器. 还是举例说明,创建如下 Pod: 启动进程首先创建文件 /tmp/healthy,30 ...

  7. 题目1032:ZOJ

    题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出. 输入: 题目包含多组用例,每组用例占一行,包含ZOJ三个 ...

  8. xlsx导入成--json

    这两天遇到大难题了,就是这个   xlsx   导入问题,之前用的xlsx.full.min.js,写的导入,结果不兼容ie浏览器,研究这个也好长时间,网上居然还没有搜到合适的,自己写从xlsx官网上 ...

  9. angularjs常用事件

    1. 视图模板加载完毕 $scope.$on('$viewContentLoaded', function() { alert('view template loaded'); });

  10. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListene解决办法

    照着以前写的项目搭一个框架,项目用到的是ssm,spring+spring mvc+mybatis,由Eclipse转战IDEA上,项目的文件夹有一些改变,在之前的小项目中喜欢把lib文件夹放在项目根 ...