HDU 3153 Pencils from the 19th Century(数学题)

题目链接: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

2008 ACM-ICPC Pacific Northwest Region

代码如下:

#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;
}
时间: 2024-07-30 10:17:00

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

HDU 3153 Pencils from the 19th Century(数学)

主题链接: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." E

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

HDU 3105 Fred&#39;s Lotto Tickets(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3105 Problem Description Fred likes to play the lotto. Whenever he does, he buys lots of tickets. Each ticket has 6 unique numbers in the range from 1 to 49, inclusive. Fred likes to ``Cover all his base

hdu 1165 Eddy&amp;#39;s research II(数学题,递推)

// Eddy 继续 Problem Description As is known, Ackermann function plays an important role in the sphere of theoretical computer science. However, in the other hand, the dramatic fast increasing pace of the function caused the value of Ackermann function

HDU 2268 How To Use The Car (数学题)

题目 //做起来很艰辛,总结起来很简单... //注意步行速度可能比车的速度快.... //推公式要仔细,,,, //一道对我来说很搞脑子的数学题,,,,, //车先送第一个人上路,第二个人步行:中途第一个人下车步行,车回去接第二个人直接到终点 //L:第一个人步行的路程; //T:总时间 //(c-L)*a/b+L+((L/a-L/b)/2)*(a+b)=c -->> L=2*a*c/(3*a+b) //T=(c-L)/b+L/a //坑啊,要比较车速快还是步行快!!!!采取不同方案....

HDU 1785 You Are All Excellent(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1785 Problem Description 本次集训队共有30多人参加,毫无疑问,你们都是很优秀的,但是由于参赛名额有限,只能选拔部分队员参加省赛.从学校的角度,总是希望选拔出最优秀的18人组成6支队伍来代表学校.但是,大家也知道,要想做到完全客观,是一件很难的事情.因为选拔的标准本身就很难统一. 为了解决这个难题,我现在把问题作了简化,现在假设每个队员都是二维平面中的一个点,用(xi,yi)坐标

HDU 1326 Box of Bricks(简单数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older siste

HDU - 2058 The sum problem(简单数学题)

题意:求出所有的情况,等差上去可以达到m值. 原来想着暴力搜索,但是题中的数据太大,所以时间超限. 百度了一下,发现可以套公式. 等差求和公式: Sn=(a1+aN)*n/2     =(a1+a1+d(n-1))*n/2     =a1*n+d(n-1)*n/2; 因为此处公差d=1,所以Sn=a1*n+(n-1)*n/2,当从第一项开始算起时(因本题首项为1,即a1=1时),Sn=M时的项的个数n最多; a1=1,现在又可化简为Sn=n+(n-1)*n/2=(n+1)n/2; 由题意得M=S

hdu 2076 夹角有多大(题目已修改,注意读题)

http://acm.hdu.edu.cn/showproblem.php?pid=2076 数学题...公式推出来就可以AC了. 思路:分别求出时针和分针与12点的位置的夹角大小,输出夹角的差值,注意取小于180度的角. 代码如下: #include<stdio.h> int main() { int t; double h,m,s,angle1,angle2,angle; while(scanf("%d",&t)!=EOF) { while(t--) { sca