Sicily 1381. A * B

Source: Internet
Author: User
Constraints

Time Limit: 1 secs, memory limit: 32 MB

Description

Give two positive integers A and B, please help us calculate a * B.

Input

The first line of the input is a positive integer T. T is the number of test cases followed.

Each test case contain two integer a, B (0 <= A <= 10 ^ 100, 0 <= B <= 10,000) given in one line.

Output

The output of each test case shoshould consist of one line, contain the result of a * B.

Sample Input
12 7
Sample output
14


There is nothing to say: high-precision multiplication. Strings replace numbers for multiplication and convert multiplication to string addition.
Example: "1234" * "123" = "123400" +
"12340" + "12340" +
"1234" + "1234" + "1234"

#include <iostream>#include <string>using namespace std;string add(string a, string b){    if(b.length() > a.length()){        string temp = a;        a = b;        b = temp;    }    a = "0" + a;    int i, len = b.length();    for(i = 0; i < a.length() - len; i++){        b = "0" + b;    }    int in = 0, t;    for(i = a.length() - 1; i >= 0; i--){        t = in;        in = (a[i] - ‘0‘ + b[i] - ‘0‘ + t)/10;        a[i] = (a[i] - ‘0‘ + b[i] - ‘0‘ + t)%10 + ‘0‘;    }    if(a[0] == ‘0‘){        a = a.substr(1, a.length()-1);    }    return a;}string multiply(string a, string b){    string temp;    if(b.length() > a.length()){        temp = a;        a = b;        b = temp;    }    int i, j;    string sum = "0";    for(i = 0; i < b.length(); i++){        temp = a;        for(j = 0; j < b.length()-i-1; j++){            temp = temp + "0";        }        for(j = 0; j < b[i]-‘0‘; j++){            sum = add(sum, temp);        }    }    return sum;}int main(){    int t;    cin >> t;    while(t--){        string a, b;        cin >> a >> b;        cout << multiply(a, b) << endl;    }    return 0;}

 

Sicily 1381. A * B

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.