循环版算出来一样吗
运行下面这段程序:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
long long fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}
long long fact_loop(int n) {
long long r = 1;
for (int i = 2; i <= n; i++) r *= i;
return r;
}
int main() {
cout << boolalpha << (fact(5) == fact_loop(5)) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)