鱼C论坛

 找回密码
 立即注册
查看: 3295|回复: 6

求求帮忙看看

[复制链接]
发表于 2022-10-22 21:23:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
姓名     高数    线代
张飞      78      75
李大刀   92      67
李墨白   84      88
王老虎   50      50
雷小米   99      98
编写程序按照总分从高到低进行排序后输出姓名和成绩

怎么不引用太多函数完成啊,新生上路,最简单基础的做法就好,谢谢大佬了谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-22 21:25:40 | 显示全部楼层
你的数据输入形式是什么?手动输入?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-22 21:35:35 | 显示全部楼层
suchocolate 发表于 2022-10-22 21:25
你的数据输入形式是什么?手动输入?


      他所有的作业都是靠求的,他要是能回答还用求别人?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-22 22:04:48 | 显示全部楼层
这题不难,Python 的话可以自己先做做看。

借用你的题,我练习练习 C
#include <stdio.h>
#include <stdlib.h>

typedef struct Person {
    char* name;
    unsigned algebra, lineage;
}Person;

int compare(const void * a, const void * b) {
    Person x = *(Person*)a, y = *(Person*)b;
    return (y.algebra + y.lineage) - (x.algebra + x.lineage);
}

int main(void)
{
    Person arr[5] = {
        {"张飞", 78, 75},
        {"李大刀", 92, 67},
        {"李墨白", 84, 88},
        {"王老虎", 50, 50},
        {"雷小米", 99, 98}
    };
    
    qsort(arr, 5, sizeof(Person), compare);
    
    for (int i = 0; i < 5; ++i) {
        printf("%s %u %u\n", arr[i].name, arr[i].algebra, arr[i].lineage);
    }

    return 0;
}
雷小米 99 98
李墨白 84 88
李大刀 92 67
张飞 78 75
王老虎 50 50
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-22 23:31:48 | 显示全部楼层
傻眼貓咪 发表于 2022-10-22 22:04
这题不难,Python 的话可以自己先做做看。

借用你的题,我练习练习 C

厉害呀,Python大蟒蛇都玩熟了,开始玩C草履虫了,哈哈哈  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-23 04:48:22 | 显示全部楼层
我也参和一下:
import pandas as pd

k = pd.DataFrame({'姓名':["张飞","李大刀","李墨白",'王老虎','雷小米'],
               '高数':[78,92,84,50,99],
               '线代':[75,67,88,50,98]})

print(k)
print('=========')
print(k.loc[((k['高数']+k['线代'])*-1).sort_values().index])
    姓名  高数  线代
0   张飞  78  75
1  李大刀  92  67
2  李墨白  84  88
3  王老虎  50  50
4  雷小米  99  98
=========
    姓名  高数  线代
4  雷小米  99  98
2  李墨白  84  88
1  李大刀  92  67
0   张飞  78  75
3  王老虎  50  50
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-27 14:53:59 | 显示全部楼层
那我就练习练习C++了
sh-5.1$ cat main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>

using std::vector, std::pair;
using std::string;
using std::sort, std::accumulate;
using std::cout, std::endl;
using std::ostream;

ostream &operator<<(ostream &os, const vector<double> &vd) {
    bool flag = false;
    for(const auto &i: vd) {
        if(flag) os << ' ';
        flag = true;
        os << i;
    }
    return os;
}

ostream &operator<<(ostream &os, const vector<pair<string, vector<double>>> &vp) {
    bool flag = false;
    for(const auto &i: vp) {
        if(flag) os << endl;
        flag = true;
        os << i.first << ": " << i.second;
    }
    return os;
}

int main() {
    vector<pair<string, vector<double>>> vp = {
        {"张飞", {78, 75}},
        {"李大刀", {92, 67}},
        {"李墨白", {84, 88}},
        {"王老虎", {50, 50}},
        {"雷小米", {99, 98}}
    };
    sort(vp.begin(), vp.end(), [](const pair<string, vector<double>> &a, const pair<string, vector<double>> &b) -> bool {
        return accumulate(a.second.begin(), a.second.end(), 0) > accumulate(b.second.begin(), b.second.end(), 0);
    });
    cout << vp << endl;
    return 0;
}
sh-5.1$ g++ -g -Wall -o main main.cpp
sh-5.1$ ./main
雷小米: 99 98
李墨白: 84 88
李大刀: 92 67
张飞: 78 75
王老虎: 50 50
sh-5.1$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 21:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表