【PTA】浙软2019年上机题目自测

个人学习记录,代码难免不尽人意。

今天做了做PTA官网的浙软2019年保研上机真题,用时90分钟,四道全对,买了时光机,做完的时候大约有16人在当年已经满分。感觉题目比PAT甲级简单很多(也从侧面证实了越来越卷了,这四道题放在今天估计所有人90起步)。下面是四道题和我的解法:
7-1 Happy Numbers
A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits in base-ten, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers and the number of iterations is called the degree of happiness, while those that do not end in 1 are unhappy numbers (or sad numbers). (Quoted from Wikipedia)

For example, 19 is happy since we obtain 82 after the first iteration, 68 after the second iteration, 100 after the third iteration, and finally 1. Hence the degree of happiness of 19 is 4.

On the other hand, 29 is sad since we obtain 85, 89, 145, 42, 20, 4, 16, 37, 58, and back to 89, then fall into an endless loop. In this case, 89 is the first loop number for 29.

Now your job is to tell if any given number is happy or not.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N lines follow, each contains a positive integer (no more than 10
4
) to be tested.

Output Specification:
For each given number, output in a line its degree of happiness if it is happy, or the first loop number if it is sad.

Sample Input:
3
19
29
1
Sample Output:
4
89
0

#include
#include 
#include
#include
using namespace std;
int cal(int n){
	int sum=0;
	while(n!=0){
		int remain=n%10;
		sum+=pow(remain,2);
		n=n/10;
	}
	return sum;
}
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		set<int> s;
		int num;
		scanf("%d",&num);
		int cnt=0;
		s.insert(num);
		bool flag=true;
		while(num!=1){
			num=cal(num);
			if(s.find(num)==s.end()){
				cnt++;
				s.insert(num);
			}
			else{
				flag=false;
				break;
			}
		}
		if(flag){
			printf("%d\n",cnt);
		}
		else{
			printf("%d\n",num);
		}
	}
}

happy number的一个题,非常简单的题意,直接模拟即可。

7-2 Zigzag Sequence
This time your job is to output a sequence of N positive integers in a zigzag format with width M in non-decreasing order. A zigzag format is to fill in the first row with M numbers from left to right, then the second row from right to left, and so on and so forth.
For example, a zigzag format with width 5 for numbers 1 to 13 is the following:

1 2 3 4 5
10 9 8 7 6
11 12 13
Input Specification:
Each input file contains one test case. For each case, the first line gives 2 positive integers N and M. Then the next line contains N positive integers as the original sequence. All the numbers are no more than 10
4
. The numbers in a line are separated by spaces.

Output Specification:
For each test case, output the sequence in the zigzag format with width M in non-decreasing order. There must be exactly 1 space between two adjacent numbers, and no extra space at the beginning or the end of each line.

Sample Input 1:
14 5
37 76 98 20 98 76 42 53 99 95 60 81 58 93
Sample Output 1:
20 37 42 53 58
93 81 76 76 60
95 98 98 99
Sample Input 2:
15 4
96 37 76 98 20 98 76 42 53 99 95 60 81 58 93
Sample Output 2:
20 37 42 53
76 76 60 58
81 93 95 96
99 98 98

#include
#include 
#include
#include
#include
using namespace std;
const int maxn=10010;
int arr[maxn];
bool cmp(int a,int b){
	return a>b;
}
int main(){
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%d",&arr[i]);
	}
	sort(arr,arr+n);
	int i=0;
	bool flag=true;
	for(i;i<n;i+=m){
		if(i+m>=n-1){
			break;
		}
		if(flag){
			sort(arr+i,arr+i+m);
			flag=false;
		}
		else{
			sort(arr+i,arr+i+m,cmp);
			flag=true;
		}
		
	}
	if(flag){
			sort(arr+i,arr+n);
			flag=false;
		}
		else{
			sort(arr+i,arr+n,cmp);
			flag=true;
		}
	for(int j=0;j<=n-1;j+=m){
		if(j+m>n-1){
			for(int k=j;k<n;k++){
			printf("%d",arr[k]);
			if(k!=n-1) printf(" ");
			else printf("\n");
		}
		}
		else{
			for(int k=j;k<j+m;k++){
			printf("%d",arr[k]);
			if(k!=j+m-1) printf(" ");
			else printf("\n");
		}
		}
	}
}

z形输出的一个题,也很简单,就是z形分块排序,用一个bool型变量来控制当前块是从小到大还是从大到小排序。

7-3 Is It An AVL Tree
In computer science, an AVL tree (Georgy Adelson-Velsky and Evgenii Landis’ tree, named after the inventors) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one. (Quoted from wikipedia)

For each given binary search tree, you are supposed to tell if it is an AVL tree.

Input Specification:
Each input file contains several test cases. The first line gives a positive integer K (≤10) which is the total number of cases. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary search tree. The second line gives the preorder traversal sequence of the tree with all the keys being distinct. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in a line “Yes” if the given tree is an AVL tree, or “No” if not.

Sample Input:
3
7
50 40 36 48 46 62 77
8
50 40 36 48 46 62 77 88
6
50 40 36 48 46 62
Sample Output:
Yes
No
No

#include
#include 
#include
#include
#include
using namespace std;
const int maxn=35;
int pre[maxn];
int in[maxn];
struct node{
	int data;
	node* lchild;
	node* rchild;
	int height;
};
node* newnode(int data){
	node* root=new node;
	root->data=data;
	root->lchild=NULL;
	root->rchild=NULL;
	root->height=1;
	return root;
}
node* create(int prel,int prer,int inl,int inr){
	if(prel>prer) return NULL;
	int mid=pre[prel];
	int index;
	for(int i=inl;i<=inr;i++){
		if(in[i]==mid){
			index=i;
			break;
		}
	}
	int leftnum=index-inl;
	node* root=newnode(mid);
	root->lchild=create(prel+1,prel+leftnum,inl,index-1);
	root->rchild=create(prel+leftnum+1,prer,index+1,inr);
	return root;
}
int getheight(node* root){
	if(root==NULL) return 0;
	else return root->height;
}
void update(node* &root){
	if(root==NULL) return;
	root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
}
int getfactor(node* root){
	return getheight(root->lchild)-getheight(root->rchild);
}
bool flag=true;
void dfs(node* root){
	if(root==NULL) return;
	dfs(root->lchild);
	dfs(root->rchild);
	update(root);
	int factor=getfactor(root);
	if(abs(factor)>=2){
//		cout << getheight(root->lchild) << " "<rchild) << endl;
//		cout << root->data <<"fk" <
		flag=false;
	} 
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
    	int num;
    	scanf("%d",&num);
    	for(int j=0;j<num;j++){
    		scanf("%d",&pre[j]);
    		in[j]=pre[j];
		}
		sort(in,in+num);
		node* root=create(0,num-1,0,num-1);
		flag=true;
		dfs(root);
		if(flag) printf("Yes\n");
		else printf("No\n");
	}
}

AVL树的一个题,如果考试前没有做过AVL树的题可能会抓瞎,但是这道题是给出二叉搜索树让你判断是不是AVL树,因此即使没有背过模板也是可以做的。我是用了dfs来从下往上更新节点的高度并且判断节点的平衡因子绝对值是否大于2,。

7-4 Index of Popularity
The index of popularity (IP) of someone in his/her circle of friends is defined to be the number of friends he/she has in that circle. Now you are supposed to list the members in any given friend circle with top 3 IP’s.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 positive integers N and M (both no more than 10 5), which are the total number of people and the number of friend relations, respectively. Hence the people here are numbered from 1 to N.

Then M lines follow, each contains the indices of a pair of friends, separated by a space. It is assumed that if A is a friend of B, then B is a friend of A.

Then several queries follow, each occupies a line. For each line of query, K (3≤K≤N), the total number of members in this friend circle is given first, with K indices of members follow. It is guaranteed that all the indices in a circle are distinct.

The input ends when K is zero, and this case must NOT be processed.

Output Specification:
For each query, print in a line the members with top 3 indices of popularity in descending order of their IP’s. If there is a tie, output the one with the smaller number. The numbers must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input:
8 10
2 1
1 3
1 4
1 5
5 8
3 5
2 3
6 3
4 6
3 4
7 8 1 2 3 4 6 5
4 1 3 5 2
4 8 7 4 2
0
Sample Output:
3 1 4
1 3 2
2 4 7

#include
#include 
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100010;
const int INF=1000000000;
vector<int> G[maxn];
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.second!=b.second) return a.second>b.second;
	else return a.first<b.first;
}
int main(){
    
   int n,m;
   scanf("%d %d",&n,&m);
   for(int i=0;i<m;i++){
   	   int a,b;
   	   scanf("%d %d",&a,&b);
   	   G[a].push_back(b);
   	   G[b].push_back(a);
   }    
   int num;
   scanf("%d",&num);
   while(num!=0){
   	unordered_set<int> s;
   	vector<pair<int,int> > v; 
   	  for(int i=0;i<num;i++){
   	  	int a;
   	  	 scanf("%d",&a);
   	  	 v.push_back(make_pair(a,0));
   	  	 s.insert(a);
		 }
   	  for(int i=0;i<num;i++){
   	  	   for(int j=0;j<G[v[i].first].size();j++){
   	  	   	    if(s.find(G[v[i].first][j])!=s.end()){
   	  	   	    	v[i].second++;
						}
				}
		 }
	sort(v.begin(),v.end(),cmp);	  
	for(int i=0;i<3;i++){
		printf("%d",v[i].first);
		if(i!=2) printf(" ");
		else printf("\n");
	}
   	  scanf("%d",&num);
   }
}

这道题可算是“有些难度”了,但是依旧不够看。如果用邻接矩阵存储的话可能会超内存,所以我用了邻接表来判断。

总的来说19年的试题借鉴意义不大,以后不可能再出这么简单的题目了,汗。

你可能感兴趣的:(PTA,算法,c++,数据结构)