Advertisement
Guest User

GreedyCode

a guest
Jul 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1.  
  2. void printMatrice(int[][] matrix) {
  3. for (int i = 0; i < matrix.length; i++) {
  4. for (int j = 0; j < matrix[i].length; j++) {
  5. System.out.print(matrix[i][j] + " ");
  6. }
  7. System.out.println();
  8.  
  9. }
  10.  
  11. }
  12.  
  13. void printArray(char [] x) {
  14. for (int i = 0; i < x.length; i++) {
  15. System.out.print(x[i] + " ");
  16. }
  17. System.out.println();
  18.  
  19. }
  20.  
  21. int[][] matrice() {
  22. int[][] m = new int[2][10];
  23. int contatore = 0;
  24. for (int i = 0; i < 10; i++) {
  25. m[0][i] = contatore;
  26. contatore++;
  27. m[1][i] = 0;
  28. }
  29. return m;
  30. }
  31.  
  32. int[][] frequenzaNumero(long n) {
  33. int[][] x = matrice();
  34. long c = n;
  35. int contatore = 0;
  36. int indice = 0;
  37. while (indice < matrice()[0].length) {
  38. while (n > 0) {
  39. if (n % 10 == indice) {
  40. contatore = contatore + 1;
  41. }
  42. n = n / 10;
  43.  
  44. }
  45. x[1][indice] = contatore;
  46. indice = indice + 1;
  47. n = c;
  48. contatore = 0;
  49. }
  50.  
  51. return x;
  52. }
  53.  
  54. int[][] ordinaMatrice(int[][] x) {
  55. int temp;
  56. int temp2;
  57. for (int i = 0; i < x[0].length; i++) {
  58. for (int j = i + 1; j < x[0].length; j++) {
  59. if (x[1][i] < x[1][j]) {
  60. temp = x[1][i];
  61. temp2 = x[0][i];
  62. x[1][i] = x[1][j];
  63. x[0][i] = x[0][j];
  64. x[1][j] = temp;
  65. x[0][j] = temp2;
  66. }
  67. }
  68. }
  69. return x;
  70. }
  71. //
  72. //char[] greedyCode(long n) {
  73. // int indice = 0 ;
  74. // char[] x = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
  75. // char[] ritorno = new char[x.length];
  76. // int[][] matrice = ordinaMatrice(frequenzaNumero(n));
  77. // while (indice <= 10) {
  78. // if (indice == matrice[1][indice]) {
  79. // ritorno[indice] = x[indice];
  80. // }
  81. // indice = indice = 1 ;
  82. // }
  83. //
  84. // return ritorno;
  85. //}
  86.  
  87. printMatrice(frequenzaNumero(2136303418));
  88. System.out.println();
  89. //System.out.println(matrice()[0].length); // lunghezza rigo matrix .
  90. printMatrice(ordinaMatrice(frequenzaNumero(2136303418)));
  91. System.out.println();
  92. //System.out.println(greedyCode(2136303418));
  93. printArray(greedyCode(0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement