# Computer problems in the second round examination of Huazhong University of science and technology

Keywords: less

https://www.nowcoder.com/ta/hust-kaoyan?page=1

### 1. Matrix transpose

Title Description
Input a matrix of N*N, transpose it and output. Requirement: do not use any array (local inversion).

```#include <iostream>
#include <vector>
#include <math.h>
#include<stdlib.h>
using namespace std;

int main()
{
int n, i, j;
cin>>n;
int a[n][n];
for(i = 0; i<n; i++){
for(j=0; j<n; j++){
cin>>a[j][i];
}
}

for(i = 0; i<n; i++){
for(j=0; j<n; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
```

### 2. Count words

Title Description
Write a program to read in a line of words entered by the user ending with ".", count the total number of words, and output how many characters each word contains. (the part separated by one or more spaces is a word)

```#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include<stdlib.h>
using namespace std;

int main()
{
vector<int> a(100);
string s;
int cnt = 0;
while(cin>>s){
//		cout<<s<<" ";
if(s.find_last_of(".")!=string::npos){
a[cnt] = s.length()-1;
break;
}
a[cnt] = s.length();
//		cout<<a[cnt];
cnt++;
}

for(int i =0; i<=cnt; i++){
cout<<a[i]<<" ";
}
}
```

Title Description
Enter an ip address string to determine whether it is legal.

```#include<stdio.h>
int check(int a){
if(a>=0 && a<=255) return 1;
return 0;
}
int main(){
int a, b, c, d;
char ip[10100];
while(~scanf("%s", ip)){
sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d);
if(check(a) && check(b) && check(c) && check(d) && a != 0) printf("Yes!\n");
else printf("No!\n");
}

}
```

### 4. Binary sort tree

Title Description
Binary sort tree, also known as binary search tree. It can be an empty tree or a non empty binary tree with the following characteristics: 1. If the left subtree is not empty, the key value of all nodes on the left subtree is not greater than the key value of the root node; 2. If the right subtree is not empty, the key value of all nodes on the right subtree is not less than the key value of the root node; 3. The left and right subtrees themselves are also a binary sorting tree. Now I give you N nodes with different keyword values. You are required to insert them into a binary sort tree with an initial empty tree in order. After each successful insertion, the keyword value of the corresponding parent node is calculated. If there is no parent node, the output is - 1.

```#include <stdio.h>
#include <stdlib.h>

typedef struct BinTreeNode *BinTree;
struct BinTreeNode{
int data;
BinTree left,right;
};

int f = -1;
BinTree Insert(BinTree T, int x, int p){
if(!T){
T = (BinTree)malloc(sizeof(struct BinTreeNode));
T->data = x;
T->left = T->right = NULL;
f = p;
}else{
if(x < T->data) T->left = Insert(T->left, x, T->data);
else T->right = Insert(T->right, x, T->data);
}
return T;
}
int main()
{
int n,x;
BinTree BT = NULL;
while(~scanf("%d",&n)){
for(int i = 0;i<n;i++){
scanf("%d",&x);
BT = Insert(BT,x,-1);
printf("%d\n",f);
}
}
return 0;
}
```

### 5. String connection

Title Description
It does not borrow any string library function to accept two strings without redundancy, and then connect them without redundancy.

```#include <stdio.h>
#include <stdlib.h>
#define Maxsize 101
int main()
{
char str1[Maxsize], str2[Maxsize];
while(~scanf("%s %s",str1, str2)){
printf("%s%s\n",str1,str2);
}
return 0;
}
```

### 6.a+b

Title Description
An adder is implemented to output the value of a+b.

