1.22 Learning Blog

Today I continue to summarize the
order of the topic structure of the winter camp. The
first question:

Insert picture description here
Insert picture description here
We can set the hotel sequence as h, the hotel is represented by a structure, h[i].dist represents the distance of the i-th hotel from the beach, and h[i].cost represents the price of the hotel. According to the meaning of the question, we take the distance as The first keyword, using price as the second keyword, sorts the hotel sequence, and then traverses the number of candidate hotels on the sorted sequence. According to the meaning of the question, if the distance from hotel a to the sea is less than the distance from hotel b to the sea, then the price of hotel b must be lower than hotel a to be counted as a candidate hotel.

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10000;
struct hotel{
    
    
	int dist;
	int cost;
}h[maxn];
int com(hotel a,hotel b){
    
    
	if(a.dist==b.dist)return a.cost<b.cost;//距离相等以宾馆价格低的优先
	return a.dist<b.dist;
}
int main(){
    
    
	int n;
	while(scanf("%d",&n)!=EOF&&n){
    
    
		int i;
		for(i=0;i<n;i++)
		 scanf("%d%d",&h[i].dist,&h[i].cost);
		sort(h,h+n,com);//结构体排序,结合com函数,这里sort是以第一关键字从小到大排序
		int min=INT_MAX;
		int ans=0;
		for(i=0;i<n;i++){
    
    
			if(h[i].cost<min){
    
    //符合
				ans++;//计数
				min=h[i].cost;//更新
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

The second question:
Insert picture description here
Insert picture description here
Insert picture description here
For this question, we use a structure to store the sequence of student information, and then use the score as the first key word, and the admission ticket number as the second key word, sort, and then judge the number of people who have reached the score line and enter them The information can be.

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1005
int que[15];//考题的正整数分值
struct node{
    
    
	char name[25];//考号
	int num;//解决的题目数
	int sum;//分数
}stu[N];
int cmp(node a,node b){
    
    //结构体比较函数
	if(a.sum==b.sum)return strcmp(a.name,b.name)<0?1:0;//分数相等以考号升序排序
	else return a.sum>b.sum;
}
int main(){
    
    
	int stu_num,text_num,line,a,cnt;//stu_num考生数,text_num考题数,line分数线
	while(scanf("%d",&stu_num)!=EOF&&stu_num){
    
    
		cnt=0;
		int i;
		scanf("%d%d",&text_num,&line);
		for(i=1;i<=text_num;i++)//输入考题的正整数分值
		 scanf("%d",&que[i]);
		for(i=1;i<=stu_num;i++){
    
    
			stu[i].sum=0;
			scanf("%s%d",stu[i].name,&stu[i].num);//单个学生的考号和解题数
			while(stu[i].num--){
    
    //根据题号统计分数
				scanf("%d",&a);
				stu[i].sum+=que[a];//计算单个同学的分数
			}
			if(stu[i].sum>=line)cnt++;//通过分数线
		}
		sort(stu+1,stu+1+stu_num,cmp);//结构体排序//从stu[1]开始
		cout<<cnt<<endl;//输出不低于分数线的人数
		for(i=1;i<=stu_num;i++){
    
    //排好序后按分数从高到低输出上线考生的考号和分数,分数相同则按考号升序输出
			if(stu[i].sum>=line)printf("%s %d\n",stu[i].name,stu[i].sum);
		    else break;
		}
	}
	return 0;
}

Question 3:
Insert picture description here
Insert picture description here
For this question, we can build a structure to store information about the cows, including their first round of votes, second round of votes, and serial numbers. Then use the number of votes in the first round as the first keyword to sort in descending order, and then sort the top k in descending order with the number of votes in the second round as the first keyword, and then output the first place.

#include<iostream>
#include<algorithm>
using namespace std;
const int MAX=50010;
int n,k;
struct node{
    
    
	int a;//第一轮预期得票
	int b;//第二轮预期得票
	int num;
}cow[MAX];
int cmpa(node p,node q){
    
    //先按a从大到小排序,若a相等则按b从大到小排序
	if(p.a==q.a)return p.b>q.b;
	return p.a>q.a;
}
int cmpb(node p,node q){
    
    //先按b从大到小排序,若b相等则按a从大到小排序
	if(p.b==q.b)return p.a>q.a;
	return p.b>q.b;
}
int main(){
    
    
	int i;
	while(scanf("%d%d",&n,&k)!=EOF){
    
    
	for(i=0;i<n;i++){
    
    
		scanf("%d%d",&cow[i].a,&cow[i].b);
		cow[i].num=i+1;//牛的编号
	}
	sort(cow,cow+n,cmpa);//第一次排序,n头牛第一轮得票排序
	sort(cow,cow+k,cmpb);//第二次排序,第一次排好序后的前k头牛,第二轮得票排序
	printf("%d\n",cow[0].num);//输出二次排序后的第一名
    }
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_47529865/article/details/112998665