测试输入包含若干测试用例,每个测试用例占一行,格式为"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 <iostream> using namespace std; int main() { int a,b,k; while(cin>>a>>b>>k && a!=0 && b!=0) { if(k == 0) cout<<a+b<<endl; else if(k==1) { if(a%10 == b%10) { cout<<-1<<endl; } else { cout<<a+b<<endl; } } else if(k==2) { if((a%10 == b%10) &&(a/10%10 == b/10%10)) { cout<<-1<<endl; } else { cout<<a+b<<endl; } } else if(k==3) { if((a%10 == b%10) &&(a/10%10 == b/10%10)&&(a/100%10 == b/100%10)) { cout<<-1<<endl; } else { cout<<a+b<<endl; } } else if(k==4) { if((a%10 == b%10) &&(a/10%10 == b/10%10)&&(a/100%10 == b/100%10)&&(a/1000%10 == b/1000%10)) { cout<<-1<<endl; } else { cout<<a+b<<endl; } } else if(k>4) { if((a%10 == b%10) &&(a/10%10 == b/10%10)&&(a/100%10 == b/100%10)&&(a/1000%10 == b/1000%10)) { cout<<-1<<endl; } else { cout<<a+b<<endl; } } } return 0; } /************************************************************** Problem: 1015 User: vhreal Language: C++ Result: Accepted Time:0 ms Memory:1520 kb ****************************************************************/
原文地址:http://blog.csdn.net/wtyvhreal/article/details/42061791