zoukankan      html  css  js  c++  java
  • 大数相乘的算法实现

    需求:

    2个大的整数(超过计算机内置的整数类型的表示范围,这里用字符数组表示)相乘,如下所示

    "234232398382893809203900923093290" * "23"。

    计算其结果。

    基本思想为把字符数组转化为int数组或者是byte数组(用byte即可,因为只需要存储0-9之间的数字)。然后两个乘数因子的每位数相乘,在相加、进位,即可得到最终的结果。

    举例说明如下:234*45

    234

    *45

    ————

    首先234的每一位乘以5(个数位)得到的结果依次为:

    10 15 20

    234的每一位乘以4(十数位)得到的结果依次为:

    8 12 16

    依次得到的每位的结果相加为:

    0 10 15 20

    8 12 16 0

    ————————

    8 22 31 20

    接下来,进行进位的处理:

    首先是个位数进位到十位,结果为:

    8 22 33 0

    十位进位到百位,结果为:

    8 25 3 0

    百位进位到千位,结果为:

    10 5 3 0

    千位进位到万位,结果为:

    1 0 5 3 0

    所以最终的结果为:10530。该结果存储在一个int数组或者是byte数组中,依次输出即可。

     C语言实现:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define N 100
    
    void ConvertToIntArray(char *s, int *a, int len);
    void multiply(int *a, int *b, int *c, int len1, int len2);
    
    void main()
    {
        // the max length of operator is limited to 100
        char s1[N], s2[N];
        printf("input number 1:");
        scanf("%s", s1);
        printf("input number 2:");
        scanf("%s", s2);
    
        int a[N], b[N];
        int i,j;
        int len1, len2;
        //get the string's length
        //printf("%d
    ", sizeof(s1));
        //printf("%d
    ", strlen(s1));
        len1 = strlen(s1);
        len2 = strlen(s2);
        ConvertToIntArray(s1, a, len1);
        ConvertToIntArray(s2, b, len2);
        
        int c[2*N];
        //clear the result array
        for(i=0; i<2*N; i++)
        {
            c[i] = 0;
        }
        // multiply a and b to c
        multiply(a, b, c, len1, len2);
    
        for(i=2*N-1; i>=0 && c[i]==0; i--);
        printf("
    
    ");
        printf("%s * %s = ", s1, s2);
        for(j=i; j>=0; j--)
        {
            printf("%d", c[j]);
        }
        printf("
    
    ");
    }
    
    /*
     * for example, ['2','3','4'] -> [4, 3, 2]
     *
     */
    void ConvertToIntArray(char *s, int *a, int len)
    {
        
        int i = 0, j = 0;
        for(i=len - 1; i>=0; i--)
        {
            a[j] = s[i] - '0';
            j++;
        }
    }
    
    void multiply(int *a, int *b, int *c, int len1, int len2)
    {
        int i,j;
        for(i=0; i<len1; i++)
        {
            for(j=0; j<len2; j++)
            {
                c[i+j] += a[i] * b[j];
            }
        }
    
        
    for(i=0;i<10;i++)
        //for 234*45, the temporary result is c={20,31,22,8,0,0,...}
        printf("c[%d] = %d ", i, c[i]);
    
        //handle carries, and get the final result
        for(i=0; i<2*N; i++)
        {
            c[i+1] += c[i]/10;
            c[i] = c[i]%10;
        }
        
        for(i=0; i<10; i++)
            //for 234*45, the result is c={0,3,5,0,1,0,0,0,0,0}, you can get the result 10530 just output the array verversely and ignore the previous zero
            printf("c[%d] = %d ", i, c[i]);
    }
    View Code

    运行结果示例:

    input number 1:2222222222222222222222222222222222222222222222222
    input number 2:33333


    2222222222222222222222222222222222222222222222222 * 33333 = 74073333333333333333333333333333333333333333333325926

  • 相关阅读:
    iTerm2使用技巧
    我的mac下有关php扩展的安装
    xmlhttprequest 1.0和2.0的区别,from qq前端哥
    PHP错误日志记录:display_errors与log_errors的区别
    目前php连接mysql的主要方式
    闭包介绍汇总
    接口设计知识总结
    git命令——从GitHub clone XXX分支,本地创建新分支push到远程仓库
    Spring错误——Junit测试——java.net.BindException: Address already in use: bind
    Java.util.Random生成随机数
  • 原文地址:https://www.cnblogs.com/wenwujuncheng/p/3383931.html
Copyright © 2011-2022 走看看