Preparing for CAIP - PTA Level A Examination

introduce

2023 RoboCom Robot Developer Competition CAIP Programming Design Track Programming Skills Competition
Examination knowledge points
https://mp.weixin.qq.com/s/lXp5axGqtSeA4eXrcU7vEA
previous years’ real questions: https://pintia.cn/market/tag/1447465711671738368
PAT Level A question solution: https://blog.csdn.net/a617976080/article/details/89676670

2023 real questions

The first question is AC

#include<bits/stdc++.h>
using namespace std;
int g[5];
int gg[5];
int n;

int main(){
    
    
	cin>>n;
	int a,b;
	while(n--){
    
    
		cin>>a>>b;
		if(a==0){
    
    
			g[b]++;
		}else gg[b]++;		
	}
	int f = 0;
	if((g[1]>gg[1])||(g[1]==gg[1]&&g[2]>gg[2])||(g[1]==gg[1]&&g[2]==gg[2]&&g[3]>gg[3])) f=1;
		cout<<g[1]<<" "<<g[2]<<" "<<g[3]<<'\n';
		cout<<gg[1]<<" "<<gg[2]<<" "<<gg[3]<<'\n';
	if(f){
    
    
		cout<<"The first win!";
	}else{
    
    
		cout<<"The second win!";
	}
	return 0;
} 

The second question got 8/15 points

#include<bits/stdc++.h>
using namespace std;
int n,m;
struct cu{
    
    
	string s;
	string den;
}cu[1010],en[1010];
int main(){
    
    
//	string s1 = "Milk";
//	string s2 = "Milk";
	cin>>n>>m;
	for(int i = 0; i <n; i++){
    
    
		cin>>cu[i].s>>cu[i].den ;
	}
	int cnt = n;
	for(int i = 0 ; i < n;i++){
    
    
		for(int j = 0 ; j < n;j++){
    
    
			cu[cnt].s  = cu[i].s + cu[j].s;
			cu[cnt].den   = cu[i].den  + cu[j].den;
//			cout<<i<<" "<<j<<" "<<cu[i].s<<" "<<cu[i].den <<" "<<cu[j].s<<" "<<cu[j].den <<endl;
//			cout<<cu[cnt].s <<" "<<cu[cnt].den <<endl;
			cnt++;
		}
	}
	for(int i = 0; i < m; i++){
    
    
		int f = 0;
		cin>>en[i].s;
		
		for(int j = 0; j < cnt; j++){
    
    
			if(en[i].s==cu[j].s){
    
    
				en[i].den = cu[j].den ; f=1;
			} 
		}
		
		if(f) f = 0;
		else en[i].den = "D";
	}
	for(int i =0 ; i < m; i++){
    
    
		cout<<en[i].den<<'\n';
	}
	return 0;
}

The three questions are slightly perverted
4. I couldn’t find anything through a deep search and didn’t want to read it. 0 points.

#include<bits/stdc++.h>
using namespace std;
struct node{
    
    
	string a;
	int sua;
	string b;
	int sub;
	bool stu;	
}tu[1010];
int n,cnt=0;
int ans[1010];
void dfs(int x){
    
    
	//这一层的推论能前一层连上,但不和第一个互异 
		cnt++;
		ans[cnt] = x;			
		if(tu[1].a == tu[x].b &&tu[1].sua != tu[x].sub){
    
    
			for(int i = 1; i < cnt+1; i++){
    
    
				cout<<tu[ans[i]].a<<" "<<tu[ans[i]].sua<<" "<<tu[ans[i]].b<<" "<<tu[ans[i]].sub<<" "<<"="; 
			}
			cout<<tu[1].a<<" "<<tu[1].sua<<" "<<tu[cnt].b<<" "<<tu[cnt].sub<<endl;
			return;
		}
	for(int i = 1; i < n+1; i++){
    
    
		if(tu[x+1].a == tu[x].b&&tu[x+1].sua==tu[x].sub&&tu[i].stu==0){
    
    
			cnt++;
			ans[cnt] = x;  tu[i].stu=1;
			dfs(x+1);
			cnt--;
			ans[cnt] = 0;  tu[i].stu=0;
		}
	}
	return;
}
int main(){
    
    
	cin>>n;
	for(int i = 1; i < n+1; i++)		cin>>tu[i].a >>tu[i].sua >>tu[i].b >>tu[i].sub;
	tu[1].stu  =1;
	dfs(1);
	
	return 0;
}

