下面这是我让AI写的代码,后面接着我的代码,看着除了输入方式不一样之外,好像就没什么差别了,想不明白为什么我自己的代码AC不了,AI的却可以。。。。求大佬解答,感谢
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Student {
string name;
int id;
string className;
};
// 比较函数
bool compare(const Student& lhs, const Student& rhs) {
if (lhs.className != rhs.className) {
return lhs.className < rhs.className;
}
return lhs.id < rhs.id;
}
int main() {
// 优化 I/O 速度
ios::sync_with_stdio(false);
cin.tie(0);
int n;
// 尝试处理多组输入,防止 WA
while (cin >> n) {
vector<Student> students(n);
for (int i = 0; i < n; ++i) {
cin >> students[i].name >> students[i].id >> students[i].className;
}
sort(students.begin(), students.end(), compare);
for (const auto& s : students) {
cout << s.name << " " << s.id << " " << s.className << endl;
}
}
return 0;
}#include <cstdio>
#include "string"
#include "vector"
#include "algorithm"
using namespace std;
struct Student{
string name;
int id;
string className;
Student(char *_name, int _id, char *_className){
name = _name;
id = _id;
className = _className;
}
};
bool compare(Student lhs, Student rhs){
if(lhs.className < rhs.className) return true;
else if(lhs.className == rhs.className && lhs.id < rhs.id) return true;
else return false;
}
int main() {
vector<Student> students;
int n, id;
char buffer_name[50], buffer_className[50];
while (scanf("%d", &n) != EOF){
students.clear();
while (n --){
scanf("%s %d %s", buffer_name, &id, buffer_className);
students.emplace_back(buffer_name, id, buffer_className);
}
sort(students.begin(), students.end(), compare);
for(auto each: students){
printf("%s %d %s\n", each.name.c_str(), each.id, each.className.c_str());
}
}
return 0;
}
1 回复
0 转发
0 喜欢
13 阅读



