# Use 1, 2, 3, 4, and 5 to form a five-digit repeat. 4 cannot be In the third place, and 3 cannot be connected to 5 (the simplest way)

Source: Internet
Author: User
`Package COM. WZS; // Add difficulty to the first question. Use the numbers 1, 2, 3, 4, and 5 to write a main function in Java and print out all the different orders, // For example, 51234, 12345, etc., the requirement: "4" cannot be In the third place, "3" and "5" cannot be connected. Public class test3 {public static void main (string [] ARGs) {int number = 0; int COUNT = 0; string numberstr; For (int A = 1; A <= 5; A ++) {for (INT B = 1; B <= 5; B ++) {for (INT c = 1; C <= 5; C ++) {for (INT d = 1; D <= 5; D ++) {for (int e = 1; E <= 5; E ++) {// print all non-repeated numbers if (! = B &! = C &! = D &! = E & B! = C & B! = D & B! = E & C! = D & C! = E & D! = E) {number = A * 10000 + B * 1000 + C * 100 + D * 10 + E; numberstr = string. valueof (number); // 4 can no longer contain 3rd bits, and "3" and "5" cannot be connected if (numberstr. indexof ("4 ")! = 2 & numberstr. indexof ("35") =-1 & numberstr. indexof ("53") =-1) {count ++; system. out. println (numberstr) ;}}}}} system. out. println ("Count =" + count );}}`

Output result:

` 1234512543132451325414325145231523415243213452154323145231542431524513251342514331245312543152431542321453215432514325413412534152342153425134512345214132541523423154251343125431524321543251451234513245213452315123451243513245134252134521435231452341541235413254213542315431254321 COUNT = 56 `
Related Keywords:

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.