fork download
  1. #include<stdio.h>
  2. int n,N,i,j,p,q,t,s,a[51];
  3. int main()
  4. {
  5. scanf("%d",&N); getchar();
  6. for(i=1;i<=N;i++)
  7. {
  8. s=0;
  9. scanf("%d",&n); getchar();
  10. for(j=1;j<=n;j++)
  11. {
  12. scanf("%d",&a[j]);
  13. } getchar(); a[n+1]='\0';
  14. for(j=1;j<=n-1;j++) /// bubble sort....
  15. {
  16. for(p=1;p<=n-1;p++)
  17. {
  18. if(a[p]>a[p+1])
  19. {
  20.  
  21. t=a[p+1]; a[p+1]=a[p]; a[p]=t; s++;
  22. }
  23. }
  24.  
  25. }
  26. printf("Optimal train swapping takes %d swaps.\n",s);
  27.  
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
Standard output is empty