高精度加(乘)法

本文档展示了如何使用C++和Python实现字符串转换为整数数组,并演示了乘法和加法操作,重点在于理解字符数组到整数数组的处理过程。通过代码实例展示了逆序输入、乘法运算和进位处理,适合初学者学习基础数值计算和数组操作。
摘要由CSDN通过智能技术生成

乘法

#include<bits/stdc++.h>

using namespace std;
int main()
{
    int ch;    
    int add=0;
    int len1,len2;
    int a[2000]={},b[2000]={},c[2000]={};
    char a1[2000],b1[2000],c1[2000];
    cin>>a1>>b1;
    int h=0;
     len1=strlen(a1);
     len2=strlen(b1);
     
/*if(len2>len1)没有意义别写 
     {
     strcpy(c1,b1);
     strcpy(b1,a1);
     strcpy(a1,c1);
    h=len2;
    len2=len1;
    len1=h;
     }*/ 
        for(int i=strlen(a1);i>=0;i--)//逆序 
    {
        a[strlen(a1)-i-1]=a1[i]-'0';
        
    }
        for(int i=strlen(b1);i>=0;i--)
    {
        b[strlen(b1)-i-1]=b1[i]-'0';
    }
     
     //    2   123 
     
     //  * 3    23 
     //    6   369
     //       246
     //          282 9 
    
          for(int i=0;i<=len2-1;i++)//乘法 
     {
         
         for(int j=0;j<=len1-1;j++)
         {
             c[j+i]+=b[i]*a[j];
        }
         
    }
     for(int i=0;i<=len1+len2-1;i++)//进位 
     {
         if(c[i]>=10)
         {
             c[i+1]+=c[i]/10;
             c[i]%=10;
             
         }
     }
     int y=len1+len2;//减除多余的0 
    while(ch==0)
    {
        y--;
        ch=c[y]; 
    }
     for(int i=y;i>=0;i--)//倒序输出 
     {
         cout<<c[i];
     }
}

加法

#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
using namespace std;
int h;
    char a1[1000],b1[1000];
int main()
{
    int add=0;
    int a[1000]={},b[1000]={},c[1000]={};
    cin>>a1>>b1;
    for(int i=strlen(a1);i>=0;i--)
    {
        a[strlen(a1)-i-1]=a1[i]-'0';
        
    }
        for(int i=strlen(b1);i>=0;i--)
    {
        b[strlen(b1)-i-1]=b1[i]-'0';
    }
    for(int i=0;i<max(strlen(a1),strlen(b1));i++)
    {
        c[i]+=a[i]+b[i];
        c[i+1]=c[i]/10;
        c[i]%=10;    
    }
if(c[max(strlen(a1),strlen(b1))]!=0)
{
    add=1;
}
    for(int i=max(strlen(a1),strlen(b1))+add-1;i>=0;i--)
    {
        h=max(strlen(a1),strlen(b1))+add-1;
        cout<<c[i];
    }
    
 } 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值