acm程序设计实验

 

 

1. 实验题目

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 twocards 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 totalmaximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cardsoverhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, wherethe top card overhangs the second by 1/2, the second overhangs tha third by1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangsthe table by 1/(n + 1). This isillustrated in the figure below.

Input

The input consists of one or more test cases, followed bya line containing the number 0.00 that signals the end of the input. Each testcase is a single line containing a positive floating-point number c whose valueis 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 leastc card lengths. Use the exact output format shown in the examples.

 

Sample Input

1.00

3.71

0.04

5.19

0.00

 

Sample Outpu

3 card(s)

61 card(s)

1 card(s)

273 card(s)

 

 

 

2. 需求分析

1. 该程序编写与运行环境VC++6.0;

2. 输入的形式和输入值的范围: 输入一个测试用例或多个测试用例c,要求是浮点数(3位数)并且它的输入范围在[0.01,5.20],当输入0.00时,结束程序。

3.  输出的形式:对于每个测试案例,输出达到c长度所需的最小数量的卡片。输出要求是浮点数(要有三位数);

如:Input:

1.00

Output:

3 card(s)

4. 程序所能达到的功能:

计算出达到c长度所需要的最少卡片数量;

5. 测试数据:

输入数据有:                    
1.00
3.71
0.04

5.19

0.00

   输出对应:

   3   card(s)

61  card(s)

1    card(s)

273 card(s)

 

 

3.概要设计

   1.为了实现以上程序的功能,需要定义的数据结构(对数据的描述)有int,float型两种;

   2.本程序包含的函数只有一个主函数main函数,所用的循环有while循环

4.详细设计

   1.输入数据类型与主函数内数据定义与初始化

     float  c, sum, n;   

     int    i;

     i = 1; n = 2.00; sum = 1.00 / n;

2.输入语句与输出语句的实现

  cin>> c;

  cout<<i <<"card(s)" << endl;  

3.while循环的实现

 while((cin >> c) && c!= 0.00)

   {   

       i = 1;   

       n = 2.00;   

       sum = 1.00 / n;   

       while(sum < c) {   

           n += 1.00;   

           sum += (1.00 / n);   

          i++;   

       }   

    cout <<i <<"card(s)" << endl;    

    }

 

5.调试分析

此函数结构比较简单,只有一个主函数,所以调试执行直接从主函数开始,遇到输出调试便结束。

 

6.使用说明

 #include <iostream.h>    

intmain()   

{    float c, sum, n;   

    int i;   

    while((cin >> c) && c!= 0.00)

  {     i= 1;  n = 2.00;    sum = 1.00 / n;   

        while(sum < c) {   

            n += 1.00;   

            sum += (1.00 / n);   

           i++;   

        }   

     cout <<i <<"card(s)"<< endl;    

    }   return 0;   

}

以上程序在VC++6.0环境下编写与运行的,该程序采用c++编程,执行本程序要求输入浮点数,总位数为3位数,当输入0.00时结束程序;执行成功后输出相对应的数值;

 

 

7.测试结果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯子佳人傻了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值