Simulate multiple nested loops with recursive return.

Source: Internet
Author: User

Original address: https://blog.csdn.net/haiboself/article/details/51946327

Simulate multiple nested loops with recursive return. such as the following loops:

1 2 3 4 5 6 for (int i=0;i<100;i++) for (int j=0;j<100;j++) for (int k=0;k<100;k++) {SYSTEM.OUT.PR          Intln ("hello!"); }

Corresponds to recursion:

[java]  View plain  copy public static void demo () {        int[] counts = new int[3];       counts[0] = 1;        counts[1] = 2;       counts[2]  = 3;           for (int i=0;i<counts[0];i++)            for (int j=0;j<counts[1];j++)                for (int k=0;k<counts[2];k++) {                     System.out.println ("hello!");                }            int index = 0;//subscript    &NBSP;&NBSP;&NBSP;&NBsp;dfs (Counts,index)//corresponds recursion   }      //corresponding recursion, and i,j,k three-layer loop execution order and effect want to pass .    Public static void dfs (int[] counts,int index) {        for (int i=0;i<counts[index];i++) {           if ( INDEX&NBSP;==&NBSP;COUNTS.LENGTH-1) {//to the last loop control variable K                 system.out.println ("Hello");            }           else{                dfs (counts,index+1);            }       }  }  

Application:
Lucas ' army of deportees had come to the Canossa city of Hebe, and Hebe finally made up his mind to assemble the army and fight the deportees.
Lucas's men had 6 days of eviction, and the 6-day eviction was the main force of Lucas.
In order to defeat Lucas, Hebe had to think about how to arrange his own berserker to confront him.
The magic of the Mad Warrior and the magical attributes of some of the days ' deportees are the phase, and the magic of the first Mad Warrior can be restrained by the collection of the deportees of the day as Si (each element of Si belongs to [0,5]).
To be fair, two Berserker fighters cannot attack the same day as the deportees.
Now he needs to know how many kinds of assignments are in total.
Cases:
S1={01},S2={23}, the Magic Warrior number 0 is able to restrain the number 0 and number 1 of the day of the expulsion, number 1 of the Berserker Magic can restrain the number 2 and number 3 of the day of the expulsion, a total of four programs: 02,03,12,13.
---represents the first Berserker who is responsible for the expulsion of the number 0, and the second Berserker, who is numbered 2;
---represents the first Berserker who is responsible for the expulsion of the number 0, and the second Berserker is responsible for the expulsion of the number 3;
---represents the first Berserker who is responsible for the expulsion of the number 1, and the second Berserker, who is numbered 2;
---represents the first Berserker who is responsible for the expulsion of the number 1, and the second Berserker, who is numbered 3;
S1={01},S2={01}, on behalf of the Mad Warrior number 0, can restrain the number 0 and number 1 of the day of the eviction, number 1 of the Berserker Magic can restrain the number 0 and number 1 of the day of the expulsion, a total of two options: 01, 10.

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.