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型数值

转载于:https://www.cnblogs.com/tianzeng/p/8519369.html

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 wan ...

  2. 【CodeForces - 349B】Color the Fence (贪心,填数)

    题干: Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on th ...

  3. ACM程序设计课程总结

    文章目录 知识总结 一.递归算法 二.贪心算法 三.动态规划 四.分治算法 五.数论 六.STL的简单应用 学习这门课后的感想 知识总结 一.递归算法 [算法描述] 程序直接或间接调用自身的编程技巧称 ...

  4. 276. Paint Fence

    题目: There is a fence with n posts, each post can be painted with one of the k colors. You have to pa ...

  5. LintCode_514 Paint Fence

    题目 here is a fence with n posts, each post can be painted with one of the k colors. You have to pain ...

  6. 276. Paint Fence篱笆涂色

    [抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...

  7. 第一篇博客:那些年磕过的题之Fence Painting

    文章目录 前言 一.题目 英文原题 中文翻译 输入样例 输出样例 二.解题思路 三.代码 总结 前言 之前从没有过要写博客的想法,觉得会写博客的都是大佬,自己的水平还是太菜.这个学期末和大佬们一起学习 ...

  8. RGB Color Codes Chart

    RGB Color Codes Chart RGB颜色空间 RGB颜色空间或RGB颜色系统,从红色.绿色和蓝色的组合中构造所有颜色. 红色.绿色和蓝色各使用8位,它们的整数值从0到255.这使得256 ...

  9. 视频色彩校正简介 Introduction to Video Color Correction

    视频色彩校正简介 Introduction to Video Color Correction 视频色彩校正简介 Introduction to Video Color Correction MP4 ...

  10. Android 中一些常用类的常用方法(Math、Random、Color、Paint、Canvas、Bitmap、BitmapFactory)...

    1.java.lang.Math类常用的常量和方法: Math.PI 记录的圆周率 Math.E 记录e的常量 Math.abs 求绝对值 Math.sin 正弦函数 Math.asin 反正弦函数 ...

最新文章

  1. 柳传志:联想又遇新坎儿 要做斯巴达克方针
  2. Mac下Sublime text2中文乱码问题的解决
  3. python异常处理优点_python各类异常处理学习笔记
  4. linux 版本的scipy,linux安装scipy
  5. calendar获取月份少一个月_VBA 技巧:计算一个月有多少天?
  6. 云计算实战系列八(存储管理I)
  7. linux卸载keystone服务,OpenStack —— 认证服务Keystone(二)
  8. [CareerCup] 17.1 Swap Number In Place 互换位置
  9. python大数据和java大数据的区别-做大数据工程师,语言选Java还是Python?
  10. c语言是高级编程语言吗,C语言是高级编程语言吗?
  11. 当客户端浏览器不支持相应版本的apple时自动下载运行环境JVM的解决办法!
  12. c语言love字符怎么打,love字母特殊符号
  13. Windows安装Redis新手教程
  14. java多线程5大框架_实现 Java 多线程并发控制框架
  15. 计算机网络防御的论文,计算机网络防御策略论文.doc
  16. 基于PLC的搬运机器手控制系统设计
  17. Delphi 全局热键注册+使用
  18. 这俩 AI 项目贼有意思
  19. AngularJS 控制器中处理DOM事件
  20. 用c语言写出10以内加减法,求助 给小学生出题,自己选加减乘除 做10题 10以内的数 然后统计分...

热门文章

  1. debian samba出错:set_variable_helper(yes ): value is not boolean!
  2. LINUX获取当前用户及信息的命令
  3. WINDOWS10的任务管理器不能自动刷新?
  4. 解决办法:ubuntu登录后,桌面空空如也,状态栏没了
  5. 解决办法:defined but not used [-Werror=unused-variable]
  6. 问题解决办法:pip tensorrt成功,PyCharm import出错
  7. 管理感悟:你说负责,关键在于怎样负责?
  8. centos mysql 二进制_CentOS 7.6 安装二进制Mysql
  9. 手机端答题页面_有奖答题来啦!科普知识等你来挑战……
  10. android设计个人简历页面_制作个人简历网站教程