C + + language learning summary and experience

Keywords: C++ Algorithm greedy algorithm

        About the language part of C + + (Information Science Orsay one pass), I myself summarized some knowledge.

        Because it is too basic, I won't talk about the introduction and use of the first part of the source code.

        I'll start with the second chapter, sequential structure programming. First of all, I think most of this chapter can be used and understood as long as a causal relationship is understood in this chapter. A source code, a header file at the beginning, which I prefer to use

#include<bits/stdc++.h>
using namespace std;
int main()

. The second is the definition. The definition includes constants and variables of the whole source code. I prefer to understand it as unknowns in mathematics, but it is not all interlinked.

        Let me give an example:

​
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

​

The above code: a+b problem; The meaning of the code: define a and b, enter a and b, output a+b, and then conclude. (the scope of the header file will not be explained below); However, it should be noted that to define characters, char is required, such as:

​
​
#include<bits/stdc++.h>
using namespace std;
int main(){
    char a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

​

​

In this way, numbers and symbols can be defined. To define single precision floating-point numbers, there should be float. Double is used for double progress floating-point numbers. When float is needed, it can be replaced by double. However, when double is used, float can not be used. Double has a wider range of functions. However, if the topic requires single precision floating-point numbers, float is used finally. To be on the safe side.

        In the third chapter, the control structure of the program mainly focuses on if and switch. In fact, "if" can be understood as "if", and switch is more like a column option. For example:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int weekday;
	cin>>weekday;
	switch(weekday)
	{
		case 1:cout<<"Monday"<<endl;break;
		case 2:cout<<"Tuesday"<<endl;break;
		case 3:cout<<"Wednesday"<<endl;break;
		case 4:cout<<"Thursday"<<endl;break;
		case 5:cout<<"Friday"<<endl;break;
		case 6:cout<<"Saturday"<<endl;break;
		case 7:cout<<"Sunday"<<endl;break;
		default:cout<<"input error!"<<endl;break;
		return 0;
	}
	return 0;
}

In this program, if you enter 1, it will display "Monday", because the program lists 7 options with switch. If you enter the number 1-7, the English of the corresponding week will be output.

        if is used to set conditions. For example, to judge the range of input numbers:

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  if(1<n&&n<100)
  cout<<"yes";
  
}

The meaning of the above code is very simple: define n, enter n, and output "yes" if n > 1 and N < 100. This is the use of if.

        In the two chapters of loop and array, the knowledge changes greatly, and most of them are used for regular repeated operations, such as even numbers contained in:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	if(i%2==0) cout<<i<<" ";
	return 0;
}

The above program means: define n, enter n, define i=1 in the for loop, and loop to N, i + +, that is, increase 1 automatically; If you enter 100, it will output: 0,2,4,6,8,10,12... Until 100.

        The advanced is to complicate it. For example, in the 1060-1090 questions of the all-in-one website, I give an example of 1070 population growth:

1070: population growth

time limit: 1000 ms         Memory limit: 65536 KB
 Number of submissions: 43668     Number of passes: 29947
[Title Description]
China's existing x Billion people, according to 0.1%The growth rate of, n How many people will there be in years? Keep four decimal places.

[[input]
A line containing two integers x and n,Population base and years, separated by a single space.

[[output]
Output the final population, in billions, to four decimal places. one<=x<=100,1<=n<=100. 

[[input sample]
13 10
[Output example]
13.1306

My approach is:

#include<bits/stdc++.h>
using namespace std;
int main(){
	double x,n;
	int s;
	cin>>x>>n;
	for(s=1;s<=n;s++)
	x=x*1.001;
	printf("%.4lf",x);
	return 0;

}

It is more complicated than the problem at the beginning, and the more difficult it becomes.

        Array is used to represent multiple number outputs, such as A1, A2, A3, A4... A100; Isn't it troublesome? Yes, at this time, you can use an array to solve the problem. Just output a [100] directly. Let me give an example:

2035: [Example 5.2]Translation data

time limit: 1000 ms         Memory limit: 65536 KB
 Number of submissions: 6315     Number of passes: 4209
[Title Description]
take a Move the first element in the array to the end of the array,The rest of the data is shifted forward one position in turn.

[[input]
First row array a Number of elements;

Second behavior n A positive integer less than 1000.

[[output]
Array elements after translation, each number is separated by a space.

[[input sample]
10
1 2 3 4 5 6 7 8 9 10
[Output example]
2 3 4 5 6 7 8 9 10 1

My code is:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,a[1001];
	cin>>n;
	for(int i=1;i<=n;i++)    cin>>a[i];
	for(int i=2;i<=n;i++)    cout<<a[i]<<" ";
	cout<<a[1];
	return 0;

}

It can be seen that the array can greatly help us solve this kind of problem. Let's talk about it: Although the topic is 1000 at most, avoid some "kind-hearted" testers. Give 1001 to directly explode the space or time. For the sake of safety, it's best to set a number larger than the topic. (if you don't understand the array, you can search the tutorial and use it)

        As for pointers, to be honest, I don't think they are very useful. Chicken ribs, in short, refer to one of a column of arrays. Find its address or insert it into an array. If you are interested, you can go and have a look.

        I won't talk about the function, because my function doesn't sound good. It's too weak. If you need to read it on the Internet.

(that's it. I've been playing for two days. These are my understanding when I study. It's best to help. If not, I can help. I can see what can be improved and have a private chat.)

Posted by Onloac on Tue, 21 Sep 2021 14:18:01 -0700