[Data Structure Combat C ++] 1 Preliminary Research on Data Structure
Author CodeAllen , please indicate the source
Why are there various programs?
What is the essence of the program?
The program exists to solve the actual problem.
Essentially, the program is to solve the problem.
How to judge the problem solving step?
Determine the quality of the solution step
/*
问题:给定一个整数 n,编程求解 1 + 2 + 3 + ... + n 的和。
*/
#include <iostream>
using namespace std;
long sum1(int n)
{
long ret = 0;
int* array = new int[n];
for(int i=0; i<n; i++)
{
array[i] = i + 1;
}
for(int i=0; i<n; i++)
{
ret += array[i];
}
delete[] array;
return ret;
}
long sum2(int n)
{
long ret = 0;
for(int 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()
{
cout << "sum1(100) = " << sum1(100) << endl;
cout << "sum2(100) = " << sum2(100) << endl;
cout << "sum3(100) = " << sum3(100) << endl;
return 0;
}
/*
sum1(100) = 5050
sum2(100) = 5050
sum3(100) = 5050
*/
Understand the standards of good and bad procedures
- Solve the problem in as little time as possible
- Solve problems with as few steps as possible
- Solve the problem with as little internal memory as possible
The origin of data structure was
founded in 1968 by Professor Gonard.
In the same year, it appeared in a degree course in computer science (required)
The research scope of the data structure course
- Non-numerical calculation type program problems
- Organization and operation problems between data
- The logical structure and storage of data
A classic formula (language is not important, the important thing is thinking)
program = data structure + algorithm
Summary
Programs exist to solve practical problems.
Multiple solutions can be used for the same problem.
Professional programmers should try to pursue high-quality programs.
Data structure courses mainly study non-numerical calculation problems.