Ordem crescente

4 respostas
java
W

Boa tarde!

Estou com um probleminha para ordenar os números em ordem crescente na minha matriz, conseguem ajudar?

Segue o código.

<package megasena;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Collections;

public class megasena {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[][] matriz = new int[20][6];
		for (int iLinha = 0; iLinha < 20; iLinha++) {
			for (int iColuna = 0; iColuna < 6; iColuna++) {
				matriz[iLinha][iColuna] = new Random().nextInt((60 - 1) + 1) + 1; 
			}
		}
		Map <Integer,Integer> map = new HashMap<>();
		for(int i = 0; i <= 60; i++){
			map.put(i, 0);
		}
		int count = 0;
		String result = "";
		for (int l = 0; l < 20; l++){
			for (int c = 0; c < 6; c++){
				count = map.get(matriz[l][c]);
				map.put(matriz[l][c],count +1);
				result = result + String.format("%02d", matriz[l][c]) + " " ;
			}
			System.out.println("Resultado : " +  result);
                        
			result = "";
		}
                
                for(int i = 1; i <= 60; i++){
			System.out.println("Numero " + String.format("%02d", i) + " repetiu : " + String.format("%02d", map.get(i)));
		}
		
                
	}
        private static void ordenamatriz(ArrayList<Integer> l){
                    Collections.sort(l);
                }
}

4 Respostas

P

Qual é o problema?
De um exemplo da saida

C
public static void main(String[] args) {
	int[][] matriz = new int[20][6];
	int n = 0;
	List<Integer> lista = new ArrayList<>();
	for (int iLinha = 0; iLinha < 20; iLinha++) {
		for (int iColuna = 0; iColuna < 6; iColuna++) {
			
			int a = new Random().nextInt((60 - 1) + 1) + 1;
				
			lista.add(a);
			Collections.sort(lista);
			matriz[iLinha][iColuna] = a;
		}
	}
	
			
	Map <Integer,Integer> map = new HashMap<>();
	for(int i = 0; i <= 60; i++){
		map.put(i, 0);
	}
	int count = 0;
	String result = "";
	for (int l = 0; l < 20; l++){
		for (int c = 0; c < 6; c++){
			count = map.get(matriz[l][c]);
			map.put(matriz[l][c],count +1);
			
			result = result + String.format("%02d", lista.get(n++)) + " " ;
		}
		System.out.println("Resultado : " +  result);
                    
		result = "";
	}
            
            for(int i = 1; i <= 60; i++){
		System.out.println("Numero " + String.format("%02d", i) + " repetiu : " + String.format("%02d", map.get(i)));
	}
	
            
}
    private static void ordenamatriz(ArrayList<Integer> l){
                Collections.sort(l);
            }
P

ok vc socou mais codigo ai. e nenhuma pista do que pode estar errado. eu nao vou executar o seu programa.

se vc nao pode dar um exemplo, talvez com menos numeros, mostrando ‘po ta saindo assim eu queria assado’ fica dificil amigão

W

Já consegui resolver aqui, agora o que eu preciso é listar as 15 duplas que mais saíram nos sorteios.
Por exemplo: Números 03 e 29 = 156 vezes.

Meu código:

package megasena;

import java.util.Arrays;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map;

import java.util.Random;

import java.util.Set;

public class megasena {

/**
 * @param args
 */
public static void main(String[] args) {
	
	int count = 0;

	int[][] matriz = new int[300][6];

	for (int iLinha = 0; iLinha < matriz.length; iLinha++) {
		
		Set<Integer> numeros = new HashSet<Integer>();
		Random random = new Random();

		while (numeros.size() < 6) {
		   numeros.add(random.nextInt((60 - 1) + 1) + 1);
		}
		
		count = 0;
		for(Integer i : numeros){
			matriz[iLinha][count] = i;
			count ++;
		}
		count = 0;

	}

	Map <Integer,Integer> map = new HashMap<>();
	for(int i = 0; i <= 60; i++){
		map.put(i, 0);
	}

	int [] sorteio = new int[6];
	
	String sorteioAux = "";
	String sorteioOrdenadoAux = "";
	int countLinha = 0;
	
	for (int l = 0; l <  matriz.length; l++){
		
		countLinha ++;
		
		for (int c = 0; c < 6; c++){

			count = map.get(matriz[l][c]);

			map.put(matriz[l][c],count + 1);

			sorteio[c] = matriz[l][c];
		}
		
		Arrays.sort(sorteio);

		count = 0;
		for (int num : sorteio) {
			sorteioAux = sorteioAux + String.format("%02d", num) + "-";
			sorteio[count] = 0;
			}
		
		sorteioAux = sorteioAux.substring(0, sorteioAux.length() - 1);
		
		count = 0;
		for (int c = 0; c < 6; c++){
			sorteioOrdenadoAux = sorteioOrdenadoAux +  String.format("%02d", matriz[l][c]) + "-";
			sorteio[count] = 0;
			}
		sorteioOrdenadoAux = sorteioOrdenadoAux.substring(0, sorteioOrdenadoAux.length() - 1);
		
		System.out.println("[Sorteio "+ String.format("%04d", l + 1) +"] = " + sorteioAux + " (" + sorteioOrdenadoAux + ")");
		sorteioAux = "";
		sorteioOrdenadoAux = "";
	}

	for(int i = 1; i <= 60; i++){
		System.out.println("Numero " + String.format("%02d", i) + " repetiu : " + String.format("%02d", map.get(i)));
	}


}

}

Criado 25 de outubro de 2016
Ultima resposta 31 de out. de 2016
Respostas 4
Participantes 3