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

Source: Internet
Author: User

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

5. Prepare to write the qiongju () function.

Because the problem to be solved requires time and the calculation object is 21 digits, it is not hard to imagine that this function may be complicated. It cannot be achieved overnight. Therefore, you must consider testing before writing.
Add a function in main () to call qiongju ();

# Ifdef CESHI // Test

Int main (void)
{
Qiongju ();
System ("PAUSE ");
Return 0;
}

# Endif // CESHI

Change QIUJIE in # define QIUJIE in question 0 _ to CESHI, which is compiled and run successfully. This indicates that there is no problem in file inclusion and qiongju () function prototype.
Go to the 2 _ exhaustive. c source file and fill in the empty qiongju () function definition.

6. Thoughts on the exhaustive Solution

The most easy-to-think-out solution is

Solution 1.

For (W_ws = Minimum W digits; W_ws <= maximum W digits; W_ws ++)
{
// Check
}

This solution first needs to solve the W-digit representation problem, that is, the design and storage of the data type.
In addition, you need to consider how to assign values, add 1, and compare the data size.
Most importantly, you also need to solve the problem that the search space is too large.
Solving these problems at the same time is always daunting (but it cannot be solved.


The second solution is to use the nested loop mode to perform exhaustive operations.
Solution 2.

For (1st bits = 1; 1st bits <JINZHI; 1st bits + +)
For (2nd bits = 0; 2nd bits <JINZHI; 2nd bits + +)
......
For (W digits = 0; W digits <JINZHI; W digits ++)
{
// Check
}

In the beginning, this solution does not have to consider the W-digit representation, but the search space is still large.
The reason for the huge space for solving is that the exhaustive list of numbers is given, which is a fairly large number (JINZHI-1) * JINZHI ^ (W-1) for 21-digit decimal numbers, the value is 9*10 ^ 20 ). However, there are quite a few of the arrays. The sum of the numbers to the nth power is the same (take the three-digit number as an example. The sum of the power to the numbers of 110 and 101 is 2 ), these values should not be repeated.
To avoid repeated calculation of these values, you should check only the combinations of numbers rather than the arrangement of numbers. To this end, we can improve the exhaustive cycle structure

Solution 2-1.
For (1st bits = JINZHI-1; 1st bits> 0; 1st bits --)
For (2nd digits = 1st digits; 2nd digits> = 0; 2nd digits --)
......
For (W digits = W-1 digits; W digits> = 0; W digits --)
{
// Check
}
This structure ensures that all numbers are no larger than the preceding ones, so we can get a combination of W digits. The search space for the solution is greatly reduced (only 14307149 (30! /9! /21! -1 ).
This solution is intuitive and easy to understand.

Close.
Conclusion: The qiongju () test preparation has been completed.
Feeling: writing code is simple, and it is difficult to think about how to write the code. It is silly to measure the workload of a programmer by the number of lines that complete the code. What are the criteria for the blog garden to remove the essay from the home page? I'm afraid it's at least a matter of opinion.

 

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.