⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jiecheng.cpp

📁 求大数的阶乘问题源代码,在vs2005上亲自调试过。
💻 CPP
字号:
// JieCheng.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include<iterator>
#include<algorithm>

using namespace std;

void zerosOfJieCheng(int n);
int weishuOfJieCheng(int n);
void resultOfJieCheng(int n);

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	cout<<"Please input an Integer :";
	cin>>n;
	zerosOfJieCheng(n);
	cout<<"The WeiShu of JieCheng of "<<n<<" is :"<<weishuOfJieCheng(n)<<endl;
	resultOfJieCheng(n);
	return 0;
}

void zerosOfJieCheng(int n)
{
	int count=0;
	int tem;
	for(int i=5;i<=n;i+=5)
	{
		tem=i;
		while(tem%5==0)
		{
			tem/=5;
			++count;
		}
	}
	cout<<"The last zeros of JieCheng of "<<n<<" is :"<<count<<endl;
}

int weishuOfJieCheng(int n)
{
	double sum = 0;
	for(double i=1;i<=n;i++)
	{
		sum +=log10(i);
	}

	return sum+1;
}

void resultOfJieCheng(int n)
{
	int carry,j,temp,digit = 1;
	int weishu = weishuOfJieCheng(n);	//计算n的阶乘的位数
    int *a = new int[weishu];			//动态分配数组的大小
    a[0]=1;
	cout<<"\nj a[j-1] carry temp"<<endl;
    for(int i=2; i<=n; i++)
    {
		cout<<i<<endl;
        for(j=1,carry=0; j<=digit; ++j)
        {
            temp=a[j-1]*i+carry;
            a[j-1]=temp%10;		//底位
            carry=temp/10;		//高位

			cout<<j<<":"<<a[j-1]<<","<<carry<<","<<temp<<endl;
        }

        while(carry)
        {
            //digit++;
            a[++digit-1]=carry%10;
            carry/=10;
			cout<<digit<<":"<<a[digit-1]<<endl;
        }

		cout<<endl;
    }
    cout<<"The factorial of "<<n<<" is:"<<endl;
    for(int k=digit; k>=1; --k)
        cout<<a[k-1];
    cout<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -