#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
int main()
{
int x[105], y[105];
long long int xs, ys;
while (scanf("%lld%lld", &xs, &ys))
{
memset(x,0,sizeof(x));memset(y,0,sizeof(y));
if (xs==0&&ys == 0)break;
int tmpi = 0;
while (xs)
{
x[tmpi] = xs % 10;
tmpi++;
xs /= 10;
}
int tmp = 0;
while (ys)
{
y[tmp] = ys % 10;
ys /= 10;
tmp++;
}
//for (int i = 0; i < tmp; i++)
//printf("%d %d\n", x[i], y[i]);
int carry = 0;
int len = std::min(tmp, tmpi);
for (int i = 0; i <len+1; i++)
if (x[i] + y[i]>=10) { carry+=(x[i]+y[i])/10; x[i + 1] +=( (x[i] + y[i]) / 10); }
if (!carry)printf("No carry operation.\n");
else if (carry == 1)printf("1 carry operation.\n");
else printf("%d carry operations.\n", carry);
}
return 0;
}
想不出原因
#include
#include
#include
#include
int main()
{
int x[105], y[105];
long long int xs, ys;
while (scanf("%lld%lld", &xs, &ys))
{
memset(x,0,sizeof(x));memset(y,0,sizeof(y));
if (xs==0&&ys == 0)break;
int tmpi = 0;
while (xs)
{
x[tmpi] = xs % 10;
tmpi++;
xs /= 10;
}
int tmp = 0;
while (ys)
{
y[tmp] = ys % 10;
ys /= 10;
tmp++;
}
//for (int i = 0; i < tmp; i++)
//printf("%d %d\n", x[i], y[i]);
int carry = 0;
int len = std::min(tmp, tmpi); <----------------------- it is wrong here.
for (int i = 0; i <len+1; i++)
if (x[i] + y[i]>=10) { carry+=(x[i]+y[i])/10; x[i + 1] +=( (x[i] + y[i]) / 10); }
if (!carry)printf("No carry operation.\n");
else if (carry == 1)printf("1 carry operation.\n");
else printf("%d carry operations.\n", carry);
}
return 0;
}
想不出原因
try this test
9999999999 1