您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页<C/C++> 成绩排序

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

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务