大数加法 模版 HDU 1002

#include <iostream>
#include <cstring>
using namespace std;
char num1[1110], num2[1110], num3[1110];
int L, M;
int mark;
void add(char a[], char b[], char c[]) {
	int len1 = strlen(a);
	int len2 = strlen(b);
	int i, j;
	int l = 0;
	L = 0;
	int temp;
	for(i = 0; i < len1/2; i++) {
		temp = a[i];
		a[i] = a[len1-1-i];
		a[len1-1-i] = temp;
	}
	for(i = 0; i < len2/2; i++) {
		temp = b[i];
		b[i] = b[len2-1-i];
		b[len2-1-i] = temp;
	}
	for(i = 0; i < len1 && i < len2; i++) {
		M = (a[i] - '0' + b[i] - '0' + L) % 10;
		L = (a[i] - '0' + b[i] - '0' + L) / 10;
		c[i] = M + '0';
	}
	if(i < len1) {
		for( ; i < len1; i++) {
			M = (a[i] - '0' + L) % 10;
			L = (a[i] - '0' + L) / 10;
			c[i] = M + '0';
		}
	}
	if(i < len2) {
		for( ; i < len2; i++) {
			M = (b[i] - '0' + L) % 10;
			L = (b[i] - '0' + L) / 10;
			c[i] = M + '0';
		}
	}
	while(L) {
		c[i++] = L + '0';
		L /= 10;
	}
	mark = i-1;
}
int main() {
	int i, j;
	int len1, len2;
	int n,cnt = 0;
	cin>>n;
	while(n--){
		scanf("%s%s",num1,num2);
		printf("Case %d:\n",++cnt);
		printf("%s + %s = ",num1,num2);
	len1 = strlen(num1);
	len2 = strlen(num2);
	add(num1,num2,num2);
	
	for(i = mark; i >= 0; i--)
		printf("%c",num2[i]);
		printf("\n");
		if(n!=0)printf("\n");	
	}
	
	return 0;

}

 

上一篇:[KMP算法]C. 【例题3】周期长度和


下一篇:百炼OJ - 1002 - 方便记忆的电话号码