HDU1229 還是A+B
還是A+BTime Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 3
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>
int a[2000];
int b[2000];
int main()
{
int i,j,n,m,k,flag1,flag2,x1,x2,v1,v2,count;
while(scanf("%d %d %d",&n,&m,&k),n!=0&&m!=0)
{
x1=n,x2=m;flag1=0,flag2=0;
count=k;v1=0,v2=0;
while(count--)
{
a[v1++]=x1%10;
b[v2++]=x2%10;
x1/=10,x2/=10;
}
for(i=v1-1;i>=0;i--)
{
flag1=flag1*10+a[i];
flag2=flag2*10+b[i];
}
if(flag1!=flag2)
printf("%d\n",n+m);
else
printf("-1\n");
}
return 0;
}
最後更新:2017-04-03 12:54:47