Welcome to Leon's Blog  
日历
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 30
  • 文章 - 0
  • 评论 - 51
  • 引用 - 0

导航

常用链接

留言簿(4)

随笔分类

随笔档案

ACM

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
    今天做的这道题感觉上面很奇怪,按照题目要求写了代码,但总觉得哪里有什么地方不对,还请大家多多指教!
 1#include <stdio.h>
 2int main(int argc, char* argv[])
 3{
 4    float s,sum = 0;
 5    int i,j;
 6    while(scanf("%f"&sum) == 1)
 7    {
 8        if(sum != 0)
 9        {
10            j = 2; s = 0;
11            while(s < sum)
12            {
13                s += 1.0/j;
14                j++;
15            }

16            printf("%d card(s)\n", j-2);
17        }

18        else
19            break;
20    }

21        return 0;
22}
posted on 2008-05-24 10:35 Leon916 阅读(1189) 评论(8)  编辑 收藏 引用
评论:
  • # re: 1003题探讨  h0rus1ee Posted @ 2008-05-24 11:10
    我当时是这样写的,好像结果正确:

    #include <stdio.h>

    int main (void)
    {
    int i;
    int counter;

    float sum;
    float c;

    scanf("%f", &c);

    while(c != 0.00 && c >= 0.01 && c <= 5.20) {
    counter = sum = 0;
    for(i = 2; sum < c; i++) {
    sum += 1.00/i;
    counter++;
    }
    printf("%d card(s)\n", counter);
    scanf("%f", &c);
    }

    return 0;
    }  回复  更多评论   

  • # re: 1003题探讨[未登录]  ron Posted @ 2008-05-24 14:25
    h0rus1ee的代码清晰些,  回复  更多评论   

  • # re: 1003题探讨  Leon916 Posted @ 2008-05-24 16:10
    是的,谢谢 h0rus1ee 。  回复  更多评论   

  • # re: 1003题探讨  Leon916 Posted @ 2008-05-24 16:13
    做如下修改:

    1#include <stdio.h>
    2int main(int argc, char* argv[])
    3{
    4 float s,sum = 0;
    5 int j;
    6 while((scanf("%f", &sum) == 1)&& (sum!=0))
    7 {
    8
    9
    10 j = 2; s = 0;
    11 while(s < sum)
    12 {
    13 s += 1.0/j;
    14 j++;
    15 }
    16 printf("%d card(s)\n", j-2);
    17
    18
    19
    20 }
    21 return 0;
    22}  回复  更多评论   

  • # re: 1003题探讨  foxtail Posted @ 2008-05-25 01:16
    没几行代码呀 是做什么的呢??  回复  更多评论   

  • # re: 1003题探讨  Leon916 Posted @ 2008-05-25 08:26
    你可以通过链接去看看题目。  回复  更多评论   

  • # re: 1003题探讨[未登录]  diwulechao Posted @ 2008-05-25 14:51
    #include<stdio.h>
    int main()
    {
    float t,x,i;
    scanf("%f",&t);
    while (t>0)
    {
    x=0.5;i=2;
    while (x<t) {i++;x+=1/i;}
    printf("%.f card(s)\n",i-1);
    scanf("%f",&t);
    }
    }
      回复  更多评论   

  • # re: 1003题探讨[未登录]  Leon916 Posted @ 2008-05-25 19:12
    x+=1/i;
    我觉得这句话写的好像有些问题,应该是x+=1.0/i吧  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © Leon916 Powered by: 博客园 模板提供:沪江博客