Undergraduate group

  • Linear list: static linked list, stack, queue
  • Tree: traversal, search tree, complete binary tree, etc.
  • Figure: basic properties, shortest distance, maximum flow/minimum cut, strongly connected branches, nearest common ancestor, minimum spanning tree, Euler sequence, etc.
  • Sorting (greedy) + search (binary, hashing)
  • Classic algorithms (search pruning, dynamic programming, etc.)
  • Have strong problem abstraction and modeling capabilities and be able to simulate and solve complex practical problems

1001 A+B Format (20 points) Add small numbers and format the output

Sample Input:
-1000000 9
Sample Output:
-999,991
The main idea of ​​the question:
Calculate a+b, −10 6 ≤ a, b ≤ 10 6 , and the result is output according to the Western way of writing numbers, starting from three numbers and one comma. kind.
Test point:
Format control output
idea 1:
The first thing to consider is to store the result of a+b into the result array ans through modulo and remainder operations, and then format the output. This involves the discussion of special values, namely the placement of "," and "-". Fortunately, OI can continuously improve the logic and range accuracy of classification judgments through testing multiple sets of data, but it is actually a waste of time.
First, the result is greater than zero, equal to zero and less than zero. Secondly, the length of the array must be judged. If n is 1, there will be no "," in the output, and there is no need to fill the 0 booth with 3. The last digit of the result array also has no comma.
Optimization: In fact, greater than zero, less than zero, and equal to zero can be judged together. As long as it is judged whether it is less than zero, and if it is less than zero, one more "-" will be output.

#include<bits/stdc++.h>
using namespace std;
long long a,b,c,res;
long long ans[10]={
    
    0};
int main(){
    
    
    cin>>a>>b;
    c = a + b;
    res = c;
    int n;
//    cout<<"c: "<<c<<'\n';
    if(c>0){
    
    
        //存结果
        for(n = 0; res != 0; n++){
    
    
        	ans[n]= res%1000;
        	res = res/1000;
		} 
		//输出
		for(int i = n-1;i >= 0;i--){
    
    
			if(i==n-1&&n==1) printf("%d",ans[n-1]);
			else if(i==n-1&&n!=1) printf("%d,",ans[n-1]);
			else if(i==0&&n!=1)printf("%03d",ans[i]);
			else printf("%03d,",ans[i]);
		}
    }else if (c<0){
    
    
        res = -c;
        for( n = 0; res != 0; n++){
    
    
        	ans[n]= res%1000;
        	res = res/1000;
		} 
        cout<<"-";
		for(int i = n-1;i >= 0;i--){
    
    
			if(i==n-1&&n==1) printf("%d",ans[n-1]);
			else if(i==n-1&&n!=1) printf("%d,",ans[n-1]);
			else if(i==0&&n!=1)printf("%03d",ans[i]);
			else printf("%03d,",ans[i]);
		}
    }else cout<<0; 
    return 0;
}

Idea 2:
The data range is not large. You can first calculate the boundary value [-2000000,2000000], and then get a very general output method:

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	long long n,m;
	cin>>n>>m;
	long long sum = n+m;
	if(sum<0){
    
    
		cout<<"-"; sum *= -1;
	} 
	if(sum<1000){
    
    
		cout<<sum;
	}else if(sum>=1000&&sum<1000000) {
    
    
		printf("%d,%03d",sum/1000,sum%1000);	 
	}else if(sum>=1000000){
    
    
		printf("%d,%03d,%03d",sum/1000000,(sum%1000000)/1000,sum%1000);
	}
	return 0;
} 

