fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. void swap(int a[100],int i,int j)
  4. {
  5. int t=a[i];
  6. a[i]=a[j];
  7. a[j]=t;
  8. }
  9. int mycompare(int x,int y)
  10. {
  11. char s1[10],s2[10];
  12. sprintf(s1,"%d",x);
  13. sprintf(s2,"%d",y);
  14. char t[10];
  15. strcpy(t,s1);
  16. strcat(t,s2);
  17. strcat(s2,s1);
  18. if(strcmp(s2,t)>0)
  19. return 1;
  20. return 0;
  21. }
  22. int main()
  23. {
  24. int T;
  25. scanf("%d",&T);
  26. while(T--)
  27. {
  28. int n,a[100],i,j,k;
  29. scanf("%d",&n);
  30. for(i=0;i<n;i++)
  31. scanf("%d",&a[i]);
  32. for(i=0;i<n;i++)
  33. {
  34. for(j=0;j<n-1;j++)
  35. {
  36. if(mycompare(a[j],a[j+1]))
  37. swap(a,j,j+1);
  38. }
  39. for(k=0;k<n;k++)
  40. printf("%d ",a[k]);
  41. printf("\n");
  42. }
  43. for(i=0;i<n;i++)
  44. printf("%d ",a[i]);
  45. printf("\n");
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 9432KB
stdin
2
5
3 30 34 5 9
4
54 546 548 60
stdout
3 34 5 9 30 
34 5 9 3 30 
5 9 34 3 30 
9 5 34 3 30 
9 5 34 3 30 
9 5 34 3 30 
546 548 60 54 
548 60 546 54 
60 548 546 54 
60 548 546 54 
60 548 546 54