Posted on 2010-08-15 17:11
MiYu 閱讀(401)
評論(0) 編輯 收藏 引用 所屬分類:
ACM ( 水題 )
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
題目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1229
題目描述:
還是A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6847 Accepted Submission(s): 3348
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>
using namespace std;
int getK(int n,int k)
{
int i = 1, j = 0;
while ( k -- )
i *= 10;
j = n % i;
j = j / ( i / 10 );
return j;
}
int main()
{
int a,b,k;
bool flag;
while ( cin >> a >> b >> k , a + b )
{
flag = true;
for ( int i = 1; i <= k; i ++ )
{
if ( getK ( a, i) != getK ( b, i ) )
{
flag = false;
break;
}
}
if ( !flag )
{
cout << a + b << endl;
}
else
cout<< -1 <<endl;
}
return 0;
}