(PAT Basic Level)1116 A little more than two

If a positive integer has 2n digits, and the number composed of the last n digits is exactly 2 more than the number composed of the first n digits, then the number is said to be "a little bit more than two". For example, 24, 6668, 233235, etc. are all numbers with a little more than two.
Given any positive integer, please judge whether it is a little more than two.

Input format:

The input is given in the first line as a positive integer N (≤101000).

Output format:

Print one of the following on one line, as appropriate:

  • If the input integer does not have an even number of digits, output Error: X digit(s), where X is the number of N digits;
  • If it is an even number and there are two more digits, output Yes: X - Y = 2, where X is the number composed of the last half of the digits, a>  is no more than 7. Note: In order to make the question simple, make sure that the single digit of Y is the number composed of the first half of the digits. Y
  • If it is an even number, but not a little more than two, output No: X - Y != 2, where X is the number composed of the last half of the digits, < /span>Y is the number composed of the first half of the digits.

Input example 1:

233235

Output sample 1:

Yes: 235 - 233 = 2

Input example 2:

5678912345

Output sample 2:

No: 12345 - 56789 != 2

Input example 3:

2331235

Output sample 3:

Error: 7 digit(s)

Code length limit

16 KB

time limit

400 ms

memory limit

64 MB

The C language I used for this question, the AC code is as follows:

#include <stdio.h>
#include <string.h>
int main(){
    char number[1111];
    gets(number);
    int len=strlen(number);
    if(len%2){
        printf("Error: %d digit(s)",len);
    }else{
        if(number[len-1]-number[len/2-1]==2&&number[len-2]-number[len/2-2]==0){
            printf("Yes: ");
            int i;
            for(i=len/2;i<len;i++) printf("%c",number[i]);
            printf(" - ");
            for(i=0;i<len/2;i++) printf("%c",number[i]);
            printf(" = 2");
        }else{
            printf("No: ");
            int i;
            for(i=len/2;i<len;i++) printf("%c",number[i]);
            printf(" - ");
            for(i=0;i<len/2;i++) printf("%c",number[i]);
            printf(" != 2");
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/gaogao0305/article/details/133758272