|
发表于 2022-12-1 10:31:02
|
显示全部楼层
本帖最后由 jhq999 于 2022-12-1 10:35 编辑
- #include<stdio.h>
- int main()
- {
- int n;
- scanf("%d",&n);
- int a[n],i,j,t,count=0,flag=1;
- for(i=0; i<n; i++)scanf("%d",&a[i]);
- for(i=0; i<n-1; i++)
- {
- flag=1;
- for(j=0; j<n-i-1; j++)
- {
- if(a[j]>a[j+1])
- {
- t=a[j];
- a[j]=a[j+1];
- a[j+1]=t;
- flag=0;
- count++;
- }
- }
- if(flag)break;
- }
- for(i=0; i<n; i++)printf("%d",a[i]);
- printf("\n%d\n",count);
- return 0;
- }
复制代码- 4
- 3 1 2 4
- 1234
- 2
- Process returned 0 (0x0) execution time : 11.623 s
- Press any key to continue.
- 4
- 2 1 3 4
- 1234
- 1
- Process returned 0 (0x0) execution time : 3.141 s
- Press any key to continue.
复制代码 |
|