c++新手都不算的问题

c++能直接阶乘吗?还是只能用for语句?

12345678_0001
浏览 794回答 2
2回答

莫回无

可以递归#include<iostream>&nbsp;&nbsp;&nbsp;using namespace std;&nbsp;int fac(int);&nbsp;int main(){&nbsp; &nbsp; int n;&nbsp;&nbsp; &nbsp; while (cin >> n)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; cout << n << "!= " << fac(n) << endl;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return 0;}&nbsp;int fac(int x)&nbsp; &nbsp;//递归函数&nbsp;&nbsp;{&nbsp; &nbsp; int f;&nbsp;&nbsp; &nbsp; if (x == 0 || x == 1)&nbsp; &nbsp; &nbsp; &nbsp; f = 1;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; f = fac(x - 1)*x;&nbsp;&nbsp; &nbsp; return f;}

繁华开满天机

没有相关函数,只能自己乘
打开App,查看更多内容
随时随地看视频慕课网APP