标签:codeforces
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.
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.
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整除
思路:
由于并没有规定要拿掉多少个,所以我们可以随意的拿掉任意数量的数字,而我们知道一个数字能不能被8整除取决于其后三位,后三位能被8整除那么这个数就能被8整除,于是我们只需要在字符串中取出三个数字,只要这三个数组成的数能被8整除,那么就是可行的答案
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <bitset> #include <algorithm> #include <climits> using namespace std; #define LS 2*i #define RS 2*i+1 #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define LL long long #define N 2000005 #define MOD 1000000007 #define INF 0x3f3f3f3f #define EXP 1e-8 char str[1005]; int main() { while(~scanf("%s",str+2)) { str[0] = str[1] = '0'; int len = strlen(str),s; int i,j,k,flag=0; for(i = 0; i<len-2; i++) { if(flag) break; for(j=i+1; j<len-1; j++) { if(flag) break; for(k = j+1; k<len; k++) { s = (str[i]-'0')*100+(str[j]-'0')*10+str[k]-'0'; if(s%8==0) { flag = 1; break; } } } } if(flag) printf("YES\n%d\n",s); else printf("NO\n"); } return 0; }
Codeforces550C:Divisibility by Eight
标签:codeforces
原文地址:http://blog.csdn.net/libin56842/article/details/46427575