[Nowcoder Shanghai five school competition] number two

Posted by bnmng on Tue, 31 Mar 2020 08:20:32 +0200

Title Description:

We call every even digit in the decimal system "two numbers".
Xiao'e said he was very smart. Recently, he not only learned from decimal to big: 2,3,4,5..., but also learned from big to small: 100,99,98..., he wanted to know that from a number to the least number, he would get a two number. But smart Xiaosen has secretly calculated in his mind which number he will count. Please find out which number he wants to count.
In other words, given a number with a maximum of 105 digits in decimal system, please find the absolute value of the difference between this number and the smallest two digits. If the answer is not unique, output the smallest one.
That is to say, given the number n, find m to make abs(n-m) minimum and m[i] mod 2 = 0

Enter a description:

1 ≤ T ≤ 100, 1 ≤ n ≤ 10100000 - 1, the total length of decimal representation of data in group T shall not exceed 1000000

Output Description:

An integer m in each line the i-th line represents the "nearest two" corresponding to the i-th number

[example 1]

input

5
42
11
1
2018
13751

output

42
8
0
2020
8888

Title Solution

① Find the odd number of the highest order of the number. If there is no odd number, it can be output directly

② Then judge the following three situations in turn:

(1) if the value of the highest odd bit is 9, change every digit including the highest odd bit to 8

(2) if this number has one digit ≤ 4 except the highest odd digit, then the value of the highest odd digit is - 1, and then the low order all changes to 8

(3) if there is a single digit > 4 except the highest odd digit, the value of the highest odd digit + 1, and then the low order all changes to 0

③ Pay attention to remove the leading 0

#include<bits/stdc++.h>
using namespace std;
#define N 1000005
char a[N];
int main()
{
    int i,j,t,flag;
    cin>>t;
    while(t--)
    {
        flag=0;
        scanf("%s",a+1);
        for(i=1;a[i];i++)
        {
            if(a[i]=='9')
            {
                for(j=i;a[j];j++)
                    printf("8");
                break;
            }
            else if((a[i]-'0')%2==1)
            {
                for(j=i+1;a[j];j++)
                {
                    if(a[j]>'4')
                    {
                        flag=1;
                        break;
                    }
                    else if(a[j]<'4')
                        break;
                }
                if(flag)
                {
                    printf("%c",a[i]+1);
                    for(j=i+1;a[j];j++)
                        printf("0");
                        
                    break;
                }
                else if(!flag)
                {
                    if(i==1)                             //If the highest odd bit changed is first 
                    {
                        if(a[i]-1!='0'||a[i+1]=='\0')    //And the first bit is not 0 and the number entered is a bit 
                            printf("%c",a[i]-1);
                    }
                    else printf("%c",a[i]-1);            //Output directly if the highest odd bit changed is not the first bit 

                    for(j=i+1;a[j];j++)
                        printf("8");
                    break;
                }
            }
            else printf("%c",a[i]);
        } 
        printf("\n");
    }
    return 0;
}

 

Topics: C++