Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
Matriz 2D Matriz 3x9 obtener el índice de los siguientes dos elementos del índice actual y verificar si es igual a 0

Soy un novato total en la programación y he estado siguiendo algunos tutoriales sobre matrices relacionadas con el generador de boletos domésticos. El punto en el que estoy atascado es que tengo que verificar cada fila y cada fila de la matriz 3x9 no debe tener más de los dos celdas vacías o no puede tener más de dos celdas llenas una al lado de la otra. Estoy poniendo números aleatorios en las matrices y tratando de validar las reglas, pero el programa falla. ¿Puede alguien por favor darme una idea.?

Esto es lo que he probado.

 for(int columnIndex=0;columnIndex<=6;columnIndex++) { if(game[i,columnIndex+2]!=0) { return -1; } }

Y este es el codigo completo

 using System; namespace HelloWorld { class Program { public static void Main (String[] args) { for(int times=0;times<2;times++) { startGame(); Console.WriteLine("******************************************************************"); } } private static void startGame() { int[,] game = new int[3, 9]; int occupancyLimit = 15; while (occupancyLimit > 0) { int i = getRandomNumber(3); int j = getRandomNumber(9); //Console.Write(i); //Console.Write(j); // Console.Write(game[i,j]+" "); int data = validateAndReturnNumber(i, j, game); if (data>0) { game[i, j] = data; occupancyLimit--; //Console.WriteLine(game[i,j]); } } for (int i = 0; i < game.GetLength(0); i++) { for (int j = 0; j < game.GetLength(1); j++) { Console.Write(game[i,j] + "\t"); } Console.WriteLine(); } } private static int validateAndReturnNumber(int i, int j, int[,] game) { //do not override existing elements in array if (game[i,j] != 0) { return -1; } //column cannot have more then two elements int columncounter = 0; for(int c=0;c<3;c++) { if(game[c,j]!=0) { columncounter++; } } if(columncounter>=2) { return -1; } /* //rows cannot have more then two cells filled in and //rows cannot have more then two empty cells for(int columnIndex=0;columnIndex<=6;columnIndex++) { if(game[i,columnIndex+2]!=0) { return -1; } } */ // rows cannot have more then 5 elements int rowcounter = 0; for(int r=0;r<9;r++) { if(game[i,r]!=0) { rowcounter++; } } //Applying, rows cannot have more then 5 elements if(rowcounter>=5) { return -1; } //return getRandomNumberForColumn(j); int data = 0; Boolean isValueSet = false; do { data = getRandomNumberForColumn(j); isValueSet = isValueExistsInCol(game, i, j, data); } while (isValueSet); return data; } private static bool isValueExistsInCol(int[,] game, int i, int j, int data) { Boolean status = false; for(int k=0;k<3;k++) { if(game[k,j]==data) { status = true; break; } } return status; } private static int getRandomNumberForColumn(int high) { if(high==0) { high = 10; } else { high=(high + 1) * 10; } int low = high - 9; Random random = new Random(); return random.Next(high-low)+low; } private static int getRandomNumber(int max) { Random random = new Random(); int num=random.Next(max); return (num); } } }
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda