c++ learning--c++ basics

initial c++

input and output

1. Input and output
Example:

#include <bits/stdc++.h>//万能头文件 
 
using namespace std;//std标准
 
int main()//主函数
{
    
    
   cout << "Hello World"<<endl;//输出,输出语句是以“;”结尾的
   //cout<< "Hello";
   return 0;//程序结束 
}

1. Output statement: cout<<…;

cout<<1+1<<endl;//输出数字,1+1可进行运算,结果为2
cout<<"1+1"<<endl;//输出字符串
cout<<1+2*3-4<<endl;//cout内可进行简单的运算

2.endl can be used for line breaks

cout<<1+2;
cout<<1+2<<endl;

3.Multiple outputs

cout<<"Hello"<<"World"<<endl;//输出两个部分
cout<<"Hello"<<""<<"World"<<endl;//输出三个部分
cout<<"Hello"<<endl<<"World"<<endl;//两部分会换行
cout<<"Hello";
cout<<"World";

variable

2. Variable
1. Data type name (commonly used)
Character type: char
Boolean type: bool
Long integer type: long long
Numeric type: int
Real type (one decimal point): float
Real type (two decimal points): double
2 .Definition of variables:
data type plus name, syntax format: data type name variable name 1, variable name 2...;
for example:

int age,score;
double height,weight;

3. Variable assignment
For example

int age=20,score=100;

It is not recommended to use a variable before it is assigned a value. Pay attention to the rule of "assign a value first and then use it"
4. Naming rules for variables
Insert image description here

5. Reading in variables (reading using cin)

cin>>a;
cin>>b;
cin>>c;
cin>>a>>b>>c;

When reading different variables, separate them with spaces.
6. Four arithmetic operations of variables
(1) Addition, subtraction, and multiplication (+, -, *). The addition, subtraction,
and multiplication operations of variables are the same as the addition, subtraction, and multiplication in mathematics.
Except that the two objects of the operation are both integer types, the result is an integer type. If there is a real type variable in the operand object, the result is a real type.
(2) Division (/)
If the two objects involved in the operation are both integer types, the result is the integer part of the mathematical operation.
For example: the result of 3/2 is 1, not 1.5.
If there is a real type in the operation object, the result is Normal operation result.
For example: the result of 7/5.0 is 1.4
(3) Modulo operation (%)
The objects involved in the operation must be integers
. For example: the result of 7%5 is 2

assignment statement

1. General format: variable name = expression.
For example:
a=3;
a=b+c;
a=a+1;
a and b are exchanged
c=a;
a=b;
b=c;
2. Relational expression Formula (result is Boolean type: true/false)
Insert image description here
3. Logical operators
Insert image description here

if selection structure

1.if statement
syntax format:

if(判断条件){
    
    
    条件成立时执行的语句
}else{
    
    
    条件不成立时执行的语句
}

2. Multiple if overlap
syntax format:

