Notes algorithms Chapter 4 Beginners (2) - algorithm preliminary study notes

4.1 Sorting

4.1.1 Selection Sort

Refers to a simple selection sort, for a sequence A of the element A [1] ~ [n] A, for i from 1 to n enumeration operation performed n times, each trip to be sorted from the portion [i, n] selected the smallest element, so that it is exchanged with the first portion of the element to be sorted a [i], so that the elements a [i] will be ordered with the current interval [1, i-1] in order to form a new interval [1 , i].

4.1.2 Insertion Sort

Refers to direct insertion sort, sequence A n elements A [1] ~ A [n], for i from 2 to n enumeration performed n-1 times operations. Assuming a trip, the front element of A i-1 sequence A [1] ~ A [i-1] has been ordered, the range [i, n] has not been ordered, then the trip from the range [1, after i-1] to find a position j, such that A [i] is inserted into a position j, the range [1, i] orderly.

4.1.3 sort of word problems and sort function

Directly in C ++ sort () function higher efficiency

1. How to use the sort () to sort

Using sort function must add header #include <algorithm> and using namespace std;

Used as follows:

sort (first element address (required), the next address (required) tail element address comparing function (optional))

Example:

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

 

The third optional parameter is sort of compare function, general writing cmp function, used to develop the collation to establish comparability

2. How to implement the comparison function cmp

Sorting (1) of an array of basic data types

If the comparison function does not fill, the default sort order in a large ascending. If you want to sort descending, you will have to use the comparison function cmp to "tell" when the sort to swap elements.

#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>
 the using  namespace STD;
 struct Student {
     char ID [ 20 is ];
     int final_rank; // final ranking 
    int location_number; // examination No. 
    int local_rank; // examination ranking 
    int Score; 
} STU [ 30010 ];
 BOOL CMP (Student A, Student B) {
     IF (! = a.score b.score) 
         return a.score> b.score;
     the 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;
}

 

 

Guess you like

Origin www.cnblogs.com/coderying/p/12233602.html