zju1045題解

題目:

HangOver


Time Limit: 1 Second      Memory Limit: 32768 KB


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.

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.

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.


Example input:

1.00
3.71
0.04
5.19
0.00


Example output:

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


 

Source: Mid-Central USA 2001

題目大意:你可以把一疊卡片放的離桌子多遠?如果有一張卡片,那麼可達到的最遠距離是卡片唱的的一半(假設卡片必須與桌子的邊緣垂直),是用兩張卡片,是上面一張能放到的最遠距離超過下面長度的一半,而下面一張超過桌子的是卡片長度的1/3,所以能達到的最遠距離是:1/2+1/3=5/6。一半來說,n張卡片能達到的最遠距離是1/2+1/3+1/4+....+1/(n+1),也就是最頂上的卡片超出第二張的1/2,第二張超出第三張的1/3,第三張超出第四張的1/4,等等,輸入一行數字0.00時表示輸入結束,每個測試例一行,是一個浮點數c(0.01<=c<=5.20),c剛好是3位數字。對每個測試例,輸出達到距離c所需要的最少卡片的數量。注意輸出格式!

算法分析:其實這道題就是1/2+1/3+...+1/(n+1)<=c,利用循環語句知道上面的等式超過c爲止。

代碼:

語言:c

#include<stdio.h>

int main()

{

float c;

while(scanf("%f",&c)&&c!=0)

{

int i=2;

float result=0;

while(result<c)

{

result=result+1.00/i;

++i;

}

printf("%d card(s)/n",i-2)

}

return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章