Codeforces Round #306 (Div. 2) C

意甲冠军

到不超过一个更100该整数字符串。采取随机从数间(不能拿)。

问:是否有可能被剩下8除尽。假设能,出口YES和任选一个数字的其余病例的。

不能够,输出NO。

思路

想法题。

首先观察到。1000能够整除8。

也就是说我们能够不关心4位数的情况。

仅仅要有可能存在题目说的数。那么它一定能够表示成3位数或更低的位数。那么就好办了,直接枚举就可以。
先看是否有0.
再看是否有8.
接着枚举两位数的情况。最后枚举三位数的情况。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 110;
char s[maxn];
int ans;
int len;
bool chk2(int x)//检查两位数
{
    char c1 = '0'+x/10;
    char c2 = '0'+x%10;
    for(int i = 0 ; i < len-1 ; i ++) {
        for(int j = i+1 ; j < len ; j ++) {
            if(s[i] == c1 && s[j] == c2) {
                ans = x;
                return true;
            }
        }
    }
    return false;
}
bool chk3(int x)//检查三位数
{
    char c1 = '0'+x/100;
    char c2 = '0'+(x%100)/10;
    char c3 = '0'+x%10;
    for(int i = 0 ; i < len-2 ; i ++) {
        for(int j = i+1 ; j < len-1 ; j ++) {
            for(int t = j+1 ; t < len ; t ++) {
                if(s[i] == c1 && s[j] == c2 && s[t] == c3) {
                    ans = x;
                    return true;
                }
            }
        }
    }
    return false;
}
int main()
{
    scanf("%s",s);
    len = strlen(s);
    for(int i = 0 ; i < len ; i ++) {
        if(s[i] == '0') {
            printf("YES
0
");
            return 0;
        }
    }
    for(int i = 0 ; i < len ; i ++) {
        if(s[i] == '8') {
            printf("YES
8
");
            return 0;
        }
    }
    for(int i = 2 ; i <= 12 ; i ++) {
        if(chk2(8*i)) {
            printf("YES
%d
",ans);
            return 0;
        }
    }
    for(int i = 13 ; i <= 124 ; i ++) {
        if(chk3(8*i)) {
            printf("YES
%d
",ans);
            return 0;
        }
    }
    printf("NO
");
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/bhlsheji/p/4747255.html