<C/C++> 成绩排序

题目描述

查找和排序

题目:

输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩都按先录入排列在前的规则处理。

例示:

jack 70
peter 96
Tom 70
smith 67

从高到低 成绩

peter 96
jack 70
Tom 70
smith 67

从低到高

smith 67
jack 70
Tom 70
peter 96

注:0代表从高到低,1代表从低到高

本题含有多组输入数据!
输入描述:

输入多行,先输入要排序的人的个数,然后分别输入他们的名字和成绩,以一个空格隔开

输出描述:

按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开

示例1
输入

3
0
fang 90
yang 50
ning 70

输出

fang 90
ning 70
yang 50

C++解法

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;
#define NAME_LENGTH     50
struct StudentScore {
    char name[NAME_LENGTH];
    int score;
};
 
void swap(struct StudentScore**array, int first, int second) {
    struct StudentScore* tmp = array[second];
    array[second] = array[first];
    array[first] = tmp;
}
int compareScore(struct StudentScore *first, struct StudentScore *second) {
    return first->score - second->score;
}
void selectionSort(struct StudentScore** array, int size, int order) {
    
    for (int i = size - 1; i >= 0; i--) {
    	int swapIndex = i;
        for (int j = 0; j < i; j++) {
            if (order == 0) {
                if (compareScore(array[j], array[swapIndex]) < 0) {
                    swapIndex = j;
                }
            }
            else {
                if (compareScore(array[j], array[swapIndex]) > 0) {
                    swapIndex = j;
                }
            }
        }
        if (swapIndex != i) {
        	swap(array, swapIndex, i);
        }
    }
}

struct StudentScore * getStudentScore(string input) {
    int size = input.length();
    char single;
    char buffer[NAME_LENGTH] = {0};
    char bufferIndex = 0;
    int valueIndex = 0;
    struct StudentScore *ss = new struct StudentScore();
    for (int i = 0; i <= size; i++) {
        if (i == size) {
            single = ' ';
        } else {
            single = input.at(i);
        }
        if (single == ' ') {
            if (valueIndex == 0) {
                memcpy(ss->name, buffer, bufferIndex);
            } else {
                ss->score = atoi(buffer);
            }
            valueIndex++;
            bufferIndex = 0;
            memset(buffer, 0, NAME_LENGTH);
        } else {
            buffer[bufferIndex++] = single;
        }
    }  
    return ss;
}

void printScores(struct StudentScore **scores, int size) {
    for (int i = 0; i < size; i++) {
        cout << scores[i]->name << " " << scores[i]->score << endl;
    }
}

int main()
{
    string input;
    vector<string> params;
    while (getline(cin, input)) {
        params.push_back(input);
    }
    int size = atoi(params.at(0).c_str());
    int order = atoi(params.at(1).c_str());
    struct StudentScore **scores = new struct StudentScore*[size];
    for (int i = 0; i < size; i++) {
        scores[i] = getStudentScore(params.at(2+i));
    }
    selectionSort(scores, size, order);
    printScores(scores, size);
    
    for (int i = 0; i < size; i++) {
        delete scores[i];
    }
    delete scores;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值