Codeforces Round #306 (Div. 2) C 模拟

来源:互联网 发布:触摸屏图编程 编辑:程序博客网 时间:2024/05/15 00:49




链接:戳这里


C. Divisibility by Eight
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
3454
output
YES
344
input
10
output
YES
0
input
111111
output
NO


题意:

给出n个数字,任意删除一些数判断是否能整除8


思路:

首先1000/8=125所以超过三位数以上的只需要判断后三位能否整除8就行了

在处理两位的一位的


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;string s;bool pd(char A,char B,char C){    int x=(A-'0')*100+(B-'0')*10+(C-'0');    if(x%8==0) return true;    return false;}int main(){    cin>>s;    int n=s.size();    for(int i=0;i<n;i++){        if(s[i]=='8' || s[i]=='0'){            cout<<"YES"<<endl;            cout<<s[i]<<endl;            return 0;        }    }    if(n==1){        int x=s[0]-'0';        if(x%8==0){            cout<<"YES"<<endl;            cout<<x<<endl;        } else {            cout<<"NO"<<endl;        }        return 0;    }    char O='0';    if(n==2){        if(pd(O,O,s[0])){            cout<<"YES"<<endl;            cout<<s[0]<<endl;        } else if(pd(O,s[1],s[0])){            cout<<"YES"<<endl;            cout<<s[1]<<s[0]<<endl;        } else if(pd(O,O,s[1])){            cout<<"YES"<<endl;            cout<<s[1]<<endl;        } else {            cout<<"NO"<<endl;        }        return 0;    }    for(int i=0;i<n;i++){        for(int j=i+1;j<n;j++){            if(pd(O,s[i],s[j])){                cout<<"YES"<<endl;                cout<<s[i]<<s[j]<<endl;                return 0;            }        }    }    for(int i=0;i<n;i++){        for(int j=i+1;j<n;j++){            for(int k=j+1;k<n;k++){                if(pd(s[i],s[j],s[k])){                    cout<<"YES"<<endl;                    cout<<s[i]<<s[j]<<s[k]<<endl;                    return 0;                }            }        }    }    cout<<"NO"<<endl;    return 0;}



0 0