码迷,mamicode.com
首页 > 其他好文 > 详细

技巧之 -- 8 的倍数

时间:2018-02-02 18:30:16      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:printf   and   section   contains   证明   规律   char   first   most   

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.

Example
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO

题目分析 :
  给你一个长度小于 100 的串,让你删除其中的一些字符,让剩下的数是 8 的倍数,如果没有输出 NO
思路分析 :
  这个题当时是没有写出来的,发现自己好笨 ... 连自己打个表看下规律都找不到,只要你打个 2000 以内的表就能发现规律,你会发现只要之后 3位是 8的倍数,不管它前面是什么数字,那么这个数一定是8的倍数
至于证明呢,我就只有一个比较 low 的办法了,因为最小的4位数是1000, 它是 8 的倍数,那么它在加上一个是 8倍数的三位数一定就能得到一个是8倍数的3位数。
那么根据这个简单的证明,我们是不就能得到进一步推广,比如我们将这个题 的对 8 的判断更改为对 125 的判断,同样也是先看最小的四位数是 1000,只要求它最后的三位是 125的倍数就可以了
代码示例 :
char pre[105];

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    scanf("%s", pre+1);
    int len = strlen(pre+1);
    for(int i = 1; i <= len; i++){
        int num = pre[i] - ‘0‘;
        if (num % 8 == 0) {printf("YES\n%d\n", num); return 0;}
        
        for(int j = i+1; j <= len; j++){
            int num = 10*(pre[i]-‘0‘) + (pre[j]-‘0‘);
            if (num % 8 == 0) {printf("YES\n%d\n", num); return 0;}
            
            for(int k = j+1; k <= len; k++){
                int num = 100*(pre[i]-‘0‘) + 10*(pre[j]-‘0‘) + (pre[k]-‘0‘);
                if (num % 8 == 0) {printf("YES\n%d\n", num); return 0;}
            }
        }
    }
    printf("NO\n");

    return 0;
}

 

技巧之 -- 8 的倍数

标签:printf   and   section   contains   证明   规律   char   first   most   

原文地址:https://www.cnblogs.com/ccut-ry/p/8405943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!