```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Maxsize 1002

void reverse(char a[], int len){
char temp;
for(int i = 0;i<len/2;i++){
temp = a[i];
a[i] = a[len-1-i];
a[len-1-i] = temp;
}
}
int main()
{
char t1[Maxsize], t2[Maxsize];
int len1,len2,i;
while(~scanf("%s %s",t1,t2)){
int res[Maxsize];
i = 0;
len1 = strlen(t1);
len2 = strlen(t2);
reverse(t1, len1);
reverse(t2, len2);
while(i<len1&&i<len2){
res[i] = t1[i]+t2[i]-'0'-'0';
i++;
}
while(i<len1){
res[i] = t1[i]-'0';
i++;
}
while(i<len2){
res[i] = t2[i]-'0';
i++;
}
res[i] = 0;
for(int j = 0;j<i;j++){
if(res[j]>9){
res[j] = res[j]%10;
res[j+1]++;
}
}
if(res[i]!=0) printf("%d",res[i]);
for(int j = i-1;j>=0;j--) printf("%d",res[j]);
printf("\n");
}

return 0;
}
```

### 7. ranking

Title Description
Sort the number of n inputs and output.

```    #include <iostream>
#include <set>

using namespace std;
template<class T>
void print(T first, T last){
for(;first!=last;++first) cout<<*first<<" ";
cout<<endl;
}

int main()
{
int n;
while(cin>>n){
int t[n];
for(int i=0;i<n;i++){
cin>>t[i];
}
multiset<int> p(t,t+n);
print(p.begin(),p.end());
}

return 0;
}
```

### 8. Special sorting

Title Description
Enter a series of integers, pick out the largest number (if there are more than one, pick one), and sort the remaining numbers. If there is no remaining number, output - 1.

```#include <iostream>
#include <set>

using namespace std;
template<class T>
void print(T first, T last){
last--;
cout<<*last<<endl;
if(first==last) cout<<"-1";
else{
for(;first!=last;++first) cout<<*first<<" ";
}
cout<<endl;
}

int main()
{
int n;
while(cin>>n){
int t[n];
for(int i=0;i<n;i++){
cin>>t[i];
}
multiset<int> p(t,t+n);
print(p.begin(),p.end());
}

return 0;
}
```

### 9. Binary tree traversal

Title Description
The definition of pre order, middle order and post order traversal of binary tree: pre order traversal: for any subtree, first visit the heel, then traverse its left subtree, and finally traverse its right subtree; middle order traversal: for any subtree, first traverse its left subtree, then visit the root, and finally traverse its right subtree; post order traversal: for any subtree, first traverse its left subtree, then traverse its right subtree, and finally traverse its right subtree Access the root after. Given a binary tree's preorder traversal and preorder traversal, the subsequent order traversal is found (hint: given preorder traversal and preorder traversal, the postorder traversal can be uniquely determined).

```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Maxsize 27

typedef struct BTreeNode *BTree;
struct BTreeNode{
char data;
BTree left,right;
};

BTree Creat(char s1[], char s2[], int lf, int ll, int rf, int rl){
if(lf>ll||rf>rl) return NULL;
int i;
for(i = rf; i<=rl; i++ ){
if(s2[i]==s1[lf]) break;
}
BTree p = (BTree)malloc(sizeof(struct BTreeNode));
p->data = s1[lf];
p->left = Creat(s1, s2, lf+1, lf+i-rf, rf, i-1);
p->right = Creat(s1, s2, lf+i-rf+1, ll, i+1, rl);
return p;
}

void postTravel(BTree BT){
if(BT){
postTravel(BT->left);
postTravel(BT->right);
printf("%c",BT->data);
}
}
int main()
{
char s1[Maxsize], s2[Maxsize];
BTree BT;
int len;
while(~scanf("%s",s1)&&~scanf("%s",s2)){
len = strlen(s1);
BT = Creat(s1, s2, 0, len-1, 0, len-1);
postTravel(BT);
printf("\n");
}
}
```

### 10. Parity

Title Description
Input a string, then perform odd check on each character, and finally output the binary number after check (such as' 3 ', output: 10110011).

