【算法笔记题解】《算法笔记知识点记录》第四章——算法初步1——排序

请添加图片描述
如果喜欢大家还希望给个收藏点赞呀0.0
相关知识点大家没基础的还是要看一下的,链接:
《算法笔记知识点记录》第三章——入门模拟

由于放原题的话文章实在太长,所以题多的话我只放思路和题解,大家请前往相应的网站查看题目呀0.0

🧑🏻作者简介:一个从工业设计改行学嵌入式的年轻人
✨联系方式:2201891280(QQ)
📔源码地址:https://gitee.com/xingleigao/algorithm-notes
全文大约阅读时间: 30min



🍕3.1小节——入门模拟->简单模拟

地址合集:《算法笔记》4.1小节——算法初步->排序

问题 A: 排序

直接调用函数就好了

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

int hash[101];
int main(){
    int n;
    while(scanf("%d",&n) != EOF){
        for(int i = 0;i < n;++i)  scanf("%d",&hash[i]);//读入数据
        sort(hash,hash + n);//排序
        for(int i = 0;i < n - 1;++i)    printf("%d ",hash[i]);//输出
        printf("%d\n",hash[n-1]);
    }
    return 0;
}

问题 B: 特殊排序

这个有个隐藏条件是最大值只有一个。。。

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

int hash[1001];
int main(){
    int n;
    while(scanf("%d",&n) != EOF){
        for(int i = 0;i < n;++i)  scanf("%d",&hash[i]);//读入数据
        sort(hash,hash + n);//排序
        printf("%d\n",hash[n-1]);   //输出最大值
        for(int i = 0;i < n - 2;++i)    printf("%d ",hash[i]);//输出
        if(n == 1)     printf("-1\n");
        else    printf("%d\n",hash[n-2]);
    }
    return 0;
}

问题 C: EXCEL排序

条件排序就好了,利用全局变量通信就不用写多个cmp函数啦

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

struct STU{
    char id[7];
    char name[9];
    unsigned char score;
}stu[100000];

int c;
bool cmp(STU a, STU b){
    if(c == 2 && strcmp(a.name,b.name) != 0) return strcmp(a.name,b.name) < 0;
    else if(c == 3 && a.score != b.score)   return a.score < b.score;
    return strcmp(a.id, b.id) < 0;
}

int main(){
    int n, Case = 1;
    while(scanf("%d %d",&n, &c) != EOF && n != 0){
        for(int i = 0;i < n;++i)    scanf("%s %s %d", stu[i].id, stu[i].name, &stu[i].score);
        sort(stu, stu + n, cmp);
        printf("Case %d:\n",Case);
        Case++;
        for(int i = 0;i < n;++i)    printf("%s %s %d\n",stu[i].id, stu[i].name, stu[i].score);
    }
    return 0;
}

问题 D: 字符串内排序

读入之后直接进行排序就好了。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
	char a[210];
	while(cin.getline(a,210)){
		sort(a,a+strlen(a));
		puts(a);	
	}
	return 0;
}

问题 E: Problem B

简单模拟求一下就好了

#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int m;
    while(scanf("%d",&m) != EOF){
        int a[m][m];
        for(int i = 0; i < m; ++i)
            for(int j = 0; j < m; ++j)
                scanf("%d",&a[i][j]);
        int b[2*m + 2];

        for(int i = 0; i < m; ++i){     //每行每列加和
            int numi = 0,numj = 0;
            for ( int j = 0; j < m; j++){
                numi += a[i][j];
                numj += a[j][i];
            }
            b[i] = numi;
            b[i+m] = numj;
        }
        int numi =0;
        int numj = 0;
        for(int i = 0; i < m; ++i){     //对角线加和
            numi += a[i][i];
            numj += a[i][m-i-1];
        }
        b[2*m] = numi;
        b[2*m+1] = numj;
        sort(b,b + 2*m + 2);
        for(int i = 2*m + 1; i >= 0; --i) printf("%d ",b[i]);
        puts("");
    }
    return 0;
}

问题 F: 小白鼠排队

按要求排输出就好了。

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

struct xiaobaishu{
    int weight;
    char color[11];
}shu[101];

bool cmp(xiaobaishu a, xiaobaishu b){
    return a.weight > b.weight;
}

int main(){
    int n;
    while( scanf ("%d", &n) != EOF){
        for(int i = 0; i < n; ++i)  scanf("%d %s",&shu[i].weight,shu[i].color);
        sort(shu, shu + n, cmp);
        for(int i = 0; i < n;++i)   printf("%s\n",shu[i].color);
    }
    return 0;
}

问题 G: 中位数

按照要求求就行了,需要注意如果数字数量为偶数的话是中间两个的平均数!

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

