Step-by-Step Algorithm writing (data selection)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

In mathematics, there are some data options. For example, there is a set of data: 1, 2, 3, and 4. Now we plan to select one data from it. How many options are there? The result should be 1, 2, 3, and 4. How can I select two data entries? The result is 12, 13, 14, and 15. And so on, we can also select three data records and four data records.

 

So how should we express it in the program? Actually, recursive methods can be used. Please count with me:

 

If you want to select two data items from 1, 2, 3, and 4, do you want to start from 1 first, and then select a data item from 2, 3, and 4, in this way, there are three situations: 12, 13, and 14. Next, starting from 2, we can select only data from 3 and 4, and 1 cannot be selected, otherwise duplicate options will be generated. And so on, when we found that there was no data behind 4 from the beginning of 4, the iteration was terminated.

 

If we select two data sets, is that true if we select n data sets? First, select 1st pieces of data. The remaining pieces of data can only be selected starting from the end of the data. n-1 pieces of data are selected, it indicates that n pieces of data exist, the N-1 data records cannot be selected. Then, we move the first data location and select n-1 data records after the current data. Similarly, if we find that there are no n-1 pieces of data after the current data, the recursion is over.

 

Now we can write the code.

 

A) define global space and print functions to save the data that has been traversed

 

 

Static int gAllData [MAX_NUMBER] = {0 };

Static int gTotal = 0;

 

Void print (int pData [], int length)

{

Int index;

 

For (index = 0; index <length; index ++)

Printf ("% d", pData [index]);

 

Printf ("\ n ");

}

Static int gAllData [MAX_NUMBER] = {0 };

Static int gTotal = 0;

 

Void print (int pData [], int length)

{

Int index;

 

For (index = 0; index <length; index ++)

Printf ("% d", pData [index]);

 

Printf ("\ n ");

} B) Start data Iteration

 

 

Void traverse (int pData [], int length, int number)

{

Int index;

If (0 = length)

Return;

For (index = 0; index <length; index ++ ){

GAllData [gTotal ++] = pData [index];

 

If (1 = number)

Print (gAllData, gTotal );

Else

Traverse (pData + (index + 1), length-(index + 1), number-1 );

 

GAllData [-- gTotal] = 0;

}

}

Void traverse (int pData [], int length, int number)

{

Int index;

If (0 = length)

Return;

For (index = 0; index <length; index ++ ){

GAllData [gTotal ++] = pData [index];

 

If (1 = number)

Print (gAllData, gTotal );

Else

Traverse (pData + (index + 1), length-(index + 1), number-1 );

 

GAllData [-- gTotal] = 0;

}

} C) Write test cases and verify the results

Void test ()

{

Int data [] = {1, 2, 3, 4, 5, 6 };

Memset (gAllData, 0, sizeof (int) * MAX_NUMBER );

Traverse (data, sizeof (data)/sizeof (int), 4 );

}

Void test ()

{

Int data [] = {1, 2, 3, 4, 5, 6 };

Memset (gAllData, 0, sizeof (int) * MAX_NUMBER );

Traverse (data, sizeof (data)/sizeof (int), 4 );

}

Note: We can constantly modify the array data and numeric number methods to verify whether the printed data is different from the results calculated by ourselves.

Related Article

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.