CSDN easy weekly competition question: can it be divided by eight, and csdn can be divided by eight in the weekly Competition

Source: Internet
Author: User

CSDN easy weekly competition question: can it be divided by eight, and csdn can be divided by eight in the weekly Competition
Easy weekly competition question: Can I be divisible by 8? Details:

Given a non-negative integer, ask if you can rearrange all its numbers so that the number after the shuffling can be divisible by 8.

Input Format:

Multiple groups of data, each of which is a non-negative integer. The number of digits of a non-negative integer cannot exceed 10000.

Output Format

Output a row of data in each group. YES or NO indicates whether all its numbers can be rearranged to get the number that can be divisible by 8. Note: resetting can start with 0.



Q & A description:

Input example

610

122

Output example

YES

NO

Explanation

The first number can be 016,160


Analysis: this is a question published at five o'clock P.M. on Friday. This question seems a little difficult. It is arranged in full and contains no more than 10000 digits. It is estimated that the question is all scared, in fact, these two points of information let us discard the full arrangement, because even if there are more than 10000-bit numbers in a full arrangement, there is no way to calculate whether each number can be divisible by 8, it must be TLE (timeout. Therefore, the focus of solving this problem should be changed to "can we be divisible by eight ", if you have questions about numbers or similar questions, you will know that the number divided by any number 0-9 is regular.

In this blog, there are conclusions that can be divided by all numbers 0-9, if you are interested, you can look at (http://blog.csdn.net/lyg105504/article/details/5495080)

Put down the conclusion that the number is divisible by 8.

When n is a single digit, it must be 8 or 0.

When n is a binary number, it should be 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96

When n is a three-digit number and above, if the end (last) three-digit number of The number can be divisible by 8, the number can be divisible by 8.


Some people may not believe it. In fact, the proof is also very simple, as shown below (Proof)

Any three-digit or more n numbers can be expressed as follows:

N = 1000 * a + 100 * B + 10 * c + d (a can be a multiple-digit number, where B, c, and d are 0-9 digits)

Because 1000 can be divisible by 8, then 1000 * a can also be divisible by 8, so whether the number n can be divisible by 8 depends on the three digits after the division, that is

N/8 = (1000 * a)/8 + (100 * B + 10 * c + d)/8

That is, (100 * B + 10 * c + d)/8


With the above conclusions, we need to process the input non-negative integer of no more than 10000 bits.


First, use what to input. If an integer is used, the number of digits obviously cannot be reached. Therefore, use a string for input and use the str variable to mark


Furthermore, the string is processed, because we only need to consider the last three digits of the number of This rearrangement, that is to say, we only need to consider which number of 0-9 appears in this str string and whether it appears more than three times or less than three times. If it is more than three times, we only need to record it three times, therefore, we need to define an array that can hold 30 elements, represented by int a [30]. For example:

For example, str = 11927289799. No matter how the str is rearranged, we only need to consider the numbers that appear in the last three digits. 1 appears twice, 2 appears twice, and 7 appears twice, 8 appears once, 9 appears four times, then these numbers can all appear at the end of three, but because 9 appears four times, the three digits at the end of the rearrangement can store up to three 9 s, so we only need to store three 9 s in array.


Finally, we get the array a after string processing. below is the three traversal of this array, because after the shuffling, 0 can start, so we do not need to consider the problem of 0, however, we need to consider repeated issues. For example, if array a [0] is one of the three places that have been placed at the end of the array, a [0] cannot be reused, obtain group a [I], a [j], a [j], form a three-digit number a [I] * 100 + a [j] * 10 + a [k], and then determine whether it can be divisible by 8, if NO combination can be divisible by 8 after traversal, NO is output. If a combination can be divisible by 8, YES is output, and terminate the triple loop at the same time (use the labeled method to terminate the loop)


Paste your own code below

// Can be divisible by 8 # include <iostream> # include <string> using namespace std; int main () {# ifdef LOCALfreopen ("input.txt", "r ", stdin); # endifstring str; while (cin> str) {// defines the number of times each number 0-9 appears in the str string, the initial full value is set to 0int cnt [10] = {0}, m = 0, a [30]; int length = str. length (); for (int I = 0; I <length; ++ I) {if (cnt [str [I]-'0'] <3) {a [m ++] = str [I]-'0'; cnt [str [I]-'0'] ++ ;}} // determine whether the condition can be divisible by 8 is if (m = 1) {if (a M-1] % 8 = 0) cout <"YES" <endl; elsecout <"NO" <endl;} else if (m = 2) {if (a M-1] * 10 + a [m-2]) % 8 = 0 | (a [m-2] * 10 + a [M-1]) % 8 = 0) cout <"YES" <endl; elsecout <"NO" <endl;} else {int I, flag1 = 0, flag2 = 0; for (I = 0; I <m; ++ I) {// determine whether the loop ends if (flag1 = 1) break; for (int j = 0; j <m; ++ j) {// determine whether the loop terminates if (flag2 = 1) break; if (j! = I) {for (int k = 0; k <m; ++ k) {if (k! = I & k! = J) {// cout <a [I] <a [j] <a [k] <endl; if (a [I] * 100 + a [j] * 10 + a [k]) % 8 = 0) {cout <"YES" <endl; flag1 = 1; flag2 = 1; break ;}}}} if (flag1 = 0 & flag2 = 0) cout <"NO" <endl;} return 0 ;}


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.