Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1.  
  2. // ט.כניסה - מקבל מספר
  3. // ט.יציאה - מחזיר האם המספר מופיע יותר מפעם אחת
  4. // לא הורס את התור
  5.  
  6. public static boolean isLongerThanOne(Queue<Integer> q, int x) {
  7.  
  8. int count = 0;
  9. Queue<Integer> temp = new Queue<Integer>();
  10. while (!q.isEmpty()) {
  11.  
  12. if (q.head() == x) {
  13.  
  14. count++;
  15.  
  16. }
  17.  
  18. temp.insert(q.remove());
  19.  
  20. }
  21.  
  22. while (!temp.isEmpty()) {
  23.  
  24. q.insert(temp.remove());
  25.  
  26. }
  27.  
  28. if (count > 1) {
  29.  
  30. return true;
  31.  
  32. }
  33. else {
  34.  
  35. return false;
  36.  
  37. }
  38.  
  39. }
  40.  
  41. // ט.כניסה - מקבל תור של זמנים
  42. // ט.יציאה - מחזיר זמן שהוא ההפרש בין הזמן האחרון לראשון
  43.  
  44. public static Time func(Queue<Time> q) {
  45.  
  46. int count = 0;
  47. int count2 = 0;
  48. int hour = 0,minute = 0,second = 0;
  49. int hour2 = 0,minute2 = 0,second2 = 0;
  50. Queue<Time> temp = new Queue<Time>();
  51. while(!q.isEmpty()) {
  52.  
  53. count++;
  54. temp.insert(q.remove());
  55.  
  56. }
  57. while(!temp.isEmpty()) {
  58.  
  59. count2++;
  60. if (count2 == 1) {
  61.  
  62. hour = temp.head().getHour();
  63. minute = temp.head().getMinute();
  64. second = temp.head().getSecond();
  65.  
  66. }
  67. else if (count2 == count) {
  68.  
  69. hour2 = temp.head().getHour();
  70. minute2 = temp.head().getMinute();
  71. second2 = temp.head().getSecond();
  72.  
  73. }
  74. q.insert(temp.remove());
  75. }
  76.  
  77. Time time = new Time(hour,minute,second);
  78. Time time2 = new Time(hour2,minute2,second2);
  79. Time time3 = diff(time,time2);
  80. return time3;
  81.  
  82. }
  83.  
  84. // ט.כניסה - מקבל שני זמנים
  85. // ט.יציאה - מחזיר את זמן שהוא הפרש הזמנים
  86.  
  87. public static Time diff(Time time, Time time2) {
  88. if (time.getHour() > time2.getHour()) {
  89.  
  90. return arrange(time,time2);
  91.  
  92. }
  93. else if (time.getHour() == time2.getHour()){
  94.  
  95. if (time.getMinute() > time2.getMinute()) {
  96.  
  97. return arrange(time,time2);
  98.  
  99. }
  100. else if (time.getMinute() == time2.getMinute()){
  101.  
  102. if (time.getSecond() > time2.getSecond()) {
  103.  
  104. return arrange(time,time2);
  105.  
  106. }
  107. else if (time.getSecond() == time2.getSecond()){
  108.  
  109. return arrange(time,time2);
  110.  
  111. }
  112. else {
  113.  
  114. return arrange(time2,time);
  115.  
  116. }
  117. }
  118. else {
  119.  
  120. return arrange(time2,time);
  121.  
  122. }
  123. }
  124. else {
  125.  
  126. return arrange(time2,time);
  127.  
  128. }
  129. }
  130.  
  131. // ט.כניסה - מקבל 2 זמנים
  132. // ט.יציאה - מסדר את השניות / דקות / שעות בסדר הגיוני
  133.  
  134. private static Time arrange(Time time, Time time2) {
  135.  
  136. int hour = time2.getHour() - time.getHour();
  137. int minute = time2.getMinute() - time.getMinute();
  138. int second = time2.getSecond() - time.getSecond();
  139.  
  140. if (second > 0) {
  141.  
  142. second = 60 - second;
  143. minute ++;
  144.  
  145. }
  146. else {
  147.  
  148. second = Math.abs(second);
  149.  
  150. }
  151.  
  152. if (minute > 0) {
  153.  
  154. minute = 60 - minute;
  155. hour ++;
  156.  
  157. }
  158. else {
  159.  
  160. minute = Math.abs(minute);
  161.  
  162. }
  163.  
  164. hour = Math.abs(hour);
  165.  
  166. Time time3 = new Time(hour,minute,second);
  167. return time3;
  168.  
  169. }
  170.  
  171. public static Queue<Integer> once(Queue<Integer> q) {
  172.  
  173. Queue<Integer> temp = new Queue<Integer>();
  174. while(!q.isEmpty()) {
  175.  
  176. if (temp.isEmpty()) {
  177.  
  178. temp.insert(q.remove());
  179.  
  180. }
  181. else {
  182.  
  183. if (!isThere(temp,q.head())) {
  184. temp.insert(q.remove());
  185.  
  186. }
  187. else {
  188.  
  189. q.remove();
  190.  
  191. }
  192.  
  193. }
  194.  
  195. }
  196. while(!temp.isEmpty()) {
  197.  
  198. q.insert(temp.remove());
  199.  
  200. }
  201.  
  202. return q;
  203.  
  204. }
  205.  
  206. // ט.כניסה - מקבל תור ומספר
  207. // ט.יציאה - מחזיר האם המספר נמצא בתור
  208. // לא הורס את התור
  209.  
  210. public static boolean isThere(Queue<Integer> q, int x) {
  211.  
  212. boolean check = false;
  213. Queue<Integer> temp = new Queue<Integer>();
  214. while (!q.isEmpty()) {
  215.  
  216. if (q.head() == x) {
  217.  
  218. check = true;
  219.  
  220. }
  221. temp.insert(q.remove());
  222.  
  223. }
  224. while(!temp.isEmpty()) {
  225.  
  226. q.insert(temp.remove());
  227.  
  228. }
  229. return check;
  230.  
  231. }
  232.  
  233. // ט.כניסה - מקבל תור של מחרוזות ומספר
  234. // ט.יציאה - התור מוציא ומכניס את מחרוזת שבראש כמספר הפעמים כערך המספר שהכניסו ומחזיר האם המחרוזת שכעת היא בראש התור שווה לערך מסויים במקרה הזה
  235. // "g" האות
  236. // הורס את התור
  237.  
  238. public static boolean lottery(Queue<String> q, int x) {
  239.  
  240. for (int i = 1 ; i <= x ; i++) {
  241.  
  242. String str = q.remove();
  243. q.insert(str);
  244.  
  245. }
  246. return q.head().equals("g");
  247. }
  248.  
  249. // ט.כניסה - מקבל תור
  250. // ט.יציאה - ממיין את התור
  251. public static Stack<Integer> orderedStack(Queue<Integer> q){
  252.  
  253. q.insert(null);
  254. int min = q.head();
  255. int count = 0;
  256. Stack<Integer> s = new Stack<Integer>();
  257. while(q.head() != null) {
  258.  
  259. count++;
  260. if (min > q.head()) {
  261.  
  262. min = q.head();
  263.  
  264. }
  265. q.insert(q.remove());
  266.  
  267. }
  268. for (int i = 0 ; i < count ; i++) {
  269. q.insert(q.remove());
  270. while (q.head() != null) {
  271.  
  272. if (q.head() == min) {
  273.  
  274. s.push(min);
  275.  
  276. }
  277. q.insert(q.remove());
  278.  
  279. }
  280. min++;
  281. }
  282.  
  283. return s;
  284.  
  285. }
  286.  
  287. // ט.כניסה - מקבל תור עבודות וזמן
  288. // ט.יציאה - מדפיס את קוד העבודות שניתן לסיים בפרק הזמן הנתון
  289. // לכל עבודה יש תכונה של קוד וזמן שלוקח לעשות את העבודה
  290.  
  291. public static void jobsDone(Queue<Job> q, int t) {
  292.  
  293. int t2 = 0;
  294. q.insert(null);
  295. while (q.head() != null) {
  296.  
  297. if (q.head().getTime() + t2 <= t) {
  298.  
  299. t2 += q.head().getTime();
  300. System.out.println(q.head().getCode());
  301. q.remove();
  302.  
  303. }
  304. else {
  305.  
  306. q.insert(q.remove());
  307.  
  308. }
  309.  
  310. }
  311. q.remove();
  312.  
  313. }
  314.  
  315. // ט.כניסה - מקבל שני תורים
  316. // ט.יציאה - מחזיר תור כך שבתור החדש כל מקום שני זה מספר מהתור השני שקיבלנו אבל אם התור הראשון נגמר זה פשוט שם את מה שנשאר מהתור השני
  317. // דוגמה:
  318. /*
  319. * queue1 : [ 9,8,23,0]
  320. * queue2: [13,18,23,6,10,27]
  321. * new queue: [9,13,8,18,23,23,0,6,10,27]
  322. */
  323.  
  324. public static Queue<Integer> sortQueues(Queue<Integer> q1, Queue<Integer> q2){
  325.  
  326. q1.insert(null);
  327. q2.insert(null);
  328. Queue<Integer> temp = new Queue<Integer>();
  329. int count = 1;
  330. boolean check = true;
  331. while (q1.head() != null && check) {
  332.  
  333. if (q2.head() != null) {
  334. if (count % 2 == 0)
  335. temp.insert(q2.remove());
  336. else
  337. temp.insert(q1.remove());
  338. }
  339. else
  340. check = false;
  341. count++;
  342.  
  343. }
  344. while(q2.head() != null) {
  345. temp.insert(q2.remove());
  346. }
  347. q1.remove();
  348. q2.remove();
  349. return temp;
  350.  
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement