Day 15 算法笔记之算法初步4.1 排序(2)

目录

1.德才论

2.THE BEST RANKS


1.德才论

其实这一题的关键在于考生被分成了五类,在每一类里按照成绩输出,所以应该在结构体里定义一个类别,而类别在输入信息时就可以鉴定出来

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

struct student{
	
	char id[10];
	int de;
	int cai;
	int sum;
	int flag;
	
}stu[100000];

bool cmp(student a,student b){
	if(a.flag!=b.flag){
		return a.flag<b.flag;
	}else if(a.sum!=b.sum){
		return a.sum>b.sum;
	}else if(a.de!=b.de){
		return a.de>b.de;
	}else{
		return strcmp(a.id,b.id)<0;
	}
}

int main(){
	
	int num,min,max;
	
	scanf("%d %d %d",&num,&min,&max);
	
	int de,cai;
	
	int final_num = num;
	
	for(int i=0;i<num;i++){
		
		
		scanf("%s %d %d",&stu[i].id,&stu[i].de,&stu[i].cai);
		
		
		
		de = stu[i].de;
		cai = stu[i].cai;
		
		stu[i].sum=stu[i].cai+stu[i].de;
		
		if(de>=max&&cai>=max){
			stu[i].flag=1;
		}else if(de>=max&&cai>=min){
			stu[i].flag=2;
		}else if(de>=min&&de>=cai&&cai>=min){
			stu[i].flag=3;
		}else if(de>=min&&cai>=min){
			stu[i].flag=4;
		}else{
			stu[i].flag=5;
			final_num-=1;
		}
		
	}
	
	sort(stu,stu+num,cmp);
	
	printf("%d\n",final_num);
	
	for(int i=0;i<final_num;i++){
		printf("%s %d %d %d\n",stu[i].id,stu[i].de,stu[i].cai,stu[i].flag);
	}
	
	return 0;
}

2.THE BEST RANKS

这题因为id比较小,可以用整数表示

所以对每一个人,也就是每一个id,都有对应的四个排名,创建一个数组用id当横坐标,随后四列分别代表四个科目

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

struct student{
	
	int id;
	int score[4];//A C M E
	
}stu[2001];

int sort_by;
int rank[10000002][4];

bool cmp(student a,student b){
	return a.score[sort_by]>b.score[sort_by];
}

int main(){
	
	int m,n;
	
	scanf("%d %d",&m,&n);
	
	for(int i=0;i<m;i++){
		
		scanf("%d %d %d %d",&stu[i].id,&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);
		stu[i].score[0] = stu[i].score[1]+stu[i].score[2]+stu[i].score[3];
	}
	
	for(sort_by =0;sort_by<4;sort_by++){
		
		sort(stu,stu+m,cmp);
		
		rank[stu[0].id][sort_by] = 1;
		
		for(int i=1;i<m;i++){
			
			if(stu[i].score[sort_by]==stu[i-1].score[sort_by]){
				rank[stu[i].id][sort_by] = rank[stu[i-1].id][sort_by];
			}else{
				rank[stu[i].id][sort_by] = i+1;
			}
			
		}
		
	}
	
	int inquire;
	
	char w[4] = {'A','C','M','E'};
	
	for(int j=0;j<n;j++){
		
		scanf("%d",&inquire);
		
		
		if(rank[inquire][0]==0){
			printf("N/A\n");
		}else{
			
			int k =0;
			
			for(int i=0;i<4;i++){
				if(rank[inquire][i]<rank[inquire][k]){
					k = i;
				}
			}
			
			printf("%d %c\n",rank[inquire][k],w[k]);
		}
		
		
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。首先,广度优先搜索(BFS)是一种图搜索算法,可以用来解决一些问题,比如迷宫问题。在迷宫问题中,我们需要找到一条从起点到终点的最短路径。下面是用 Python 实现 BFS 解决迷宫问题的示例代码: ```python from collections import deque # 定义一个二维数组表示迷宫,0 表示可以走,1 表示障碍物 maze = [ [0, 1, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 1, 0], ] # 定义起点和终点 start = (0, 0) end = (4, 4) # 定义四个方向,上下左右 directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] def bfs(maze, start, end): # 定义队列,初始时将起点加入队列 queue = deque([start]) # 定义 visited 集合,用于记录已经访问过的位置 visited = set([start]) while queue: # 取出队列中的第一个节点 node = queue.popleft() # 如果当前节点是终点,返回到达终点的最短距离 if node == end: return distance[node[0]][node[1]] # 对于当前节点,遍历它的四个方向 for direction in directions: # 计算出下一个节点的坐标 next_node = (node[0] + direction[0], node[1] + direction[1]) # 如果下一个节点不越界且没有访问过,并且可以走(maze[next_node[0]][next_node[1]] == 0),将它加入队列和 visited 集合 if 0 <= next_node[0] < len(maze) and 0 <= next_node[1] < len(maze[0]) and next_node not in visited and maze[next_node[0]][next_node[1]] == 0: queue.append(next_node) visited.add(next_node) # 如果没有找到到达终点的路径,返回 -1 return -1 # 计算每个位置到起点的最短距离 distance = [[float('inf') for _ in range(len(maze[0]))] for _ in range(len(maze))] distance[start[0]][start[1]] = 0 bfs(maze, start, end) ``` 在上面的代码中,我们首先定义了一个迷宫,然后定义了起点和终点。接着,我们定义了四个方向,上下左右。接下来,我们定义了 bfs 函数,用于实现广度优先搜索。在 bfs 函数中,我们首先定义了一个队列和 visited 集合,用于记录已经访问过的位置。然后,我们将起点加入队列和 visited 集合。接着,我们进行循环,取出队列中的第一个节点,遍历它的四个方向。如果下一个节点不越界且没有访问过,并且可以走,我们将它加入队列和 visited 集合。最后,如果没有找到到达终点的路径,返回 -1。 最后,我们计算每个位置到起点的最短距离,并调用 bfs 函数求解最短路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值