- Curso
- Matrices
- Buscar en una matriz de enteros
Buscar en una matriz de enteros
Última actualización:
23/08/2020
⁃
Dificultad:
Intermedio
Cree un programa en C# que sirva para buscar en una matriz de valores enteros. Para ello siga los siguientes pasos:
- Obtenga la cantidad (x) de valores de la matriz de enteros
- Prepare una matriz de enteros con la cantidad anterior
- Obtenga el número (y) a buscar en la matriz de enteros
- Muestre si el número (y) existe en la matriz de enteros o no
- Repítalo hasta que se introduzca el texto fin
Entrada
- 3
- 2
- 5
- 3
- 5
- fin
Salida
- El número 5 existe
Solución
- using System;
- public class BuscarArray
- {
- public static void Main(string[] args)
- {
- int x = Convert.ToInt32(Console.ReadLine());
-
- int[] lista = new int[x];
- for (int i = 0; i < x; i++)
- {
- lista[i] = Convert.ToInt32(Console.ReadLine());
- }
-
- string y;
-
- do
- {
- y = Console.ReadLine();
-
- if (y != "fin")
- {
- int yToInt = Convert.ToInt32(y);
-
- if (Array.BinarySearch(lista, yToInt) >= 0)
- {
- Console.WriteLine("El número {0} existe", y);
- }
- else
- {
- Console.WriteLine("El número {0} no existe", y);
- }
- }
- }
- while (y != "fin");
- }
- }