求大佬帮忙

P1116 车厢重组

代码就是过不了,每次都是WA
by You_Yang @ 2023-05-20 19:09:26


for (int j = 0;j < N-i-1;j++)
by 2044_space_elevator @ 2023-05-20 19:31:14


@[Ruiyouyang](/user/766810) 改成这样 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int N,a = 0; cin>>N; int n[N]; for (int i = 0;i < N;i++) cin>>n[i]; for (int i = 0;i < N;i++) for (int j = 0;j < N-i-1;j++) if (n[j] > n[j+1]){ swap(n[j],n[j+1]); a++; } cout<<a; return 0; } ```
by 2044_space_elevator @ 2023-05-20 19:31:49


我 * 你 * * 你个 * * 这都不会 ``` #include<iostream> using namespace std; int main() { int n, ans = 0; cin >> n; int* a = new int[N]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (a[j] > a[j+1]) { int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; ans++; } } } cout << ans << endl; return 0; } ```
by 303YYDS @ 2023-05-20 20:00:41


``` #include <iostream> using namespace std; int n, sum; int main() { cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) for (int j = 0; j < i; ++j) if (a[j] > a[i]) ++sum; cout << sum; return 0; }
by zhanlinchuan @ 2023-05-20 20:22:20


@[2044_space_elevator](/user/824363) 可以麻烦大佬讲一下为什么要在N-i后再-1吗?
by You_Yang @ 2023-05-24 09:58:19


@[RuiYouyang](/user/766810) 因为你索引是从0开始的。
by 2044_space_elevator @ 2023-05-24 11:23:57


|