#include<iostream>
#include<string.h>
using namespace std;
void add(char a[], char b[], char tmp[]);
int main()
{
int t;
cin >> t;
for(int i = 1; i <= t; i++)
{
char a[1005], b[1005], tmp[1005];
a[0] = b[0] = '0';
scanf("%s %s", &a[1], &b[1]);
add(a, b, tmp);
printf("Case %d:\n", i);
printf("%s + %s = ", &a[1], &b[1]);
if(tmp[0] != '0') printf("%c",tmp[0]);
printf("%s\n", &tmp[1]);
if(i != t)
cout << endl;
}
return 0;
}
void add(char a[], char b[], char tmp[])
{
int carry = 0, t;
int sza = strlen(a), szb = strlen(b);
int sz = (sza > szb)? sza: szb;
tmp[sz] = '\0';
for(int i = sz - 1; i >= 0; i--)
{
sza--; szb--;
if(sza >= 0 && szb < 0)
{
t = a[sza]-48 + carry, carry = 0;
if(t > 9)
tmp[i] = t - 10 +48, carry = 1;
else
tmp[i] = t + 48;
}
if(szb >= 0 && sza < 0)
{
t = b[szb] - 48 + carry, carry = 0;
if(t > 9)
tmp[i] = t - 10 + 48, carry = 1;
else
tmp[i] = t + 48;
}
if(sza >= 0 && szb >= 0)
{
t = a[sza] - 48 + b[szb] - 48 + carry, carry = 0;
if(t > 9)
tmp[i] = t - 10 + 48, carry = 1;
else
tmp[i] = t + 48;
}
}
}
hdoj1002
最新推荐文章于 2019-03-30 23:29:13 发布