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