Digital and text-back games, digital and text-back Games

Source: Internet
Author: User

Digital and text-back games, digital and text-back Games

The beauty of programming two questions.

Question 1: Find the nine-digit numbers that meet the condition. Each digit is different (1 ~ 9) to enable n-bit division by n. For example, abcdefghi, the two high AB nodes can be divisible by two, the three abc nodes can be divisible by three, and so on.

When it comes to arrangement, it is easy to think of recursion + backtracking. This method is frequently used in interviews. 1 ~ 9 The Nine numbers are arranged in a full arrangement. The numbers meet the conditions and are returned if they do not meet the conditions. Here, we can use some pruning methods. For example, the second high position can only be an even number, the fourth high position can only be an even number, and the fifth high position can only be an integer number. The sixth high position can only be an even number, this pruning greatly improves the program performance.

<Span style = "font-size: 14px;"> void FinANumber (char a [], int len, int index) {// The second number must be an even number if (index = 1 & (a [index]-'0') % 2! = 0) return; // The fourth number must be an even number if (index = 3 & (a [index]-'0') % 2! = 0) return; // The fifth number must be a multiple of 5. if (index = 4 & (a [index]-'0') % 5! = 0) return; // The sixth number must be an even number if (index = 5 & (a [index]-'0') % 2! = 0) return; // The eighth number must be an even number if (index = 7 & (a [index]-'0') % 2! = 0) return; if (index = len) {for (int I = 0; I <len; I ++) {// The number of the first n must be divisible by n. int n = 0; for (int j = 0; j <= I; j ++) n = n * 10 + a [j]-'0'; if (n % (I + 1 )! = 0) return ;}for (int I = 0; I <len; I ++) cout <a [I]; cout <endl; return ;} // recursion + backtracking for (int I = index; I <len; I ++) {char tmp = a [index]; a [index] = a [I]; a [I] = tmp; FinANumber (a, len, index + 1); tmp = a [index]; a [index] = a [I]; a [I] = tmp ;}} int main () {char a [] = "123456789"; FinANumber (a, 9, 0); system ("pause "); return 0 ;}</span>


Question 2: abcde * f = edcba, find the five abcdef numbers.

Method of exhaustion. Use a 1 ~ The hash table of '9' can record which quantities have been used and which quantities have not been used to determine whether they match the meaning of the question.

<Span style = "font-size: 14px;"> // void Palindrome () {bool isUsed [10]; for (int n = 10000; n <100000; n ++) {bool flag = true; int copy = n; memset (isUsed, 0, sizeof (isUsed); int reverseNum = 0; // returns the inverse Number of n in a loop for (int I = 0; I <5; I ++) {int tmp = copy % 10; reverseNum = reverseNum * 10 + tmp; copy/= 10; if (isUsed [tmp]) {flag = false; break;} elseisUsed [tmp] = true ;} if (flag & (reverseNum % n = 0) {int x = r EverseNum/n; if (x <10 &&! IsUsed [x]) cout <n <"*" <x <"=" <reverseNum <endl ;}} int main () {Palindrome (); system ("pause"); return 0 ;}</span>

Refer:
The beauty of programming 4.10


Digital puzzles

A/B/C = 5
A/B = 5c
A/B-C = 12 c = 3
Then a/B = 15
A-B = 84
A = 90, B = 6

How to use java to write a program to determine whether a number is a return number?

Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
System. out. println ("Enter the number of rows in the Matrix 1 :");
String a = SC. nextLine ();
Boolean B = false;
For (int I = 0; I <a. length (); I ++ ){
If (a. charAt (I )! = A. charAt (a. length ()-i-1 )){
B = false;
Break;
} Else {
B = true;
}
}
If (B ){
System. out. println (a + "文 ");
} Else {
System. out. println (a + "not 文 ");
}

}

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.