博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3153 Pencils from the 19th Century(数学)
阅读量:4676 次
发布时间:2019-06-09

本文共 3267 字,大约阅读时间需要 10 分钟。

主题链接:

Problem Description
Before "automaton" was a theoretic computer science concept, it meant "mechanical figure or contrivance constructed to act as if by its own motive power; robot." Examples include fortunetellers, as shown above, but could also describe a pencil seller, moving pencils from several baskets to a delivery trough.
On National Public Radio, the Sunday Weekend Edition program has a "Sunday Puzzle" segment. The show that aired on Sunday, 29 June 2008, had the following puzzle for listeners to respond to (by Thursday, 3 July, at noon through the NPR web site):
  • From a 19
th century trade card advertising Bassetts Horehound Troches, a remedy for coughs and colds: A man buys 20 pencils for 20 cents and gets three kinds of pencils in return. Some of the pencils cost four cents each, some are two for a penny and the rest are four for a penny. How many pencils of each type does the man get?

One clarification from the program of 6 July: correct solutions contain at least 
one of each pencil type.
For our purposes, we will expand on the problem, rather than just getting 20 pencils for 20 cents (which is shown in the sample output below). The input file will present a number of cases. For each case, give all solutions or print the text "No solution found". Solutions are to be ordered by increasing numbers of four-cent pencils.
 
Input
Each line gives a value for 
N (2 <= 
N <= 256), and your program is to end when 
N = 0 (at most 32 problems).
 
Output
The first line gives the instance, starting from 1, followed by a line giving the statement of the problem. Solutions are shown in the three-line format below followed by a blank line, or the single line "No solution found", followed by a blank line. Note that by the nature of the problem, once the number of four-cent pencils is determined, the numbers of half-cent and quarter-cent pencils are also determined.
Case n:nn pencils for nn centsnn at four cents eachnn at two for a pennynn at four for a penny
 
Sample Input
 
10 20 40 0
 
Sample Output
 
Case 1: 10 pencils for 10 cents No solution found. Case 2: 20 pencils for 20 cents 3 at four cents each 15 at two for a penny 2 at four for a penny Case 3: 40 pencils for 40 cents 6 at four cents each 30 at two for a penny 4 at four for a penny 7 at four cents each 15 at two for a penny 18 at four for a penny
 
Source

代码例如以下:

#include
int main(){ int n; int flag; int cas = 0; while(scanf("%d",&n)&&n) { double sum = n*1.0; flag = 0; printf("Case %d:\n",++cas); printf("%d pencils for %d cents\n",n,n); for(int i = 1; i < n; i++) //1 { for(int j = 1; j < n; j++) //2 { for(int k = 1; k < n; k++) //3 { if(sum==i*4+j*0.5+k*0.25 && i+j+k==n) { flag = 1; printf("%d at four cents each\n",i); printf("%d at two for a penny\n",j); printf("%d at four for a penny\n\n",k); } } } } if(!flag) printf("No solution found.\n\n"); } return 0;}

版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/gcczhongduan/p/4803473.html

你可能感兴趣的文章
Java线程的定义
查看>>
UglifyJS 压缩选项
查看>>
面向对象1
查看>>
Python-面向对象(组合、封装与多态)
查看>>
Mininet
查看>>
COSC2531 Programming Fundamentals
查看>>
设计模式系列 - 访问者模式
查看>>
20180507小测
查看>>
前端鼠标点击弹出浮动文字--民主、和谐、爱国、自由等
查看>>
eclipse左侧不见
查看>>
python会缓存小的整数和短小的字符
查看>>
格网与四叉树索引
查看>>
Linux网卡配置文件路径是什么?要使服务器上外网,必须满足的条件有哪些?需要配置什么?...
查看>>
多张照片拍摄、图片浏览
查看>>
html(5) css
查看>>
微信小程序时间戳 页面中时间戳转成自己需要的格式(支持列表循环等)
查看>>
CSS笔记2
查看>>
Azure Web连接到Azure MySql Db
查看>>
Python2快速入门教程,只需要这十五张图片就够了!
查看>>
cdoj 1131 男神的礼物 区间dp
查看>>