C++計算一個大數的階乘的代碼


下邊內容段是關於C++計算一個大數的階乘的內容。

include

include

using namespace std;

int main()
{
unsigned int j, n, q, temp;
int i;
double p;

while(1)
{
cout << "nEnter an integer to calculate factorial (0 to exit): ";
cin >> n;

if (n == 0) break;
 
p = 0.0;
for(j = 2; j <= n; j++)
{
}

nd = (int)p + 1;
ca = new unsigned char[nd];
if (!ca)
{
  cout << "Could not allocate memory!!!";
  exit(0);
}
for (i = 1; i < nd; i++)
{
  ca[i] = 0;
}
ca[0] = 1;

p = 0.0;
for (j = 2; j <= n; j++)
{
  for (i = 0; i <= nz; i++)
  {
    q = (temp / 10);
    ca[i] = (char)(temp % 10);
  }
}

cout << "nThe Factorial of " << n << " is: ";
for( i = nd - 1; i >= 0; i--)
{
  cout << (int)ca[i];
}
cout << endl;

delete []ca;    

return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM