Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Calcula o fatorial do inteiro nao negativo passado como parametro
- *
- *
- @param n
- * numero que se deseja calcular o fatorial
- *
- @return o fatorial de n
- */
- public static int fatorial(int n) {
- if (n == 0)
- return 1;
- return n * fatorial(n - 1);
- }
- /**
- * Calcula n escolhe k
- *
- *
- @param n
- * numero de elementos do conjunto
- *
- @param k
- * numero de elementos escolhidos do conjunto
- *
- @return numero de combinacoes possiveis de n elementos k a k
- */
- public static int nEscolheK(int n, int k) {
- return fatorial(n) / (fatorial(n - k) * fatorial(k));
- }
- /**
- * cria um vetor de m elementos dos quais k sao iguais a 1 e m
- -k sao iguais
- * a 0
- *
- *
- @param n
- * numero de elementos do vetor
- *
- @param k
- * numero de elementos iguais a 1
- *
- @return vetor criado com k elementos 1 e m-k elementos 0
- */
- public static int[] criaVetor(int n, int k) {
- Set<Integer> casasPreenchidas =
- new HashSet<Integer>();
- while (casasPreenchidas.size() < k) {
- int random = (int) (Math.random() * (n - 1));
- casasPreenchidas.add(random);
- }
- int[] resultado = new int[n];
- Iterator<Integer> it = casasPreenchidas.iterator();
- while (it.hasNext()) {
- resultado[it.next()] = 1;
- }
- return resultado;
- }
- /**
- * verifica se dois vetores de mesmo tamanho sao iguais, ou seja, possuem os mesmos
- * elementos na mesma ordem
- *
- *
- @param a
- *
- @param b
- *
- @return true se forem iguais, false caso contrario
- */
- public static boolean vetoresSaoIguais(int[] a, int[] b){
- for(int i = 0; i < a.length; i++){
- if(a[i] != b[i]){
- return false;
- }
- }
- return true;
- }
- /**
- * Dado um vetor e um conjunto de vetores de mesmo tamanho do primeiro, verifica
- * se o vetor já existe no conjunto
- *
- *
- @param vetor
- *
- @param conjunto
- *
- @return true, se o vetor ja existe no conjunto. False, caso contrario
- */
- public static boolean vetorExisteNoConjunto(int[] vetor, Set<int[]> conjunto) {
- Iterator<
- int[]> it = conjunto.iterator();
- while(it.hasNext()){
- int[] vetorDoConjunto = it.next();
- if(vetoresSaoIguais(vetor, vetorDoConjunto)){
- return true;
- }
- }
- return false;
- }
- /**
- * cria todos os subconjuntos possíveis de n elementos k a k
- *
- *
- @param n
- * numero de elementos
- *
- @param k
- * numero de elementos preenchidos
- *
- @return conjunto contendo todos os vetores criados
- */
- public static Set<int[]> criaTodosOsVetores(int n, int k) {
- Set<
- int[]> resultado = new HashSet<int[]>();
- while (resultado.size() < nEscolheK(n, k)) {
- int[] vetor = criaVetor(n, k);
- if(resultado.isEmpty()){
- resultado.add(vetor);
- }
- else{
- if(!vetorExisteNoConjunto(vetor, resultado)){
- resultado.add(vetor);
- }
- }
- }
- return resultado;
- }
- }
- ----- Mensagem encaminhada -----
- De: regis hattori <regis_hattori@yahoo.com.br>
- Para: Daniel Tsuha <tsuha.daniel@gmail.com>
- Enviadas: Terça-feira, 28 de Junho de 2011 11:28
- Assunto: Re: EP MD implementação
- Só pq resolvi fazer, o Fábio me deu coisa pra fazer xD
- Ta aí a prévia:
- package
- main;
- import
- java.io.BufferedReader;
- import
- java.io.FileReader;
- import
- java.util.HashSet;
- import
- java.util.Iterator;
- import
- java.util.Set;
- import
- java.util.StringTokenizer;
- public
- class selprod {
- public static void main(String[] args) throws Exception {
- criaVetor(10, 2);
- //int m = Integer.parseInt(args[0]);
- //int n = Integer.parseInt(args[1]);
- //int k = Integer.parseInt(args[2]);
- //String entrada = args[3];
- //String saida = args[4];
- //int[][] matriz = leArquivo(entrada, m, n);
- }
- public static int[][] leArquivo(String entrada, int m, int n) throws Exception {
- int[][] matriz = new int[m][n];
- FileReader fluxo =
- new FileReader(entrada);
- BufferedReader buffer =
- new BufferedReader(fluxo);
- for(int i = 0; i < m; i++) {
- String linha = buffer.readLine();
- StringTokenizer tokens =
- new StringTokenizer(linha, " ");
- for(int j = 0; j < n; j++) {
- matriz[i][j] = Integer.parseInt(tokens.nextToken());
- }
- }
- buffer.close();
- return matriz;
- }
- /**
- * Calcula do inteiro passado como parametro
- *
- *
- @param n numero que se deseja calcular o fatorial
- *
- @return o fatorial de n
- */
- public static int fatorial(int n){
- if(n == 0) return 1;
- return n*fatorial(n-1);
- }
- /**
- * Calcula n escolhe k
- *
- *
- @param n numero de elementos do conjunto
- *
- @param k numero de elementos escolhidos do conjunto
- *
- @return numero de combinacoes possiveis de n elementos k a k
- */
- public static int nEscolheK(int n, int k){
- return fatorial(n)/(fatorial(n-k)*fatorial(k));
- }
- /**
- * cria um vetor de m elementos dos quais k sao iguais a 1 e m
- -k sao iguais a 0
- *
- *
- @param m numero de elementos do vetor
- *
- @param k numero de elementos iguais a 1
- *
- @return vetor criado com k elementos 1 e m-k elementos 0
- */
- public static int[] criaVetor(int m, int k){
- Set<Integer> casasPreenchidas =
- new HashSet<Integer>();
- while(casasPreenchidas.size() <= k){
- System.
- out.println("primeiro while");
- int random = (int) Math.random()*(m-1);
- System.
- out.println("random = " + random);
- casasPreenchidas.add(random);
- System.
- out.println(casasPreenchidas.size());
- }
- int[] resultado = new int[m];
- Iterator<Integer> it = casasPreenchidas.iterator();
- while(it.hasNext()){
- System.
- out.println("segundo while");
- resultado[it.next()] = 1;
- }
- System.
- out.println(resultado);
- return resultado;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement