Lesson 1st-the door to advanced masters
- Understanding the nature of a program
Programs exist for actual problems. Essentially, programs are steps to solve the problem.
Problem:
How to put elephants in the refrigerator?
(1) open the refrigerator door (2) Put the elephant in (3) Close the refrigerator door
Elephan * E = getelep ();
Int F = open ("fridge ");
Put (F, E );
Close (F
- First understand the actual problem
(1) confirm the problem type.
For example, calculate the number of minimum values.
(2) confirm the solution steps.
For example, open a file, read data, close a file, computation, and.
Let's look at a program that calculates the sum of the First n items:
# Include <stdio. h>
# Include <malloc. h>
Long sum1 (int n)
{
Long ret = 0;
Int * array = (int *) malloc (N * sizeof (INT ));
Int I = 0;
For (I = 0; I <n; I ++)
{
Array [I] = I + 1;
}
For (I = 0; I <n; I ++)
{
RET + = array [I];
}
Free (array );
Return ret;
}
Long sum2 (int n)
{
Long ret = 0;
Int I = 0;
For (I = 1; I <= N; I ++)
{
RET + = I;
}
Return ret;
}
Long sum3 (int n)
{
Long ret = 0;
If (n> 0)
{
Ret = (1 + n) * n/2;
}
Return ret;
}
Int main ()
{
Printf ("% d \ n", sum1 (100); // 5050
Printf ("% d \ n", sum2 (100); // 5050
Printf ("% d \ n", sum3 (100); // 5050
Return 0;
}
- Program Evaluation
(1) use up a small amount of memory space to solve the problem
(2) step-by-step solution
Excellent developers need to pursue the high "cost-effectiveness" of code "!
Summary:
(1) The program is stored for specific problems.
(2) The program should solve the problem.
(3) The same problem can be solved in many ways.
Data-course 1st-door to advanced masters