代碼:
#include <iostream>
using namespace std;
int main() {
int answer = 1;
int i = 1;
for (; i <= 50; i ){
answer = answer * i;
}
cout << answer << endl;
return 0;
}
結果:
0
...Program finished with exit code 0
Press ENTER to exit console.
當我在在線 C 編譯器中運行此代碼時,它在控制臺中顯示零(0)。為什么?
uj5u.com熱心網友回復:
我將具體回答被問到的問題“為什么?” 而不是在評論“如何?”中添加的那個。
您得到結果 0,因為 的中間值之一answer
是 0,任何與它相乘的值都將保持為 0。
這是中間值(我通過將您的輸出移動到回圈中找到了它們。):
1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600
1932053504
1278945280
2004310016
2004189184
-288522240
-898433024
109641728
-2102132736
-1195114496
-522715136
862453760
-775946240
2076180480
-1853882368
1484783616
-1375731712
-1241513984
1409286144
738197504
-2147483648
-2147483648
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
例如這里https://www.tutorialspoint.com/compile_cpp_online.php
現在來解釋為什么其中一個是 0 開始:
由于這些值,faculties 的序列,很快離開了所選資料型別中可表示的值范圍(請注意,數字十進制數字在某些時候不會增加;盡管二進制數字是相關的數字)。
在那之后,這些值不再與正確的值真正相關,甚至可以看到它們跳到零以下然后又跳回來……
其中一個恰好是 0。
對于“如何?” 請查看評論(也許還有其他有價值的答案)。
uj5u.com熱心網友回復:
簡答:您的代碼無法正常作業,因為它執行 50 階乘,答案是 3.04*10^64。這個數字大于 int 大小,即 2^31 - 1。
長答案 您可以檢查記錄中間答案的問題。這可以幫助您對代碼情況有一些了解。在這里您可以看到數字從正數旋轉到負數,這顯示了此代碼策略的最大可能乘法。 https://onlinegdb.com/ycnNADKmX
答案 30414093201713378043612608166064768844377641568960512000000000000
要存檔任何階乘情況的正確答案,您需要有一些策略來操作大量數字。事實上,如果您在一家大公司作業,您可能有一些庫可以處理大量數字。在這種情況下,使用這個庫來保持代碼一致是非常重要的。另一方面,假設這是一個學術作業,您可以選擇互聯網上的任何策略。在這種情況下,我使用了使用字串來表示大數的策略。您可以在此處查看解決方案https://www.geeksforgeeks.org/multiply-large-numbers-represented-as-strings
計算 50 的最終程式!以正確的方式使用字串策略來表示大數,您可以在此處找到https://onlinegdb.com/XRL9akYKb
PS:我會將完整的答案放在這里以存檔代碼以備將來參考。
#include <iostream>
#include<bits/stdc .h>
using namespace std;
//@see https://www.geeksforgeeks.org/multiply-large-numbers-represented-as-strings/
// Multiplies str1 and str2, and prints result.
string multiply(string num1, string num2)
{
int len1 = num1.size();
int len2 = num2.size();
if (len1 == 0 || len2 == 0)
return "0";
// will keep the result number in vector
// in reverse order
vector<int> result(len1 len2, 0);
// Below two indexes are used to find positions
// in result.
int i_n1 = 0;
int i_n2 = 0;
// Go from right to left in num1
for (int i=len1-1; i>=0; i--)
{
int carry = 0;
int n1 = num1[i] - '0';
// To shift position to left after every
// multiplication of a digit in num2
i_n2 = 0;
// Go from right to left in num2
for (int j=len2-1; j>=0; j--)
{
// Take current digit of second number
int n2 = num2[j] - '0';
// Multiply with current digit of first number
// and add result to previously stored result
// at current position.
int sum = n1*n2 result[i_n1 i_n2] carry;
// Carry for next iteration
carry = sum/10;
// Store result
result[i_n1 i_n2] = sum % 10;
i_n2 ;
}
// store carry in next cell
if (carry > 0)
result[i_n1 i_n2] = carry;
// To shift position to left after every
// multiplication of a digit in num1.
i_n1 ;
}
// ignore '0's from the right
int i = result.size() - 1;
while (i>=0 && result[i] == 0)
i--;
// If all were '0's - means either both or
// one of num1 or num2 were '0'
if (i == -1)
return "0";
// generate the result string
string s = "";
while (i >= 0)
s = std::to_string(result[i--]);
return s;
}
// Calculates the factorial of an inputed number
string fact(int in) {
string answer = "1";
for (int i = 2 ; i <= in; i ) {
string tmp = std::to_string(i);
answer = multiply(answer, tmp);
}
return answer;
}
int main()
{
string answer = fact(50);
cout << answer << endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374906.html