fork download
  1. /* IMPORTANT: Multiple classes and nested static classes are supported */
  2.  
  3. /*
  4.  * uncomment this if you want to read input.
  5. //imports for BufferedReader*/
  6. import java.io.BufferedReader;
  7. import java.io.InputStreamReader;
  8.  
  9. //import for Scanner and other utility classes
  10. import java.util.*;
  11.  
  12.  
  13. class TestClass {
  14. public static void main(String args[] ) throws Exception {
  15. /*
  16.   * Read input from stdin and provide input before running
  17.   * Use either of these methods for input
  18.  
  19.   //BufferedReader*/
  20. String line;
  21. Scanner s = new Scanner(System.in);
  22. int count,temp;
  23. int t = Integer.parseInt(br.readLine());
  24.  
  25. for(int i = 0; i<t; i++){
  26. int nod = Integer.parseInt(br.readLine());
  27. line = br.readLine();
  28. int[] tasks = new int[nod];
  29. int[] noo = new int[nod];
  30. StringTokenizer st = new StringTokenizer(line," ");
  31. for(int j = 0; j<nod; j++){
  32. tasks[j] = Integer.parseInt(st.nextToken());
  33. temp = tasks[j];
  34. count = 0;
  35. while(temp!=0){
  36. temp = temp & temp-1;
  37. count++;
  38. }
  39. noo[j] = count;
  40. }
  41. for(int b=0;b<nod;b++){
  42. System.out.print(noo[b]+" ");
  43. }
  44. System.out.println();
  45. for(int x=0; x<nod-1;x++){
  46. for(int y= 0;y<nod-1;y++){
  47. if(noo[y] > noo[y+1]){
  48. temp = noo[y];
  49. noo[y] = noo[y+1];
  50. noo[y+1] = temp;
  51.  
  52. temp = tasks[y];
  53. tasks[y] = tasks[y+1];
  54. tasks[y+1] = temp;
  55. }
  56. }
  57. }
  58. for(int z = 0;z<nod;z++){
  59. System.out.print(tasks[z]);
  60. if(z!=nod-1){
  61. System.out.print(" ");
  62. }
  63. }
  64. System.out.println();
  65. }
  66. /*
  67.   //Scanner
  68.   Scanner s = new Scanner(System.in);
  69.   int N = s.nextInt();
  70.  
  71.   for (int i = 0; i < N; i++) {
  72.   System.out.println("hello world");
  73.   }
  74.  
  75.  
  76.   System.out.println("Hello World!");*/
  77. }
  78. }
  79.  
Success #stdin #stdout 0.13s 54856KB
stdin
1
4
3 4 7 10
stdout
2 1 3 2 
4 3 10 7