3201 A+B问题
Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others)
Total Submissions: 198 Accepted Submissions: 102
Problem Description
读入两个小于10000的正整数A和B,计算A+B。需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1。
Input
测试输入包含若干测试数据,每个测试数据占一行,格式为"A B K",相邻两数字有一个空格间隔。当A和B同时为0时输入结束,相应的结果不要输出。
Output
对每个测试数据输出1行,即A+B的值或者是-1。
Sample Input
1 2 1
11 21 1
108 8 2
36 64 3
0 0 1
Sample Output
3
-1
-1
100
#include <stdio.h>
#include <string.h>
#include <math.h>
int k;
bool p(int x,int y)
{
int i = 0,j = 0;
int a1[10];
int a2[10];
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
while(x)
{
a1[i++] = x%10;
x /= 10;
}
while(y)
{
a2[j++] = y%10;
y /= 10;
}
for(int m=1;m<k;m++)
{
a1[0] += a1[m];
}
for(int m=1;m<k;m++)
{
a2[0] += a2[m];
}
if(a1[0]==a2[0])
return 1;
else
return 0;
}
int main()
{
int a,b;
while(1)
{
scanf("%d %d %d",&a,&b,&k);
if(a==0||b==0)
break;
if(!p(a,b))
{
printf("%d\n",a+b);
}
else
printf("-1\n");
}
return 0;
}