CF 550 C divisibility by Eight

Source: Internet
Author: User

You is given a non-negative integer n, its decimal representation consists of in most 100digits and does N ' t contain leading zeroes.

Your task is to determine if it's possible in this case to remove some of the digits (possibly no remove any digit at Al L) The result contains at least one digit, forms a non-negative integer, doesn ' t has leading zeroes and is Divisi BLE by 8. After the removing, it's 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 numbern doesn ' t contain any leading zeroes and its length doesn ' t exceed digits .

Output

Print "No" (without quotes), if there is NO such-to-remove some digits from number n.

Otherwise, print "YES" in the first line and the resulting number afterremoving digits from number n in The second line. The printed number must is divisible by 8.

If There is multiple possible answers, you could print any of the them.

Sample Test (s) input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO

The main idea is to give you a decimal number less than 100 digits, so that you can delete the number in it so that it is divisible by 8 (0 is also divisible by 8), and if there are multiple results can be arbitrary output one (100 bits plus any output I smell the smell of violence)

Knot idea because 1000/8 = 125, so as long as a maximum of 3 numbers can be found, you output a multiple of 10,001 under 8, you will find that in addition to 0 8, when the first is even or no time the last two bits are fixed, odd, because it is the output of any one. Direct violence in 100 digits find 3 numbers and the order does not change O (n) casually play.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 using namespacestd;8 #defineINF 0XFFFFFF9 #defineMAXN 105Ten  One CharS[MAXN]; A intA[MAXN]; - intdis1[7][2] = {1,6,2,4,3,2,5,6,6,4,7,2,9,6}; - intdis2[6][2] = {1,2,3,6,4,4,5,2,7,6,9,2}; the  - intMain () - { -     /* + for (int i = 0; i < 7; i++) - cout<<dis1[i][0]<<dis1[i][1]<<endl; + for (int i = 0; i < 6; i++) A cout<<dis2[i][0]<<dis2[i][1]<<endl; at     */ -      while(~SCANF ("%s", s)) -     { -         intn =strlen (s); -         intx =-1, ans =false; -          for(inti =0; I < n; i++) inA[i] = s[i]-'0'; -          for(inti =0; I < n; i++) to             if(A[i] = =0|| A[i] = =8) {cout<<"YES"<<endl<<a[i]<<endl;ans =true; Break;} +         if(ans = =true)Continue; -          for(intK =0; K <7; k++) the         { *             if(ans = =true) Break; $             intFlag =-1;Panax Notoginseng              for(inti =0; I < n; i++) -                 if(A[i] = = dis1[k][0]) {flag = i; Break;} the             if(Flag = =-1)Continue; +              for(inti = flag +1; I < n; i++) A                 if(A[i] = = dis1[k][1]) {cout<<"YES"<<endl<<a[flag]<<a[i]<<endl; Ans =true; Break;} the         } +         if(ans = =true)Continue; -          for(inti =0; I < n; i++) $             if(A[i] &1) {x = i; Break;} $         if(x = =-1) {cout<<"NO"<<endl;Continue;} -          for(intK =0; K <6; k++) -         { the             if(ans = =true) Break; -             intFlag =-1;Wuyi              for(inti = x+1; I < n; i++) the                 if(A[i] = = dis2[k][0]) {flag = i; Break;} -             if(Flag = =-1)Continue; Wu              for(inti = flag +1; I < n; i++) -                 if(A[i] = = dis2[k][1]) {cout<<"YES"<<endl<<a[x]<<a[flag]<<a[i]<<endl; Ans =true; Break;} About         } $         if(ans = =true)Continue; -cout<<"NO"<<Endl; -     } -     return 0; A}
View Code

CF 550 C divisibility by Eight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.