标题:新人求助!C++
只看楼主
进击的种子
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-12-29
结帖率:40%
已结贴  问题点数:10 回复次数:1 
新人求助!C++
递归函数求阶乘‘’
输出时多输出了第三行空格
怎么去掉第三行

程序代码:
#include<iostream>
using namespace std;
int t;
int fac(int);
int main()
{
    int x;
    cin>>x;
    fac(x);
    cout<<t<<endl;
    return 0;
}
int fac(int x)
{
    if(x==1)
    t=1;
    else
    {
        fac(x-1);
        t*=x;
    }
    
}

搜索更多相关主题的帖子: C++ 输出 std int return 
2017-12-30 08:06
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:10 
#include<iostream>
using namespace std;
int fac(int);
int main()
{
    int x;
    cin>>x;
    int t = fac(x);
    cout<<t<<endl;
    return 0;
}
int fac(int x)
{
    int t=0;
    if (x < 0)
        cout << "x<0, error!" << endl;
    else if (x < 2)
        t = 1;
    else
        t = x * fac(x-1);
    return t;
}
2017-12-30 10:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-483839-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.615697 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved