您的位置:首页 > 其它

Codeforces Round #306 (Div. 2)C. Divisibility by Eight--模拟

2015-06-09 17:56 363 查看
You are given a non-negative integer n, its decimal representation consists of at most 100 digitsand 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. Afterthe removing, it is forbidden to rearrange the digits.If a solution exists, you should print it.InputThe single line of the input contains a non-negative integer n. The representation of number n doesn'tcontain any leading zeroes and its length doesn't exceed 100 digits.OutputPrint "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 inthe 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整除
思路:
由数学的一个定理:一个数的后三位如果能够被8整除,那么这个数就可以被8整除:
所以我们就直接暴力就ok,但是还是要特判给出的数长度小于2的情况;
#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<vector>#include<algorithm>#include<queue>#include<stack>#include<map>#define nn 1009#define inff 0x3fffffff#define mod 100000000#define eps 1e-9#define ll long longusing namespace std;int main(){    int  len;    char str[nn];    int i,j;    while(scanf("%s",str)!=EOF)    {        len=strlen(str);        int n=len;        if(n<=2)        {            if(n==1)            {                if( (str[0]-'0')%8==0)                {                    printf("YES\n");                    printf("%c\n",str[0]);                }                else                printf("NO\n");            }            if(n==2)            {                int m=(str[0]-'0')*10+(str[1]-'0');                if( m%8==0)                {                    printf("YES\n");                    printf("%d\n",m);                }                else                {                    int o=0;                    for(i=0;i<len;i++)                    {                        if(str[i]=='0' ||str[i]=='8')                        {                            printf("YES\n");                            printf("%c\n",str[i]);                            o=1;                            break;                        }                    }                    if(o==0)                        printf("NO\n");                }            }            continue;        }        int flag=0;        int mid;        for(i=0;i<len;i++)        {            if(str[i]=='0')            {                printf("YES\n");                printf("%c\n",str[i]);                flag=1;                break;            }        }        if(flag==1)            continue;        flag=0;        for(i=0;i<len;i++)        {            if(str[i]=='8')            {               flag=1;               printf("YES\n");               printf("%c\n",str[i]);               break;            }            for(j=i+1;j<len;j++)            {                mid=(str[i]-'0')*10+(str[j]-'0');                if(mid%8==0)                {                    flag=1;                    printf("YES\n");                    printf("%d\n",mid);                    break;                }                for(int k=j+1;k<len;k++)                {                    mid=(str[i]-'0')*100+(str[j]-'0')*10+(str[k]-'0');                    if(mid%8==0)                    {                      flag=1;                      printf("YES\n");                      printf("%d\n",mid);                      break;                    }                }              if(flag==1)                break;            }            if(flag==1)                break;        }        if(flag==0)            printf("NO\n");flag=0;    }}

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