題目描述:
讀入兩個小於10000的正整數A和B,計算A+B。需要注意的是:如果A和B的末尾K(不超過8)位數字相同,請直接輸出-1。
輸入:
測試輸入包含若干測試用例,每個測試用例佔一行,格式爲”A B K”,相鄰兩數字有一個空格間隔。當A和B同時爲0時輸入結束,相應的結果不要輸出。
輸出:
對每個測試用例輸出1行,即A+B的值或者是-1。
樣例輸入:
1 2 1
11 21 1
108 8 2
36 64 3
0 0 1
樣例輸出:
3
-1
-1
100
來源:
2006年浙江大學計算機及軟件工程研究生機試真題
#include<stdio.h>
#include<math.h>
int main(){
int a, b;
double k;
while(scanf("%d %d %lf", &a, &b, &k)!=EOF){
if(a== 0 && b== 0){
return 0;
}
int na= 0;
int nb= 0;
double p= pow(10,k);
na= a% (int)p;
nb= b% (int)p;
if(na== nb){
printf("-1\n");
}
else{
printf("%d\n", a+b);
}
}
return 0;
}