"Nine degrees OJ" topic 1015: or a+b Problem solving report

Source: Internet
Author: User
Tags pow
"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.