Or a + B
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 15760 accepted submission (s): 7662
Problem description reads two integers A and B smaller than 10000, and calculates a + B. Note that if K (no more than 8) digits at the end of A and B are the same, output-1 directly.
The input test input contains several test cases. Each test case occupies one row in the format of "A B K". There is a space interval between two adjacent numbers. When both A and B are 0, the input ends, and the corresponding results are not output.
Output outputs one line for each test case, that is, the value of A + B or-1.
Sample Input
1 2 111 21 1108 8 236 64 30 0 1
Sample output
3-1-1100
Idea: Use % to get the end of the number and mark it with a flag to determine how to jump out of the loop.
#include<stdio.h>int main(){ int A,B,K,sum; while(scanf("%d%d%d",&A,&B,&K)&&A!=0&&B!=0) {int i,temp,a,b;sum=A+B;for(i=0,temp=0;i<K;i++){a=A%10;b=B%10;if(a==b){A/=10;B/=10;}else{ temp=1; break;}}if(temp)printf("%d\n",sum);elseprintf("-1\n");}return 0;}