Codeforces Round #306 (Div. 2) C 模拟
来源:互联网 发布:触摸屏图编程 编辑:程序博客网 时间:2024/05/15 00:49
链接:戳这里
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
题意:
给出n个数字,任意删除一些数判断是否能整除8
思路:
首先1000/8=125所以超过三位数以上的只需要判断后三位能否整除8就行了
在处理两位的一位的
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;string s;bool pd(char A,char B,char C){ int x=(A-'0')*100+(B-'0')*10+(C-'0'); if(x%8==0) return true; return false;}int main(){ cin>>s; int n=s.size(); for(int i=0;i<n;i++){ if(s[i]=='8' || s[i]=='0'){ cout<<"YES"<<endl; cout<<s[i]<<endl; return 0; } } if(n==1){ int x=s[0]-'0'; if(x%8==0){ cout<<"YES"<<endl; cout<<x<<endl; } else { cout<<"NO"<<endl; } return 0; } char O='0'; if(n==2){ if(pd(O,O,s[0])){ cout<<"YES"<<endl; cout<<s[0]<<endl; } else if(pd(O,s[1],s[0])){ cout<<"YES"<<endl; cout<<s[1]<<s[0]<<endl; } else if(pd(O,O,s[1])){ cout<<"YES"<<endl; cout<<s[1]<<endl; } else { cout<<"NO"<<endl; } return 0; } for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(pd(O,s[i],s[j])){ cout<<"YES"<<endl; cout<<s[i]<<s[j]<<endl; return 0; } } } for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ for(int k=j+1;k<n;k++){ if(pd(s[i],s[j],s[k])){ cout<<"YES"<<endl; cout<<s[i]<<s[j]<<s[k]<<endl; return 0; } } } } cout<<"NO"<<endl; return 0;}
0 0
- Codeforces Round #306 (Div. 2) C 模拟
- Codeforces Round #346 (Div. 2) C模拟
- Codeforces Round #325 (Div. 2) C 模拟
- Codeforces Round #316 (Div. 2) C 模拟
- Codeforces Round #126 (Div. 2) C 模拟
- Codeforces Round #373 (Div. 2) C 模拟
- Codeforces Round #377 (Div. 2) C 模拟
- Codeforces Round #306 (Div. 2)C. Divisibility by Eight--模拟
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) C 模拟
- Codeforces Round #138 (Div. 2) c 数据结构模拟
- Codeforces Round #283 (Div. 2) C. Removing Columns 模拟+构造
- Codeforces Round #200 (Div. 2)344C Rational Resistance(模拟)
- C. Replacement( Codeforces Round #316 (Div. 2) 模拟)
- Codeforces Round #316 (Div. 2) C. Replacement (模拟)
- Codeforces Round #249 (Div. 2)453C Cardiogram(模拟)
- Codeforces Round #340 (Div. 2)(C)贪心,模拟
- Codeforces Round #321 (Div. 2)(C)模拟,DFS
- Codeforces Round #303 (Div. 2)-C. Woodcutters(模拟)
- 调皮的MFC(1)
- Java烧脑驴游(七)--运算符
- as ndk错误解决
- 这是一个新的开始
- [Leetcode]289. Game of Life
- Codeforces Round #306 (Div. 2) C 模拟
- ubuntu下安装Android Studio &添加至Launcher
- 找库
- Ubuntu Linux 继续统领云操作系统江湖
- Linux下cmake简易教程
- Python - 生成Robot Framework 类
- 计算机组成原理(1)
- Activity的四种启动模式及其应用
- java类之间继承、实现、组合、聚合、关联、依赖