Idea 3:
Use the length of the string to determine when "," needs to be output. Treat the value as a positive number (or not, add one digit in front, it does not matter), output from front to back, when the number counted is equal to the value of the overall length modulo 3, it is time to output ",", and at the same time Be careful not to output "," at the end of the exclusion.
Advantages: The amount of code is small, and the coding speed will be accelerated after becoming proficient.

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

	string s = to_string(c);
	int len = s.size();
	//cout<<"s: "<<len<<endl;
	int t = len %3;
	for(int i = 0; i < len; i++){
    
    
		cout<<s[i];
		if((i+1)%3==t&&i!=len-1) cout<<",";
	}
	return 0;
}

Note: One question has multiple solutions and the ideas are clear.
Use three storage methods (int, array, string)
%md: m is the width of the specified output field. If the number of digits in the data is less than m, spaces will be added to the left end. If it is greater than m, the actual number of digits will be output.
%0md uses 0 to fill spaces

1002 A+B for Polynomials (25 points) Addition of polynomials

Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 2 1.5 1 2.9 0 3.2
Question:
Add two polynomials. The first number in each row represents the number of non-zero terms of the polynomial, followed by non-zero terms. The zero terms, the first one is the order and the second one is the coefficient. Finally, the added polynomial is output in the same format, retaining 1 decimal place.
Idea:
First complain! The data range of this question was messed up, which caused me to open the array too small and get a segfault.
Small details are also quite time-consuming:
1. It may be swallowed when typing.
2. Data type issues will lead to incorrect answers.
3. Array range
. 4. The result should be kept to one decimal place.Floating point numbers retain one decimal place! ! !
The idea is to simulate the addition of polynomials with an array, and just record the number of terms and the highest power of the obtained polynomial. When reading in, the coefficients are stored in the corresponding powers to facilitate the addition of two polynomials.

#include<bits/stdc++.h>
using namespace std;
double a[1005],b[1005]; //系数 
int ea[1005],eb[1005];//幂 
double ans[1005];
int main(){
    
    
    int na,nb,k;
    cin>>na;
	for(int i = 0; i <na ;i++){
    
    
		cin>>ea[i];
		int t = ea[i]; 
		cin>>a[t];  //按照对应幂去存系数 
	}
	scanf("%d",&nb); 
	for(int i = 0; i < nb; i++){
    
    
		cin>>eb[i];
		int t = eb[i]; 
		cin>>b[t];  //按照对应幂去存系数 
	}
	k = ea[0] > eb[0] ? ea[0]:eb[0];//最高次幂 
	int  n = 0;
	for(int i = 0; i <= k; i++){
    
    
		ans[i] = a[i] + b[i];
		//cout<<"i: "<<i<<" "<<a[i]<<" "<<b[i]<<" "<<ans[i]<<endl;
		if(ans[i]!=0) n++;
	}
	cout<<n;
	for(int i = k; i >= 0; i--){
    
    
		if(ans[i]) printf(" %d %.1f",i,ans[i]);
	}
	return 0;
}

1003 Emergency rescue shortest path and maximum rescue force

First, let’s look at the shortest path algorithm and knowledge structure graph.
Insert image description here
Dense graphs use adjacency matrices to store graphs, while sparse graphs generally use adjacency tables to store graphs to save space. Use naive Dijkstra for dense graphs, and use heap-optimized Dijkstra for sparse graphs. Multi-source refers to the shortest path that requires multiple starting points and ending points.

Our question is obviously a single source. To find the shortest path from one point to another point, you can find the shortest path from s to t by finding the shortest path from one point to all other points.

Guess you like

Origin blog.csdn.net/qq_52626583/article/details/131350869