```#include <stdio.h>
#include <string.h>
#define Maxsize 101

int judge[8],sum;
void toBinary(char c){
int temp = (int)c;
for(int i = 7;temp!=0;i--){
judge[i] = temp%2;
if(judge[i]==1) sum++;
temp = temp/2;
}
}

int main()
{
char a[Maxsize];
while(~scanf("%s",a)){
for(int i = 0;i<strlen(a);i++){
sum = 0;
for(int j = 0;j<8;j++) judge[j] = 0;
toBinary(a[i]);
if(sum%2==1){
for(int j =0;j<8;j++) printf("%d",judge[j]);
}else{
judge[0] = 1;
for(int j =0;j<8;j++) printf("%d",judge[j]);
}
printf("\n");
}
}
return 0;
}

```

### 11. Maximum two numbers

Title Description
Enter a matrix of four rows and five columns to find the two largest numbers in each column.
Enter a description:
The next four lines each contain five integers. Represents a matrix of four rows and five columns. All matrix elements are integers.
Output Description:
There may be multiple groups of test data. For each group of data, output the two largest numbers of each column according to the format of sample output. If one of the two largest numbers has multiple identical values in this column, the row value is the one with the smaller row value.
When output, the row and column order of the original matrix should be preserved, that is, if the row value in the original matrix is small, the row value in the output matrix is still small.

```#include <stdio.h>
#include <limits.h>
#define min INT_MIN

int main()
{
int a[4][5],out[2][5];
int max1,max2,m1,m2;
while(~scanf("%d",&a[0][0])){

for(int i = 0;i<4;i++){
for(int j = 0;j<5;j++){
if(i==0&&j==0) continue;
scanf("%d",&a[i][j]);
}
}
for(int j = 0;j<5;j++){
max1 = max2 = min;
for(int i = 0;i<4;i++){
if(a[i][j]>max1){
max1 = a[i][j];
m1 = i;
}
}
for(int i = 0;i<4;i++){
if(a[i][j]>max2&&i!=m1){
max2 = a[i][j];
m2 = i;
}
}
if(m1<m2){
out[0][j] = max1; out[1][j] = max2;
}else
{
out[0][j] = max2; out[1][j] = max1;
}
}
for(int i=0;i<2;i++){
for(int j = 0;j<5;j++){
printf("%d ",out[i][j]);
}
printf("\n");
}
}
return 0;
}
```

### 12. Ranking of achievements

Title Description
For the data of N students, sort the student data according to the score from high to low. If the scores are the same, sort the data according to the dictionary order of the name characters. If the dictionary order of the names is the same, sort the data according to the age of the students, and output the sorted information of N students.

```#include <stdio.h>
#include <string.h>

struct Students{
char name[105];
int age;
int score;
};

int comp(const void *a,const void *b){
struct Students *s1, *s2;
s1 = (const struct Students *)a;
s2 = (const struct Students *)b;
if(s1->score > s2->score) return 1;
else if(s1->score < s2->score) return -1;
else{
if(strcmp(s1->name,s2->name)>0) return 1;
else if(strcmp(s1->name,s2->name)<0) return -1;
else{
if(s1->age > s2->age ) return 1;
else return -1;
}
}
}
int main(){
int n;
struct Students stu[1001];
while(~scanf("%d",&n)){
for(int i = 0;i<n;i++){
scanf("%s %d %d",stu[i].name,&stu[i].age,&stu[i].score);
}
qsort(stu,n,sizeof(struct Students),comp);
for(int i = 0;i<n;i++){
printf("%s %d %d\n",stu[i].name,stu[i].age,stu[i].score);
}
}
return 0;
}
```

### 13. Traversal list

Title Description
Create an ascending list and traverse the output.

```#include <stdio.h>
int comp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int main()
{
int n, a[1000];
while(~scanf("%d",&n)){
for(int i = 0;i<n;i++){
scanf("%d",&a[i]);
}
qsort(a,n,sizeof(int),comp);
for(int i = 0;i<n;i++)printf("%d ",a[i]);
printf("\n");
}
}
```

### 14. Shou form number

Title Description
Conformal number is such an integer that the lower part of its square is equal to itself. For example, the square of 25 is 625, and the lower part is 25, so 25 is a conformal number. Make a program to judge whether N is conformal.

