浙江省高考成绩排名程序 - 快速排序算法
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
typedef struct Node {
int score;
long long id;
int rank;
int same_rank;
struct Node* next;
} Node, * Nodelink;
Node* createNode(long long id, int score) {
Node* newNode = new Node;
newNode->id = id;
newNode->score = score;
newNode->rank = 0;
newNode->same_rank = 1;
newNode->next = NULL;
return newNode;
}
void quicksort(Nodelink head, Nodelink end) {
if (head == end)
return;
Nodelink ret = head;
Nodelink x = head->next;
Nodelink px = head;
Nodelink z = head;
Nodelink p;
while (x != end) {
if (x->score < z->score || (x->score == z->score && x->id < z->id)) {
p = ret->next;
ret->next = px->next;
px->next->next = p;
px->next = x;
}
else {
px = px->next;
}
x = x->next;
}
ret->next = quicksort(ret->next, z);
z->next = quicksort(z->next, end);
}
int main() {
ifstream ifs('C:\\Users\\86139\\Desktop\\P03_TextData500.in');
if (!ifs.is_open()) {
cout << 'Failed to open input file' << endl;
return 1;
}
ofstream ofs('C:\\Users\\86139\\Desktop\\P03_TextData500.txt');
if (!ofs.is_open()) {
cout << 'Failed to open output file' << endl;
return 1;
}
Nodelink ret = createNode(0, 0);
Nodelink head = ret;
long long id;
int score;
int count = 0;
while (ifs >> id && ifs >> score) {
Nodelink newNode = createNode(id, score);
head->next = newNode;
head = newNode;
count++;
}
ifs.close();
quicksort(ret->next, NULL);
head = ret->next;
int rank = 1;
int same_rank = 1;
while (head) {
head->rank = rank;
ofs << head->id << ' ' << head->score << ' ' << head->rank << ' ' << head->same_rank << endl;
rank++;
if (head->next && (head->score != head->next->score || head->id != head->next->id)) {
head->same_rank = same_rank;
same_rank = 1;
}
else {
same_rank++;
}
head = head->next;
}
ofs.close();
return 0;
}
原文地址: https://www.cveoy.top/t/topic/cqOn 著作权归作者所有。请勿转载和采集!