主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3153

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 19th 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 cents
nn at four cents each
nn at two for a penny
nn 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<cstdio>
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;
}

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

HDU 3153 Pencils from the 19th Century(数学)的更多相关文章

  1. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  2. HDU 5478 Can you find it 随机化 数学

    Can you find it Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  3. HDU 2080 夹角有多大II (数学) atan(y/x)分类求角度

    夹角有多大II Problem Description 这次xhd面临的问题是这样的:在一个平面内有两个点,求两个点分别和原点的连线的夹角的大小.注:夹角的范围[0,180],两个点不会在圆心出现. ...

  4. HDU 2050 【dp】【简单数学】

    题意: 中文. 思路: 不难发现数学规律是这样的,每次增加的划分区域的数量是每次增加的交点的数量再加一.然后就总结出了递推公式. #include<stdio.h> ]; int main ...

  5. 题解报告:hdu 1284 钱币兑换问题(简单数学orDP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1284 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很 ...

  6. HDU 5063 Operation the Sequence(暴力 数学)

    题目链接:pid=5063" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5063 Prob ...

  7. hdu 4278 2012天津赛区网络赛 数学 *

    8进制转为10进制 #include<cstdio> #include<iostream> #include<algorithm> #include<cstr ...

  8. hdu 4061 福州赛区网络赛A 数学 ***

    a1/sum #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...

  9. HDU 5441 Travel (并查集+数学+计数)

    题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u ...

随机推荐

  1. CSDN学院 免费技术答疑公开课,本周三场即将开播~~~

    为了酬谢广大学员.CSDN学院特推出免费技术答疑公开课,让您开启一段充实的学习之旅~ 本周三场即将开播! ----------------------------------------------- ...

  2. Android(Lollipop/5.0) Material Design(四) 创建列表和卡片

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

  3. 最简单的基于FFmpeg的AVUtil样例 (AVLog, AVOption等)

    本文的演示样例程序记录了FFmpeg的libavutil中几种工具函数的用法: AVLog:日志输出AVOption (AVClass):选项设置AVDictionary:键值对存储ParseUtil ...

  4. <Linux> Linux下一些常用命令(个人记录)

    mount -o loop XXXX  XXX ./XXX &   后台运行 rm -rf XXX  删除文件夹 rm -rf *  删除当前目录下的所有文件 包括文件夹 ps -ef | g ...

  5. UUIDUtils工具类

    原理是根据自身的操作系统和电脑硬件生成的一个32位的随机字符串,如果是一台电脑使用的话,一年不会重复,经过java之后貌似编程了36位,多了4个"-",下面是代码实现: impor ...

  6. 【54.38%】【BZOJ 4300】绝世好题

    Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1120  Solved: 609 [Submit][Status][Discuss] Descript ...

  7. JVM 调优 —— OutOfMemory

    零. 简单介绍 OutOfMemory 意思就是须要申请更大的内存, 可是内存限制无法申请到须要的内存. 一. 解决方法 基本上解决方向有两种: 检查程序是否有问题. 是不是写死循环不停地创建并持有对 ...

  8. C++ 工具类 —— 词条类(Entry)

    Entry 以键值对(key-value pair)的形式定义. template <typename K, typename V> struct Entry{ K key; V valu ...

  9. 机器学习 Softmax classifier (一个隐含层)

    程序实现 softmax classifier, 含有一个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...

  10. android应用开发 button显示两行文本

    自己写了个电话拨号器的程序给男朋友玩,用来专门打我的手机号,为了把界面做的好看一点,须要用到显示两行文本的button.在网上搜罗了两种方法,记录在以下.跟大家一起分享一下,希望能帮到须要的人. 方法 ...