```#include <stdio.h>

int IsShouxing(int n){
int m = n*n;
while(n!=0){
if(n%10!=m%10){
return 0;
}
n =n/10;
m = m/10;
}
return 1;
}
int main(){
int n,m;
while(~scanf("%d",&n)){
if(IsShouxing(n)) printf("Yes!\n");
else printf("No!\n");
}
return 0;
}
```

### 15. Matrix maximum

Title Description
Write a program to input a mXn matrix to store and output, and calculate the maximum value of each line and the sum of each line. It is required to put the sum of each row into the position of the maximum value of each row. If there are multiple maximum values, take the one with the lowest subscript value as the maximum value. Finally, output the result matrix

```#include <stdio.h>
int square[100][100];

int main()
{
int m,n,sum,max,c;
while(~scanf("%d %d",&m,&n))
{
for(int i = 0;i<m;i++)
{
sum = 0;
for(int j=0;j<n;j++){
scanf("%d",&square[i][j]);
sum +=square[i][j];
}
c = 0;
max = square[i][0];
for(int j=0;j<n;j++)
{
if(square[i][j]>max){
c = j;
max = square[i][j];
}
}
square[i][c] = sum;
}
for(int i = 0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",square[i][j]);
}
printf("\n");
}
}
return 0;
}
```

### 16. Three employees of the minimum age

Title Description
Employees have employee number, name and age. Input the information of n employees and find out 3 employees with the youngest age to print out.

```#include <stdio.h>
#include <string.h>

struct Employees{
char name[11];
int age;
int number;
};

int comp(const void *a,const void *b){
struct Employees *s1, *s2;
s1 = (const struct Employees *)a;
s2 = (const struct Employees *)b;
if(s1->age > s2->age) return 1;
else if(s1->age < s2->age) return -1;
else{
if(s1->number > s2->number) return 1;
else if(s1->number < s2->number) return -1;
else{
if(strcmp(s1->name,s2->name)>0) return 1;
else return -1;
}
}
}
int main(){
int n;
struct Employees stu[1001];
while(~scanf("%d",&n)){
for(int i = 0;i<n;i++){
scanf("%d %s %d",&stu[i].number,stu[i].name,&stu[i].age);
}
qsort(stu,n,sizeof(struct Employees),comp);
if(n>=3){
for(int i = 0;i<3;i++){
printf("%d %s %d\n",stu[i].number,stu[i].name,stu[i].age);
}
}else{
for(int i = 0;i<n;i++){
printf("%d %s %d\n",stu[i].number,stu[i].name,stu[i].age);
}
}
}
return 0;
}
```

### 17. Symmetric matrix

Title Description
Input an N-dimensional matrix to determine whether it is symmetric.

```#include <stdio.h>

int square[100][100];

int main()
{
int n,flag;
while(~scanf("%d",&n)){
for(int i = 0; i<n; i++)
{
for(int j=0;j<n;j++)
{
scanf("%d", &square[i][j]);
}
}
flag = 1;
for(int i = 0; i<n; i++)
{
for(int j=0;j<i;j++)
{
if(square[i][j]!=square[j][i])
{
flag = 0;
}
}
}
if(flag ==0) printf("No!\n");
else printf("Yes!\n");
}
return 0;
}
```

### 18.A+B

Title Description
Given two integers A and B, they are represented as follows: starting from bits, each three digits are separated by commas "," and ". Now calculate the result of A+B and output it in normal form.

```#include <stdio.h>
#include <string.h>
int transfer(char a[]){
int f = 0,i = 0,sum = 0;
if(a[0]=='-'){
f = 1; i++;
}
for(;i<strlen(a);i++){
if(a[i]==',') continue;
sum = sum*10+a[i]-'0';
}
if(f) sum = -sum;
return sum;
}
int main()
{
char a[15],b[15];
while(~scanf("%s %s",a,b)){
printf("%d\n",transfer(a)+transfer(b));
}
return 0;
}
```

