首页 > 编程语言 > 详细

算法笔记 第4章 入门篇(2) --算法初步 学习笔记

时间:2020-01-25 23:48:57      阅读:142      评论:0      收藏:0      [点我收藏+]

4.1 排序

4.1.1 选择排序

简单选择排序是指,对一个序列A中的元素A[1] ~ A[n],令i从1到n枚举,进行n趟操作,每趟从待排序部分[i,n]中选择最小的元素,令其与待排序部分的第一个元素A[i]进行交换,这样元素A[i]就会与当前有序区间[1,i-1]形成新的有序区间[1,i]。

4.1.2 插入排序

直接插入排序是指,对序列A的n个元素A[1] ~ A[n],令i从2到n枚举,进行n-1趟操作。假设某一趟时,序列A的前i-1个元素A[1] ~ A[i-1]已经有序,而范围[i,n]还未有序,那么该趟从范围[1,i-1]中寻找某个位置j,使得将A[i]插入位置j后,范围[1,i]有序。

4.1.3 排序题与sort函数的应用

直接使用C++中的sort()函数,效率较高

1.如何使用sort()排序

sort函数的使用必须加上头文件#include<algorithm>和using namespace std;

使用方式如下:

sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(非必填))

示例:

#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int a[6] = {9,4,2,5,6,-1};
    sort(a,a+4);
    for(int i=0;i<6;i++){
        printf("%d ",a[i]);
    }
    printf("\n");
    sort(a,a+6);
    for(int i=0;i<6;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

技术分享图片

 

sort的第三个可选参数是compare函数,一般写作cmp函数,用来制定排序规则来建立可比性

2.如何实现比较函数cmp

(1)基本数据类型数组的排序

若比较函数不填,则默认按照从小到大的顺序排序。如果想要从大到小来排序,则要使用比较函数cmp来“告诉”sort何时要交换元素。

#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
    return a > b;
}
int main(){
    int a[] = {3,1,4,2};
    sort(a,a+4,cmp);
    for(int i=0;i<4;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

PAT A1025 PAT Ranking (25分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct student{
    char id[20];
    int final_rank; //最终排名 
    int location_number; //考场号 
    int local_rank; //考场排名 
    int score;
}stu[30010];
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;
    scanf("%d",&n);
    int count = 0;
    for(int i=1;i<=n;i++){
        int num;
        scanf("%d",&num);
        for(int j=0;j<num;j++){
            scanf("%s %d",stu[count].id,&stu[count].score);
            stu[count].location_number = i;
            count++;
        } 
        sort(stu+count-num,stu+count,cmp);
        int r=1;
        stu[count-num].local_rank = r;
        for(int j=count-num+1;j<count;j++){
            if(stu[j].score == stu[j-1].score){
                stu[j].local_rank = stu[j-1].local_rank;
                r++;
            }else{
                r++;
                stu[j].local_rank = r;
            }
        }
    }
    sort(stu,stu+count,cmp);
    int rank = 1;
    stu[0].final_rank = 1;
    for(int i=1;i<count;i++){
        if(stu[i].score == stu[i-1].score){
            stu[i].final_rank = stu[i-1].final_rank;
            rank++;
        }else{
            rank++;
            stu[i].final_rank = rank;
        }
    }
    printf("%d\n",count);
    for(int i=0;i<count;i++){
        printf("%s %d %d %d\n",stu[i].id,stu[i].final_rank,stu[i].location_number,stu[i].local_rank);
    }
    return 0;
}

PAT B1015/A1062 德才论 (25分)

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤),即考生总数;L(≥),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct student{
    char id[10];
    int de,cai,sum;
    int 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.de != b.de) 
        return a.de > b.de;
    else
        return strcmp(a.id,b.id) < 0;
}
int main(){
    int N,L,H;
    scanf("%d%d%d",&N,&L,&H);
    int m = N;
    for(int i=0;i<N;i++){
        scanf("%s %d %d",stu[i].id,&stu[i].de,&stu[i].cai);
        stu[i].sum = stu[i].de + stu[i].cai;
        if(stu[i].de < L || stu[i].cai < L){
            stu[i].flag = 5;
            m--;
        }else if(stu[i].de>=H && stu[i].cai >= H){
            stu[i].flag = 1;
        }else if(stu[i].de >= H && stu[i].cai < H){
            stu[i].flag = 2;
        }else if(stu[i].de >= stu[i].cai){
            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].de,stu[i].cai);
    }

    return 0;
}
PAT A1012 The Best Rank (25分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of CME and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of CM and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A C M E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999 

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int Rank[1000000][4]={0};
char course[4]={A,C,M,E};
struct Student{
    int id;
    int score[4];
}stu[2010];
int n,m,now;
bool cmp(Student a,Student b){
    return a.score[now] > b.score[now];
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;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] = round((stu[i].score[1] + stu[i].score[2] + stu[i].score[3])/3.0);
    }
    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].score[now] == stu[i-1].score[now]){
                Rank[stu[i].id][now] = Rank[stu[i-1].id][now];
            }else{
                Rank[stu[i].id][now] = i + 1;
            }
        }    
    }
    int query;
    for(int i=0;i<m;i++){
        scanf("%d",&query);
        if(Rank[query][0]==0){
            printf("N/A\n");
        }else{
            int k=0;
            for(int j=0;j<4;j++){
                if(Rank[query][j] < Rank[query][k]){
                    k = j;
                }
            }
            printf("%d %c\n",Rank[query][k],course[k]);
        }
    }
    return 0;
}

 

 

算法笔记 第4章 入门篇(2) --算法初步 学习笔记

原文:https://www.cnblogs.com/coderying/p/12233602.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!