"Nine degrees OJ" topic 1015: or a+b problem solving report
Label (Space delimited): nine degrees OJ
HTTP://AC.JOBDU.COM/PROBLEM.PHP?PID=1015 Topic Description:
Read in two positive integers a and B less than 10000, calculate a+b. It is important to note that if the end of A and B (not more than 8) digits are the same, please output-1 directly. Input:
The test input contains several test cases, one row for each test case, the format "A B K", and two adjacent numbers with a space interval. When A and B are at the end of the 0 o'clock input, the corresponding result is not output. Output:
Output 1 rows For each test case, that is, the value of a+b or 1. Sample Input:
1 2 1
1
108 8 2 +
3
0 0 1
Sample output:
3
-1
-1
100
Ways
Once again the taste of AC is so cool ha ha ~
This question, my first feeling is to use the number of bits to disassemble, one one of the comparison, the following K-bit is the same. But this is very troublesome, because the number of a B may be different, it is necessary to consider whether the number has been exhausted and so on, very troublesome.
But then, I think, I directly to the rest of the line, so that is the number of the next few, and even if the number of K-digits in the back of the lead number of 0 does not matter, because put in int automatically removed.
This is easy to implement.
I use the power of the 10 form, you can also write a cycle of constant *10, get the number used for redundancy.
#include <stdio.h>
#include <math.h>
int main () {
int A, b, K;
while (scanf ("%d%d", &a, &b)! = EOF && A! = 0 && B! = 0) {
scanf ("%d", &k);
int a_ = a% ((int) Pow (n, k));
int b_ = b% ((int) Pow (n, k));
if (A_ = = b_) {
printf (" -1\n");
} else {
printf ("%d\n", A + b);}
}
return 0;
}
Date
March 5, 2017