Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

水一发普通乘法代码

Posted by 1007783493 at 2016-06-18 01:57:04 on Problem 2389
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator