高精度 加法 减法 乘法 更新ing~

好的cpdd,额跑题了跑题了

先来高精度加法
怎么说 我一看 什么时候成为高精度呢?? 显然易见 很高 嗯 哈哈哈

话说到这就行了 我也不啰嗦了 OK。。。
来 偷袭
先来x个50位的数字相加

///记得输入的时候用字符串哦 不然输入不进去哦
//高精度好烦 还有乘法 减法 我超了 真的 没话说
#include<bits/stdc++.h>
using namespace std;
char shu[55]; //要加的数
int jg[100]={
    
    0}; 结果
int main()
{
    
    
int x;	cin>>x;
	for(int i=0;i<x;i++)
	{
    
    
		cin>>shu;
		int len=strlen(shu);
		if(jg[0]<len)
		jg[0]=len; ///j[0]代表结果的长度

		for(int j=1;j<=len;j++)
		jg[j]+=shu[len-j]-'0';
	}
	for(int i=1;i<=jg[0];i++)
	{
    
    
	if(jg[i]>10)
	{
    
    	jg[i+1]++;
		jg[i]%=10;
		jg[0]+=(i==jg[0]);  //判断最高位有没有进位 如果有那么长度加加
	}
	}
	for(int i=jg[0];i>=1;i--)
	{
    
    
		cout<<jg[i];
		}
}

来 减法
我只学了两个相减 慢慢来


///记得输入的时候用字符串哦 不然输入不进去哦
#include<bits/stdc++.h>
using namespace std;
char shu1[55]; //与qo1[]相互对应 转化成int了哦
char shu2[55];
char zj[55]; //中介啊
int qo1[55];
int qo2[55];
int jg[60]; //结果结果
int main()
{
    
    
	cin>>shu1>>shu2;
	int len1=strlen(shu1);
    int len2=strlen(shu2);
	if(len1 < len2 || (len1 == len2 && strcmp(shu2,shu1) > 0))
    {
    
    
        printf("-");
        strcpy(zj,shu1);
        strcpy(shu1,shu2);
        strcpy(shu2,zj);
    }
    len1=strlen(shu1);
    len2=strlen(shu2);
    for(int i=0;i<len1;i++)
    {
    
    
        qo1[len1-i]=shu1[i]-'0';
    }
    for(int i=0;i<len2;i++)
    {
    
    
        qo2[len2-i]=shu2[i]-'0';
    }

    jg[0]=len1;
    for(int i=1;i<=jg[0];i++)
    {
    
    
        if(qo1[i]>=qo2[i])
            jg[i]=(qo1[i]-qo2[i]);
        else
        {
    
    
            jg[len1-i]=qo1[i]+10-qo2[i];
            qo1[i+1]--;
        }
    }
增加一个消除前导零的步骤;
    while(jg[0]>1&&jg[jg[0]]==0)
    {
    
    
        jg[0]--;
    }

    for(int i=jg[0];i>=1;i--)
        cout<<jg[i];
    return 0;
}

来高精度乘法
来 偷袭,骗
结合这个东西理解
我尽力了啊
jg[i+j-1]+=qo1[i]*qo2[j];

(还有什么来着,没头绪唉)

             1   2   3

     ×       5   6   7

————————————————————————

​              7   14  216   12   18

    5   10   15   

   ——————————————————————

    5    16    34  32  21

///记得输入的时候用字符串哦 不然输入不进去哦
#include<bits/stdc++.h>
using namespace std;
char shu1[505];//存被乘数  //例如 a*b 那么就是存a
char shu2[505];//存乘数 //存b;
char zj[55];
int qo1[505];
int qo2[505];
int jg[600]={
    
    0};
int main()
{
    
    
	cin>>shu1>>shu2;
	int len1=strlen(shu1);
    int len2=strlen(shu2);
	//转化成int
	for(int i=0;i<len1;i++)
    {
    
    
        qo1[len1-i]=shu1[i]-'0';

    }
    for(int i=0;i<len2;i++)
    {
    
    
        qo2[len2-i]=shu2[i]-'0';

    }
    //转化完毕;

    jg[0]=len1+len2;
    for(int i=1;i<=len1;i++)
    {
    
    
        for(int j=1;j<=len2;j++)
        {
    
    
            jg[i+j-1]+=qo1[i]*qo2[j];
        }
    }

    for(int i=1;i<=jg[0];i++)
    {
    
    
        if(jg[i]>=10)
        {
    
    
            jg[i+1]+=jg[i]/10;
            jg[i]%=10;
            jg[0]+=(i==jg[0]);
        }

    }
    //继续处理前导零
    while(jg[0]>1&&jg[jg[0]]==0)
    {
    
    
        jg[0]--;
    }
    for(int i=jg[0];i>=1;i--)
        cout<<jg[i];
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_52172364/article/details/113191645
今日推荐