codeforces C. Divisibility by Eight (纯属无聊来发一篇博客)
2015-06-05 23:02
489 查看
题目:
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
output
input
output
input
output
首先得明白什么样的数能被8整除 ,主要是超过3位数的 。。一个数abcd 可以写成 a*1000+bcd,因为1000%8==0,所以只要bcd%8==0,那么这个数就可以被8整除。。那么就是一个模拟了。。记得数字长度大于3位的时候 里面可能有2位的可能,所以要枚举两位长度的。。。。
代码:
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
YES 344
input
10
output
YES 0
input
111111
output
NO
首先得明白什么样的数能被8整除 ,主要是超过3位数的 。。一个数abcd 可以写成 a*1000+bcd,因为1000%8==0,所以只要bcd%8==0,那么这个数就可以被8整除。。那么就是一个模拟了。。记得数字长度大于3位的时候 里面可能有2位的可能,所以要枚举两位长度的。。。。
代码:
#include<cstdio> #include<cstring> #include<cstdlib> const int maxn=100+10; char s[maxn]; int main() { int l; while(~scanf("%s",s+1)) { int first,second,third; bool ok=false; l=strlen(s+1); for(int i=0;i<=l;i++) { if(s[i]=='0'||s[i]=='8') { ok=true; puts("YES"); if(s[i]=='8') printf("8\n"); else printf("0\n"); break; } }//只要满足0和8即可结束 ,最坏情况下删除只剩0和8 if(ok) continue; if(l==2) { if(((s[1]-'0')*10+(s[2]-'0'))%8==0) { puts("YES"); printf("%s\n",s+1); } else puts("NO"); } else { for(int i=1;i<=l-2;i++) { for(int j=i+1;j<=l-1;j++) { for(int k=j+1;k<=l;k++) { int ff=(s[i]-'0')*100+(s[j]-'0')*10+(s[k]-'0'); if(ff%8==0) { ok=true; first=i,second=j,third=k; break; } } if(ok) break; } if(ok) break; } if(ok) { puts("YES"); for(int i=1;i<first;i++) printf("%c",s[i]); for(int i=first;i<=l;i++) { if(i==first||i==second||i==third) printf("%c",s[i]); } printf("\n"); } else { int a,b; for(int i=1;i<=l-1;i++) for(int j=i+1;j<=l;j++) { int ff=(s[i]-'0')*10+s[j]-'0'; if(ff%8==0) { ok=true; a=i,b=j; break; } } if(ok) { puts("YES"); for(int i=a;i<=l;i++) if(i==a||i==b) printf("%c",s[i]); printf("\n"); } else puts("NO"); } } } return 0; }
相关文章推荐
- ct任务添加与中控机批量后台操作
- hdu 5256 序列变换 (LIS变形)
- python虚拟环境的使用
- 操作系统--存储管理的任务
- 操作系统--存储管理的任务
- 初探字符集和字符集编码
- Python函数式编程实践
- CentOS5.2安装mysql用163yum源安装具体步骤。
- 欢迎使用CSDN-markdown编辑器
- Android之drawable state各个属性详解 .
- tomcat服务器解析(五)-- Poller
- 2015-05-29
- 【Android】ListView 优化
- GTD时间管理(3)---项目
- OpenCSP开源程序解析之OPENCSP_AuthUI.cpp
- linux 下php安装过程笔记
- 上班的第五百天
- BST(Binary Search Tree) demo, quote from Stanford cs106b course reader
- JAVA回炉笔记——不定期更新
- Activity来了