这套题跟2019年考研上机题难度差了几个数量级,建议完成时间不超过80分钟。
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.
Sample Input:
3
19
29
1
Sample Output:
4
89
0
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
int getNext(int a)
{
int ans=0;
do{
ans+=pow(a%10,2);
a/=10;
}while(a!=0);
return ans;
}
int main()
{
int n,a;
scanf("%d",&n);
for(int i=0; i<n; i++){
scanf("%d",&a);
set<int> s;
int cnt=0;
while(a!=1&&s.count(a)==0){
cnt++;
s.insert(a);
a = getNext(a);
}
if(a==1) printf("%d\n",cnt);
else printf("%d\n",a);
}
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 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
#include<cstdio>
#include<algorithm>
using namespace std;
int A[10010];
void deal(int left, int right)
{
for(int i=left,k=0; i<=(right+left)/2; i++,k++){
swap(A[i],A[right-k]);
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++){
scanf("%d",&A[i]);
}
sort(A,A+n);
for(int i=m; i<n; i+=2*m){
if(i+m-1<n){
deal(i,i+m-1);
}
else{
deal(i,n-1);
}
}
int z=0;
while(z<n){
int cnt=0;
while(cnt<m&&z<n){
printf("%d",A[z++]);
cnt++;
if(cnt==m||z==n) printf("\n");
else printf(" ");
}
}
return 0;
}
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<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
int data;
node* lchild;
node* rchild;
};
node* Insert(node* root,int x)
{
if(root==NULL){
root = (node*)malloc(sizeof(struct node));
root->data = x;
root->lchild=root->rchild=NULL;
}
else if(x<root->data) root->lchild=Insert(root->lchild,x);
else root->rchild = Insert(root->rchild,x);
return root;
}
int getHeight(node* root)
{
if(root==NULL) return 0;
int HL,HR;
HL=getHeight(root->lchild);
HR=getHeight(root->rchild);
return max(HL,HR)+1;
}
bool flag=true;
void dfs(node* root)
{
if(root==NULL) return;
int cha=getHeight(root->lchild)-getHeight(root->rchild);
if(abs(cha)>1){
flag=false;
return;
}
dfs(root->lchild);
dfs(root->rchild);
}
int main()
{
int k,n,x;
scanf("%d",&k);
for(int i=0; i<k; i++){
scanf("%d",&n);
node* root=NULL;
for(int j=0; j<n; j++){
scanf("%d",&x);
root = Insert(root,x);
}
flag=true;
dfs(root);
if(flag) printf("Yes\n");
else printf("No\n");
}
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 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≤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<cstdio>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 100010;
vector<int> adj[maxn];
vector<int> v,vi;
bool cmp(int a, int b)
{
if(vi[a]!=vi[b]) return vi[a]>vi[b];
else return a<b;
}
int main()
{
int n,m,u,v1,k;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++){
scanf("%d%d",&u,&v1);
adj[u].push_back(v1);
adj[v1].push_back(u);
}
scanf("%d",&k);
while(k!=0){
v.clear();
v.resize(k);
set<int> s;
vi.clear();
vi.resize(n+1);
for(int i=0; i<k; i++){
scanf("%d",&v[i]);
s.insert(v[i]);
}
for(int i=0; i<k; i++){
u = v[i];
for(int j=0; j<adj[u].size(); j++){
if(s.count(adj[u][j])==1){
vi[v[i]]++;
}
}
}
sort(v.begin(),v.end(),cmp);
for(int i=0; i<3; i++){
printf("%d",v[i]);
if(i!=2) printf(" ");
else printf("\n");
}
scanf("%d",&k);
}
return 0;
}