您的位置:首页 > 其它

codeforces C. Divisibility by Eight (纯属无聊来发一篇博客)

2015-06-05 23:02 489 查看
题目:

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.

Sample test(s)

input
3454


output
YES
344


input
10


output
YES
0


input
111111


output
NO


首先得明白什么样的数能被8整除 ,主要是超过3位数的 。。一个数abcd 可以写成 a*1000+bcd,因为1000%8==0,所以只要bcd%8==0,那么这个数就可以被8整除。。那么就是一个模拟了。。记得数字长度大于3位的时候 里面可能有2位的可能,所以要枚举两位长度的。。。。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>

const int maxn=100+10;

char s[maxn];

int main()
{
    int l;
    while(~scanf("%s",s+1))
    {
        int first,second,third;
        bool ok=false;
        l=strlen(s+1);
        for(int i=0;i<=l;i++)
        {
            if(s[i]=='0'||s[i]=='8')  
            {
                ok=true;
                puts("YES");
                if(s[i]=='8')  printf("8\n");
                else           printf("0\n");
                break;
            }
        }//只要满足0和8即可结束 ,最坏情况下删除只剩0和8
        if(ok)   continue;
        if(l==2)
        {
            if(((s[1]-'0')*10+(s[2]-'0'))%8==0)
            {
                  puts("YES");
                  printf("%s\n",s+1);
            }
            else   puts("NO");
        }
        else
        {
            for(int i=1;i<=l-2;i++)
            {
                for(int j=i+1;j<=l-1;j++)
                {
                    for(int k=j+1;k<=l;k++)
                    {
                         int ff=(s[i]-'0')*100+(s[j]-'0')*10+(s[k]-'0');
                         if(ff%8==0)
                         {
                                ok=true;
                                first=i,second=j,third=k;
                                break;

                         }
                    }
                    if(ok)  break;
                }
                if(ok)  break;
            }
            if(ok)
            {
                puts("YES");
                for(int i=1;i<first;i++)  printf("%c",s[i]);
                for(int i=first;i<=l;i++)
                {
                    if(i==first||i==second||i==third)  printf("%c",s[i]);
                }
                printf("\n");
            }
            else
            {
                int a,b;
                for(int i=1;i<=l-1;i++)
                     for(int j=i+1;j<=l;j++)
                {
                    int ff=(s[i]-'0')*10+s[j]-'0';
                    if(ff%8==0)
                    {
                        ok=true;
                        a=i,b=j;
                        break;
                    }
                }
                if(ok)
                {
                    puts("YES");
                    for(int i=a;i<=l;i++)  if(i==a||i==b)  printf("%c",s[i]);
                    printf("\n");
                }
                else puts("NO");
            }

        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: