首页 > 题解 > CV3115-3117高精度

CV3115-3117高精度

都是以前写残了的,别吐槽。。。

不知为何高精除在钻石233

高精乘

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int* suma(int m[],int n[]);
int* mul(int m[],int e);
int main(void){
	const int max = 200;
	int m[max] = {0};
	int n[max] = {0};
	int c[2*max] = {0};
	string a,b;
	cin >> a;
	int s = a.length();
	cin >> b;
	int k = b.length();
	if(s>=k){
	for(int i = 0;i<s;i++)
		m[s-i-1] = a[i]-'0';
	for(int i = 0;i<k;i++)
		n[k-i-1] = b[i]-'0';
	}
	else{
		int temp = s;
		s = k;
		k = temp;
        for(int i = 0;i<s;i++)
		m[s-i-1] = b[i]-'0';
	    for(int i = 0;i<k;i++)
		n[k-i-1] = a[i]-'0';
	}
	int y = 0,i = 0,j = 0,e = 0;
	for(i = 0;i<k;i++){
		y = 0;
		for(j = 0;j<s;j++){
			c[2*max-y-e-1] += m[j]*n[i];
			y++;
		}
		e++;
		}
	int p = 2*max-1;
	while(p>0){
		c[p-1] += c[p]/10;
		c[p] %= 10;
		p--;
	}
	int z = 0;
	while(c[z]==0){
	    z++;
	}
	for(int i = z;i<2*max;i++){
		printf("%d",c[i]);
	}
}

高精减

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int gjj(string s1,string s2)
{
    string zzz;
    cin>>s1>>s2;
    int sa[501]={0},sb[501]={0},c[501]={0},l=1;
    sa[0]=s1.length();
    sb[0]=s2.length();
    if(sb[0]>sa[0])
    {
        cout<<"-";
        zzz=s1;
        s1=s2;
        s2=zzz;
    }
    if(sb[0]==sa[0])
    {
        for(int i=0;i<sa[0];i++)
        {
            if(s2[i]>s1[i])
            {
                cout<<"-";
                zzz=s1;
                s1=s2;
                s2=zzz;
                break;
            }
            if(s1[i]>s2[i])
            {
                break;
            }
        }
    }
    sa[0]=s1.length();
    sb[0]=s2.length();
    for(int i=1;i<=sa[0];i++)
    {
        sa[i]=s1[sa[0]-i]-'0';
    }
    for(int i=1;i<=sb[0];i++)
    {
        sb[i]=s2[sb[0]-i]-'0';
    }
    while(l<=sa[0] || l<=sb[0])
    {
        if(sa[l]<sb[l])
        {
            sa[l]+=10;
            sa[l+1]--;
        }
        c[l]=sa[l]-sb[l];
        l++;
    }
    l-=1;
    while(c[l]==0 && l>1)
        l--;
    while(l>0)
    {
        cout<<c[l];
        l--;
    }
}
main()
{
    string a,b;
    gjj(a,b);
    cout<<endl;
    return 0;
}

高精加

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
main()
{
	char a1[500]={0},b1[500]={0};
  	int a[500]={0},b[500]={0},c[500]={0},lena,lenb,lenc,i,x;
	int n;
	scanf("%s%s",a1,b1);
	n=max(strlen(a1),strlen(b1));
	lena=strlen(a1);
  	lenb=strlen(b1);
  	for (i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48;
    for (i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48;
    lenc =1;
    x=0;
    while (lenc <=lena||lenc <=lenb)
	{
            c[lenc]=a[lenc]+b[lenc]+x;
            x=c[lenc]/10;
            c[lenc]%=10;
            lenc++;
	}
	c[lenc]=x;
	if (c[lenc]==0)
		lenc--;
	for (i=lenc;i>=1;i--)
	cout<<c[i];
	cout<<endl;
	return 0;
}