Codeforces Round #306 (Div. 2)C. Divisibility by Eight--模拟
2015-06-09 17:56
363 查看
You 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整除
思路:
由数学的一个定理:一个数的后三位如果能够被8整除,那么这个数就可以被8整除:
所以我们就直接暴力就ok,但是还是要特判给出的数长度小于2的情况;
#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<vector>#include<algorithm>#include<queue>#include<stack>#include<map>#define nn 1009#define inff 0x3fffffff#define mod 100000000#define eps 1e-9#define ll long longusing namespace std;int main(){ int len; char str[nn]; int i,j; while(scanf("%s",str)!=EOF) { len=strlen(str); int n=len; if(n<=2) { if(n==1) { if( (str[0]-'0')%8==0) { printf("YES\n"); printf("%c\n",str[0]); } else printf("NO\n"); } if(n==2) { int m=(str[0]-'0')*10+(str[1]-'0'); if( m%8==0) { printf("YES\n"); printf("%d\n",m); } else { int o=0; for(i=0;i<len;i++) { if(str[i]=='0' ||str[i]=='8') { printf("YES\n"); printf("%c\n",str[i]); o=1; break; } } if(o==0) printf("NO\n"); } } continue; } int flag=0; int mid; for(i=0;i<len;i++) { if(str[i]=='0') { printf("YES\n"); printf("%c\n",str[i]); flag=1; break; } } if(flag==1) continue; flag=0; for(i=0;i<len;i++) { if(str[i]=='8') { flag=1; printf("YES\n"); printf("%c\n",str[i]); break; } for(j=i+1;j<len;j++) { mid=(str[i]-'0')*10+(str[j]-'0'); if(mid%8==0) { flag=1; printf("YES\n"); printf("%d\n",mid); break; } for(int k=j+1;k<len;k++) { mid=(str[i]-'0')*100+(str[j]-'0')*10+(str[k]-'0'); if(mid%8==0) { flag=1; printf("YES\n"); printf("%d\n",mid); break; } } if(flag==1) break; } if(flag==1) break; } if(flag==0) printf("NO\n");flag=0; }}
相关文章推荐
- 数据库锁
- 解读郭神LitePal源码-数据库的创建以及如何修改源码来创建外置数据库
- 模板文件是否有大小限制?
- Tiny框架增加RESTful访问支持
- Android中轴旋转特效实现,制作别样的图片浏览器
- UITableView创建样式
- Distinct Subsequences leetcode 115
- 糊涂窗口综合症及其解决方法(Nagle算法,Clark方法,延时确认方法,CORK算法)
- 为什么web应用在tomcat启动时报java.lang.ClassCastException异常?
- hdoj1058_Humble Numbers(dp)
- windows服务程序的编写
- WinForm中DataGridView控件绑定列顺序
- 通用方法解决dedecms导航调用二级、三级栏目菜单
- 悯码农 * 慧哥
- python环境变量的配置
- 集合框架2
- Nuget管理我们的类库
- ASP.NET forms凭据设置和跳转的几种方法
- dedecms织梦导航栏二级菜单的实现方法
- 微信投票怎么刷票之微信怎么刷投票快速提升票数方法