{2018.4.10} Array integration time of Xun (gou) Yu (huo)

The night before yesterday finally ended the historic struggle with the for loop;

The 70 questions are basically over the water;

brain is not enough

You can only look at other people's code and then think for yourself;

In the end, I worked hard to code a small program with more than 30 lines;

When compiling, it is still blood red;

despair

Then we started the most basic one-dimensional array problem;

tell the truth;

Xun Yu's eyes are not very good;

What I saw was "a bunch of arrays" (Mr. Yue would probably cry and faint in the dormitory);

Let's integrate the basic operations of a bunch of arrays for you;

Some basic operations in #76 on oj (50+ lines of code are really annoying);

1> Find out if a number is in the array;

int x;

cin>>x;

int a[110]; //The parentheses of the array should be opened to a larger size (important things are said three times), otherwise there may be some strange things in the final result due to overflow when running

for(int i=1;i<=n;i++) //This is the for loop that makes me vomit blood. I don't have enough brains, so I just figured out the mechanism of the for loop before I started doing array problems. As for the application, it is still poor. Some, more questions and less strokes

    if(a[i]==x) cout<<i<<endl; //yu If a[i] and x are equal, don't you find it? The output is ok; the first blog that summarizes low-level errors also said that the double equals sign cannot be forgotten, the fault is on the computer, and it hurts in my heart; there is also the problem of indentation, in order to make the code easier (good) to understand ( Look), if the code in the following line belongs to the above line (forgive me for not using terminology), then indent it by 3-4 spaces, depending on personal habits

2> Delete an element (the annotations in the next code may be a little less, because after all, there are more than one above)

for(int i=k;i<n;i++) a[i]=a[i+1] //This is the process of deletion, mentioning the latter number to the previous one

n--; //Because the last number is mentioned in the previous step, the last two digits are the same, and the array needs to be subtracted by one, so that the deletion of an element is truly completed.

3> Insert an element

for(int in;i>=k;i--) a[i+1]=a[i]; //Here, first free the kth position

a[k]=x; //Here is to insert k into the vacated position

n++; //Last total digits +1

//In fact, the principles of insertion and deletion are very similar, except that one is to vacate a seat and add it in, and the other is to subtract a position. After understanding it, it is easy to understand the n++, n-- at the end, Xinao Everything is based on understanding

//Maybe what I said is a little watery, and there are many things wrong. After all, I am a newcomer who only came into contact with C++ to make a summary for myself. I hope you can correct and guide you;

End of full text.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325954378&siteId=291194637