您的位置:首页 > 其它

Divisibility by Eight

2015-09-08 00:38 417 查看
C. Divisibility by Eighttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a non-negative integer n, its decimal representation consists of at most 100 digitsand 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. Afterthe removing, it is forbidden to rearrange the digits.If a solution exists, you should print it.InputThe single line of the input contains a non-negative integer n. The representation of number n doesn'tcontain any leading zeroes and its length doesn't exceed 100 digits.OutputPrint "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 inthe 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
题意:
就是找到能被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;}//*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces