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. [LOJ2230][BJOI2014]大融合

    题面戳我 sol LCT维护子树size. 开一个数组\(sz_i\)表示一个节点的所有虚儿子的size和,\(sum_i\)表示以一个节点为根的子树的\(size\)和,可见\(sz_u=\sum_ ...

  2. [ZJOI2007]时态同步

    题目描述 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点 ...

  3. 使用非直接缓冲区与直接缓冲区进行文件的复制(基于Channel)

    一.利用通道完成文件的复制(非直接缓冲区)     long start = System.currentTimeMillis(); FileInputStream fis = new FileInp ...

  4. C#服务器端生成报告文档:使用帆软报表生成Word、Pdf报告

    一.帆软报表简介 报表工具中,帆软报表相比Crystal Report(水晶报表).SQL Server Report Service(SSRS)等报表工具来说算是佼佼者,此外帆软报表在统计图表.数据 ...

  5. 三大家族,offset,scroll,client

    1.client 1.1主要成员 1.clientWidth 获取网页可视区域宽度(两种用法)    clientHeight 获取网页可视区域高度 (两张用法) 盒子调用: 指盒子本省 浏览器调用: ...

  6. Java 对IP请求进行限流.

    高并发系统下, 有三把利器 缓存 降级 限流. 缓存: 将常用数据缓存起来, 减少数据库或者磁盘IO 降级: 保护核心系统, 降低非核心业务请求响应 限流: 在某一个时间窗口内对请求进行限速, 保护系 ...

  7. java的枚举2

    首先先理解一下java中枚举的本质. java的世界中一切皆是类,下面通过一个例子解释一下enum的本质: package cn.xnchall.enumeration; public class G ...

  8. Kon-boot v2.5介绍与使用方法总结(支持win10)

    Kon-boot这个工具相信大家都不陌生,这是一款专门针对Windows.Linux.MAC登陆密码破解工具,他能绕过系统所设有的登陆密码,让你的登陆畅通无阻.KON-Boot的原理是在于处理BIOS ...

  9. 通过Beego将之前实现的短url项目实现

    正好通过这个小例子对之前了解的beego框架的基本内容进行一个简单的应用 实现的完整代码地址:https://github.com/pythonsite/go_simple_code/tree/mas ...

  10. EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)

    前言 终于踏出第一步探索EF Core原理和本质,过程虽然比较漫长且枯燥乏味还得反复论证,其中滋味自知,EF Core的强大想必不用我再过多废话,有时候我们是否思考过背后到底做了些什么,到底怎么实现的 ...