#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> using namespace std; const int g_kMaxs = 1001; char g_a[g_kMaxs], g_b[g_kMaxs]; void StrRes(char* str) { int len = strlen(str); int i = 0, j = len - 1; while (i < j) { char c = str[i]; str[i] = str[j]; str[j] = c; ++i; --j; } } int* Mul(char* lhs, char* rhs) { if (NULL == lhs || NULL == rhs) { // 爲空則出錯,其他的需要假設保證輸入沒有問題 fprintf(stderr, "Nul: Invalid arguments"); exit(1); } int lhsLen = (int)strlen(lhs); int rhsLen = (int)strlen(rhs); int* ret = new int[lhsLen + rhsLen + 1]; // 兩個數相乘之積的位數不會超過兩個數的位數之和, // 而申請多一位是把積的長度絕對值放在ret[0]中,並用ret[0]的正負代表積的正負 memset(ret, 0, sizeof(int)*(lhsLen + rhsLen + 1)); if ((isdigit(lhs[0]) && isdigit(rhs[0])) || (lhs[0] == '-' && rhs[0] == '-')) { ret[0] = 1; } else ret[0] = -1; if (lhs[0] == '-') --lhsLen; if (rhs[0] == '-') --rhsLen; StrRes(lhs); //把輸入的數字串反轉 StrRes(rhs); for (int i = 0; i < lhsLen; i++) { for (int j = 0; j < rhsLen; j++) { ret[i + j + 1] += ((lhs[i] - '0') * (rhs[j] - '0')) % 10; ret[i + j + 2] += ((lhs[i] - '0') * (rhs[j] - '0')) / 10; } } StrRes(lhs); //把輸入的數字串反轉恢復 StrRes(rhs); if (ret[0] < 0) printf("-"); // ret[0] < 0 爲負數 ret[0] = ret[0] * ((ret[lhsLen + rhsLen] > 0) ? (lhsLen + rhsLen) : (lhsLen + rhsLen - 1)); for (int i = abs(ret[0]); i >= 1; i--) printf("%d", ret[i]); printf("\n"); return ret; } int main() { while (EOF != scanf("%s%s", g_a, g_b)) { Mul(g_a, g_b); } return 0; }