我好困惑啊 下面是我写的代码:
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
struct people {
int number;
int time;
int wating;
bool operator < (const people& other) const {
return time < other.time;
}
};
struct people p[1000];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
p[i].number = i + 1;
cin >> p[i].time;
}
sort(p, p + n);
for (int i = 0; i < n; i++)
{
cout << p[i].number << ' ';
}
cout << endl;
int sum = 0;
for (int i = 0; i < n; i++)
{
p[i].wating = 0;
for (int j = i - 1; j >= 0; j--)
{
p[i].wating += p[j].time;
}
}
for (int i = 0; i < n; i++)
sum += p[i].wating;
double avg = (double)sum / n;
cout << setiosflags(ios::fixed) << setprecision(2) << avg << endl;
return 0;
}
这个代码是未通过的,有两个错,下面是gpt帮我修改的代码:
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
struct person {
int number;
int time;
};
bool compareTime(const person& a, const person& b) {
return a.time < b.time;
}
int main() {
int n;
cin >> n;
person p[1000];
for (int i = 0; i < n; i++) {
p[i].number = i + 1;
cin >> p[i].time;
}
sort(p, p + n, compareTime);
for (int i = 0; i < n; i++) {
cout << p[i].number << ' ';
}
cout << endl;
double sum = 0.0;
double waitingTime = 0.0;
for (int i = 0; i < n; i++) {
sum += waitingTime;
waitingTime += p[i].time;
}
double avg = sum / n;
cout << setiosflags(ios::fixed) << setprecision(2) << avg << endl;
return 0;
}
这个代码是可以通过的 为什么这两个代码结果不一样啊,离谱