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




链接:戳这里


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)-1
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值