Posted By


balazs99 on 01/10/14

Tagged


Statistics


Viewed 110 times
Favorited by 0 user(s)

Tomb Eljarasok2


/ Published in: Java
Save to your folder(s)

Sort...


Copy this code and paste it in your HTML
  1. package H8_9;
  2. import java.util.*;
  3. /**
  4.  *
  5.  * @author Balazs
  6.  */
  7. public class TombEljarasok2 {
  8. public static void main(String[] args) {
  9. int[] halmazA = tombGeneralo(20);
  10. int[] halmazB = tombGeneralo(15);
  11.  
  12. tombListazo("\'A\' halmaz:", halmazA);
  13. tombListazo("\'B\' halmaz:", halmazB);
  14. int[] metszetHalmaz = metszet(halmazA, halmazB);
  15. tombListazo("Metszet:", metszetHalmaz);
  16. }
  17.  
  18. public static int[] unio(int[] tomb1, int[] tomb2){
  19. int[] a = new int[2];
  20. int[] tmp;
  21. int counter = 0;
  22.  
  23. if (tomb1.length <= tomb2.length){
  24. tmp = new int[tomb2.length];
  25. loop:
  26. for (int i = 0 ; i <= tomb2.length - 1; i++){
  27. for (int j = 0 ; j <= tomb1.length - 1 ; j++){
  28. if (tomb2[i] == tomb1[j]){
  29. tmp[counter] = tomb2[i];
  30. counter += 1;
  31. continue loop;
  32. }
  33. }
  34. }
  35. }
  36.  
  37.  
  38. return a;
  39. }
  40.  
  41. public static int[] metszet(int[] tomb1, int[] tomb2){
  42. int[] tmp;
  43. int counter = 0;
  44.  
  45. if (tomb1.length <= tomb2.length){
  46. tmp = new int[tomb2.length];
  47. loop:
  48. for (int i = 0 ; i <= tomb2.length - 1; i++){
  49. for (int j = 0 ; j <= tomb1.length - 1 ; j++){
  50. if (tomb2[i] == tomb1[j]){
  51. tmp[counter++] = tomb2[i];
  52. continue loop;
  53. }
  54. }
  55. }
  56. }
  57. else {
  58. tmp = new int[tomb1.length];
  59. loop:
  60. for (int i = 0 ; i <= tomb1.length - 1; i++){
  61. for (int j = 0 ; j <= tomb2.length - 1 ; j++){
  62. if (tomb1[i] == tomb2[j]){
  63. tmp[counter++] = tomb1[i];
  64. continue loop;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. int[] result = new int[counter];
  71.  
  72. System.arraycopy(tmp, 0, result, 0, result.length);
  73. return result;
  74. }
  75.  
  76. public static void tombListazo(String cim, int[] tomb){
  77. System.out.println(cim);
  78.  
  79. for (int i=0 ; i <= tomb.length - 2 ; i++) {
  80. System.out.print(tomb[i] + ", ");
  81. }
  82. System.out.println(tomb[tomb.length - 1]);
  83. }
  84. /**
  85.   * @param tombHossza A letrehozando tomb hossza
  86.   */
  87. public static int[] tombGeneralo(int tombHossza){
  88. int[] tomb = new int[tombHossza];
  89. Random rnd = new Random();
  90. int num;
  91.  
  92. for (int i=0 ; i <= tombHossza - 1 ; i++){
  93. num = rnd.nextInt(100);
  94. for (int j = 0 ; j < i ; j++){
  95. if (tomb[j] == num){
  96. num = rnd.nextInt(100);
  97. j = 0;
  98. }
  99. }
  100. tomb[i]=num;
  101. }
  102.  
  103. tombRendezo(tomb);
  104. return tomb;
  105. }
  106.  
  107. public static void tombRendezo(int[] tomb){
  108. int num;
  109. int counter = 2;
  110.  
  111. for (int j=0 ; j <= tomb.length - 2 ; j++){
  112. for (int i=0 ; i <= tomb.length - counter ; i++){
  113. if (tomb[i] > tomb[i+1]){
  114. num = tomb[i];
  115. tomb[i] = tomb[i+1];
  116. tomb[i+1] = num;
  117. }
  118. }
  119. counter += 1;
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126. }
  127. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.