zoukankan      html  css  js  c++  java
  • 大数乘法

    给出2个大整数A,B,计算A*B的结果。

     
    Input
    第1行:大数A
    第2行:大数B
    (A,B的长度 <= 1000,A,B >= 0)
    Output
    输出A * B
    Input示例
    123456
    234567
    Output示例
    28958703552

    解法:
    第i位数乘第j位数,乘积是第i+j位数(从0开始) 
    如123*456 
    乘积各位数为 
    个位 3*6 
    十位 2*6 + 3*5 
    百位 2*5 + 1*6 + 3*4 
    千位 1*5 + 2*4 
    万位 1*4 
    然后从后往前,取余更新。

     1 #include <iostream>
     2 #include <string.h>
     3 
     4 using namespace std;
     5 
     6 char A1[1003],B1[1005];
     7 int A[1010]={0},B[1010]={0},C[2020]={0};
     8 
     9 int main()
    10 {
    11     int len1,len2;
    12     cin>>A1>>B1;
    13     len1 = strlen(A1);
    14     len2 = strlen(B1);
    15 
    16     for(int i = 0;i <len1;i++)
    17         A[len1-i-1] = A1[i] - '0';
    18     for(int i = 0;i <len2;i++)
    19         B[len2-i-1] = B1[i] - '0';
    20 
    21    
    22 
    23     for(int i = 0;i < 1010;i++)
    24         for(int j = 0;j <1010;j++)
    25         {
    26             C[i+j] += A[i]*B[j];
    27         }
    28 
    29     for(int i = 0;i < 2020-1;i++)
    30     {
    31         C[i+1] += C[i] / 10;
    32         C[i] = C[i] % 10;
    33     }
    34 
    35     int t = 2020;
    36     while(C[t]==0)
    37         t--;
    38 
    39     for(int i = t;i>=0;i--)
    40         cout<<C[i];
    41     cout<<endl;
    42 
    43     return 0;
    44 }
  • 相关阅读:
    正则表达式详解<一>
    multimap详讲
    map详讲<二>
    map详解<一>
    priority_queue详解
    容器适配器(一):queue
    用 input() 函数返回的数据是字符串类型
    学习python的基本了解
    学习oracle的SQL语句 练习
    oracle 练习题
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7500238.html
Copyright © 2011-2022 走看看