Last active
April 17, 2019 08:32
-
-
Save a1exlism/f5e3affd5fd3f68125b09a7b897ca9b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstring> | |
#include <algorithm> | |
using namespace std; | |
const int NUM = 20; | |
typedef struct { | |
char ID[20]; | |
int grade; | |
int rank; | |
} Stu; | |
bool cmp(Stu a, Stu b); | |
int main() | |
{ | |
int arr[NUM] = {73, 68, 80, 85, 73, 74, 33, 49, 74, 90, 23, 22, 1, 0, 57, 11, 27, 35, 60, 45}; | |
Stu stu[NUM]; | |
for(int i = 0; i < NUM; i++) { | |
sprintf(stu[i].ID, "Std_NO.%03d", i+1); | |
stu[i].grade = arr[i]; | |
} | |
// Ranking RULES: same grade for same rank | |
sort(stu, stu+NUM, cmp); | |
// * init | |
int duplicate_num = 0; | |
stu[0].rank = 1; | |
for(int i = 1; i < NUM; i++) { | |
if(stu[i].grade == stu[i-1].grade) { | |
stu[i].rank = stu[i-1].rank; | |
duplicate_num ++; | |
} else { | |
stu[i].rank = stu[i-1].rank + 1 + duplicate_num; // Next | |
duplicate_num = 0; // RE-init | |
} | |
} | |
for(int i = 0; i < NUM; i++) { | |
printf("No: %2d | ID: %s | Grades: %2d | Rank: %d\n", i+1, stu[i].ID, stu[i].grade, stu[i].rank); | |
} | |
return 0; | |
} | |
// as front.grade > later.grade ORDER | |
bool cmp(Stu a, Stu b) { | |
return a.grade > b.grade; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int arr[] = {73, 68, 80, 85, 73, 74, 33, 49, 74, 90, 23, 22, 1, 0, 57, 11, 27, 35, 60, 45}; | |
int len = sizeof(arr) / sizeof(arr[0]); | |
sort(arr, arr+len, compare); | |
// SET the purpose condition. | |
bool compare(int front, int end) { | |
return front > end; // DESC | |
return front < end; // ASC | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment