Code Stream of Consciousness-the number of flowers (3)

Source: Internet
Author: User

Links in the previous section
Html> http://www.bkjia.com/kf/201105/92330.html

7. more straightforward and exhaustive solutions

Since the second solution is essentially a combination of numbers, it may not be as straightforward as simply. Although the 3rd solutions are slightly abstract, they are more direct.
Solution 3.
For (number of digits (JINZHI-1) = 0; number of digits (JINZHI-1) <= Total number of digits; number of digits (JINZHI-1) + +)
For (number of digits (JINZHI-2) = 0; number of digits (JINZHI-2) number <= Total number of digits-number of digits (JINZHI-1); number of digits (JINZHI-2) + +)
......
For (number of digits 1 = 0; number of digits 1 <= Total number of digits-sum of the preceding numbers; number of digits 1 ++)
{
// Number of digits 0 = total digits-sum of the numbers above
// <=> Check result <=> record result
}

The depth of loop nesting in this solution is lighter than that in the second solution, which is more advantageous for a large number of computations.
8. test the feasibility of the Solution
Test whether the three-in-five-digit testing scheme is feasible.
Modify

View sourceprint? /* 2 _ example. c */

# Include "2 _ example. h"

Extern void qiongju (void)

{

Int n2;

For (n2 = 0; n2 <= WEISHU; n2 ++ ){

Int n1;

Printf ("% d", n2, JINZHI-1 );

For (n1 = 0; n1 <= WEISHU-n2; n1 ++)

{

Int n0;

Printf ("% d", n1, JINZHI-2 );

{

N0 = WEISHU-n2-n1;

Printf ("% d", n0, 0 );

Putchar ();

// <=> Check result <=> record result

}

}

}

}

Modify

View sourceprint? /* 0 _ problem. h */

# Ifndef WENTI_H

# Define WENTI_H

# Define CESHI // perform the test

// # Define QIUJIE // solve the 21-bit problem

# Ifdef CESHI // Test

# Define JINZHI 3 // 10 // decimal

# Define WEISHU 5 // 3 // 3 flowers

# Define n weishu // power = digits

# Endif // CESHI

# Ifdef QIUJIE // solve

# Define JINZHI 10 // decimal

# Define WEISHU 21 // number of digits

# Define n weishu // power = digits

# Endif // QIUJIE

# Endif // WENTI_H

Modify

View sourceprint? /* 2 _ example. h */

# Ifndef QIONGJU_H

# Define QIONGJU_H

# Include "0 _ problem. h" // The qiongju () function uses JINZHI, WEISHU

*********** ***************/

/************************** Function prototype *********** ***************/

Extern void qiongju (void );

# Endif // QIONGJU_H

The running results meet expectations, indicating that the solution is feasible.
9. Change nesting to recursion.
Loop nesting can only describe the nested structure of a fixed number of layers, so the previous scheme can only write a specific hexadecimal situation (3-in-2-layer loop ...... 10-in-9 loops ). If you want the code to be true for different operating systems, you need to replace the nested loop into the recursion of the function, that is, use the function to describe the operation of each loop and the innermost part of the loop body.
This modification is also necessary even when only decimal is taken into account. Because the structure of nested loops is complex and cumbersome, it is not conducive to modification (a lot of content needs to be added later) and maintenance. Besides, writing 9-layer loops is chilling, at least for me. It is hard to make up my mind to write 9-layer loop nesting.
Each layer of loop (including the innermost loop body) requires two parameters to determine which number is enumerated, and the maximum number of this number is several (upper limit ). Take the first loop as an Example

For (n2 = 0; n2 <= WEISHU; n2 ++ ){
Printf ("% d", n2, JINZHI-1 );
......
}
The "WEISHU" and "JINZHI-1" parameters are required. The second-layer loop and the innermost-layer loop body also need these two parameters.
For recursive function calls, there may be three ways to obtain these parameters: real parameters of function calls, external variables, and local static variables. The first method is most commonly used. The external variable method is usually not writable, and the local static variable method is slightly more difficult to write.

The modified qiongju () function is defined

View sourceprint? /* 2 _ example. c */

# Include "2 _ example. h"

Static void xunhuan (const int, const int );

Extern void qiongju (void)

{

Xunhuan (WEISHU, JINZHI-1 );

}

Static void xunhuan (const int gssx/* Number Limit */,

Const int sz/* about which number */)

{

If (sz> 0 ){

Int I;

For (I = 0; I <= gssx; I ++ ){

Printf ("% d", I, sz );

Xunhuan (gssx-I, sz-1 );

}

}

Else {

Printf ("% d", gssx, sz );

& Nbs

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.