poj 1003 Hangover
Hangover
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 150577 | Accepted: 72728 |
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)
宿醉
描述
你能讓一堆卡片懸在桌子上方多遠?如果你有一張卡片,你可以創(chuàng)造出半張卡片長度的最大懸垂。(我們假設紙牌必須垂直于桌子。)用兩張卡片,你可以使頂部的卡片懸在底部的卡片長度的一半,底部的卡片懸在桌子的卡片長度的三分之一,總的最大懸在1/2 + 1/3 = 5/6卡片長度。一般來說,你可以讓n張卡片以1/2 + 1/3 + 1/4 +…+ 1/(n + 1)牌長,上面的牌比第二張高出1/2,第二張比第三張高出1/3,第三張比第四張高出1/4,等等,下面的牌比桌子高出1/(n + 1),如下圖所示。

輸入
輸入由一個或多個測試用例組成,后面跟著一行包含數(shù)字0.00,表示輸入的結(jié)束。每個測試用例都是包含一個浮點數(shù)c的單行,其值至少為0.01,最多為5.20;C正好包含三個數(shù)字。
輸出
對于每個測試用例,輸出所需的最小卡數(shù),以達到至少c卡長度的懸垂。使用示例中所示的輸出格式。
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)
題意:求最小的n讓1+1/2+1/3+...+1/n大于給的一個實數(shù)。
思路:找一個數(shù)sum記錄結(jié)果,寫一個循環(huán)不斷累加 sum+=1.0/n,
直到sum大于c,輸出n-2.
代碼:
#include<stdio.h>
int main()
{
double c,sum;
int n,card;
while(scanf("%lf",&c)!=EOF&&c)
{
if((int)(c*100)==0)
break;
sum=0;
n=2;
while(sum<=c)//循環(huán)模擬。直到超過
{
sum+=1.0/n;
n++;
}
printf("%d card(s)\n",n-2);
}
return 0;
}

