Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 130664 | Accepted: 63540 |
How far can you make a stack of cardsoverhang a table? If you have one card, you can create a maximum overhang ofhalf a card length. (We're assuming that the cards must be perpendicular to thetable.) With two cards you can make the top card overhang the bottom one byhalf a card length, and the bottom one overhang the table by a third of a cardlength, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. Ingeneral you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) cardlengths, where the top card overhangs the second by 1/2, the second overhangstha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottomcard overhangs the table by 1/(n + 1). This is illustrated in the figure below.
Input
The input consists of one or more testcases, followed by a line containing the number 0.00 that signals the end ofthe input. Each test case is a single line containing a positive floating-pointnumber c whose value is at least 0.01 and at most 5.20; c will contain exactly threedigits.
Output
For each test case, output the minimumnumber 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) 61card(s) 1 card(s) 273 card(s)
程序代码:
#include<stdio.h>
double sum(int a); //定义一个double型的求和函数,用来求放置a个卡片后露出桌面的长度和
int main()
{
int n; //n表示卡片的数量
float C; //C表示输入的长度(浮点型)
scanf("%f",&C); //输入
while(C!=0) //当输入的C不等于0时就进入循环进行判断和计算
{
for(n=1;;n++) //因为最少是一块,所以n赋初值为1,每次增加一块
{
if(C<=sum(n)) //如果卡片露出桌面的长度和大于等于输入的长度C时,跳出循环
break;
}
printf("%d card(s)\n",n); //输出
scanf("%f",&C); //再次输入,进入下一次循环,直到输入0时停止
}
return 0;
}
double sum(int a) //累加求和函数
{
int i;
double s=0;
for(i=1;i<=a;i++)
{
s=s+1/(i+1.0);
}
return s;
}