Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.14 KB | None | 0 0
  1. /**
  2. * Calcula o fatorial do inteiro nao negativo passado como parametro
  3. *
  4. *
  5. @param n
  6. * numero que se deseja calcular o fatorial
  7. *
  8. @return o fatorial de n
  9. */
  10. public static int fatorial(int n) {
  11. if (n == 0)
  12. return 1;
  13. return n * fatorial(n - 1);
  14. }
  15. /**
  16. * Calcula n escolhe k
  17. *
  18. *
  19. @param n
  20. * numero de elementos do conjunto
  21. *
  22. @param k
  23. * numero de elementos escolhidos do conjunto
  24. *
  25. @return numero de combinacoes possiveis de n elementos k a k
  26. */
  27. public static int nEscolheK(int n, int k) {
  28. return fatorial(n) / (fatorial(n - k) * fatorial(k));
  29. }
  30. /**
  31. * cria um vetor de m elementos dos quais k sao iguais a 1 e m
  32. -k sao iguais
  33. * a 0
  34. *
  35. *
  36. @param n
  37. * numero de elementos do vetor
  38. *
  39. @param k
  40. * numero de elementos iguais a 1
  41. *
  42. @return vetor criado com k elementos 1 e m-k elementos 0
  43. */
  44. public static int[] criaVetor(int n, int k) {
  45. Set<Integer> casasPreenchidas =
  46. new HashSet<Integer>();
  47. while (casasPreenchidas.size() < k) {
  48. int random = (int) (Math.random() * (n - 1));
  49. casasPreenchidas.add(random);
  50. }
  51. int[] resultado = new int[n];
  52. Iterator<Integer> it = casasPreenchidas.iterator();
  53. while (it.hasNext()) {
  54. resultado[it.next()] = 1;
  55. }
  56. return resultado;
  57. }
  58. /**
  59. * verifica se dois vetores de mesmo tamanho sao iguais, ou seja, possuem os mesmos
  60. * elementos na mesma ordem
  61. *
  62. *
  63. @param a
  64. *
  65. @param b
  66. *
  67. @return true se forem iguais, false caso contrario
  68. */
  69. public static boolean vetoresSaoIguais(int[] a, int[] b){
  70. for(int i = 0; i < a.length; i++){
  71. if(a[i] != b[i]){
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. /**
  78. * Dado um vetor e um conjunto de vetores de mesmo tamanho do primeiro, verifica
  79. * se o vetor já existe no conjunto
  80. *
  81. *
  82. @param vetor
  83. *
  84. @param conjunto
  85. *
  86. @return true, se o vetor ja existe no conjunto. False, caso contrario
  87. */
  88. public static boolean vetorExisteNoConjunto(int[] vetor, Set<int[]> conjunto) {
  89. Iterator<
  90. int[]> it = conjunto.iterator();
  91. while(it.hasNext()){
  92. int[] vetorDoConjunto = it.next();
  93. if(vetoresSaoIguais(vetor, vetorDoConjunto)){
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. /**
  100. * cria todos os subconjuntos possíveis de n elementos k a k
  101. *
  102. *
  103. @param n
  104. * numero de elementos
  105. *
  106. @param k
  107. * numero de elementos preenchidos
  108. *
  109. @return conjunto contendo todos os vetores criados
  110. */
  111. public static Set<int[]> criaTodosOsVetores(int n, int k) {
  112. Set<
  113. int[]> resultado = new HashSet<int[]>();
  114. while (resultado.size() < nEscolheK(n, k)) {
  115. int[] vetor = criaVetor(n, k);
  116. if(resultado.isEmpty()){
  117. resultado.add(vetor);
  118. }
  119. else{
  120. if(!vetorExisteNoConjunto(vetor, resultado)){
  121. resultado.add(vetor);
  122. }
  123. }
  124. }
  125. return resultado;
  126. }
  127. }
  128.  
  129. ----- Mensagem encaminhada -----
  130. De: regis hattori <regis_hattori@yahoo.com.br>
  131. Para: Daniel Tsuha <tsuha.daniel@gmail.com>
  132. Enviadas: Terça-feira, 28 de Junho de 2011 11:28
  133. Assunto: Re: EP MD implementação
  134.  
  135. Só pq resolvi fazer, o Fábio me deu coisa pra fazer xD
  136.  
  137. Ta aí a prévia:
  138.  
  139. package
  140. main;
  141. import
  142. java.io.BufferedReader;
  143. import
  144. java.io.FileReader;
  145. import
  146. java.util.HashSet;
  147. import
  148. java.util.Iterator;
  149. import
  150. java.util.Set;
  151. import
  152. java.util.StringTokenizer;
  153. public
  154. class selprod {
  155. public static void main(String[] args) throws Exception {
  156. criaVetor(10, 2);
  157. //int m = Integer.parseInt(args[0]);
  158. //int n = Integer.parseInt(args[1]);
  159. //int k = Integer.parseInt(args[2]);
  160. //String entrada = args[3];
  161. //String saida = args[4];
  162. //int[][] matriz = leArquivo(entrada, m, n);
  163. }
  164. public static int[][] leArquivo(String entrada, int m, int n) throws Exception {
  165. int[][] matriz = new int[m][n];
  166. FileReader fluxo =
  167. new FileReader(entrada);
  168. BufferedReader buffer =
  169. new BufferedReader(fluxo);
  170. for(int i = 0; i < m; i++) {
  171. String linha = buffer.readLine();
  172. StringTokenizer tokens =
  173. new StringTokenizer(linha, " ");
  174. for(int j = 0; j < n; j++) {
  175. matriz[i][j] = Integer.parseInt(tokens.nextToken());
  176. }
  177. }
  178. buffer.close();
  179. return matriz;
  180. }
  181. /**
  182. * Calcula do inteiro passado como parametro
  183. *
  184. *
  185. @param n numero que se deseja calcular o fatorial
  186. *
  187. @return o fatorial de n
  188. */
  189. public static int fatorial(int n){
  190. if(n == 0) return 1;
  191. return n*fatorial(n-1);
  192. }
  193. /**
  194. * Calcula n escolhe k
  195. *
  196. *
  197. @param n numero de elementos do conjunto
  198. *
  199. @param k numero de elementos escolhidos do conjunto
  200. *
  201. @return numero de combinacoes possiveis de n elementos k a k
  202. */
  203. public static int nEscolheK(int n, int k){
  204. return fatorial(n)/(fatorial(n-k)*fatorial(k));
  205. }
  206. /**
  207. * cria um vetor de m elementos dos quais k sao iguais a 1 e m
  208. -k sao iguais a 0
  209. *
  210. *
  211. @param m numero de elementos do vetor
  212. *
  213. @param k numero de elementos iguais a 1
  214. *
  215. @return vetor criado com k elementos 1 e m-k elementos 0
  216. */
  217. public static int[] criaVetor(int m, int k){
  218. Set<Integer> casasPreenchidas =
  219. new HashSet<Integer>();
  220. while(casasPreenchidas.size() <= k){
  221. System.
  222. out.println("primeiro while");
  223. int random = (int) Math.random()*(m-1);
  224. System.
  225. out.println("random = " + random);
  226. casasPreenchidas.add(random);
  227. System.
  228. out.println(casasPreenchidas.size());
  229. }
  230. int[] resultado = new int[m];
  231. Iterator<Integer> it = casasPreenchidas.iterator();
  232. while(it.hasNext()){
  233. System.
  234. out.println("segundo while");
  235. resultado[it.next()] = 1;
  236. }
  237. System.
  238. out.println(resultado);
  239. return resultado;
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement