HU 1002

1668 ワード

http://acm.hdu.edu.cn/showproblem.php?pid=1002
#include <stdio.h>
#include <string.h>

#define MAX 2000

char a[MAX], b[MAX];
int c[MAX];

int atoi(char c)
{
	return c - '0';
}
//    :        ,      
int main()
{
	int t, i, j, k, l, cnt;
	scanf("%d", &t);
	for(i = 1; i <= t; i++)
	{
		scanf("%s%s", a, b);
		int alen = strlen(a);
		int blen = strlen(b);
		cnt = 0;
		for(j = alen - 1, l = blen - 1, k = 0; j >= 0 && l>= 0; j--,l--)
		{
			c[k++] = (cnt + atoi(a[j]) + atoi(b[l])) % 10;
			cnt = (cnt + atoi(a[j]) + atoi(b[l])) / 10;
		}
		while(j >= 0)
		{
			if(cnt != 0)
			{
				c[k++] = (cnt + atoi(a[j])) % 10;
				cnt = (cnt + atoi(a[j])) / 10;
			}else
			{
				c[k++] = atoi(a[j]);
			}
			j--;
		}
		while(l >= 0)
		{
			if(cnt != 0)
			{
				c[k++] = (cnt + atoi(b[l])) % 10;
				cnt = (cnt + atoi(b[l])) / 10;
			}else
			{
				c[k++] = atoi(b[l]);
			}
			l--;
		}
		if(cnt != 0)
		{
			c[k++] = cnt;
		}
		printf("Case %d:
%s + %s = ", i, a, b); k--; while(k >= 0) { printf("%d", c[k]); k--; } printf("
"); if(i != t) printf("
"); } return 0; }