Divisibility by Eight

来源:互联网 发布:电脑数据库在哪里 编辑:程序博客网 时间:2024/06/05 18:54
C. Divisibility by Eight
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

Sample test(s)
input
3454
output
YES344
input
10
output
YES0
input
111111
output
NO
题意:
   就是找到能被8整除的数。
思路:
   深搜1-3长度的字符串,再将其转为整形后看能否被8整除,能就YES,否则NO。   
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdlib>#include<string>#include<cstdio>using namespace std;typedef __int64 ll;#define T 110int bo[T],flag,len,t;char s[T];void dfs(int i,int sum,int c){if(c<=3&&c>0){if(sum%8==0){flag=1,t=sum;return;}}if(flag||c==3)return;for(int k=i;k<len;++k){if(s[k]=='0')flag=1;if(!bo[k]){bo[k]=1;dfs(k+1,sum*10+s[k]-'0',c+1);bo[k]=0;}}}int main(){   /* freopen("input.txt","r",stdin);*/while(~scanf("%s",&s)){len=strlen(s);flag=0;t=0;dfs(0,0,0);if(flag) printf("YES\n%d\n",t);else printf("NO\n");}    return 0;}

神牛代码:
//*#include <stdio.h>#include <string.h>#include <ctype.h>#include <iostream>#include <queue>#include <algorithm>#include <vector>#include <set>#include <map>#include <string>#include <functional>#define MOD 1000000007#define MAX ((1<<30)-1)#define MAX2 ((1ll<<62)-1)#define mp make_pair#pragma warning(disable:4996)using namespace std;typedef long long ll;typedef unsigned int ui;typedef long double ldb;typedef pair<int, int> pii;typedef pair<ll, ll> pll;typedef pair<double, double> pdd;string s;int main(){int i, j, k;cin>>s;for(i=0;i<1000;i+=8){char a[10];sprintf(a, "%d", i);int len=strlen(a);int now=0;for(auto j : s){if(a[now] == j) now++;}if(now == len) return printf("YES\n%d", i), 0;}printf("NO");return 0;}//*/


0 0
原创粉丝点击