变异错误求救!!!(望大佬运行下看看)

P1223 排队接水

《变异错误》 1. 数组名不能和定义的结构体类型名重复 2. 你要比较的是结构体类型,但是你的比较函数比较的是 int
by Sprague_Garundy @ 2023-04-05 10:35:48


@[dongrq_cs](/user/644394) cmp写成了啥
by xiaoming007 @ 2023-04-05 10:36:15


@[Sprague_Garundy](/user/764746) 现在改成了这样 ```cpp #include <bits/stdc++.h> using namespace std; struct a{ int b,num; }q[1010]; bool cmp(a x,a y){ return x.b < y.num; } int main(){ int n,i,j; double t = 0; cin >> n; for(i = 1;i <= n;i++){ cin >> q[i].b; q[i].num = i; } sort(q + 1,q + n + 1,cmp); for(i = 1;i <= n;i++){ cout << q[i].num << " "; } cout << endl; for(j = n - 1;j >= 1;j--){ i = n - j; t += q[i].b * j; } cout << fixed << setprecision(2) << t; return 0; } ``` 听取WA声一片
by dongrq_cs @ 2023-04-05 11:05:36


@[dongrq_cs](/user/644394) 6
by aidsao @ 2023-04-05 13:08:10


《真·编译错误》
by aidsao @ 2023-04-05 13:08:52


```cpp #include<bits/stdc++.h> using namespace std; struct stu { int id,s; }a[1005]; bool cmp(stu a,stu b) { return a.s<b.s; } int main() { double ans=0; int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i].s; a[i].id=i; } sort(a+1,a+n+1,cmp); int j=n-1; for(int i=1;i<=n;i++) { cout<<a[i].id<<" "; ans+=a[i].s*j; j--; } cout<<endl; printf("%.2lf",ans/n); } ``` 这样写试试。
by luojunkai @ 2023-04-07 16:42:17


|