### 19. Printing date

Title Description
Give the annual minute m and the nth day of the year, and calculate the nth day of the month.

```#include <stdio.h>

int isLeafYear(int y)
{
if(y%4==0&&y%100!=0) return 1;
else if(y%400==0) return 1;
else return 0;
}

int main()
{
int year,n,m,day,sum;
int mouth[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
while(~scanf("%d %d",&year,&n)){
sum = 0;
if(isLeafYear(year)==0) mouth[1]=28;
else  mouth[1]=29;
for(m = 0;n>sum;m++)
{
sum = sum+mouth[m];
}
day = n-sum+mouth[m-1];
printf("%04d-%02d-%02d\n",year,m,day);
}
return 0;
}
```

### 20. Binary sort tree

Title Description
Input a series of integers, set up a binary sort tree, and traverse the pre order, middle order and post order.

```#include <stdio.h>
#include <stdlib.h>

typedef struct BinTreeNode *BinTree;
struct BinTreeNode{
int data;
BinTree left,right;
};

BinTree Insert(BinTree BT, int x)
{
if(!BT){
BT = (BinTree)malloc(sizeof(struct BinTreeNode));
BT->data = x;
BT->left = BT->right = NULL;
}else{
if(x<BT->data)
BT->left = Insert(BT->left, x);
else if(x>BT->data)
BT->right = Insert(BT->right, x);
}
return BT;
}

void PreTravel(BinTree BT)
{
if(BT){
printf("%d ",BT->data);
PreTravel(BT->left);
PreTravel(BT->right);
}

}

void MiddleTravel(BinTree BT)
{
if(BT){
MiddleTravel(BT->left);
printf("%d ",BT->data);
MiddleTravel(BT->right);
}
}

void PostTravel(BinTree BT)
{
if(BT){
PostTravel(BT->left);
PostTravel(BT->right);
printf("%d ",BT->data);
}
}

int main()
{
int n,x;
BinTree BT;
while(~scanf("%d",&n)){
BT = NULL;
for(int i = 0;i<n;i++){
scanf("%d",&x);
BT = Insert(BT,x);
}
PreTravel(BT); printf("\n");
MiddleTravel(BT);  printf("\n");
PostTravel(BT);  printf("\n");
}
return 0;
}
```

### 21. Large integer sorting

Title Description
Sort the number of N lengths up to 1000.

```#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int comp(const void *a, const void *b)
{
char **c1,**c2;
c1 = (const char **)a;
c2 = (const char **)b;
if(strlen(*c1)>strlen(*c2)) return 1;
else if(strlen(*c1)<strlen(*c2)) return -1;
else{
if(strcmp(*c1,*c2)>0) return 1;
else return -1;
}
}

int main()
{
char *p[101];
int n;
while(~scanf("%d\n",&n)){
for(int i = 0;i<n;i++){
p[i] = (char *)malloc(1001*sizeof(char));
gets(p[i]);
}
qsort(p,n,sizeof(char *),comp);
for(int i = 0;i<n;i++){
printf("%s\n",p[i]);
}
}
return 0;
}
```

### 22. The problem of N stairs going upstairs

Title Description
N-step stairs: you can walk two or one step at a time, and ask how many ways to go upstairs. (non recursive required)

```#include <stdio.h>
int arr[90];

int main()
{
int n;
arr[1] = 1;
arr[2] = 2;
while(~scanf("%d",&n)){
for(int i = 3;i<=n;i++){
arr[i] = arr[i-1] +arr[i-2];
}
printf("%d\n",arr[n]);
}
return 0;
}
```

### 23.a+b

Title Description
Calculate the sum of a+b

```#include <stdio.h>

int main()
{
int a,b;
while(~scanf("%d %d",&a,&b)){
printf("%d\n",a+b);
}
return 0;
}
```

### 24. Palindrome string

Title Description
Give a string with a length of no more than 1000, and judge whether it is palindrome (both forward reading and reverse reading are the same).

