作者:Leon916 来源:C++博客   酷勤网收集 2008-05-25

摘要
  How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.)

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1003

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30785   Accepted: 14203

Description

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.



Input

The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.

Output

For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.

Sample Input

1.00
3.71
0.04
5.19
0.00

Sample Output

3 card(s)
61 card(s)
1 card(s)
273 card(s)

Source

解答:

1003题探讨
    今天做的这道题感觉上面很奇怪,按照题目要求写了代码,但总觉得哪里有什么地方不对,还请大家多多指教!

 1#include <stdio.h>
 2int main(int argc, char* argv[])
 3{
 4    float s,sum = 0;
 5    int i,j;
 6    while(scanf("%f"&sum) == 1)
 7    {
 8        if(sum != 0)
 9        {
10            j = 2; s = 0;
11            while(s < sum)
12            {
13                s += 1.0/j;
14                j++;
15            }

16            printf("%d card(s)\n", j-2);
17        }

18        else
19            break;
20    }

21        return 0;
22}

评论:

re: 1003题探讨 2008-05-24 11:10 h0rus1ee

我当时是这样写的,好像结果正确:

#include <stdio.h>

int main (void)
{
int i;
int counter;

float sum;
float c;

scanf("%f", &c);

while(c != 0.00 && c >= 0.01 && c <= 5.20) {
counter = sum = 0;
for(i = 2; sum < c; i++) {
sum += 1.00/i;
counter++;
}
printf("%d card(s)\n", counter);
scanf("%f", &c);
}

return 0;
}

re: 1003题探讨 2008-05-24 16:13 Leon916

做如下修改:

1#include <stdio.h>
2int main(int argc, char* argv[])
3{
4 float s,sum = 0;
5 int j;
6 while((scanf("%f", &sum) == 1)&& (sum!=0))
7 {
8
9
10 j = 2; s = 0;
11 while(s < sum)
12 {
13 s += 1.0/j;
14 j++;
15 }
16 printf("%d card(s)\n", j-2);
17
18
19
20 }
21 return 0;
22} 

re: 1003题探讨[未登录] 2008-05-25 14:51 diwulechao

#include<stdio.h>
int main()
{
float t,x,i;
scanf("%f",&t);
while (t>0)
{
x=0.5;i=2;
while (x<t) {i++;x+=1/i;}
printf("%.f card(s)\n",i-1);
scanf("%f",&t);
}
}

来自:1003题探讨

分类: 算法艺术 设计模式

上一篇:ACM1001探讨:求高精度幂   下一篇:动态规划与排列组合