int main(){
    int n;
    while(scanf("%d" ,&n) != EOF&&n != 0){
        int a[n];
        for(int i = 0; i < n; ++i)  scanf("%d", &a[i]);
        sort(a,a +n);
        if(n % 2 == 1)
            printf("%d\n",a[n/2]);
        else
            printf("%d\n",(a[n/2-1]+a[n/2])/2);
    }
    return 0;
}

问题 H: 整数奇偶排序

按照写就好了,可以利用异或的特性简化if的书写。值得学习呀,我也是偷师偷来的。

#include<cstdio>
#include<algorithm>
using namespace std;
bool cmp(int a, int b){
    if((a % 2) ^ (b % 2)) return a % 2 == 1;    //同0异1 所以这里就是不一样的情况 奇数在前 所以这么写
    else if(a % 2 == 1 ) return a > b;
    else return a < b;
}

int main(){
    int a[10];
    while(scanf("%d %d %d %d %d %d %d %d %d %d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9]) != EOF){
        sort(a , a + 10, cmp);
        for(int i = 0; i< 10; ++i)  printf("%d ",a[i]);
        puts("");
    }
    return 0;
}

问题 I: 排名

按照要求选就好了呀。

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

struct student{
    char id[21];
    int score;
}stu[10001];

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

int main(){
    int n, m, g;
    while(scanf("%d",&n) && n!= 0){
        scanf("%d %d", &m , &g);
        int s[m],num = 0;
        for(int i = 0; i < m; ++i)  scanf("%d",&s[i]);
        for(int i = 0; i < n; ++i){
            scanf("%s",stu[i].id);
            stu[i].score = 0;
            int ms,mn;
            scanf("%d",&ms);
            for( int j = 0; j < ms; ++j){
                scanf("%d",&mn);
                stu[i].score += s[mn-1];
            }
            if(stu[i].score >= g)   num++;
        }
        sort(stu, stu + n, cmp);
        printf("%d\n",num);
        for(int i = 0;i < n; ++i)   printf("%s %d\n",stu[i].id,stu[i].score);
    }
    return 0;
}

A1062 Talent and Virtue (25 分)

地址:A1062 Talent and Virtue (25 分)|B1015 德才论 (25 分)

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

struct student{
	char id[10];
	int D, C, sum, flag;
}stu[100010];

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.D != b.D)	return a.D > b.D;
	else			return strcmp(a.id, b.id) < 0;
}

int main(int argc, char *argv[]){
	int n, L, H;

	while(scanf("%d%d%d", &n, &L, &H) != EOF){
		int m = n;
		for(int i = 0; i < n; ++i){
			scanf("%s%d%d",stu[i].id, &stu[i].D, &stu[i].C);
			stu[i].sum = stu[i].D + stu[i].C;
			if(stu[i].D < L || stu[i].C < L){
				m--;
				stu[i].flag = 5;
			}
			else if(stu[i].D >= H && stu[i].C >= H)	stu[i].flag = 1;
			else if(stu[i].D >= H )	stu[i].flag = 2;
			else if(stu[i].D >= stu[i].C)		stu[i].flag = 3;
			else					stu[i].flag = 4;	
		}
		sort(stu, stu + n, cmp);
		printf("%d\n", m);
		for(int i = 0; i< m; i++)	printf("%s %d %d\n",stu[i].id, stu[i].D, stu[i].C);			
	}
		return 0;
}

A1012 The Best Rank (25 分)

地址:A1012 The Best Rank (25 分)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;
struct student{
    int id;
    int grade[4];
}stu[2010];

char course[4] = {'A' , 'C', 'M', 'E'};
int Rank[10000000][4] ={0};
int now;

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

int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i = 0;i < n;++i) {
        scanf("%d %d %d %d",&stu[i].id,&stu[i].grade[1],&stu[i].grade[2],&stu[i].grade[3]);
        stu[i].grade[0] = round((stu[i].grade[1]+stu[i].grade[2]+stu[i].grade[3])/3.0) + 0.5;
     }
    for(now = 0;now<4;now++){
        sort(stu,stu + n,cmp);
        Rank[stu[0].id][now] = 1;
        for(int i = 1;i < n;++i){
            if(stu[i].grade[now] == stu[i-1].grade[now])    Rank[stu[i].id][now] = Rank[stu[i-1].id][now];
            else Rank[stu[i].id][now] = i + 1;
        }
    }
    int query;
    while(m--){
        scanf("%d",&query);
        if(!Rank[query][0]) printf("N/A\n");
        else{
            int min=10000000,mini;
            for(int i = 0; i < 4;++i){
                if(Rank[query][i] < min){
                    min = Rank[query][i];
                    mini = i;
                }
            }
            printf("%d %c\n",min,course[mini]);
        }
    }
    return 0;
}

A1025 PAT Ranking (25 分)

地址:A1025 PAT Ranking (25 分)

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

struct student{
    char id[17];
    int score;
    int local;
    int local_rank;
}stu[30001];

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

int main(){
    int N, K, num = 0;
    //数据接收 并对考场内用户进行排名
    scanf("%d",&N);
    for(int i = 0; i < N; ++i){
        //获取考场内考生数据
        scanf("%d",&K);
        for( int j = 0; j < K; ++j){
            scanf("%s %d",stu[num].id,&stu[num].score);
            stu[num].local = i + 1;
            num++;
        }
        //对考场内考生进行排名
        sort(stu + num - K,stu + num, cmp);
        stu[num - K].local_rank = 1;
        for(int j = num - K + 1; j < num; ++j)
            if(stu[j].score == stu[j - 1].score)
                stu[j].local_rank = stu[j - 1].local_rank;
            else
                stu[j].local_rank = j + 1 - num +K;
    }
    printf("%d\n",num);
    sort(stu,stu + num, cmp);
    int r = 1;
    for(int i = 0; i < num; ++i){
        if(i > 0 && stu[i].score != stu[i-1].score)
            r = i + 1;
        printf("%s ",stu[i].id);
        printf("%d %d %d\n", r, stu[i].local, stu[i].local_rank);
    }

    return 0;
}

A 1028 List Sorting (25 分)

地址:A 1028 List Sorting (25 分)

#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;

typedef struct Student{
    int id;
    char name[10];
    int goal;
}student;

int sorti;

bool cmp(student a,student b){
    if(sorti == 1)  return a.id < b.id;
    else if(sorti == 2) return strcmp(a.name, b.name) < 0;
    else return a.goal < b.goal;
}

int main(){
    int n;
    scanf("%d%d", &n, &sorti);
    student stu[n];
    for(int i = 0; i < n; ++i)
        scanf("%d%s%d",&stu[i].id,&stu[i].name,&stu[i].goal);
    sort(stu, stu + n, cmp);
    for(int i = 0; i < n; ++i)
        printf("%06d %s %d\n", stu[i].id, stu[i].name, stu[i].goal);
    return 0;
}

A 1055 The World’s Richest (25 分)

地址:A 1055 The World’s Richest (25 分)

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

typedef struct Student{
    char name[10];
    int age,worth;
}student;

bool cmp(student a, student b){
    if(a.worth != b.worth)  return a.worth > b.worth;
    else if(a.age != b.age) return a.age < b.age;
    else return strcmp(a.name,b.name) < 0;
}

int main(){
    int N,K, Age[201] = {0};
    scanf("%d%d",&N,&K);
    student stu[N];

    //读入数据
    for(int i = 0;i < N; ++i){
        scanf("%s%d%d",&stu[i].name, &stu[i].age, &stu[i].worth);
    }
    sort(stu, stu + N, cmp);

    int ansnum = 0;
    student *ans[N];
    for(int i = 0; i < N; ++i){
        if(Age[stu[i].age] < 100){
            Age[stu[i].age] ++;
            ans[ansnum ++] = &stu[i];
        }
    }

    int m ,agel, ager;
    for(int i = 1; i <= K; ++i){
        printf("Case #%d:\n",i);
        scanf("%d %d %d",&m,&agel,&ager);
        int printednum = 0;
        for(int j = 0;j < ansnum && printednum < m;++j){
            if(ans[j]->age >= agel &&ans[j]->age <= ager){
                printf("%s %d %d\n",ans[j]->name,ans[j]->age,ans[j]->worth);
                printednum ++;
            }
        }
        if(!printednum) printf("None\n");
    }
    return 0;
}

A 1083 List Grades (25 分)

地址: A 1083 List Grades (25 分)

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

struct student{
    char name[11];  //姓名
    char id[11];    //准考证号
    int grade;      //成绩
};

bool cmp(student a, student b){//降序排列
    return a.grade > b.grade;
}

int main(){
    int n, low ,high;
    //读入所有的值
    scanf("%d",&n);
    student stu[n];
    for(int i = 0; i < n; ++i)  scanf("%s%s%d", stu[i].name, stu[i].id, &stu[i].grade);
    scanf("%d%d",&low,&high);
    sort(stu, stu + n, cmp);
    //初始化变量
    int i = 0;
    bool flag = true;

    while(stu[i].grade > high && i < n) i++;//找到第一个在范围内的元素
    while(stu[i].grade >= low && i < n){    //限制范围
        flag = false;
        printf("%s %s\n",stu[i].name, stu[i].id);
        i++;
    }

    if(flag) printf("NONE\n");
    
    return 0;
}

A 1080 Graduate Admission (30 分)

地址: A 1080 Graduate Admission (30 分)

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

struct student{
    int GE, GI, sum;//学生成绩
    int rank, id;      //学生id和排名
    int select[6];      //选择的志愿
}stu[40010];

