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

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

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


🧑🏻作者简介:一个从工业设计改行学嵌入式的年轻人

✨联系方式:2201891280(QQ)

📔源码地址:https://gitee.com/xingleigao/algorithm-notes

⏳全文大约阅读时间: 30min


全文目录

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

问题 A: 排序

问题 B: 特殊排序

问题 C: EXCEL排序

问题 D: 字符串内排序

问题 E: Problem B

问题 F: 小白鼠排队

问题 G: 中位数

问题 H: 整数奇偶排序

问题 I: 排名

A1062 Talent and Virtue (25 分)

A1012 The Best Rank (25 分)

A1025 PAT Ranking (25 分)

A 1028 List Sorting (25 分)

A 1055 The World's Richest (25 分)

A 1083 List Grades (25 分)

A 1080 Graduate Admission (30 分)

1095 Cars on Campus (30 分)

🍕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;
}



相关文章
|
25天前
|
算法 调度
【软件设计师备考 专题 】算法探索:排序、查找、数值计算和字符串处理(二)
【软件设计师备考 专题 】算法探索:排序、查找、数值计算和字符串处理
32 0
|
20天前
|
存储 搜索推荐 算法
【数据结构】八大排序之计数排序算法
【数据结构】八大排序之计数排序算法
11 4
|
20天前
|
搜索推荐 算法
【数据结构】八大排序之归并排序算法
【数据结构】八大排序之归并排序算法
20 5
|
20天前
|
搜索推荐 算法 编译器
【数据结构】八大排序之快速排序算法
【数据结构】八大排序之快速排序算法
35 4
|
22天前
|
算法 Python
数据结构与算法 经典排序方法(Python)
数据结构与算法 经典排序方法(Python)
24 0
|
23天前
|
存储 算法 搜索推荐
【算法】七大经典排序(插入,选择,冒泡,希尔,堆,快速,归并)(含可视化算法动图,清晰易懂,零基础入门)
【算法】七大经典排序(插入,选择,冒泡,希尔,堆,快速,归并)(含可视化算法动图,清晰易懂,零基础入门)
|
25天前
|
存储 算法 搜索推荐
【C++ 数据结构与算法 一站式备考指南】一文掌握 数据结构与算法课程 知识点(二)
【C++ 数据结构与算法 一站式备考指南】一文掌握 数据结构与算法课程 知识点
91 2
|
25天前
|
存储 算法 C++
【C++ 数据结构与算法 一站式备考指南】一文掌握 数据结构与算法课程 知识点(一)
【C++ 数据结构与算法 一站式备考指南】一文掌握 数据结构与算法课程 知识点
49 2
|
25天前
|
存储 算法
【软件设计师备考 专题 】算法探索:排序、查找、数值计算和字符串处理(三)
【软件设计师备考 专题 】算法探索:排序、查找、数值计算和字符串处理
27 0
|
25天前
|
存储 算法 搜索推荐
【软件设计师备考 专题 】算法探索:排序、查找、数值计算和字符串处理(一)
【软件设计师备考 专题 】算法探索:排序、查找、数值计算和字符串处理
106 0