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

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

YES344

input

10

output

YES0

input

111111

output

NO渣代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    char a[110];
    scanf("%s",a);
    int n = strlen(a);
    if(n == 1){
        if((a[0] - ‘0‘)%8 == 0) {
            printf("YES\n");
            printf("%c",a[0]);
            return 0;
            }
    }
    else if(n == 2){
        if((a[0] - ‘0‘)%8 == 0) {
            printf("YES\n");
            printf("%c",a[0]);
            return 0;
            }
        if((a[1] - ‘0‘)%8 == 0) {
            printf("YES\n");
            printf("%c",a[1]);
            return 0;
            }
        if(((a[0]-‘0‘)*10+(a[1]-‘0‘))%8 == 0&&a[0]!=‘0‘){
            printf("YES\n");
            printf("%c%c",a[0],a[1]);
           return 0;
        }
        if(((a[1]-‘0‘)*10+(a[0]-‘0‘))%8 == 0&&a[1]!=‘0‘){
            printf("YES\n");
            printf("%c%c",a[1],a[0]);
            return 0;
        }
    }
    else {
    for(int i = 0 ; i < n ; i++){
        for(int j = i + 1; j < n; j++){
            for(int k = j + 1; k < n; k++){

              //   printf("%d ",a[i]-‘0‘+a[k]-‘0‘+a[j]-‘0‘);
        if((a[i] - ‘0‘)%8 == 0) {
            printf("YES\n");
            printf("%c",a[i]);

            return 0;
            }
        if((a[j] - ‘0‘)%8 == 0) {
            printf("YES\n");
            printf("%c",a[j]);

            return 0;
            }
           if((a[k] - ‘0‘)%8 == 0) {
            printf("YES\n");
            printf("%c",a[k]);
            return 0;
            }
        if(((a[i]-‘0‘)*10+(a[j]-‘0‘))%8 == 0&&a[i]!=‘0‘){
            printf("YES\n");
            printf("%c%c",a[i],a[j]);
           return 0;
        }
        if(((a[j]-‘0‘)*10+(a[k]-‘0‘))%8 == 0&&a[j]!=‘0‘){
            printf("YES\n");
            printf("%c%c",a[j],a[k]);
            return 0;
        }
        if(((a[i]-‘0‘)*10+(a[k]-‘0‘))%8 == 0&&a[i]!=‘0‘){
            printf("YES\n");
            printf("%c%c",a[i],a[k]);
            return 0;
        }

                if(a[i]!=‘0‘ &&( (a[i] -‘0‘)*100 + (a[j] - ‘0‘)*10 + (a[k] -‘0‘))%8 == 0){
                    printf("YES\n");
                    printf("%c%c%c",a[i],a[j],a[k]);
                    return 0;
                }

            }
        }
    }
}
    printf("NO");
    return 0;
}

  

时间: 2024-11-02 21:39:01

Codeforces Round #306 (Div. 2)——C模拟——Divisibility by Eight的相关文章

数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

题目传送门 1 /* 2 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11

DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad

题目传送门 1 /* 2 DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <cstring> 7 #include <cmath> 8 #include <algorithm> 9 #include <vector> 10 #include <map> 11 #include

Codeforces Round #306 (Div. 2) (ABCE题解)

比赛链接:http://codeforces.com/contest/550 A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and &

Codeforces Round #306 (Div. 2) (构造)

A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全,最后只能很蠢的暴力,把所有的AB和BA的位置求出来,能有一对AB和BA不重叠即可. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 char a[100005]; 5 vector<int> ab; 6 vector<i

题解——Codeforces Round #508 (Div. 2) T1 (模拟)

依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include <set> using namespace std; char s[100100]; int n,k,barrel[30]; int main(){ scanf("%d %d",&n,&k); scanf("%s",s+1); for(int i

Codeforces Round #306 (Div. 2)

Two Substrings 题意:问是否存在不重叠的串AB和BA. 思路:注意ABABA.BABAB这两种情况都应该是YES.所以可以找第一个AB,最后一个BA,如果两者不重叠(即两者不是ABA和BAB这样)可以确保一定是YES,同样如果找第一个BA和最后一个AB可以不重叠一样也是YES. 在python中,str的方法s1.find(s2)可以从字符串s1查找s2第一次出现的位置,找不到则返回-1.rfind()是查找最后一次出现位置. s=raw_input() x1,y1=s.find(

Codeforces Round #306 (Div. 2) D.E. 解题报告

D题:Regular Bridge 乱搞.构造 这题乱搞一下就行了.构造一个有桥而且每个点的度数都为k的无向图.方法很多,也不好叙述.. 代码如下: #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #define INF 0x

Codeforces Round #306 (Div. 2)——A——Two Substrings

You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 an

Codeforces Round #306 (Div. 2) A

题意 给一个字符串(长度<=10^5).问当中有没有一个"BA"和一个"AB"呢?假设都有而且它们不反复(即ABA不算),输出YES.否则输出NO. 思路 一開始想简单了-.. 我们扫一遍,把全部"AB"字符串中A的索引放入一个vector a,把全部"BA"字符串中B的索引放入还有一个vector b.最后扫一遍两个vector.假设发现一个b的值既不是一个a的值+1,也不是那个a的值-1,那么肯定就存在不反复的&qu