Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Problem {
- public static void main(String[] args) {
- Scanner teclado = new Scanner(System.in);
- Arraylist star = new Arraylist();
- int casos = teclado.nextInt();
- for (int i = 1; i <= casos; i++) {
- System.out.println("Caso #" + i + ":");
- star=new Arraylist();
- int cases = teclado.nextInt();
- for (int j = 0; j < cases; j++) {
- switch (teclado.next()) {
- case "insertar": {
- int o = teclado.nextInt();
- int p = teclado.nextInt();
- star.agregar(o, p);
- break;}
- case "buscar": {
- int b = teclado.nextInt();
- star.busca(b);
- break;}
- case "consultar": {
- int c = teclado.nextInt();
- star.consultar(c);
- break;}
- case "eliminar": {
- int x = teclado.nextInt();
- star.eliminar(x);
- break;}
- case "costo": {
- System.out.println("costo: " + star.m + " " + (star.n));
- break;}
- }
- }
- }
- teclado.close();
- }
- }
- class Arraylist {
- int tamano;
- int m=0;
- int n = 0;
- int numeros[];
- public Arraylist() {
- this.tamano = 0;
- this.numeros = new int[1000];
- }
- public int tamano() {
- return tamano;
- }
- public void agrega(int x) {
- agregar(x, tamano());
- }
- public int m() {
- return m;
- }
- public int n() {
- return n;
- }
- public void agregar(int x, int indc) {
- System.out.print("insertar: ");
- if (indc > tamano()) {
- System.out.println("posicion invalida");
- return;
- }
- else {
- System.out.println("posicion valida");
- }
- if (numeros.length == tamano()) {
- capacidad(tamano() * 2);
- }
- for (int i = tamano(); i > indc; i--) {
- numeros[i] = numeros[i - 1];
- m++;
- }
- n = n + indc;
- numeros[indc] = x;
- tamano++;
- }
- public void consultar(int pos) {
- System.out.print("consulta: ");
- if(tamano() <= pos) {
- System.out.println("no encontrado");
- }
- else{
- for (int j = 0; j <tamano; j++){
- if (j == pos){
- n = n + j;
- System.out.println(numeros[pos]);
- return;
- }
- }
- }
- }
- public void busca(int num) {
- for (int i = 0; i < tamano(); i++) {
- m++;
- n++;
- if (numeros[i] == num) {
- System.out.println("buscar: " + i);
- return;
- }
- }
- System.out.println("no existe numero");
- }
- public void setItems(int x) {
- int [] conten = new int[x];
- numeros = conten;
- }
- public void eliminar(int ndx) {
- System.out.print("eliminar: ");
- if (ndx >=tamano()) {
- System.out.println("posicion invalida");
- return;
- } else {
- System.out.println("posicion valida");
- }
- for (int k = ndx;k<tamano()-1; k++) {
- numeros[k] = numeros[k + 1];
- m++;
- }
- n = n + ndx;
- tamano--;
- }
- public void capacidad(int x) {
- if (x <tamano) {
- return;
- }
- int dimen[] = numeros;
- setItems(x);
- for (int h = 0; h <tamano(); h++) {
- numeros[h] = dimen[h];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement