标签:
You are given a non-negative integer n, its decimal representation consists of at most 100digits 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 numbern 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
YES
344
10
YES
0
111111
NO
题目大意给你一个小于100位的十进制数字,要你在在里面进行删除数字使它可以被8整除(0也算可以被8整除的数),而且如果有多个结果可以任意输出一个(100位加上任意输出我嗅到了暴力的味道)
结题思路因为1000/8 = 125,所以最多只要找到3个数字就可以了,你输出一下1000一下8的倍数,你会发现除了0 8,当首位是偶数或没有的时候最后两位是固定的,奇数同理,由于又是输出任意一个。直接暴力在100位里面找3个数字并且顺序不变O(n)随便玩。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 #define inf 0xffffff 9 #define maxn 105 10 11 char s[maxn]; 12 int a[maxn]; 13 int dis1[7][2] = {1,6,2,4,3,2,5,6,6,4,7,2,9,6}; 14 int dis2[6][2] = {1,2,3,6,4,4,5,2,7,6,9,2}; 15 16 int main() 17 { 18 /* 19 for(int i = 0; i < 7; i++) 20 cout<<dis1[i][0]<<dis1[i][1]<<endl; 21 for(int i = 0; i < 6; i++) 22 cout<<dis2[i][0]<<dis2[i][1]<<endl; 23 */ 24 while(~scanf("%s",s)) 25 { 26 int n = strlen(s); 27 int x = -1,ans = false; 28 for(int i = 0; i < n; i++) 29 a[i] = s[i] - ‘0‘; 30 for(int i = 0; i < n; i++) 31 if(a[i] == 0||a[i] == 8) {cout<<"YES"<<endl<<a[i]<<endl;ans = true; break;} 32 if(ans == true) continue; 33 for(int k = 0; k < 7; k++) 34 { 35 if(ans == true) break; 36 int flag = -1; 37 for(int i = 0; i < n; i++) 38 if(a[i] == dis1[k][0]) {flag = i;break;} 39 if(flag == -1) continue; 40 for(int i = flag + 1; i < n; i++) 41 if(a[i] == dis1[k][1]) {cout<<"YES"<<endl<<a[flag]<<a[i]<<endl; ans = true;break;} 42 } 43 if(ans == true) continue; 44 for(int i = 0; i < n; i++) 45 if(a[i] & 1) {x = i;break;} 46 if(x == -1) {cout<<"NO"<<endl;continue;} 47 for(int k = 0; k < 6; k++) 48 { 49 if(ans == true) break; 50 int flag = -1; 51 for(int i = x+1; i < n; i++) 52 if(a[i] == dis2[k][0]) {flag = i; break;} 53 if(flag == -1) continue; 54 for(int i = flag + 1; i < n; i++) 55 if(a[i] == dis2[k][1]) {cout<<"YES"<<endl<<a[x]<<a[flag]<<a[i]<<endl; ans = true;break;} 56 } 57 if(ans == true) continue; 58 cout<<"NO"<<endl; 59 } 60 return 0; 61 }
cf 550 C Divisibility by Eight
标签:
原文地址:http://www.cnblogs.com/LQBZ/p/4561260.html