Txerrinko

Arrays

Mar 18th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package packArrays;
  2.  
  3. public class Arrays {
  4.  
  5.     public static int maxrepetidos(int t[]) {
  6.         int max = t[7];
  7.         int pmax = 0;
  8.  
  9.         for (int i = 1; i < t.length; i++) {
  10.             if (t[i] > max) {
  11.                 max = t[i];
  12.                 pmax = i;
  13.             }
  14.         }
  15.  
  16.         // el maximo es max y esta en la casilla pmax
  17.  
  18.         int cont = 0;
  19.  
  20.         for (int i = 0; i < t.length; i++)
  21.             if (t[i] == max)
  22.                 cont++;
  23.  
  24.         return cont;
  25.     }
  26.  
  27.     public static int buscar(int t[], int num) {
  28.         int i = 0;
  29.  
  30.         boolean parar = false;
  31.  
  32.         while (i < t.length && parar == false) {
  33.             if (t[i] > num)
  34.                 i++;
  35.             else
  36.                 parar = true;
  37.         }
  38.  
  39.         if (parar == true) {
  40.             if (t[i] == num)
  41.                 return i;
  42.             else
  43.                 return -1;
  44.         } else {
  45.             return -1;
  46.         }
  47.     }
  48.  
  49.     public static int[] desordenar(int t[]) {
  50.         // el nuevo array desordenado
  51.         int nueva[] = new int[t.length];
  52.  
  53.         int posiciones[] = new int[t.length];
  54.  
  55.         for (int i = 0; i < t.length; i++) {
  56.             int posi = (int) (Math.random() * t.length);
  57.  
  58.             // buscar posi en el array de posiciones, si no est� guardarlo
  59.            
  60.             int j = 0;
  61.            
  62.             boolean encontrado = false;
  63.            
  64.             while ( j < i && !encontrado )
  65.             {
  66.                 if ( posiciones[j] == posi )
  67.                    
  68.                     encontrado = true;
  69.            
  70.                 else
  71.                
  72.                     j++;
  73.             }
  74.            
  75.             if ( encontrado == false )
  76.                
  77.                 posiciones[i] = posi;
  78.            
  79.             else
  80.                
  81.                 i--;
  82.         }
  83.        
  84.         for ( int i = 0 ; i < t.length ; i++ )
  85.         {
  86.             nueva[ posiciones[i] ] = t[ i ];
  87.         }
  88.        
  89.         return nueva;
  90.     }
  91.    
  92.     public static void main ( String args[] )
  93.     {
  94.         int t[] = {0,0,0,0,4,5,6,7,8,9,10,11,12};
  95.        
  96.         int num = 14354;
  97.        
  98.         int posicion = buscar( t , num );
  99.                
  100.         System.out.println("El numero esta en la posicion " + posicion );
  101.        
  102.         int cont = maxrepetidos( t );
  103.        
  104.         System.out.println("El maximo se repite " + cont + " veces.");
  105.        
  106.         t = desordenar( t );
  107.        
  108.         for ( int i = 0 ; i < t.length ; i++)
  109.             System.out.print( t[i] + " " );
  110.        
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment