Codeforces 550C
来源:互联网 发布:python mako 编辑:程序博客网 时间:2024/05/16 05:41
C. Divisibility by Eight
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Examples
input
3454
output
YES
344
input
10
output
YES
0
input
111111
output
NO
题意:
给出一串数,去除任意多个数,使得剩下的数能被8整除.
解题思路:
1000能被8整除,那么对于一个大于等于1000的数可以分解成1000*n+k.
所以只要看长度为1,2,3的数能否被8整除即可.
AC代码:
#include <bits/stdc++.h>using namespace std;string a;int main(){ ios::sync_with_stdio(0); cin >> a; for(int i = 0; a[i]; i++) { if((a[i]-'0') % 8 == 0) { cout << "YES" << endl; cout << a[i]; return 0; } } for(int i = 0; a[i]; i++) { for(int j = i+1; a[j]; j++) { if(((a[i]-'0')*10 + a[j]-'0') % 8 == 0) { cout << "YES" << endl; cout << a[i] << a[j]; return 0; } } } for(int i = 0; a[i]; i++) { for(int j = i+1; a[j]; j++) { for(int k = j+1; a[k]; k++) { if(((a[i]-'0')*100 + (a[j]-'0')*10 + a[k]-'0') % 8 == 0) { cout << "YES" << endl; cout << a[i] << a[j] << a[k]; return 0; } } } } cout << "NO"; return 0;}
- CodeForces 550C
- Codeforces 550C
- codeforces 550c (数学)
- CODEFORCES 550 C. Divisibility by Eight
- codeforces 550C Divisibility by Eight(数学题)
- CodeForces 550C Divisibility by Eight(枚举)
- Codeforces 550 C. Divisibility by Eight
- 【codeforces 550C】Divisibility by Eight
- Codeforces-340-C(c++)
- Codeforces-507-C(c++)
- CodeForces 731C C
- CodeForces-612C C
- CODEFORCES 265C CODEFORCES, 265C
- codeforces #78 div2 C
- Codeforces 117C Cycle
- CodeForces 209C
- CodeForces 156C Cipher
- codeforces 148 div2 C
- linux下systemctl命令整理(以apache为例)
- 1086. Tree Traversals Again
- 皮皮java笔记——数据库连接DriverManager
- mouseout() 方法和mouseleave() 方法的不同
- 自学Python day6--------面向对象编程(类和实例)
- Codeforces 550C
- Laravel 读取 config 下的数据
- 算法导论 CH01 The Role of Algorithms in Computing
- SDOI 2008沙拉公主的困惑
- 28、利用递归计算自然数 n 的阶乘
- 第七章课后简答题
- Session
- HTML li如何垂直居中
- 疾病管理