加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数相乘

发布时间:2020-12-14 01:43:21 所属栏目:大数据 来源:网络整理
导读:#include iostream using namespace std ; void matoi ( const string s , short * arr ) { for ( int i = 0 ; i s . length () ; ++ i ) { arr [ i ] = s [ i ] - '0' ; } } void printArr ( short * arr , int len ) { bool begin = false ; for ( int i =
#include <iostream>
using namespace std;

void matoi(const string s,short * arr) {
    for (int i = 0; i < s.length(); ++i) {
        arr[i] = s[i] - '0';
    }
}

void printArr(short * arr,int len) {
    bool begin = false;
    for (int i = 0; i < len; ++i) {
        if(begin) {
            cout << arr[i];
        } else if(!begin && arr[i] != 0){
            begin = true;
            cout << arr[i];
        }
    }
    if(!begin) cout << 0 << endl;
    cout << endl;
}

int main() {
    string s1,s2;
    short i1[500],i2[500],res[1000];
    int len = 0;
    cin >> s1 >> s2;
    matoi(s1,i1);
    matoi(s2,i2);

    for (int k = 0; k < 200; ++k) {
        res[k] = 0;
    }

    for (int i = 0; i < s1.length(); ++i) {
        len = i + 1;
        for (int j = 0; j < s2.length(); ++j) {
            res[len] += i1[i] * i2[j];
            int index = len;
            while(index >= 1 && res[index] >= 10) {
                res[index-1] += res[index] / 10;
                res[index] = res[index] % 10;
                -- index;
            }
            len ++;
        }
    }


    printArr(res,len);
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读