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

Source: Internet
Author: User

0. Problem (simhei, 15)

An N-digit decimal positive integer. If the sum of the N-digit power of each digit is equal to the number itself, it is called the number of flowers. (, 15, Arial)
For example:
When N = 3, 153 meets the condition, because 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153, such a number is also called the number of daffodils (where, "^" indicates the Multiplication Side, and 5 ^ 3 indicates the 3rd power of 5, that is, the cube ).
When N = 4, 1634 meets the conditions because 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 = 1634.
When N = 5, 92727 meets the conditions.
In fact, each value of N may have multiple numbers that meet the conditions.
The task of the program is: Find the number of flowers that meet the conditions when N = 21. Note: This integer has 21 digits, and the sum of the 2nd power of each digit is exactly equal to this number. If there are more than one number that meets the condition, output all numbers that meet the condition from small to large. Each number occupies one row. Because this number is large, pay attention to the feasibility of the solution time. The program is required to run within 3 minutes.

1. Use C language to express the problem

/* 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 10 // decimal
# Define WEISHU 3 // number of 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
To compile the code, you must first raise a question, understand the problem, and use the C language to express the problem.

The symbolic constant here is the C language description of the problem. Although it is impossible to fully describe it, it fully shows the characteristics of the problem. The problem is abstracted so that the code can be adapted to a wider scope. That is, it is not only suitable for solving the number of flowers in 21 bits, but also for solving the number of flowers in other bits. It is not only suitable for solving the decimal problem, but also for other hexadecimal problems.
Writing constants in the problem into symbolic constants is also required for testing. No one dares to drive a car that has not been tested, but it is strange that people dare to use software that has not been fully tested. In fact, the latter is often more dangerous.

The necessity of testing is also reflected in the fact that code of a relatively large scale cannot be correctly written in one go. Testing is actually a powerful assistant for development.
When I started writing the code, I didn't know whether there were 21 flowers. If so, I still don't know what the number of flowers is. If you want to have some confidence in your code, this confidence can only come from testing. However, there is almost no possibility of testing the number of 21 flowers. In this case, we can only test similar problems of small scale. After the tests of small-scale problems are passed, we can have some confidence in the solution of large problems.

Here, "# define CESHI // test" is like a switch. If you change CESHI to QIUJIE, you do not need to modify the code. You only need to re-compile it, then we can get a program to solve the 21-bit problem.


2. Start solving

First, write main (). Because the test is involved in the code writing process, two main () are written ().

/* Main.c */
/*
Name: Number of flowers
Author: keyboard farmer
Date: 2011,5, 30
Description:
*/

# Include "main.h"

# Ifdef CESHI // Test

Int main (void)
{

System ("PAUSE ");
Return 0;
}

# Endif // CESHI

# Ifdef QIUJIE // solve

Int main (void)
{

System ("PAUSE ");
Return 0;
}

# Endif // QIUJIE
There is also

/* Specify main.h */
# Ifndef MAIN_H
# Define MAIN_H

# Include "0 _ problem. h"
*********** ***************/


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


# Include <stdlib. h> // system ()

# Endif // MAIN_H
This is the link between "0 _ Question. h" and main (). It is also used to supplement the type definition and function prototype in the future.

3. Solution

It is not difficult to review the problem. All the requirements of the problem can be divided into two steps: "solving" and "output.

Searching for your own mathematical knowledge does not have any magical theorem to help solve this problem. No way, you have to use the most stupid way-to make effort. You can only check the enumerated possibilities during the exhaustive process. Therefore, the main () is rewritten

# Ifdef QIUJIE // solve

Int main (void)
{
// Solution: Calculate the result
// Output
System ("PAUSE ");
Return 0;
}

# Endif // QIUJIE
The reason for "record results" instead of "output immediately" is that the problem requires "output from small to large ".

Compilation successful, running properly! Close.

4. Complete the first custom function framework process

There must be at least four functions to write. First, we should start with "Exhaustion. Steps:

1. Write the function call in main (), qiongju ();

2. Describe the prototype of this function in situ, void qiongju (void );

# Ifdef QIUJIE // solve

Int main (void)
{
// Solution: Calculate the result
Qiongju (); // void qiongju (void );
// Output
System ("PAUSE ");
Return 0;
}

# Endif // QIUJIE
3. considering the location defined by this function, this function may be complicated, so we arrange this function in another source file "2 _ exhaustive. c. c and 2 _ poor. h"

4. Add the # include "2 _ exhaustive. h" preprocessing command to "cmdmain.h ".

5. Move the qiongju () function prototype in main () to the function prototype section of "2 _ exhaustive. h" and modify it
Extern void qiongju (void );

/* 2 _ example. h */# ifndef QIONGJU_H # define QIONGJU_H/*************************** type definition ** ************************//************** ************************** /extern void qiongju (void ); # endif // QIONGJU_H 6. in the "2 _ example. c "writes an empty definition of the qiongju () function. By the way, some comments in main () are moved.

/* 2 _ example. c */

# Include "2 _ example. h"

Extern void qiongju (void)
{// <=> Check result <=> record result}: replace CESHI in # define CESHI in 0 _ problem. h with QIUJIE. The compile is passed and runs properly! Close.

(To be continued)

 

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.