1. 程式人生 > >九度題目1015:還是A+B

九度題目1015:還是A+B

題目描述:
讀入兩個小於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

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<stack>
#include<vector>
#include<string.h>
#include<limits.h>
#include<stdlib.h>
#include<math.h>

using namespace std;

int main()
{
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);

    int a,b,k;
    int aa,bb;
    while(cin>>a>>b>>k&&(a*a+b*b!=0))
    {
        if(k>=6)
            cout<<-1<<endl;
        else
        {
            aa = a%((int)pow(10,k));
            bb = b%((int)pow(10,k));
            if(aa==bb)
                cout<<-1<<endl;
            else
                cout<<a+b<<endl;
        }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}