"Luogu Deep and Shallow" Chapter 5---Array and Data Batch Storage

insert image description here


foreword

This section mainly studies the knowledge behind one-dimensional arrays and multi-dimensional arrays. I think you can try to understand such difficult topics and do not require you to master them. Because ACM is more about thinking questions, it is enough to master the important topics here.
The remaining topics are self-study with the explanation in the book.
insert image description here


1. Small fish are cuter

Luogu p1428—little fish is cute :
insert image description here

Problem-solving idea : First of all, it must use the for loop to traverse each fish first, and then start traversing from the front of the traversed fish, and then compare and find that it is satisfied. When I am not cute, count the ++ and then complete the traversal from the beginning to me Then output the counted value.

code show as below:

#include<iostream>
using namespace std;
int n,a[101];//全局变量自动初始化为0
//这里多开1个数组是为了防止数组越界
int main()
{
    
    
	cin>>n;
	
	for(int i=0;i<n;++i)//输入小鱼的可爱值
		cin>>a[i];
	
	for(int i=0;i<n;i++)//遍历小鱼 
	{
    
    
		int cnt=0;//计数
		for(int j=0;j<i;j++)//从头到我这条鱼遍历
		{
    
    
			if(a[j]<a[i]) cnt++;//满足条件 就cnt++
		}
		cout<<cnt<<" ";//输出没我可爱的鱼的个数
	}
	return 0;
}

2. Xiaoyu's number game

P1427 Little Fish’s Number Game The topics are as follows:

insert image description here
Problem-solving idea : open an array, and then output it backwards

code show as below:

#include<iostream>
using namespace std;
int a[110],n,i;
//多开10个数组和上一题目一样 防止数组越界
int main()
{
    
    
	cin>>n;
	while(n)
	{
    
    
		a[i++]=n;//存放到数组里面
		cin>>n;
	}
	//倒着输出
	for(int j=i-1;j>=0;--j) cout<<a[j]<<" ";
	//cout<<endl;
	return 0;
}

3. Hail conjecture

P5727 [Deep Foundation 5. Example 3] The title of the hail conjecture is as follows:

insert image description here
Problem-solving idea : Open an array to store the process of each digital change in the calculation, and then output it conveniently. The next step is to process the digital change. In fact, it is enough to change the even number or the odd number according to the topic requirements.

The code is as follows :

#include<iostream>
using namespace std;
int n,book[201],i;
int main()
{
    
    
	cin>>n;
	while(n!=1)
	{
    
    
		book[i++]=n;
		if(n%2==0) n/=2;//偶数操作
		else n=3*n+1;//奇数操作
	}
	book[i]=1;//把最开始的1存储进去
	for(int j=i;j>=0;j--) cout<<book[j]<<" ";//输出
	return 0;
}

4. The tree outside the school gate

P1047 [NOIP2005 Popularization Group] The tree outside the school gate

insert image description here
Problem-solving idea : Initialize the tree using an array to 0, then set the tree to be cut down to the number 1, and then traverse the array to see who is 0 and who is 1

The code is as follows :

#include<iostream>
using namespace std;
const int N=10010;
int tree[N]={
    
    0};
int l,m;//左边右边
int main()
{
    
    
    cin>>l>>m;
    while(m--)
    {
    
    
        int u,v;
        cin>>u>>v;
        for(int i=u;i<=v;++i)
        {
    
    
            tree[i]=1;
        }
    }
    int sum=0;
    for(int i=0;i<=l;i++)
    {
    
    
        if(tree[i]==0) sum++;
    }
    cout<<sum<<endl;//输出结果
    return 0;
}

Five, evenly matched opponents

P5728 [Deep Foundation 5. Example 5] A well-matched opponent’s
insert image description here
problem-solving ideas : It is relatively simple to input according to the topic and then use the given judgment conditions to judge and output

code show as below:

#include<iostream>
#include<cmath>
using namespace std;
int n,a[3][1001],ans;
int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;++i)
		cin>>a[0][i]>>a[1][i]>>a[2][i];
	for(int i=1;i<=n-1;++i)
		for(int j=i+1;j<=n;++j)
		if(abs(a[0][i]-a[0][j])<=5 
		 &&abs(a[1][i]-a[1][j])<=5
		 &&abs(a[2][i]-a[2][j])<=5
		 &&abs(a[0][i]+a[1][i]+a[2][i]-a[0][j]-a[1][j]-a[2][j])<=10)
		ans++;
	cout<<ans<<endl;
	return 0;
}

Six, evenly matched opponents

P3397 Carpet
insert image description here
problem-solving ideas : The algorithm can use the difference algorithm, but the basic algorithm just needs to be added sequentially. It will be relatively basic and easy to think of.

code show as below:

#include<iostream>
using namespace std;
int a[1001][1001]={
    
    0},m,n,i,j,k,x1,x2,y1,y2;
int main()
{
    
    
    cin>>n>>m;
    for(k=1;k<=m;++k)
    {
    
    
        cin>>x1>>y1>>x2>>y2;
        for(i=x1;i<=x2;++i)
        	for(j=y1;j<=y2;++j)
        	++a[i][j];
    }
    for(i=1;i<=n;++i)
    {
    
    
        for(j=1;j<n;++j)
       	 cout<<a[i][j]<<' ';
        cout<<a[i][n]<<endl;
    }
    return 0;
}

Summarize

In this section, you have studied the practice of array-related knowledge topics. I believe that your understanding of arrays will be more in-depth!
In the next section, I will explain the use of string and string, wait and see!

Guess you like

Origin blog.csdn.net/congfen214/article/details/131750649