求助

P1116 车厢重组

@[SDUTxph](/user/882363) 每一次 `i` 循环你只交换了两节车厢的位置,但 `cnt` 却加了不止一次,要把交换放进 `if` 语句里,且你 `if` 语句后有多个操作但没加括号
by Jason12 @ 2022-11-17 10:11:29


@[SDUTxph](/user/882363) ```cpp #include<stdlib.h> #include<stdio.h> int main() { int N; int i,j,k,f=1; int cnt=0; int a[10000]; scanf("%d",&N); for(i=0;i<N;i++) { scanf("%d",&a[i]); } for(i=0;i<N-1;i++) { for(j=0;j<N-1-i;j++) { if(a[j]>a[j+1]) { cnt++; f=0; int t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } if(f==1)break; } printf("%d",cnt); } ```
by Jason12 @ 2022-11-17 10:14:24


- 多谢多谢
by SDUTxph @ 2022-11-19 21:09:49


|