# 浙江大学计算机与软件学院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 104) 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.

```3
19
29
1```

### Sample Output:

```4
89
0```
```//
//  main.cpp
//  7-1 Happy Numbers (20分)
//
//  Created by apple on 2020/10/26.
//

#include<bits/stdc++.h>
using namespace std;
int n;
int f(int x)
{
int sum=0;
while(x)
{
sum+=(x%10)*(x%10);
x/=10;
}
return sum;
}
int main(int argc, const char * argv[]) {
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(x==1)
{
cout<<0<<endl;
continue;
}
set<int>t;
t.insert(x);
int cnt=0;
while(1)
{
int k=f(x);
if(t.find(k)==t.end())
{
t.insert(k);
x=k;
cnt++;
}
else
{
cout<<k<<endl;
break;
}
x=k;
if(k==1)
{
cout<<cnt<<endl;
break;
}
}
}
return 0;
}
/*
3
19
29
1
*/```

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 104. 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```
```//
//  main.cpp
//  7-2 Zigzag Sequence (25分)
//
//  Created by apple on 2020/10/26.
//

#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<int>v;
int main(int argc, const char * argv[]) {
cin>>n>>m;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
v.push_back(x);
}
sort(v.begin(),v.end());
int cntt=0;
for(int i=0;i<v.size();i+=m)
{
vector<int>tep;
if(i+m-1>v.size())
{
for(int j=i;j<v.size();j++)
{
tep.push_back(v[j]);
}
}
else
{
for(int j=i;j<v.size();j++)
{
tep.push_back(v[j]);
if(tep.size()==m)break;
}
}
if(cntt%2)reverse(tep.begin(), tep.end());
//cout<<tep.size()<<endl;
for(int i=0;i<tep.size();i++)
{
i==tep.size()-1?cout<<tep[i]<<endl:cout<<tep[i]<<" ";
}
cntt++;
}
return 0;
}
/*
14 5
37 76 98 20 98 76 42 53 99 95 60 81 58 93
20 37 42 53 58
93 81 76 76 60
95 98 98 99
*/```

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```
```//
//  main.cpp
//  7-3 Is It An AVL Tree (25分)
//
//  Created by apple on 2020/10/26.
//

#include<bits/stdc++.h>
using namespace std;
int n;
struct node
{
int data;
node *left,*right;
node(int d): data(d),left(NULL),right(NULL){}
};
inline int geth(node *p)
{
if(!p)return 0;
return max(geth(p->left),geth(p->right))+1;
}
inline bool judge(node *p)
{
if(!p)return true;
if(abs(geth(p->left)-geth(p->right))>1)return false;
return judge(p->left)&&judge(p->right);
}
inline node* build(node* p,int val)
{
if(!p)return new node(val);
if(val>p->data)
{
p->right=build(p->right, val);
return p;
}
if(val<p->data)
{
p->left=build(p->left, val);
return p;
}
return p;
}
int main(int argc, const char * argv[]) {
cin>>n;
for(int i=0;i<n;i++)
{
node *p=NULL;
int k;
cin>>k;
for(int j=0;j<k;j++)
{
int x;
cin>>x;
p=build(p,x);
}
judge(p)?cout<<"Yes"<<endl:cout<<"No"<<endl;
}
return 0;
}
/*
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
Yes
No
No
*/```

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 105), 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≤KN), 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.

```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```
```//
//  main.cpp
//  7-4 Index of Popularity (30分)
//
//  Created by apple on 2020/10/26.
//

#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>>edg;
int n,m,vis;
inline bool cmp(const pair<int,int>&a, const pair<int,int>&b)
{
if(a.second==b.second)return a.first<b.first;
return a.second>b.second;
}
int main(int argc, const char * argv[]) {
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
edg.push_back(make_pair(a, b));
}
while(1)
{
memset(vis, 0, sizeof(vis));
int k;
cin>>k;
if(!k)break;
map<int,int>q;
for(int i=0;i<k;i++)
{
int x;
cin>>x;
vis[x]=1;
q[x]=0;
}
vector<pair<int, int>>ans;
for(int i=0;i<edg.size();i++)
{
int x=edg[i].first,y=edg[i].second;
if(vis[x]&&vis[y])
{
q[x]++,q[y]++;
}
}
for(auto it:q)
{
ans.push_back(make_pair(it.first, it.second));
}
sort(ans.begin(),ans.end(),cmp);
cout<<ans.first<<" "<<ans.first<<" "<<ans.first<<endl;
}
return 0;
}
/*
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
*/```

0 条评论

• ### Codeforces Round #547 (Div. 3)E. Superhero Battle

A superhero fights with a monster. The battle consists of rounds, each of which ...

• ### Codeforces Round #199 (Div. 2)C. Cupboard and Balloons

A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is ...

• ### 2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记

Recently, on the course of algorithms and data structures, Valeriy learned how t...

• ### poj-1207 THE 3n+1 problem

Problems in Computer Science are often classified as belonging to a certain clas...

• ### HDUOJ---2642Stars（二维树状数组）

Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Jav...

• ### Codeforces 833E Caramel Clouds

E. Caramel Clouds time limit per test：3 seconds memory limit per test：256 megaby...

• ### 为什么SpringBoot2.x官方首推Caffeine作为缓存框架？

Caffeine是一个高性能，高命中率，低内存占用，near optimal 的本地缓存，简单来说它是Guava Cache的优化加强版，有些文章把Caffei...

• ### 机器人控制器编程课程-教案02-基础

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。

• ### Android LayoutInflater的用法详解

相信我们在开发过程中肯定接触过LayoutInflater，比如ListView的适配器里的getView方法里通过LayoutInflater.from(Co...

• ### hackerrank challenges median

The median of M numbers is defined as the middle number after sorting them in or...

### 活动推荐 