Sicily - 1381 大数乘法

题目:http://soj.sysu.edu.cn/1381

#include <cstdio>
#include <iostream> 
#include <cstring>
#include <algorithm>
using namespace std;

// http://soj.sysu.edu.cn/1381

string a, b;
int c[110];

void mul(string& a, string& b, int* c)
{
    if(a.size() < b.size())    swap(a,b);
    memset(c, 0, sizeof(int)*110);
    for(int i=a.size()-1; i>=0; i--) {
        for(int j=b.size()-1; j>=0; j--) {
            int cur = (a.size()-1-i) + (b.size()-1-j);
            c[cur] += (a[i]-'0')*(b[j]-'0');
            c[cur+1] += c[cur]/10;
            c[cur] %= 10;
        }
    }
    int ok = 0;
    for(int i=a.size()+b.size(); i>=0; i--) {
        if(c[i])    ok = 1;
        if(ok) {
            printf("%d", c[i]);
        }
    }
    cout << endl;
}

int main ()
{
    int t;
    cin >> t;
    while(t--) {
        
        cin >> a >> b;
        if(a=="0" || b=="0") {
            printf("%d\n", 0);
        } else {
            mul(a, b, c);
        }
    }
    
    return 0;    
}

 

posted on 2016-03-22 10:22  SuperChan  阅读(203)  评论(0)    收藏  举报

导航