Contents and Applications of One-Dimensional Arrays in C++ Series

One-dimensional array part

Class time and arrangement

Time 20231808 18:10-20:10 Application of one-dimensional arrays Simulation method and switching doors

Class content

Course link:

C++ Level Test Easy Access/C++ Intermediate (20) Application of One-Dimensional Boolean Arrays + Review (11) Decomposition of Prime Factors + Review (8) Break Statement in Loops

A prime number is a natural number greater than 1 that has no other factors except 1 and itself. How to factor prime factors

360=2*2*2*3*3*5

Insert image description here

one-dimensional array

数据类型 数组名 [数组长度];
数据类型 数组名[数组长度]={
    
    1,2...};
数据类型 数组名[ ]={
    
    1,值2...};
	//数据类型 数组名 数组长度
	int arr[5];
	//给数组中的元素进行赋值
	arr[0] = 10;
	arr[1] = 20;
	arr[2] = 30;
	arr[3] = 40;
	arr[4] = 50;
	//访问数据元素
	cout << arr[0] << endl;
	cout << arr[1] << endl;
	cout << arr[2] << endl;
	cout << arr[3] << endl;
	cout << arr[4] << endl;

  int arr[5] = {
    
     10,20,30,40,50 };
	//print all the elements in the array in a loop
	for (int i = 0; i < 5; i++)
	{
    
    
		cout << arr[i] << endl;
	}

code part

========================================< a i=1> Time: 20231208 Code name: Breaking out of infinite loop: Knowledge points: The use of loops, the use of if judgments, and the use of break.


Programming:
A program that can accept your input and output in an infinite loop.
When you enter 9999, the program ends.

#include <iostream>

using namespace std;

int main() {
    
    
	for (int i = 0; i < 2;) {
    
    
		int num;
		cin >> num;
		if (num != 9999) {
    
    
			cout << num << endl;
			//TODO
		} else {
    
    
			break;
		}
	}
}

========================================< a i=1> Time: 20231208 Code name: Determine whether it is a prime number: Knowledge points: Use of loops, use of if judgment, use of break, prime numbers understanding.


Programming:
A program that accepts your input. If the input is a prime number, the output is yes. If the input is not a prime number, the output is no.

#include <iostream>
using namespace std;
int main() {
    
    
	
	int num;
	cin>>num;
	if(num==1){
    
    
		cout<<"否";
		return 0;
	}
	if(num==2){
    
    
		cout<<"是";
		return 0;
	}
	for(int i=3;i<=num;i++){
    
    
		if(num % i==0 && i<num){
    
    
			cout<<"否";
			return 0;
		}
		if(i==num){
    
    
			cout<<"是";
			return 0;
		}
	}
	return 0;
}

========================================< a i=1> Time: 20231208 Code name: Opposite action of the door: Knowledge point:


Programming:
A program that accepts your input n, where n represents n people and n rooms. If the waiter No. 1 opens all the rooms, the waiter No. 2 opens all the rooms. Perform the opposite operation for rooms that are multiples of 3
Waiter No. 3 performs the opposite operation for rooms that are multiples of 3, and so on.

Idea:
Simulation method, write the simulation process on the blackboard, and then implement it with algorithms.

Write:

#include <iostream>
using namespace std;
// 定义一个数组 存储房间的状态 初始值都是0
const int N=1000;
int arr[N+1];
int main() {
    
    
	// 定义变量num 接受人和房间数量
	int num;
	cin>>num;
	// 循环num个人 i为服务员号
	for(int i=1;i<=num;i++){
    
    
		
		// 循环num个房间 j为房间号
		for(int j=1;j<=num;j++){
    
    
			// j房间号是i服务员的倍数,服务员就把该房间执行相反操作
			if(j%i==0){
    
    
				if(arr[j]==0){
    
    
					arr[j]=1;
				}else{
    
    
					arr[j]=0;
				}
			}
		}
	}
	//输出
	for(int i=1;i<=num;i++){
    
    
		if(arr[i]==1){
    
    
			cout<<i<<" ";
		}
	}	
	return 0;
}

Prime number problem

The definition of prime number: If a positive integer cannot be divided by any natural number except 1 and itself, then the number is called a prime number, also called a prime number. Otherwise it is a composite number.
It can be seen from the definition that all numbers less than or equal to 1 are neither prime numbers nor composite numbers.
The distribution of prime numbers is relatively sparse. For a sufficiently large number S, the number of prime numbers not exceeding S is approximately N I n N \frac{N}{InN} InNN个、也将说每 I n N InN There is approximately one prime number among the InN numbers. Readers only need to understand this.

Determining prime numbers (trial division)

For judging prime numbers, the simplest and easiest way to think of is to screen one by one, also called trial division.

If we want to judge a number N, do we need to screen all the numbers from 2 to N-1? Obviously not. First of all, it is certain that N-1 cannot divide N, so can the range be further reduced? Let me give the answer first: 2~sqrt(N)

code show as below:

#include<cstdio>
bool isprime(int num){
    
    
	if(num==2)
	    return true;
	if(num%2==0 || num<2)
	    return false;
	else{
    
    
		for(int i=3;i*i<=num;i+=2){
    
    
			if(num%i==0){
    
    
				return false;
			}
		}
		return true;
	}
}
int main(){
    
    
	int x;
	scanf("%d",&x);
	if(isprime(x)){
    
    
		printf("Yes");
	}
	else{
    
    
	    printf("No");
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/m0_38139250/article/details/134976990