POJ 1003
原题
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)
翻译
描述
您可以将多张纸牌悬在桌子上多远?如果您有一张卡,则最多可以创建一半的卡长。(我们假设这些卡片必须垂直于桌子。)使用两张卡片,您可以使最上面的卡片悬垂在底部的卡片长度的一半,而最下面的卡片悬垂在桌面的卡片长度的三分之一,总最大悬垂为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)。如下图所示。
输入项
输入由一个或多个测试用例组成,其后一行包含表示输入结束的数字0.00。每个测试用例都是一条包含正浮点数c的单行,其浮点数c至少为0.01且最大为5.20;c将精确地包含三个数字。
输出量
对于每个测试用例,输出实现至少c个卡长度的突出所需的最小卡数。使用示例中显示的确切输出格式。
样本输入
1.00
3.71
0.04
5.19
0.00
样本输出
3卡
61卡
1卡
273卡
思路分析:
根据题中的测试数据可以知道这个问题的规模并不大,题中说输入的最大为5.20,而测试数据中的5.19的输出结果为273, 因此可以知道数据的大概规模。
我的思路是先在一个数组中将结果存贮下来,这样就不必每次都循环判断,降低时间复杂度。在定义数组时不必规定很大,但是也不要规定为274左右,虽然数据规模在273右边一点,但是要知道分母越来越大,数字 1 除以这个数字的结果很小,所以求和时即使是0.01也可能是还几个数字相加的结果,因此这里讲数组定为280个
然后循环判断即可
另外, 1/n 是发散的,并没有直接的求和公式
下面是AC代码
#include <stdio.h>
#include <math.h>
int main()
{
int i;
float temp,sum=0; //temp用于存放每一个分数的结果, sum用于存放累加和
float resu[280]={0,0}; //用于存放所有结果
float n; //输入测试数据
scanf("%f",&n);
for(i=2;i<280;++i) //计算所有结果并存储
{
temp=1.0/i;
sum+=temp;
resu[i]=sum;
}
while(n != 0) //循环判断
{
for(i=2;i<280;++i)
{
if(resu[i] > n && resu[i-1] < n)
printf("%d card(s)\n",i-1); //打印结果,注意结果格式
}
scanf("%f",&n);//用于循环
}
return 0;
}
344

被折叠的 条评论
为什么被折叠?



