当前位置:   article > 正文

浙江大学计算机与软件学院2019年保研上机_7-1 happy numbers

7-1 happy numbers

这套题跟2019年考研上机题难度差了几个数量级,建议完成时间不超过80分钟。

7-1 Happy Numbers (20 分)

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:

  1. 3
  2. 19
  3. 29
  4. 1

Sample Output:

  1. 4
  2. 89
  3. 0
  1. #include<cstdio>
  2. #include<cmath>
  3. #include<set>
  4. using namespace std;
  5. int getNext(int a)
  6. {
  7. int ans=0;
  8. do{
  9. ans+=pow(a%10,2);
  10. a/=10;
  11. }while(a!=0);
  12. return ans;
  13. }
  14. int main()
  15. {
  16. int n,a;
  17. scanf("%d",&n);
  18. for(int i=0; i<n; i++){
  19. scanf("%d",&a);
  20. set<int> s;
  21. int cnt=0;
  22. while(a!=1&&s.count(a)==0){
  23. cnt++;
  24. s.insert(a);
  25. a = getNext(a);
  26. }
  27. if(a==1) printf("%d\n",cnt);
  28. else printf("%d\n",a);
  29. }
  30. return 0;
  31. }

 

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. 1 2 3 4 5
  2. 10 9 8 7 6
  3. 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:

  1. 14 5
  2. 37 76 98 20 98 76 42 53 99 95 60 81 58 93

Sample Output 1:

  1. 20 37 42 53 58
  2. 93 81 76 76 60
  3. 95 98 98 99

Sample Input 2:

  1. 15 4
  2. 96 37 76 98 20 98 76 42 53 99 95 60 81 58 93

Sample Output 2:

  1. 20 37 42 53
  2. 76 76 60 58
  3. 81 93 95 96
  4. 99 98 98

 

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int A[10010];
  5. void deal(int left, int right)
  6. {
  7. for(int i=left,k=0; i<=(right+left)/2; i++,k++){
  8. swap(A[i],A[right-k]);
  9. }
  10. }
  11. int main()
  12. {
  13. int n,m;
  14. scanf("%d%d",&n,&m);
  15. for(int i=0; i<n; i++){
  16. scanf("%d",&A[i]);
  17. }
  18. sort(A,A+n);
  19. for(int i=m; i<n; i+=2*m){
  20. if(i+m-1<n){
  21. deal(i,i+m-1);
  22. }
  23. else{
  24. deal(i,n-1);
  25. }
  26. }
  27. int z=0;
  28. while(z<n){
  29. int cnt=0;
  30. while(cnt<m&&z<n){
  31. printf("%d",A[z++]);
  32. cnt++;
  33. if(cnt==m||z==n) printf("\n");
  34. else printf(" ");
  35. }
  36. }
  37. return 0;
  38. }

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:

  1. 3
  2. 7
  3. 50 40 36 48 46 62 77
  4. 8
  5. 50 40 36 48 46 62 77 88
  6. 6
  7. 50 40 36 48 46 62

Sample Output:

  1. Yes
  2. No
  3. No

 

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;
  6. struct node{
  7. int data;
  8. node* lchild;
  9. node* rchild;
  10. };
  11. node* Insert(node* root,int x)
  12. {
  13. if(root==NULL){
  14. root = (node*)malloc(sizeof(struct node));
  15. root->data = x;
  16. root->lchild=root->rchild=NULL;
  17. }
  18. else if(x<root->data) root->lchild=Insert(root->lchild,x);
  19. else root->rchild = Insert(root->rchild,x);
  20. return root;
  21. }
  22. int getHeight(node* root)
  23. {
  24. if(root==NULL) return 0;
  25. int HL,HR;
  26. HL=getHeight(root->lchild);
  27. HR=getHeight(root->rchild);
  28. return max(HL,HR)+1;
  29. }
  30. bool flag=true;
  31. void dfs(node* root)
  32. {
  33. if(root==NULL) return;
  34. int cha=getHeight(root->lchild)-getHeight(root->rchild);
  35. if(abs(cha)>1){
  36. flag=false;
  37. return;
  38. }
  39. dfs(root->lchild);
  40. dfs(root->rchild);
  41. }
  42. int main()
  43. {
  44. int k,n,x;
  45. scanf("%d",&k);
  46. for(int i=0; i<k; i++){
  47. scanf("%d",&n);
  48. node* root=NULL;
  49. for(int j=0; j<n; j++){
  50. scanf("%d",&x);
  51. root = Insert(root,x);
  52. }
  53. flag=true;
  54. dfs(root);
  55. if(flag) printf("Yes\n");
  56. else printf("No\n");
  57. }
  58. return 0;
  59. }

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:

  1. 8 10
  2. 2 1
  3. 1 3
  4. 1 4
  5. 1 5
  6. 5 8
  7. 3 5
  8. 2 3
  9. 6 3
  10. 4 6
  11. 3 4
  12. 7 8 1 2 3 4 6 5
  13. 4 1 3 5 2
  14. 4 8 7 4 2
  15. 0

Sample Output:

  1. 3 1 4
  2. 1 3 2
  3. 2 4 7
  1. #include<cstdio>
  2. #include<vector>
  3. #include<set>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn = 100010;
  7. vector<int> adj[maxn];
  8. vector<int> v,vi;
  9. bool cmp(int a, int b)
  10. {
  11. if(vi[a]!=vi[b]) return vi[a]>vi[b];
  12. else return a<b;
  13. }
  14. int main()
  15. {
  16. int n,m,u,v1,k;
  17. scanf("%d%d",&n,&m);
  18. for(int i=0; i<m; i++){
  19. scanf("%d%d",&u,&v1);
  20. adj[u].push_back(v1);
  21. adj[v1].push_back(u);
  22. }
  23. scanf("%d",&k);
  24. while(k!=0){
  25. v.clear();
  26. v.resize(k);
  27. set<int> s;
  28. vi.clear();
  29. vi.resize(n+1);
  30. for(int i=0; i<k; i++){
  31. scanf("%d",&v[i]);
  32. s.insert(v[i]);
  33. }
  34. for(int i=0; i<k; i++){
  35. u = v[i];
  36. for(int j=0; j<adj[u].size(); j++){
  37. if(s.count(adj[u][j])==1){
  38. vi[v[i]]++;
  39. }
  40. }
  41. }
  42. sort(v.begin(),v.end(),cmp);
  43. for(int i=0; i<3; i++){
  44. printf("%d",v[i]);
  45. if(i!=2) printf(" ");
  46. else printf("\n");
  47. }
  48. scanf("%d",&k);
  49. }
  50. return 0;
  51. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号