Easy weekly competition question: can the competition be divided by 8, and can the competition be divided by 8?
Exercise questions link: http://student.csdn.net/mcs/programming_challenges
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. The output format outputs 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.
Today, a younger brother asked me how to solve this problem. I directly replied that he arranged all the numbers to verify whether they could be divisible by 8, for example, 123 in full order: 123, 231, 312, 213, 132, 321 (3 in total! = 6)
After listening to me, he asked me how to make a full arrangement, and then I found a method to copy and paste it to him (this, huh, this method can be found online ), he just turned his ass off to me. The full arrangement method is the easiest and most effective way to verify the developer's capabilities.
The following provides a full sorting method to solve this problem. Of course, this method can be seen everywhere on the Internet:
1 class Program 2 {3 static int Count = 1; 4 static int DivNum = 8; 5 6/* 7*8 *: {}, {} 9, 3: 10 * {, 2}, {, 2}, {, 3} (randomly inserted in {}) 11 * {, 1 }, randomly inserted 3 in {2, 3}, {2, 1, 3} ({2, 1}) 12*13*1, 2, 3, 4: 14 * is to insert 415*16 * randomly in the above six groups of numbers and so on 17 */18 19 static void Main (string [] args) 20 {21 string [] arr = new string [] 22 {23 "11223344", 24 "23456798", 25 "89289238", 26 "23458203945829345" 27 }; 28 29 foreach (string item in arr) 30 {31 if (Perm (item. toCharArray (), 0) 32 Console. writeLine ("{0} Yes", item); 33 else34 Console. writeLine ("{0} No", item); 35} 36} 37 38 static void Swap (ref Char lc, ref Char rc) 39 {40 Char tmp = lc; 41 lc = rc; 42 rc = tmp; 43} 44 45 static bool Perm (char [] arrChar, int cur) 46 {47 if (cur = arrChar. length-1) 48 {49 long val1 = 0; 50 51 if (long. tryParse (new string (arrChar), out val1) & val1 % DivNum = 0) 52 {53 return true; 54} 55 else56 return false; 57} 58 else59 {60 for (int I = cur; I <arrChar. length; I ++) 61 {62 Swap (ref arrChar [cur], ref arrChar [I]); 63 if (Perm (arrChar, cur + 1) 64 return true; 65 Swap (ref arrChar [cur], ref arrChar [I]); 66} 67} 68 return false; 69} 70}C # complete sorting
Sorting Algorithm result:
There is also a more concise method, the erlang language. I remember in the chapter of the Modified Word, the example is full sorting. It is roughly as follows:
-Module (lib_misc ). -export ([perms/1]). perms ([])-> [[]; perms (L)-> [[H | T] | H <-L, T <-perms (L -- [H])].Erlang full sorting
Sorting Algorithm result:
The full sorting method was abandoned in my subsequent questions, because this question cannot be fully sorted and there is a harsh condition in it.
Below I will extract my ideas to help readers.
In fact, it is better to use induction for this question. Why? Question promptThe number of digits of a non-negative integer cannot exceed 10000What does this mean? The length of a non-negative integer may reach 9999 bits. If such a large number is in full order, it is estimated that the computer will die. What's more, my development machine is an old antique eight years ago, if you think of such an idea, it will surely be unable to stand it. So why can we use induction? Well, I guess, don't laugh. I'm telling the truth. I guess it can use induction, so I have to find out what rule it follows. Here we start to make some assumptions:
How is it? It's easy. Just judge it.The number composed of the last three digits can be divisible by 8, which is bound to be divisible by 8This is a civil servant examination question. Don't ask me why I know it. This is a secret. O (∩) o haha
If you encounter a problem and don't want to do it, there is only one kind of result, in vain. It is better to do nothing. I started to sort this question in full order, because I had a good idea, but after carefully reading the question, I found that the problem was not so serious and there was a requirement that could be hard to achieve, that is, how to calculate the number of digits reaching 9999? So, I analyzed it carefully and found it familiar. Haha.
Sorry, bro, it's not over yet. I just made this inference and haven't written a method for combining numbers. Sorry, please wait.
1 string aaa = "123123213840213849102384192034801923480192381234"; 2 int length = aaa. length-3; 3 4 for (int x = 0; x <length; x ++) // corresponds to a hundred digits 5 {6 for (int y = 0; y <length; y ++) // corresponds to 10 digits 7 {8 if (y = x) 9 continue; 10 11 for (int z = 0; z <length; z ++) // corresponding single digit 12 {13 if (z = x | z = y) 14 continue; 15 16 if (Convert. toInt32 (new string (new char [] {aaa [x], aaa [y], aaa [z]}) % 8 = 0) 17 {18 char [] arr = aaa. toCharArray (); 19 20 Swap (ref arr [x], ref arr [length + 0]); 21 Swap (ref arr [y], ref arr [length + 1]); 22 Swap (ref arr [z], ref arr [length + 2]); 23 24 Console. writeLine ("rearranged string:" + new string (arr); 25 goto GT_1; 26} 27} 28} 29} 30 31 GT_1 :;Simple Writing point
Okay, that's all. The questions on this website are not bad. I have read a lot of questions. If you are fine, you can practice them. Http://student.csdn.net/mcs/programming_challenges