Codeforces550C: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
YES
344
input
10
output
YES
0
input
111111
output
NO
 
   
题意:
对于一个最长100位的数,问能不能通过拿掉一些数字使得它能被8整除
 
   
思路:
由于并没有规定要拿掉多少个,所以我们可以随意的拿掉任意数量的数字,而我们知道一个数字能不能被8整除取决于其后三位,后三位能被8整除那么这个数就能被8整除,于是我们只需要在字符串中取出三个数字,只要这三个数组成的数能被8整除,那么就是可行的答案
 
   
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 2000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8

char str[1005];

int main()
{
    while(~scanf("%s",str+2))
    {
        str[0] = str[1] = '0';
        int len = strlen(str),s;
        int i,j,k,flag=0;
        for(i = 0; i<len-2; i++)
        {
            if(flag) break;
            for(j=i+1; j<len-1; j++)
            {
                if(flag) break;
                for(k = j+1; k<len; k++)
                {
                    s = (str[i]-'0')*100+(str[j]-'0')*10+str[k]-'0';
                    if(s%8==0)
                    {
                        flag = 1;
                        break;
                    }
                }
            }
        }
        if(flag)
            printf("YES\n%d\n",s);
        else
            printf("NO\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值