2093 考试排名
思考过程:在储存数据这一步出现了问题,因为对vector和string实在是不熟悉,然后一心想着用它们,忽略了问题。用什么数据结构来储存数据这是一个大问题,它直接影响处理数据的有效性。
学到的东西:
- 结构体
- sort()函数的自定义比较函数
- getline()和stringstream()
- 引用,还是没从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;
}