Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
水一发普通乘法代码#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std; #define ll long long #define RE freopen("1.in","r",stdin); #define WE freopen("1.out","w",stdout); const int maxn = 50; const int inf = 0x3f3f3f3f; //大数乘,POJ 2389 //逆序乘,乘完再处理进位,之后倒序输出(去前导0) void mul(char *s1, char *s2) { int len1 = strlen(s1); int len2 = strlen(s2); int len = len1 + len2; int p[maxn * 2]; memset(p, 0, sizeof(p)); for (int i = 0; i < len1; ++i) { for (int j = 0; j < len2; ++j) { p[i + j] += (s1[len1 - 1 - i] - '0') * (s2[len2 - 1 - j] - '0'); } } for (int i = 0; i < len; ++i) { if (p[i] >= 10) { p[i + 1] += p[i] / 10; p[i] %= 10; } } while (len > 0 && !p[len]) { len--; } for (int i = len; i >= 0; i--) { printf("%d", p[i]); } printf("\n"); } int main() { char s1[maxn], s2[maxn]; while (~scanf("%s%s", s1, s2)) { mul(s1, s2); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator