关于 C 语言 For 循环的问题请教 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
FalconWang
V2EX    C

关于 C 语言 For 循环的问题请教

  •  
  •   FalconWang 2018-11-22 20:12:49 +08:00 2794 次点击
    这是一个创建于 2529 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int level(int l)
    {
    int sum;
    if(l==1)
    sum=1;
    else
    sum=level(l-1)*2;
    return(sum);
    }

    int main()
    {
    int level(int l);
    char ch;
    int a,b=1;
    printf("Study is a long and hard road.\n");
    printf("The more you study,the further you will reach.\n");
    printf("Such as if you learn 1 year, you will reached level %d.\n",level(1));
    do
    {
    b=b+1;
    printf("such as if you learn %d years, you will reached %d level.\n",b,level(b));
    printf("do you want to continue?(y/n).\n");
    ch=getchar();
    getchar();
    }while(ch=='y'||ch=='Y');
    }

    期望结果为:
    学习 1 年,到达第 1 级
    学习 2 年,到达第 2 级
    以此类推...

    上面这段程序结果:到达的级别数呈指数增长,想知道原因,拜托各位了,感激不尽...
    4 条回复    2018-11-23 11:45:19 +08:00
    weifengzi2009
        1
    weifengzi2009  
       2018-11-22 20:21:38 +08:00
    你这个 level 函数递归每次都乘以 2,那不是 2*2*2*2*2 这样子么?
    yemoluo
        2
    yemoluo  
       2018-11-22 20:29:36 +08:00   1
    ```
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>

    int level(int l)
    {
    return l == 1 ? 1 : level(l-1)+1;
    }

    int main()
    {
    char ch;
    int a,b=1;

    printf("Study is a long and hard road.\n");
    printf("The more you study,the further you will reach.\n");
    printf("Such as if you learn 1 year, you will reached level %d.\n",level(1));

    do {
    b=b+1;
    printf("such as if you learn %d years, you will reached %d level.\n",
    b,level(b));
    printf("do you want to continue?(y/n).\n");

    ch=getchar();
    getchar();

    } while(ch == 'y'|| ch== 'Y');
    }
    ```

    测试格式化代码

    以上是楼主代码的简化和想要的版本
    FalconWang
        3
    FalconWang  
    OP
       2018-11-22 20:32:36 +08:00
    感谢各位解答 问题已解决
    wutiantong
        4
    wutiantong  
       2018-11-23 11:45:19 +08:00
    @GTim 只有主题正文支持 markdown,回复内容不支持。。。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2594 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 13:42 PVG 21:42 LAX 06:42 JFK 09:42
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86