這是一個簡單的因子計算器。我用三個不同的參數調用函數factorial
來測試它。出於某種原因,該功能不返回預期值:定製析因函數不按預期工作
#include <iostream>
using namespace std;
int factorial(int a) {
int result = 1;
for (int i = 0; i < a; i++) {
result *= result + 1;
}
return result;
}
int main() {
cout << factorial(4) << endl; // returns 1806, expected 24
cout << factorial(5) << endl; // returns 3263442, expected 120
cout << factorial(6) << endl; // returns -1461943274, expected 720
return 0;
}
階乘的定義是什麼?顯然,你不太熟悉它。 – ForceBru
也許你可以試着在紙上寫下你的程序爲你的一些輸入做什麼。 (不一定是紙張,關鍵是要像你的程序一樣一步一步來) – TGar
並使用你的調試器。這比張貼在這裏更快。 –