#include <string> #include <iostream> #include <algorithm> using namespace std; intmain() { string s1 = ""; string s2 = ""; string product = ""; //windows Ctrl+Z and Enter to end input //linux Ctrl+D end input while (cin >> s1 >> s2) { //cin >> s1 >> s2; //s1 = "98765432123"; //s2 = "12345678987"; int len1 = s1.size(); int len2 = s2.size(); int carry = 0; int remain = 0; product.resize(len1 + len2);//Maximum length of the product reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); for (int i = 0; i < len1; i++) { for (int j = 0; j < len2; j++) { int tmp = (s1[i] - '0') * (s2[j] - '0') + product[i+j]; carry = tmp / 10; remain = tmp % 10; product[i + j + 1] += carry; product[i + j] = remain; } } if (product[len1+len2-1]==0)//There is no carry at the end, the highest bit is 0 product.resize(product.size() - 1); reverse(product.begin(), product.end()); for (int i = 0; i < product.size(); i++) { cout << char(product[i] + '0'); } cout << endl; //--------------------------------------------- s1 = ""; s2 = ""; product = ""; } return 0; }
Multiplying long integers
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325559507&siteId=291194637
Recommended
Ranking