Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1951453
  • 博文数量: 261
  • 博客积分: 8073
  • 博客等级: 中将
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-10 15:23
文章分类

全部博文(261)

文章存档

2013年(1)

2012年(1)

2011年(50)

2010年(34)

2009年(4)

2008年(17)

2007年(55)

2006年(99)

分类:

2007-06-14 10:28:34

题目

Hangover
Time Limit:1000MS  Memory Limit:10000K
Total Submit:21457 Accepted:9612

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)

程序:

#include 
#include 

int compute(float *len, int idx);
int main()
{
        float *len, tmp;
        int i, idx = 0, n;
        if((len = (float *)malloc(sizeof(float))) == NULL)
                return;
        while(1){
                scanf("%f", &tmp);
                if(tmp == 0.00)
                        break;
                len[idx++] = tmp;
                if((len = (float *)realloc(len, sizeof(float)*(idx + 1))) == NULL)
                        return;

        }
        for(i = 0;i < idx;i++){
                n = compute(len, i);
                printf("%d card(s)\n", n);
        }
}

int compute(float *len, int idx)
{
        int i;
        float l = 0;
        for(i = 1;l < len[idx];i++){
                l += 1/((float)i + 1);
        }
        return i - 1;
}

结果

Run ID User Problem Result Memory Time Language Code Length Submit Time
2267283 Accepted 44K 0MS 838B 2007-06-14 09:51:30

阅读(1285) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~