考研复试机试 | C++ | 名校复试上机题

1.排序(华中科技大学上机题)

题目链接:https://www.nowcoder.com/practice/508f66c6c93d4191ab25151066cb50ef?tpId=40&tqId=21542&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

题目:

对输入的n个数进行排序并输出
输入:
输入的第一行包括一个整数n(1<=n<=100)。 接下来的一行包括n个整数。
输出:
可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格。
每组测试数据的结果占一行。

示例:

输入:
4
1 4 3 2
输出:
1 2 3 4

代码:

#include<cstdio>
#include<algorithm>

using namespace std;
int main(){
    
    
	int n;
	int arr[101];
	scanf("%d",&n);
	// 所有的待排序的数存到数组中 
	for(int i=0;i<n;i++){
    
    
		scanf("%d",&arr[i]);
	} 
	// sort(begin,end)  左闭右开的区间 [0,4)
	// arr 是0号元素的地址  arr+n是n号元素的地址 
	sort(arr,arr+n); // 排序
	for(int i =0;i<n;i++){
    
    
		printf("%d ",arr[i]);
	} 
	printf("\n");
}

pass:
sort是默认是升序排序;
sort(left,right)这个区间是左闭右开的;

C++的特殊机制:不同参数的函数可以有相同的名字 (函数重载)
sort(left,right,comp)
comp指比较和交换的条件

sort实现降序排序如下:

#include<cstdio>
#include<algorithm>

using namespace std;

// ture表示不发生交换   false表示发生交换 
bool comp(int lhs,int rhs){
    
    
	//不发生交换的条件下返回真
	// 如果左面的数字大于右面的数字, 
	if(lhs>rhs) return true;
	else return false; 
}

int main(){
    
    
	int arr[8];
	// 所有的待排序的数存到数组中 
	for(int i=0;i<8;i++){
    
    
		scanf("%d",&arr[i]);
	} 
	
	// 降序排序 
	sort(arr,arr+8,comp); 
	
	// 打印 
	for(int i =0;i<8;i++){
    
    
		printf("%d ",arr[i]);
	} 
	printf("\n");
}

2.整数奇偶排序(北大上机题)

题目链接:http://t.cn/E9glPvp

题目:

输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到小排列; 2.然后输出其中的偶数,并按从小到大排列。

输入描述:
任意排序的10个整数(0~100),彼此以空格分隔。

输出描述:
可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。 1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>…>>a[9])类似的做法来实现; 2. 输入数据随机,有可能相等。

示例1
输入:
4 7 3 13 11 12 0 47 34 98
输出:
47 13 11 7 3 0 4 12 34 98

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;
 
int main()
{
    
    
    int a[10];
    for (int i=0;i<10;i++){
    
    
    	scanf("%d",&a[i]);
	}

    // 先整体进行升序排序 
	sort(a, a + 10);
	// 奇数逆序输出 
    for(int i = 9; i >= 0; i--)
        if(a[i] % 2 == 1)
            printf("%d ",a[i]);
    // 偶数顺序输出 
	for(int i = 0; i < 10; i++)
        if(a[i] % 2 == 0)
            printf("%d ",a[i]);
    
}

3.成绩排序 (清华上机题)

题目链接:http://t.cn/E9d3ysv

题目:

用一维数组存储学号和成绩,然后,按成绩排序输出。

输入描述:
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。 接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。

输出描述:
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。 如果学生的成绩相同,则按照学号的大小进行从小到大排序。

样例输入:

3
1 90
2 87
3 92

样例输出:
2 87
1 90
3 92

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;

struct Student {
    
    
	int num; // 学号 
	int grade; // 成绩 
};

// 按成绩从小到大排序 
bool comp(Student lhs,Student rhs){
    
    
	//不发生交换的情况 (原本num小的学生成绩就num大的学生成绩) 
	if(lhs.grade < rhs.grade){
    
    
		return true; 
	}
	// 如果学生的成绩相同,按照学号从小到大排序 
	else if(lhs.grade == rhs.grade && lhs.num <= rhs.num){
    
    
		return true;
	} 

	else return false;
} 

int main()
{
    
    
	// 声明结构体数组 
    Student arr[100];
	int n;
	scanf("%d",&n);
	for (int i=0;i<n;i++){
    
    
		scanf("%d %d",&arr[i].num,&arr[i].grade);
	} 
    sort(arr,arr+n,comp);
    for (int i=0;i<n;i++){
    
    
		printf("%d %d\n",arr[i].num,arr[i].grade);
	}  
}

4. 成绩排序2 (清华上机)

题目链接: http://t.cn/E9gyHM1

题目:

输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩都按先录入排列在前的规则处理。

示例:
输入:
jack 70
peter 96
Tom 70
smith 67
输出:
从高到低 成绩
peter 96
jack 70
Tom 70
smith 67
从低到高 成绩
smith 67
jack 70
Tom 70
peter 96

输入描述:
注意一个case里面有多组样例,请用循环处理输入 输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开。
输出描述:
按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开

示例1
输入:
3
0
fang 90
yang 50
ning 70
输出:
fang 90
ning 70
yang 50

