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
3454output
YES 344input
10output
YES 0input
111111output
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 Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima
- Codeforces Gym100342J Triatrip
- Codeforces Gym100286B Blind Walk (dfs)
- Codeforces Gym100342J Triatrip