codeforces 550C Divisibility by Eight

Click to open the link

C. Divisibility by Eight
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.

Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.

If a solution exists, you should print it.

Input

The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.

Output

Print "NO" (without quotes), if there is no such way to remove some digits from number n.

Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.

If there are multiple possible answers, you may print any of them.

Examples
input
Copy
3454
output
Copy
YES
344
input
Copy
10
output
Copy
YES
0
input
Copy
111111
output
Copy
NO
My approach is violence, I judged up to 3 digits, when it is greater than 3 digits, I wrote it wrong. The reason for the mistake is that if it is a single digit, if it is 8 or 0, it can be divisible by 8. I wrote = =8, so in the 20th set of data--303, the error is in this set of data.

#include<bits/stdc++.h>  
using namespace std;  
typedef long long ll;  
const int inf = 0x3f3f3f3f;  
intmain()   
{  
    // freopen("shuju.txt","r",stdin);
    char a[110];
    cin>>a;
    int len ​​= strlen (a);
    if(len==1)
    {
        if((a[0]-'0')%8==0)
        {
            printf("YES\n");
            printf("%d\n",a[0]-'0');
        }
        else
            printf("NO\n");
    }
    else if(len==2)
    {
        int ans=(a[0]-'0')*10+a[1]-'0';
        if(ans%8==0)
        {
            printf("YES\n");
            printf("%d\n",ans);
        }
        else if((ans/10)%8==0)
        {
            printf("YES\n");
            printf("%d\n",ans/10);
        }
        else if((ans%10)%8==0)
        {
            printf("YES\n");
            printf("%d\n",ans%10);
        }
        else
            printf("NO\n");
    }
    else
    {
        for(int i=0;i<len;i++)
        {
            for(int j=i+1;j<len;j++)
            {
                for(int k=j+1;k<len;k++)
                {
                    int x=a[i]-'0',y=a[j]-'0',z=a[k]-'0';
                    if(x%8==0)
                    {
                        printf("YES\n");
                        printf("%d\n",x);
                        return 0;
                    }
                    if(y%8==0)
                    {
                        printf("YES\n");
                        printf("%d\n",y);
                        return 0;
                    }
                    if(z%8==0)
                    {
                        printf("YES\n");
                        printf("%d\n",z);
                        return 0;
                    }
                    int ans=((x*10)+y)*10+z;
                    if(ans%8==0)
                    {
                        printf("YES\n");
                        printf("%d\n",ans);
                        return 0;
                    }
                    ans=x*10+y;
                    if(ans%8==0)
                    {
                        printf("YES\n");
                        printf("%d\n",ans);
                        return 0;
                    }
                    ans=x*10+z;
                    if(ans%8==0)
                    {
                        printf("YES\n");
                        printf("%d\n",ans);
                        return 0;
                    }
                    ans=y*10+z;
                    if(ans%8==0)
                    {
                        printf("YES\n");
                        printf("%d\n",ans);
                        return 0;
                    }
                }
            }
        }
        printf("NO\n");
    }
    return 0;  
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324651471&siteId=291194637