高精度之加减乘除&阶乘的和

输入:50

输出:31035053229546199656252032972759319953190362094566672920420940313

#include<stdio.h>
#include<stdlib.h>

#define length 100000
int b[length];

void add(int a[],int b[])
{
    int i;
    int j;
    for(i=0;i<length-1;i++)
    {
        //printf("%d ",a[i]);
        j = a[i]+b[i];
        if(j>=10)
        {
            b[i] = j%10;
            b[i+1]+=j/10;
        }
        else
            b[i]= j ;
    }
}

int main()
{
    int a[length];
    int i;
    for(i=0;i<length;i++)
        b[i] = 0;
    int n;
    scanf("%d",&n);
    int k;
    for(k=1;k<=n;k++)
    {
        for(i=0;i<length;i++)
        {
            a[i] = 0;
        }
        a[0] = 1;
        //printf("test");
        for(i=1;i<=k;i++)
        {
        int jinwei = 0;
        int j = 0;
        int temp;
        while(j<length)
        {
            temp = jinwei;
            jinwei = (a[j]*i+jinwei)/10;
            a[j] = (a[j]*i + temp)%10;
            j++;
        }
        }
        add(a,b);
    }
    int count;
    count = length-1;
    while(!b[count])
    {
        count--;
    }
    while(count>=0)
    {
        printf("%d",b[count]);
        count--;
    }
    return 0;
}

参考链接:高精度计算_flymist的博客-CSDN博客_高精度计算

阶乘(高精度)_我如一阵风飘过的博客-CSDN博客_高精度阶乘

 

高精度加减乘除:

#include <iostream>
#include <string>
#include <cstring>

#define N 100087

using namespace std;


void Read(int a[])
{
    string s;
    cin>>s;
    a[0]=s.size();
    for(int i=1;i<=a[0];++i)
        a[i]=s[a[0]-i]-'0';
}

void Add(int a[],int b[])
{
    if(a[0]<b[0])  a[0] = b[0];
    for(int i=1;i<=a[0];++i)
    {
        a[i]+=b[i];
        a[i+1]+=a[i]/10;
        a[i]%=10;
    }
    if(a[a[0]+1]>0) a[0]++;
}

int Com(int a[],int b[])
{
	if(a[0]>b[0]) return 1;
	if(a[0]<b[0]) return -1;
	for(int i=a[0];i>0;--i)
	{
		if(a[i]>b[i]) return 1;
		if(a[i]<b[i]) return -1;
 	}
 	return 0;
}

void Minus(int a[],int b[])
{
	for(int i=1;i<=a[0];++i){
		if(a[i]<b[i]){a[i+1]--;a[i]+=10;}
		a[i]=a[i]-b[i];
	}
	while(a[0]>0 && a[a[0]]==0) a[0]--;
}

void Out(int a[])
{
    for(int i=a[0];i>0;--i)
        cout<<a[i];
}

void Mul(int a[],int b[],int c[])
{
    int i,j;
    for(i=1;i<=a[0];++i)
        for(j=1;j<=b[0];++j)
    {
        c[i+j-1]+=a[i]*b[j];
    }
    c[0]=a[0]+b[0];
    for(i=1;i<=c[0];++i)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    while(c[0]>1 && c[c[0]]==0) c[0]--;
}

void numcopy(int b[],int t[],int i)
{
	t[0] = b[0]+i-1;
	for(int j=b[0];j>0;--j)
		t[i+j-1]=b[j];
}

void Div(int a[],int b[],int c[])
{
	int i,t[N];
	c[0] = a[0]-b[0]+1;
	for(i=c[0];i>0;--i)
	{
		memset(t,0,sizeof(t));
		numcopy(b,t,i);
		while(Com(a,t)>=0){
			c[i]++;
			Minus(a,t);
		}
	}
	while(c[0]>0 && c[c[0]]==0) c[0]--;
}

int main()
{
    int a[N],b[N],c[N],f;

    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    Read(a);
    Read(b);
    //Mul(a,b,c);
    //Add(a,b);
    f = Com(a,b);
    if(f==0){cout<<"1";}
    else if(f>0){Div(a,b,c);Out(c);}
    else if(f<0)
    	{cout<<"0";}
    //Out(a);
    return 0;
}

  

  

posted @ 2022-07-12 12:25  kangobs  阅读(86)  评论(0)    收藏  举报