HD1002

大数运算,由于只实现加法,故未使用大数结构体。

#include<stdio.h>
#include<string.h>
int main()
{
	int t, i = 1, lena, lenb;
	char a[1010], b[1010], c[1010];
	c[1009] = '\0';
	scanf("%d", &t);
	for (; i <= t; i++)
	{
		scanf("%s%s", a, b);
		lena = strlen(a);
		lenb = strlen(b);
		int carry = 0, j = 1008, m = lena - 1, n = lenb - 1, tmp;
		while (m >= 0 || n >= 0 || carry)
		{
			tmp = (m >= 0 ? *(a + m) - '0' : 0) + (n >= 0 ? *(b + n) - '0' : 0) + carry;
			c[j] = tmp % 10 + '0';
			carry = tmp / 10;
			m--;
			n--;
			j--;
		}
		printf("Case %d:\n", i);
		printf("%s + %s = %s\n", a, b, c + j + 1);
		if (i < t)
			printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SmartLucius/article/details/81205747