• 大数乘法


    原理上也是采用数组模拟。

     a[i]   12345

     b[j]        23

     用c[k]来保存每次的运算结果,k=i+j;

     c[i+j]=c[i+j]+a[i]*b[j];

     这里来模拟一次乘法过程:

                123

             *   12

          --------------

                 246   

            + 123

          --------------

                1476

    #include<iostream>
    #include<string.h>
    usingnamespace std;
    
    
    int main(void)
    {
        char s1[510],s2[510],temp[510];
        int a[510],b[510],c[1010];
        while(scanf("%s%s",s1,s2)==2)
        {
            int i,j,h;
            int len1,len2;
            if(strlen(s1)<strlen(s2))
            {
                strcpy(temp,s1);
                strcpy(s1,s2);
                strcpy(s2,temp);
            }
            len1=strlen(s1);
            len2=strlen(s2);
            memset(c,0,sizeof(c));
            for(i=len1-1,j=0;i>=0;i--,j++)
            {
                a[j]=s1[i]-48;
            }
            for(i=len2-1,j=0;i>=0;i--,j++)
            {
                b[j]=s2[i]-48;
            }
            for(i=0;i<len2;i++)
            {
                for(j=0;j<len1;j++)
                {
                    c[i+j]=a[j]*b[i]+c[i+j];
                }
            }
            for(i=0;i<2*len1;i++)
            {
                if(c[i]>=10)
                {
                    c[i+1]=c[i+1]+c[i]/10;
                    c[i]=c[i]%10;
                }
            }
            i=2*len1;
            while(c[i]==0)
            {
                i--;
            }
            if(i<0)
            {
                printf("0
    ");
            }
            else
            {
                for(;i>=0;i--)
                    printf("%d",c[i]);
                printf("
    ");
            }    
        }
        return0;
    }


  • 相关阅读:
    python count函数
    kubenetes服务发现
    k8s网络
    k8s创建pod流程
    openstack创建虚拟流程、各组件介绍
    生产者消费者问题
    Date类和Calendar类
    Timer定时器
    Java中的克隆
    注解
  • 原文地址:https://www.cnblogs.com/Tovi/p/6194923.html
Copyright © 2020-2023  润新知