HDOJ2093考试排名笔记

本文深入探讨了使用C++处理考试排名数据的方法,包括结构体的应用、自定义比较函数的实现以及getline()和stringstream()的使用技巧。通过对比两种不同的数据读取方式,阐述了其对程序效率的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2093 考试排名
思考过程:在储存数据这一步出现了问题,因为对vector和string实在是不熟悉,然后一心想着用它们,忽略了问题。用什么数据结构来储存数据这是一个大问题,它直接影响处理数据的有效性。
学到的东西:

  1. 结构体
  2. sort()函数的自定义比较函数
  3. getline()和stringstream()
  4. 引用,还是没从c转到cpp
    有利用结构体,sort()函数中的自定义比较函数,如果用getline()搭配上stringstream读取一行数据后,变成流,即便时示例正确,也会WA,不知为何,引用
    贴上利用getline()和stringstream()这个是错误的,但是示例正确,如果有哪位朋友恰巧读到了,请评论告诉我为什么错,无限感激
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
struct Student {
	string name;
	int get = 0, time = 0;
}student[10000];
void S_I(string s, Student *student, int m) { //string转int
	//cout << s << endl;
	int sum = 0;                               //总耗时
	int error = 0;                             //错误次数
	for(unsigned int i = 0; i < s.size(); i++) {
		if(s[i] == '(') {                     //碰到 ( ,就i++
			i++;
			while(s[i] != ')') {
				error = error * 10 + s[i] - '0';    //加上次数
				i++;
			}
			break;                            //括号完就结束了,可以不加break
		}
		sum = sum * 10 + s[i] - '0';
	}
	//cout << sum << " " << error << endl;
	(*student).get++;
	(*student).time = (*student).time + sum + error * m;
	//cout << (*student).time << endl;
}
bool com(const Student &a, const Student &b)   //自定义比较函数,sort()函数
{
    if (a.get != b.get)
        return a.get > b.get;
    else
        if (a.time!= b.time)
            return a.time < b.time;
        else
            return a.name <= b.name;
}

int main() {
	int n, m, index = 0;
	cin >> n >> m;
	string line;
	getline(cin, line);  //因为读取数字后,如果不加这句,第一个会读取紧跟第一行数字n, m 空白行
	while(getline(cin, line)) {
		stringstream ss(line);
		string s;
		bool c = 0;
		while(ss >> s) {
			//cout << s << endl;
			if(c == 0) {
				student[index].name = s;
				c = 1;
				continue;
			}
			if(s[0] == '-' || s[0] == '0') {   //遇到0或-直接跳,直到遇到正数
				continue;
			}
			S_I(s, &student[index], m); 
		}
		//cout << index << " s " << endl;
		index++;
	}
	
	sort(student, student + index, com);
	for(int i = 0; i < index; i++)	 {
		cout << left << setw(10) << student[i].name << " ";
		cout << right << setw(2) << student[i].get << " ";
		cout << right << setw(4) << student[i].time << endl;
	}
	return 0;
}

这个代码是正确的,不用getline() 和stringstream()读取数据

#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
struct Student {
    string name;
    int get = 0, time = 0;
}student[10000];
void S_I(string s, Student *student, int m) {
    (*student).get++;
    //cout << s << endl;
    int sum = 0;
    int error = 0;
    for(unsigned int i = 0; i < s.size(); i++) {
        if(s[i] == '(') {
            i++;
            while(s[i] != ')') {
                error = error * 10 + s[i] - '0';
                i++;
            }
            break;
        }
        sum = sum * 10 + s[i] - '0';
    }
    //cout << sum << " " << error << endl;
    (*student).time = (*student).time + sum + error * m;
    //cout << (*student).time << endl;
}
bool com(const Student &a, const Student &b)
{
    if (a.get != b.get)
        return a.get > b.get;
    else
        if (a.time!= b.time)
            return a.time < b.time;
        else
            return a.name <= b.name;
}
int main() {
    int n, m, index = 0;
    cin >> n >> m;
    string ss;
    int tem = n;
    while(cin >> ss) {
        student[index].name = ss;
        string t;
        while(tem--) {
            cin >> t;
            //cout << t << endl;
            if(t[0] == '0' || t[0] == '-') {
                continue;
            } 
            S_I(t, &student[index], m);
        }
        index++;
        tem = n;
    }
    sort(student, student + index, com);
    for(int i = 0; i < index; i++)  {
        cout << left << setw(10) << student[i].name << " ";
        cout << right << setw(2) << student[i].get << " ";
        cout << right << setw(4) << student[i].time << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Greatljc

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值