hdu 1229 hdoj 1229

還是A+B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9130    Accepted Submission(s): 4490


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<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main(){
    int a,b,n,k;
    while(cin>>a>>b>>n,a!=0||b!=0){
        k=pow(10,n);
        if(abs(a-b)%k!=0) cout<<a+b<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章