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-12-29 11:44:10

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 5171 GTY&#39;s birthday gift(数学,矩阵快速幂)

题意: 开始时集合中有n个数. 现在要进行k次操作. 每次操作:从集合中挑最大的两个数a,b进行相加,得到的数添加进集合中. 以此反复k次. 问最后集合中所有数的和是多少. (2≤n≤100000,1≤k≤1000000000) 思路: 写出来发现是要求Fibonaci的前n个数的和. Fibonaci是用矩阵快速幂求的,这个也可以. [Sn,Fn,Fn-1]=[某个矩阵]*[Sn-1,Fn-1,Fn-2] [S2,F2,F1]=[2,1,1] 然后写,,, 这个代码有些繁琐,应该把矩阵操作单独

HDU 5175 Misaki&#39;s Kiss again(数学,暴力枚举)

题目大意: After the Ferries Wheel, many friends hope to receive the Misaki's kiss again,so Misaki numbers them 1,2...N?1,N,if someone's number is M and satisfied the GCD(N,M) equals to N XOR M,he will be kissed again. Please help Misaki to find all M(1<=

HDU 2446 Shell Pyramid(二分查找 数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2446 Problem Description In the 17th century, with thunderous noise, dense smoke and blazing fire, battles on the sea were just the same as those in the modern times. But at that time, the cannon ,were e

HDU 1990 &amp; ZOJ 2992 Monkey Vines(数学啊)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1990 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1991 Problem Description Deep in the Amazon jungle, exceptionally tall trees grow that support a rich biosphere of figs and junipe

HDU 5053 the Sum of Cube(数学求立方和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5053 Problem Description A range is given, the begin and the end are both integers. You should sum the cube of all the integers in the range. Input The first line of the input is T(1 <= T <= 1000), whic

HDU 5063 Operation the Sequence(暴力 数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5063 Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,-,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types: T

HDU 2050 【dp】【简单数学】

题意: 中文. 思路: 不难发现数学规律是这样的,每次增加的划分区域的数量是每次增加的交点的数量再加一.然后就总结出了递推公式. #include<stdio.h> long long ans[10005]; int main() { ans[1]=2; long long tmp=2; for(int i=2;i<=10000;i++) { ans[i]=ans[i-1]+tmp*2+1; tmp+=2; } int t; scanf("%d",&t); f

HDU 4985 Little Pony and Permutation(数学 置换群)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 置换群:http://baike.baidu.com/view/1879054.htm?fr=aladdin Little Pony and Permutation Problem Description As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony