Divisibility by Eight
来源:互联网 发布:电脑数据库在哪里 编辑:程序博客网 时间:2024/06/05 18:54
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.
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.
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.
3454
YES344
10
YES0
111111
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;}//*/
- Divisibility by Eight
- Codeforces550C:Divisibility by Eight
- Divisibility by Eight
- C. Divisibility by Eight
- Divisibility by Eight
- Codeforces Divisibility by Eight
- CF C. Divisibility by Eight
- [CodeForces550C]Divisibility by Eight[数学]
- CODEFORCES 550 C. Divisibility by Eight
- codeforces 550C Divisibility by Eight(数学题)
- 550C Divisibility by Eight(还是枚举)
- CodeForces 550C Divisibility by Eight(枚举)
- Codeforces 550 C. Divisibility by Eight
- codeforce 550c Divisibility by Eight (DFS)
- 【codeforces 550C】Divisibility by Eight
- cf 550c Divisibility by Eight 【规律】
- Codeforces Round #306 (Div. 2)C. Divisibility by Eight--模拟
- Codeforces Round #306 (Div. 2) C.Divisibility by Eight
- Eclipse中新建工程出现红色感叹号以及appcompat-v7编译错误
- lintcode-插入区间
- Android开发---记事本(二)
- fragment+RadioGroup实现底部导航栏 多个 fragment 重影问题
- Leet Code 4 Median of Two Sorted Arrays
- Divisibility by Eight
- SpringMVC的@ModelAttribute和@RequestParam、@PathVariable、@CookieValue、@SessionAttributes
- 【OpenCV 基础】Mat和IplImage间的相互转换
- java实现最基础的中国5角星
- Gradle文件结构介绍
- 【CCF】命令行选项
- 【SeqList】线性表代码实现总结
- mysql_thread_safe()
- POJ 2516--Minimum Cost【最小费用最大流 && 经典】