Review University-Sophomore data structure experiment-experiment 1 recursive exercise

Source: Internet
Author: User
Lab 1 recursive exercise

I. Tutorial Purpose

1. Familiar with the use of development tools.

2. Grasp the implementation idea of recursion.

Ii. experiment content

1. output the full arrangement of N integers.

2. Output all subsets of N integers.

First, install the development tool:

Visual c ++ 6.0 (based on your preferences)

Baidu search downloads:

1:Visual c ++ 6.0 Simplified Chinese enterprise integrated SP6 perfect Edition

2:Visual c ++ 6.0 Simplified Chinese enterprise integrated SP6 perfect Edition

------------------------------------------------------------------

The following is my code:Click here to download the full arrangement of N Integers

------------------------------------------------------------------

1. output the full arrangement of N integers.

The following are the main methods:

template<class T>void Perm(T list[],int k,int m){int i;if(k==m){for(i=0;i<=m;i++)cout<<list[i];cout<<endl;}else for(i=k;i<=m;i++){Swap(list[k],list[i]);Perm(list,k+1,m);Swap(list[k],list[i]);}}

This method is called the arrangement method perm: The full arrangement of the K to M elements in the output list

Example:

The list is ABCD (the elements are 0th to 3rd, that is, a is 0th elements, and B is 1st elements)

After perm is arranged between 1st and 3rd (BCD), the result is:

abcdabdcacbdacdbadbcadcb

It can be seen that 0th elements a remain unchanged, and 1st to 3rd (that is, BCD) are fully arranged, but a is also output in the output.

If the perm method is understood, the entire program will be similar.

The following is a recursive idea:

1. If there is only one sorting object, for example, if the above ABCD object needs to be sorted only by the last d, the output is directly the ABCD object.

2. Otherwise, all possible 0th elements and all subsequent elements are output in full order, such as the ABCD Column

0th elements are a, 0th elements are arranged in BCD as B, 0th elements are arranged in ACD as C, and 0th elements in bad are arranged as D, then sort BCA in full order

------------------------------------------------------------------

The following is my code:Click here to download all subsets of N Integers

------------------------------------------------------------------

The main recursive method is as follows:

Void set (char * s, int Len, int bit) // recursive main method {If (bit = Len) {Show (s );} else {char c = * (S + bit); * (S + bit) = '\ 0'; // remove the first bit element set (s, Len, bit + 1); * (S + bit) = C; // Add the first bit element set (s, Len, bit + 1 );}}

First clear the set method: output the second BIT in S to the subset of the len-1 elements, (here with the same, are given a starting position, and finally to the last element on the line)

Example:

S is ABC (the element is 0th to 2nd, that is, a is 0th elements, and B is 1st elements), and outputs a subset of 1st to 2nd elements (that is, BC:

Output result:

{} {b} {c} {bc}

The following is a recursive idea:

1, if bit exceeds the last element len-1, that is, bit = Len, then the output empty set

2. Otherwise, we can use the subset of N-1 elements to represent the subset of n elements. The following recursive relationship can be obtained through observation:

N element subsets: add the first element and n-1 element subsets to each n-1 element subset. For example, if {ABC} removes the first element a as {BC}, its subset is: {} {B} {c} {BC}, and the first element a is: {A} {AB} {AC} {ABC}, the four subsets of {ABC}, which are more than {BC}, are: {} {B} {c} {BC}, {A} {AC} {AB} {ABC }. Exactly the union of the above two

Recursive View:

Recursion is hard to understand. I once tried to understand and understand all recursion. Later, I just made myself depressed. Now let's think about it as a matter of course. If you can think of it, you can't think of it as a matter of luck. Most of the time, I would admire who came up with this solution when I was doing a math problem? How can ordinary people think of it like this? Obviously, it's just coming together! Haha, it's good to keep yourself happy!

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.