1004 成绩排名 (20 分)

1004 成绩排名 (20 分)

读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:

每个测试输入包含 1 个测试用例,格式为

第 1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
  ... ... ...
第 n+1 行:第 n 个学生的姓名 学号 成绩

其中姓名学号均为不超过 10 个字符的字符串,成绩为 0 到 100 之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式:

对每个测试用例输出 2 行,第 1 行是成绩最高学生的姓名和学号,第 2 行是成绩最低学生的姓名和学号,字符串间有 1 空格。

输入样例:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出样例:

Mike CS991301
Joe Math990112

 

思路:

把前面的姓名、学号放一堆

弄到字符串数组中

然后把分数另外判别一下

 

算法:

① 字符串数组 string str[n][15]

// n是人数,n个人,n个info

// 15是总信息的长度

 

② 设置max,min

记录最高分、最低分的是第几个人

最后按max,min去字符串数组中提取相应info

 

③ 分离 info 和 score 的判别方式

空格数量!

第二个空格之后才是score

 

尝试AC

 

(一)

#include<iostream>
#include<string>
using namespace std;

int main() {
    int n(0), score(0);
    // record max.position && min.position
    int max(0), min(0);
    cin>>n;
    // string array, line n, n*15
    string str[n][15];

    while(n--) {
        // sum of ' '
        int flat(0);
        cin>>score;
        for(int i=1; i<n; i++) {
            // cin info
            if(flat!=2) {
                cin>> *str[i];
            } else {
                cin>>score;
                if(score>max) {
                    max++;
                    continue;
                }
                if(score<min) {
                    min++;
                    continue;
                }
                else break;
            }
            cout<< str[max][15] <<endl << str[min][15];
            return 0;

        }
    }

}

好像跑不出答案,逻辑有问题

 

(二)

我发现,数space的逻辑有问题

我得先把串录入,才能后续统计space num

所以换了思路

用char数组先把各项info按顺序录入

然后对score进行循环比较

#include<iostream>
#include<string>
#include<stack>
using namespace std;

int main() {
    int n(0);
    int max(0), min(0);
    cin>>n;

    char name[n][15];
    char id[n][15];
    int score[n];

     for(int i=0; i<n; i++) {
            cin>>name[i];
            cin>>id[i];
            cin>>score[i];

        }

        for(int i=0; i<n; i++) {
            if(score[i]>max) {
                max= i;
                i++;
                continue;
            }
            if(score[i]<min) {
                min= i;
                i++;
                continue;
            } else
                break;
        }



    cout<< name[max] << ' '<< id[max]<<endl;
    cout<< name[min] << ' '<< id[min]<<endl;
    return 0;

}

 

结果如下

这次是排序出了问题

 

(三)

删删改改

#include<iostream>
#include<string>
#include<stack>
using namespace std;

int main() {
    int n(0);
    int max(0), min(0);
    //record the position of max & min
    int max_pst(0),min_pst(0);
    cin>>n;

    char name[n][15];
    char id[n][15];
    int score[n];

     for(int i=0; i<n; i++) {
            cin>>name[i];
            cin>>id[i];
            cin>>score[i];

        }

        for(int i=0; i<n; i++) {
            //score's scope condition
            if(score[i]>max) {
                max=score[i];
                max_pst=i;
//                i++;
//                continue;
            }
            if(score[i]< min) {
                min=score[i];
                min_pst=i;
//                i++;
//                continue;
            }
            
//           我的妈啊啊,就是这两行狗东西!!!
//           让我来来回回看了三四遍!!
//              else
//                break;
        }



    cout<< name[max_pst] << ' '<< id[max_pst]<<endl;
    cout<< name[min_pst] << ' '<< id[min_pst]<<endl;
    return 0;

}

 

怀着即将AC的笃定

迎来了WA

有一个测试用例卡住了(上一次是全错)

 

(三)

这次完全没有思路啊

到底为什么卡住了??

于是选择了场外求助

szx说我这样是找不到min的,要把min初始化成101

我觉得很有道理

改了之后,迎来了AC

#include<iostream>
#include<string>

using namespace std;

int main() {
    int n(0);
    int max(0), min(101);
    //record the position of max & min
    int max_pst(0),min_pst(0);
    cin>>n;

    char name[n][15];
    char id[n][15];
    int score[n];

     for(int i=0; i<n; i++) {
            cin>>name[i];
            cin>>id[i];
            cin>>score[i];

        }

        for(int i=0; i<n; i++) {
            //score's scope condition
            if(score[i]>max) {
                max=score[i];
                max_pst=i;
//                i++;
//                continue;
            }
            if(score[i]< min) {
                min=score[i];
                min_pst=i;
//                i++;
//                continue;
            }

//           我的妈啊啊,就是这两行狗东西!!!
//           让我来来回回看了三四遍!!
//              else
//                break;
        }



    cout<< name[max_pst] << ' '<< id[max_pst]<<endl;
    cout<< name[min_pst] << ' '<< id[min_pst]<<endl;
    return 0;

}

 

 

其他:

看到有人用了结构体,很好的思路

于是尝试了一波!

(好吧,事实证明我尝试了好几波=-=)

#include<iostream>
#include<string>
using namespace std;

struct student {
    string name, id;
    int score;
};

int main() {
    int n;
    int max(-1), min(101);
    int max_pst(0), min_pst(0);

    cin>> n;
    student stu[n];
    for(int i=0; i<n; i++) {
        cin>> stu[i].name >> stu[i].id >> stu[i].score;
        if(stu[i].score > max) {
            max = stu[i].score;
            max_pst=i;
        }
        if(stu[i].score < min) {
            min = stu[i].score;
            min_pst =i;
        }

    }

    cout << stu[max_pst].name <<' '<< stu[max_pst].id << endl;
    cout << stu[min_pst].name <<' '<< stu[min_pst].id << endl;
    return 0;

}

 

总结:

① max(0),min(101)  的设置

这是个大坑啊!

我掉进去好几次,傻傻分不清楚

易错点!!

有的人明明叫max,实际上却小的很…

 

② struct的灵活应用

 

③ 胆大心细

第二轮用结构体的时候

好几次都是部分正确,只给了2

说我巴拉巴拉一堆错,答案错,格式错

后来AC之后回头想想,其实都是小错误,小警告

你的代码并没有看起来那样错得无法挽救一塌糊涂

事实上,当初报一堆错的时候,我也只是输出的时候少加了space

外加 max,min的赋值搞混了而已

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值