示例2
输入:
3
1
fang 90
yang 50
ning 70
3
0
moolgouua 43
aebjag 87
b 67
输出:
yang 50
ning 70
fang 90
aebjag 87
b 67
moolgouua 43

说明:
第一组用例:
3
1
fang 90
yang 50
ning 70
升序排序为:
yang 50
ning 70
fang 90
第二组降序为:
aebjag 87
b 67
moolgouua 43

代码:

不稳定排序 变为 稳定排序的小技巧,添加一个seq顺序字段,记录元素原本的顺序

#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;

// 这也是稳定排序的一种技巧 
struct Student {
    
    
	char name[30]; // 姓名 
	int grade; // 成绩 
	int seq; //记录录入顺序 
};

// 升序排序 
bool comp1(Student lhs,Student rhs){
    
    
	//不发生交换的情况 (原本num小的学生成绩就num大的学生成绩) 
	if(lhs.grade < rhs.grade){
    
    
		return true; 
	} 
	// 如果成绩相同,录入顺序有序,则不交换 
	else if(lhs.grade==rhs.grade && lhs.seq < rhs.seq) return true; 
	else return false;
} 

// 降序排序
bool comp0(Student lhs,Student rhs){
    
    
	//不发生交换的情况 (原本num小的学生成绩就num大的学生成绩) 
	if(lhs.grade > rhs.grade){
    
    
		return true; 
	} 
		// 如果成绩相同,录入顺序有序,则不交换 
	else if(lhs.grade==rhs.grade && lhs.seq < rhs.seq) return true; 
	else return false;
} 

int main()
{
    
    
	int N; // 数组大小
	int order; //升序 or 降序
	Student arr[200];
	while(scanf("%d%d",&N,&order)!=EOF){
    
    
		int seq = 0;
		for(int i=0;i<N;i++){
    
    
			scanf("%s%d",arr[i].name,&arr[i].grade);
			arr[i].seq = seq;
			seq++; // 记录录入顺序 
		};
		//0是降序 
		if (order == 0 ){
    
    
			sort(arr,arr+N,comp0);
		}
		// 1是升序 
		else{
    
    
			sort(arr,arr+N,comp1);
		}
		for(int i=0;i<N;i++){
    
    
			printf("%s %d\n",arr[i].name,arr[i].grade);
		}
	} 
}

5.找X (哈工大上机题)

提交网址:http://t.cn/E9gHFnS

题目:

输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。

输入描述:
测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
输出描述:
对于每组输入,请输出结果。

示例1
输入:
2
1 3
0

输出:
-1

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;

int main()
{
    
    
	int n;
	int arr[100];
	scanf("%d",&n);
	for(int i=0;i<n;i++){
    
    
		scanf("%d",&arr[i]);
	} 
	int x;
	scanf("%d",&x);
	int idx;
	for(idx = 0; idx<n;idx++){
    
    
		if(arr[idx]==x){
    
    
			printf("%d\n",idx);
			break;
		}
	}
	if (idx == n){
    
    
		printf("-1\n");
	}
}

6. 查找 (北邮上机题)

题目链接:
http://t.cn/E9g8aaR

题目:

输入数组长度 n; 输入数组 a[1…n]; 输入查找个数m; 输入查找数字b[1…m];
输出 YES or NO
查找有则YES,否则NO 。

输入描述:
输入有多组数据。 每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m,n<=100)。

输出描述:
如果在n个数组中输出YES否则输出NO。

示例1
输入:
5
1 5 2 4 3
3
2 5 6
输出:
YES
YES
NO

代码:

二分查找

#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;

int arr[100];

// 二分查找 
bool binarysearch(int n,int x){
    
    
	int left = 0;
	int right = n-1;
	while(left<=right){
    
    
		int mid=(left + right)/2;
		if(arr[mid]==x){
    
    
			return true;
		}
		else if(arr[mid] > x){
    
    
			right = mid - 1; 
		}
		else left = mid+1;
	}
	return false;
}

int main()
{
    
    
	int n,m;
	while(scanf("%d",&n)!=EOF){
    
    
		for(int i=0;i<n;i++){
    
    
			scanf("%d",&arr[i]);
		}
		// 排序
		sort(arr,arr+n);
		scanf("%d",&m);
		for(int i=0;i<m;i++){
    
    
			int x;
			scanf("%d",&x);
			if(binarysearch(n,x)){
    
    
				printf("YES\n");
			}
			else{
    
    
				printf("NO\n");
			}
		} 
	}
}

另一种解法,使用map解题

#include <iostream>
#include <cstdio>
#include <map>
 
using namespace std;

int arr[100];

int main()
{
    
    
	map<int,int> findIndex;
	int m,n;
	int arr[101];
	while(scanf("%d",&n)!=EOF){
    
    
		for(int i=0;i<n;i++){
    
    
			scanf("%d",&arr[i]);
			findIndex[arr[i]] = i; //将数组元素作为键,数组元素下标作为值插入到map中  
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++){
    
    
			int findNum; //待查找的元素 
			scanf("%d",&findNum);
			// find函数会返回找到的那个元素的迭代器 
			if (findIndex.find(findNum)==findIndex.end()){
    
    
				printf("NO\n");
			} 
			else{
    
    
				printf("YES\n");
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/lijiamingccc/article/details/129099189