struct School{
    int quota;      //招生额度
    int stuNum;     //当前招生人数
    int id[40010];  //招收的考生编号
    int lastAdmit;  //最后一个招收的编号
}sch[110];

bool cmp(student a, student b){//根据要求排序
    if(a.sum != b.sum)  return a.sum > b.sum;
    else return a.GE > b.GE;
}
bool cmpID(int a, int b){
    return stu[a].id < stu[b].id;
}

int main(){
    int n, k, m;
    scanf("%d%d%d", &n, &m, &k);

    for(int i = 0; i < m; ++i){
        scanf("%d", &sch[i].quota);//读入学校招生额度
        sch[i].lastAdmit = -1;
        sch[i].stuNum = 0;
    }

    for(int i = 0; i < n; ++i){                         //读入考生数据
        scanf("%d%d", &stu[i].GE, &stu[i].GI);
        stu[i].sum = stu[i].GE + stu[i].GI;
        stu[i].id = i;
        for(int j = 0; j < k; ++j)  scanf("%d",&stu[i].select[j]);
    }
    
    sort(stu, stu + n, cmp);    //排序确定名词
    for(int i = 0; i < n; ++i){
        if(i > 0&& stu[i].sum == stu[i - 1].sum && stu[i].GE == stu[i - 1].GE)  stu[i].rank = stu[i - 1].rank;
        else stu[i].rank = i;
    }

    for(int i = 0; i < n; ++i){ //对每个学生进行判定录取
        for(int j = 0; j < k; j++){
            int choice = stu[i].select[j];
            int num = sch[choice].stuNum;
            int last = sch[choice].lastAdmit;
            if(num < sch[choice].quota || (last != -1 && stu[i].rank == stu[last].rank)){//满足条件则录取
                sch[choice].id[num] = i;
                sch[choice].stuNum ++;
                sch[choice].lastAdmit = i;
                break;
            }
        }
    }

    for(int i = 0; i < m; i++){
        if(sch[i].stuNum){
            sort(sch[i].id, sch[i].id + sch[i].stuNum, cmpID);
            printf("%d",stu[sch[i].id[0]].id);
            for(int j = 1; j < sch[i].stuNum; j++)  printf(" %d",stu[sch[i].id[j]].id);
        }
        printf("\n");
    }

    return 0;   
}

1095 Cars on Campus (30 分)

地址: 1095 Cars on Campus (30 分)

#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 10010;
struct Car{
    char id[8];
    int time;
    char status[4];
}all[maxn],valid[maxn];
int  num = 0;
map<string, int> partTime;
int timeToInt(int hh,int mm,int ss){
    return hh * 3600 + mm * 60 + ss;//使用s做单位计时
}
bool cmp(Car a, Car b){ //按照id、时间排序;
    if(strcmp(a.id,b.id))    return strcmp(a.id,b.id) < 0;
    else return a.time < b.time;
}
bool cmptime(Car a, Car b){ //按照时间排序;
    return a.time < b.time;
}
int main(){
    int n,k,hh,mm,ss;
    scanf("%d%d",&n,&k);

    for(int i = 0;i < n; ++i){      //读入记录
        scanf("%s%d:%d:%d%s",all[i].id,&hh,&mm,&ss,all[i].status);
        all[i].time = timeToInt(hh,mm,ss);
    }
    sort(all, all + n,cmp);//排序

    int maxTime = -1;//最长停留时间
    for(int i = 1;i < n;++i){
        if(!strcmp(all[i].id,all[i - 1].id) && !strcmp(all[i - 1].status,"in") && !strcmp(all[i].status,"out")){//是否满足条件
            valid[num++] = all[i - 1];
            valid[num++] = all[i];
            int inTime = all[i].time - all[i - 1].time;
            if(partTime.count(all[i].id) == 0)  partTime[all[i].id] = 0;    //无记录插入记录
            partTime[all[i].id] += inTime;                                  //计算总停车时间
            if(partTime[all[i].id] > maxTime)   maxTime = partTime[all[i].id]; //记录最大停留时间
        }
    }

    sort(valid,valid + num,cmptime);
    int now = 0,numCar = 0;
    for(int i =0;i < k;++i){                //输出当前时刻的车数
        scanf("%d:%d:%d",&hh,&mm,&ss);
        int time = timeToInt(hh,mm,ss);
        while(now < num && valid[now].time <= time){
            if(!strcmp(valid[now].status,"in"))   numCar++;
            else   numCar--;
            now ++;
        }
        printf("%d\n",numCar);
    }

    map<string, int>::iterator it;
    for(it = partTime.begin();it != partTime.end();it ++){
        if(it->second == maxTime)   printf("%s ",it->first.c_str());
    }
    printf("%02d:%02d:%02d\n",maxTime/3600,maxTime%3600/60,maxTime%60);
    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XingleiGao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值