if(判断语句1{
    
    
	语句1
}else{
    
    
	if(判断语句2{
    
    
		语句2
	}else(){
    
    
		if。。。
			else。。。{
    
    
				语句n
			}
	}
}

3. If contains multiple if statements.
Syntax format:

if(条件1{
    
    
	if(条件2{
    
    
		12都成立的执行语句
	}else{
    
    
		1成立,2不成立的执行语句
	}
}else{
    
    
	if(条件3{
    
    
		1不成立,3成立的执行语句
	}else{
    
    
		13都不成立的执行语句
	}
}

4. Parallel if statements

if(条件1){
    
    
   结果1
}else if(条件2){
    
    
   结果2
}else if(条件3){
    
    
   结果3
}else if(条件4){
    
    
   结果4
}
return 0;

while loop structure

1.while statement
Syntax format:
while (conditional expression) { statement } Note: The program will continue until the conditional expression is not established. 2.break syntax format:




while(){
    
    
	if(){
    
    
		...
		break;
	}
	...
}

Note: When encountering a break statement, the program will terminate the loop and execute the next statement at the end of the loop.
3.continue
syntax format:

while(){
    
    
	if(){
    
    
		...
		continue;
	}
	...
}

4. Solutions to common algorithm examples
Insert image description here

for loop structure

1.for statement

for(初始化;循环条件;变量变化){
    
    
	执行语句
}

Note: The initialization, loop conditions, and variable changes in the loop statement can be partially or completely omitted, but the two ";" in the three parts cannot be omitted.
2. Omitting initialization, loop conditions, and variable changes in the for loop statement

for(;;){
    
    
	执行语句
	...
	break;//终止循环
}

Note: If the above loop is not terminated by break, it will continue to execute and become an infinite loop.

Algorithm practice examples

1. Question: Input 3 integers and output these three numbers from large to small.

Input format:
three integers (separate each integer with a space)

Output format:
Output the three input integers from large to small (separate each integer with a space)

Limitation:
All three numbers range from 0 to 1000

Example 1:
Input:
1 2 3
Output:
3 2 1

Example 2:
Input:
0 1 1
Output:
1 1 0

#include <bits/stdc++.h>
using namespace std;
int main(){
    
    
	int a,b,c;
	cin>>a>>b>>c;
	if(a>=b&&b>=c){
    
    
		cout<<a<<" "<<b<<" "<<c;
	}else if(a>=c&&c>=b){
    
    
		cout<<a<<" "<<c<<" "<<b;
	}else if(c>=b&&b>=a){
    
    
		cout<<c<<" "<<b<<" "<<a;
	}else if(c>=a&&a>=b){
    
    
		cout<<c<<" "<<a<<" "<<b;
	}else if(b>=a&&a>=c){
    
    
		cout<<b<<" "<<a<<" "<<c;
	}else if(b>=c&&c>=a){
    
    
		cout<<b<<" "<<c<<" "<<a;
	}
}

2. Question: The size of the rectangle is known to be n×m. Now fill the rectangle with a square of a×a. Input three positive integers n, m, a (n, m, a ≤ 10^9), calculate how many squares can be filled at most? (The square can just touch the boundary of the rectangle, but cannot extend outside the rectangle)
Input format:

A line of three positive integers n, m, and a separated by a space.
Output format:

Output the number of squares that can be filled.
Example: 1

Input:
3 4 1
Output:
12

#include <bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n,m,a;
	cin>>n>>m>>a;
	cout<<(n/a)*(m/a);
}

Note: The trap of this question is that you cannot divide the area of ​​the entire large rectangle by the area of ​​the small rectangle. Instead, you should divide the length of the large rectangle by the side length of the small rectangle, and discard the decimal part (the decimal part indicates that it exceeds the boundary). Divide the width of the large rectangle by the side length of the small rectangle, and round off the decimal part.
3. Question: Enter a number n, find the sum of all multiples of 7 between 1 and n and the numbers with 7 at the end.
Input format:

An integer n (0<=n<=123456)
output format:

Find the sum of all multiples of 7 between 1 and n and numbers with 7 at the end.
Example 1:

Input:
21
Output:
59
Description:
7+14+17+21=59

#include <bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n,i,m=0;
	cin>>n;
	for(i=1;i<=n;i++){
    
    
	   if(i%7==0||(i-7)%10==0){
    
    
		   m=m+i;
	   }
	}
	cout<<m;//在for循环语句和if语句之外
}

4. Question: A ball falls from the air, and the height it rebounds each time it hits the ground is half of its original height (rounded down).
When the initial height of the ball is n meters (n is an integer and <10000), how many times can the ball bounce? How many times does it bounce, and what is the total distance the ball travels in the air?

Input format:

A positive integer n, n<10000
output format:

Two numbers, separated by spaces. The first number is the number of bounces of the ball, and the second number is the total distance the ball flew in the air.
Example 1:

Input:
8
Output:
4 22
Example 2:

Input:
80
Output:
7 236

#include <bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n,i,m=0,s=0;//m为次数,s为距离
	cin>>n;
	i=n;//将初始高度n赋值给i(后续将会使用i)
	while(n>=1){
    
    
		if(n>=1){
    
    
			s=s+n*2;//距离应为初始高度+每次高度一半的二倍
			m++;//每循环一次,m的值加一
		}
		n=n/2;//高度减半
	} 
	cout<<m<<" "<<s-i;//初始高度加了两次,应再减去一次初始高度
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_63599362/article/details/126842382