Record: C # programming magic number 6174

Source: Internet
Author: User


A four-digit magic number.

Description:

Step 1: split each digit into four digits;

Step 2: Use the four digits split in step 1 to form a maximum of four digits and a minimum of four digits;

Step 3: subtract the minimum number from the maximum number in step 2. If the difference is 6174, the fourth digit in the first step is a magic number. Otherwise, the first step is performed for the difference, after 15 cycles.

Code:


/** Created by SharpDevelop. * User: Ajax * Date: Saturday * Time: ** the following code is used to determine whether a four-digit number meets the mathematical 6174-digit number */using System; namespace Demo2 {class Program {public static void Main (string [] args) {// number detected: {5643,5140} // number detected: {1111,9999} int startNumber = 5643; int initNumber = startNumber; int [] number = new int [4]; int maxNumber = 0; int minNumber = 0; // count is used to count the number of cycles. To ensure that no endless loop occurs, int coun T = 15; int tempCount = count; // because count times may not detect 6174 numbers, // so it indicates that if the print output of the count jump out of the loop "is used for count Detection, it still cannot be judged as a 6174 number" bool flag = true; do {tempCount --; if (tempCount <0) {flag = false; break;} // each time the loop body is entered, the two values return to the initial value maxNumber = 0; minNumber = 0; int index = 0; int initNumberTemp = initNumber; while (initNumberTemp> 0) {int temp = initNumberTemp % 10; initNumberTemp/= 10; number [index] = temp; Console. write (n Umber [index] + ""); index ++;} Console. writeLine (); Console. writeLine ("Step 1: Split numbers =" + initNumber + "= complete"); // sort the numbers in the array from large to small (less efficient Bubble Sorting) for (int I = 0; I <number. length-1; I ++) {for (int j = I + 1; j <number. length; j ++) {if (number [I] <number [j]) {int temp = number [I]; number [I] = number [j]; number [j] = temp ;}}// print the number sequence Console after sorting from large to small. write ("Step 2: print the order of numbers sorted in ascending order"); for (int j = 0; j <number. lengt H; j ++) {Console. write (number [j] + "");} Console. writeLine (); // The maxcompute Console after merging and sorting. write ("Step 3: The largest numeric value after sorting"); for (int I = 0; I <number. length; I ++) {maxNumber = maxNumber * 10 + number [I];} Console. writeLine (maxNumber); // sorts the numbers in the array from small to large (essentially exchange, dependent on the sorting in the second step above) for (int I = 0; I <number. length/2; I ++) {int temp = number [I]; number [I] = number [number. length-1-I]; number [number. length-1-I] = t Emp;} // print the number sequence Console after the output is sorted in ascending order. write ("Step 4: print the order of numbers in ascending order"); for (int j = 0; j <number. length; j ++) {Console. write (number [j] + "");} Console. writeLine (); // The minimum number Console after merging and sorting. write ("Step 5: minimum number after sorting"); for (int I = 0; I <number. length; I ++) {minNumber = minNumber * 10 + number [I];} Console. writeLine (minNumber);} while (initNumber = maxNumber-minNumber )! = 6174); // The result is judged and the final output if (flag) {Console. writeLine ("{0} is a 6174 number", startNumber);} else {Console. writeLine ("{0} detection still cannot judge {1} is a 6174 number", count, startNumber);} Console. readKey ();}}}

Running result diagram:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2033235636-0.png "style =" float: none; "title =" 1.png" data-pinit = "registered"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/20332313W-1.png "style =" float: none; "title =" 2.png" data-pinit = "registered"/>

In fact, if each digit in four digits is the same, it cannot constitute a magic character number. Because the difference between the maximum number and the minimum number is always equal to 0.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2033234251-2.gif "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2033234251-2.gif "/> but this is a human interference to get such a judgment, if the computation through the computer, we can only add a threshold for the number of operations, otherwise, only an endless loop is entered.


This article is from the "wild horse red dust" blog and will not be reposted!

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.