B1004 成绩排名

[原题链接]https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448

emmm,这题比较简单,也没啥好说的,字符串掌握的不好 主要是总结一下各种输入方法吧
我用的结构体(其实也有点需要注意的地方 name ID的大小必须至少是11要为\0留出空间 总之往大里设置就好)
第一种:

#include <iostream>
#include <cstdio>
const int Maxnum=10000;
struct Student
{
    char name[15];   //字符串存储格式
    char ID[15];
    int score;
}Student[10000];

int main()
{
    int n,i=0;
    int score,Minscore=100,Maxscore=-1;
    int min=0,max=0;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s%s%d",Student[i].name,Student[i].ID,&Student[i].score);
        if(Student[i].score>Maxscore)
        {
            Maxscore=Student[i].score;
            max=i;
        }
        if(Student[i].score<Minscore)
        {
            Minscore=Student[i].score;
            min=i;
        }
        i++;
    }
    printf("%s %s\n",Student[max].name,Student[max].ID);
    printf("%s %s",Student[min].name,Student[min].ID);
    return 0;
}

第二种:

const int Maxnum=10000;
struct Student
{
    char name[15];   //字符串存储格式
    char ID[15];
    int score;
};
int main()
{
        struct Student Stu[Maxnum];
        scanf("%s%s%d",Stu[i].name,Stu[i].ID,&Stu[i].score);
}

第三种:


typedef struct{
    char name[15];
    char num[15];
    int score;
} Student;                     //Student 现在就变成了一种类型 类似int,double这种

int main()
{
    Student stu[n];
     scanf("%s%s%d",stu[i].name,stu[i].num,&stu[i].score);
}

第四种:c++ string输入(学习大神 原贴:https://www.liuchuo.net/archives/516


#include <iostream>
using namespace std;
int main() {
    int n, max = -1, min = 101, score;
    cin >> n;
    string maxname, minname, maxnum, minnum, name, num;
    for (int i = 0; i < n; i++) {
        cin >> name >> num >> score;
        if (max < score) {
            max = score;
            maxname = name;
            maxnum = num;
        }
        if (min > score) {
            min = score;
            minname = name;
            minnum = num;
        }
    }
    cout << maxname << " " << maxnum << endl << minname << " " << minnum;
    return 0;
}

hhh 大神代码好好看 字符串用c++输入输出效率好高学到了学到了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moumoumouwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值