```#include <stdio.h>
#include <string.h>
int main()
{
char str[1001];
while(~scanf("%s",str)){
int flag = 1;
for(int i = 0;i<strlen(str)/2;i++)
{
if(str[i]!=str[strlen(str)-1-i])
{
flag = 0;
break;
}
}
if(flag == 0) printf("No!\n");
else printf("Yes!\n");
}
return 0;
}
```

### 25. find location

Title Description
For a given string, find out the repeated characters and give their positions, for example: abcaaAB12ab12 output: A, 1; a, 4; a, 5; a, 10, b, 2; b, 11, 1, 8; 1, 12, 2, 9; 2, 13.

```#include <stdio.h>
#include <string.h>

int main()
{
char str[101];
while(~scanf("%s",str)){
int hash[128];
for(int i = 0;i<128;i++) hash[i] = 0;
for(int i = 0;i<strlen(str);i++){
hash[(int)str[i]]++;
}
for(int i = 0;i<strlen(str);i++){
if(hash[(int)str[i]]>1)
{
for(int j = 0;j<strlen(str);j++){
if(str[j]==str[i]){
printf("%c:%d",str[j],j);
if(--hash[(int)str[i]]!=0) printf(",");
}
}
printf("\n");
}
}
}
return 0;
}
```

### 26. factorial

Title Description
Input n, and find y1=1!+3! + m!(m is the largest odd number less than or equal to n) y2=2!+4! + p!(p is the maximum even number less than or equal to n).

```#include <stdio.h>

int main()
{
int n;
while(~scanf("%d",&n)){
if(n%2==0){
int odd[n/2],even[n/2],sum1=1,sum2=2;
odd[0] = 1;
even[0] = 2;
for(int i=1;i<n/2;i++){
odd[i] = odd[i-1]*(2*i)*(2*i+1);
even[i] = even[i-1]*(2*i+1)*(2*i+2);
sum1 = sum1+odd[i];
sum2 = sum2 +even[i];
}
printf("%d %d\n",sum1,sum2);
}else{
int odd[(n+1)/2],even[n/2],sum1=1,sum2=2;
odd[0] = 1;
even[0] = 2;
for(int i=1;i<n/2;i++){
odd[i] = odd[i-1]*(2*i)*(2*i+1);
even[i] = even[i-1]*(2*i+1)*(2*i+2);
sum1 = sum1+odd[i];
sum2 = sum2 +even[i];
}
odd[(n+1)/2-1] = odd[(n-1)/2-1]*(n-1)*n;
sum1 = sum1 + odd[(n+1)/2-1];
printf("%d %d\n",sum1,sum2);
}

}
return 0;
}
```

### 27. octal

Title Description
Input an integer and convert it to octal output.

```#include <stdio.h>

int main()
{
int n;
while(~scanf("%d",&n)){
printf("%o\n",n);
}
return 0;
}
```

### 28. Longest & shortest text

Title Description
Input multiple lines of strings. Please output the shortest and longest strings in the order in the original text. If there is more than one shortest and longest string, please output all.

```#include<stdio.h>
#include<string.h>

int main()
{
int m=0,i;
char zi[1001][1001];
while(gets(zi[m])!=NULL) {
m++;
}
int max,min;
max = strlen(zi[0]);
min=strlen(zi[0]);
for(i=1;i<m;i++)
{
if(min>strlen(zi[i])) min=strlen(zi[i]);
if(max<strlen(zi[i])) max=strlen(zi[i]);
}
for(i=0;i<m;i++)
{
if(min==strlen(zi[i])) printf("%s\n",zi[i]);
}
for(i=0;i<m;i++)
{
if(max==strlen(zi[i])) printf("%s\n",zi[i]);
}
return 0;
}
```

### 29. The story of farmers, sheep, vegetables and wolves

Refer to:
https://blog.csdn.net/sharon_1995/article/details/104086941

52 original articles published, 24 praised, 10000 visitors+

Posted by roadkillguy on Sun, 26 Jan 2020 03:22:48 -0800