Advertisement
santiagol26

LISTAS Y SUS COSTOS

Oct 22nd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import java.util.*;
  2. public class Problem {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. Scanner teclado = new Scanner(System.in);
  7. Arraylist star = new Arraylist();
  8. int casos = teclado.nextInt();
  9.  
  10. for (int i = 1; i <= casos; i++) {
  11. System.out.println("Caso #" + i + ":");
  12. star=new Arraylist();
  13.  
  14. int cases = teclado.nextInt();
  15. for (int j = 0; j < cases; j++) {
  16. switch (teclado.next()) {
  17. case "insertar": {
  18. int o = teclado.nextInt();
  19. int p = teclado.nextInt();
  20. star.agregar(o, p);
  21. break;}
  22. case "buscar": {
  23. int b = teclado.nextInt();
  24. star.busca(b);
  25. break;}
  26. case "consultar": {
  27. int c = teclado.nextInt();
  28. star.consultar(c);
  29. break;}
  30. case "eliminar": {
  31. int x = teclado.nextInt();
  32. star.eliminar(x);
  33. break;}
  34. case "costo": {
  35. System.out.println("costo: " + star.m + " " + (star.n));
  36. break;}
  37.  
  38. }
  39. }
  40. }
  41. teclado.close();
  42. }
  43. }
  44. class Arraylist {
  45. int tamano;
  46. int m=0;
  47. int n = 0;
  48. int numeros[];
  49.  
  50. public Arraylist() {
  51. this.tamano = 0;
  52. this.numeros = new int[1000];
  53. }
  54.  
  55. public int tamano() {
  56. return tamano;
  57. }
  58.  
  59. public void agrega(int x) {
  60. agregar(x, tamano());
  61. }
  62.  
  63. public int m() {
  64. return m;
  65. }
  66.  
  67. public int n() {
  68. return n;
  69. }
  70.  
  71. public void agregar(int x, int indc) {
  72. System.out.print("insertar: ");
  73. if (indc > tamano()) {
  74. System.out.println("posicion invalida");
  75. return;
  76. }
  77. else {
  78. System.out.println("posicion valida");
  79. }
  80. if (numeros.length == tamano()) {
  81. capacidad(tamano() * 2);
  82. }
  83. for (int i = tamano(); i > indc; i--) {
  84. numeros[i] = numeros[i - 1];
  85. m++;
  86. }
  87. n = n + indc;
  88. numeros[indc] = x;
  89. tamano++;
  90. }
  91. public void consultar(int pos) {
  92. System.out.print("consulta: ");
  93. if(tamano() <= pos) {
  94. System.out.println("no encontrado");
  95. }
  96. else{
  97. for (int j = 0; j <tamano; j++){
  98. if (j == pos){
  99. n = n + j;
  100. System.out.println(numeros[pos]);
  101. return;
  102. }
  103. }
  104. }
  105. }
  106.  
  107. public void busca(int num) {
  108. for (int i = 0; i < tamano(); i++) {
  109. m++;
  110. n++;
  111. if (numeros[i] == num) {
  112. System.out.println("buscar: " + i);
  113. return;
  114. }
  115. }
  116. System.out.println("no existe numero");
  117. }
  118. public void setItems(int x) {
  119. int [] conten = new int[x];
  120. numeros = conten;
  121. }
  122. public void eliminar(int ndx) {
  123. System.out.print("eliminar: ");
  124. if (ndx >=tamano()) {
  125. System.out.println("posicion invalida");
  126. return;
  127. } else {
  128. System.out.println("posicion valida");
  129. }
  130.  
  131. for (int k = ndx;k<tamano()-1; k++) {
  132. numeros[k] = numeros[k + 1];
  133. m++;
  134. }
  135. n = n + ndx;
  136. tamano--;
  137. }
  138.  
  139. public void capacidad(int x) {
  140.  
  141. if (x <tamano) {
  142. return;
  143. }
  144. int dimen[] = numeros;
  145. setItems(x);
  146.  
  147. for (int h = 0; h <tamano(); h++) {
  148. numeros[h] = dimen[h];
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement