浙江大学计算机与软件学院2019年保研上机模拟练习

7-1 Happy Numbers (20分)

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 <iostream>
#include <vector>
#include <math.h>
#include <set>
using namespace std;
int get(int target){
	int sum=0;
	while(target!=0){
		sum+=pow(target%10,2);
		target/=10;
	}
	return sum;
}
int main(){
	int num,target;
	scanf("%d\n",&num);
	for(int i=0;i<num;i++){
		scanf("%d\n",&target);
		set<int> dp;
		int step=0;
		int temp;
		dp.insert(target);
		bool go=false;
		while(target!=1){
			target=get(target);
			if(dp.find(target)!=dp.end()){
				printf("%d\n",target);
				go=true;
				break;
			}else{
				dp.insert(target);
			}
			step++;
		}
		if(go) continue;
		printf("%d\n",step);
	}
	return 0;
}

7-2 Zigzag Sequence (25分)

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 <iostream>
#include <vector>
#include <algorithm>
#define min(a,b) (a>b?b:a)
using namespace std;
void pr(vector<int> &temp,int &type,int &line_count,int &count);
int main(){
	int num,width,a;
	scanf("%d %d\n",&num,&width);
	vector<int> dp(10001,0);
	//int count=0;
	scanf("%d",&a);
	dp[a]++;
	for(int i=1;i<num;i++){
		scanf(" %d",&a);
		dp[a]++;
	}
	vector<int> temp(width);
	int count=0;
	int index=1;
	int type=1;
	int line_count=0;
	while(num!=0){
		while(dp[index]!=0){
			dp[index]--;
			temp[count++]=index;
			num--;
			if(count==width){
				pr(temp,type,line_count,count);
			}
		}
		index++;
	}
	if(count!=0) pr(temp,type,line_count,count);
	return 0;
}
void pr(vector<int> &temp,int &type,int &line_count,int &count){
	if(line_count) printf("\n");
	if(type){
		for(int i=0;i<count;i++){
			if(i) printf(" %d",temp[i]);
			else printf("%d",temp[i]);
		}
	}else{
		printf("%d",temp[count-1]);
		for(int i=count-2;i>=0;i--) printf(" %d",temp[i]);
	}
	type^=1;
	count=0;
	line_count++;
}

7-3 Is It An AVL Tree (25分)

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 <iostream>
#include <vector>
using namespace std;
struct node{
	int val;
	node* left;;
    node* right;
};
node* build(vector<int> &temp,int low,int high){
	if(low==high) return NULL;
	node *ret=new node();
	ret->val=temp[low];
	int index=low+1;
	while(index<high && temp[index]<temp[low]) index++;
	ret->left=build(temp,low+1,index);
	ret->right=build(temp,index,high);
	return ret;
}
int check(node *root,bool &good){
	int left=0,right=0;
	if(root->left) left=check(root->left,good);
	if(root->right) right=check(root->right,good);
	if(abs(left-right)>1) good=false;
	return 1+(left>right?left:right);
}
void input(){
	int num;
	scanf("%d\n",&num);
	vector<int> temp(num);
	scanf("%d\n",&temp[0]);
	for(int i=1;i<num;i++){
		scanf(" %d\n",&temp[i]);
	}
	node *root=build(temp,0,num);
	bool good=true;
	check(root,good);
	if(good) printf("Yes\n");
	else printf("No\n");
}
int main(){
	int num;
	scanf("%d\n",&num);
	for(int i=0;i<num;i++){
		input();
	}
	return 0;
}

7-4 Index of Popularity (30分)

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 <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define min(a,b) (a>b?b:a)
struct infor{
	int index,grade;
};
bool cmp1(infor &a,infor &b){
	return a.index<b.index;
}
bool cmp2(infor &a,infor &b){
	if(a.grade!=b.grade) return a.grade>b.grade;
	else return a.index<b.index;
}
struct edge{
	int a,b;
};
int main(){
	int num,relation,A,B,mini;
	scanf("%d %d\n",&num,&relation);

	vector<vector<int>> dp(num+1,vector<int>(100,1));
	
	for(int i=0;i<relation;i++){
		scanf("%d %d\n",&B,&A);
		mini=min(A,B);
		dp[mini][dp[mini][0]++]=A+B-mini;
	}
	while(1){
		scanf("%d",&num);
		if(num==0) break;
		vector<infor> temp(num);
		int count=0;
		for(int i=0;i<num;i++){
			scanf(" %d\n",&A);
			temp[count++].index=A;
		}
		sort(temp.begin(),temp.end(),cmp1);
		for(int i=0;i<num-1;i++){
			for(int j=i+1;j<num;j++){
				for(int c=1;c<dp[temp[i].index][0];c++){
					if(dp[temp[i].index][c]==temp[j].index){
						temp[i].grade++;
						temp[j].grade++;						
						break;
					}
				}
			}
		}
		sort(temp.begin(),temp.end(),cmp2);
		printf("%d",temp[0]);
		printf(" %d",temp[1]);
		printf(" %d\n",temp[2]);
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值