Maxim_Leo

Untitled

May 13th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.17 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.PrintStream;
  4. import java.math.BigInteger;
  5. import java.util.*;
  6. import java.util.concurrent.ThreadLocalRandom;
  7.  
  8. class Pair<T,M>{
  9. private T first;
  10. private M second;
  11. final T getFirst(){
  12. return first;
  13. }
  14. final M getSecond(){
  15. return second;
  16. }
  17. final void setFirst(T m){
  18. this.first=m;
  19. }
  20. final void setSecond(M i){
  21. this.second=i;
  22. }
  23. protected Pair(){}
  24. public Pair(T max,M index){
  25. this.first=max;
  26. this.second=index;
  27.  
  28. }
  29. public static <T,M> Pair make_pair(T max,M index){
  30. return new Pair<T,M>(max,index);
  31. }
  32. final void show(){
  33. System.out.print(first+" ");
  34. System.out.print(second+" ");
  35. }
  36.  
  37. }
  38.  
  39.  
  40. class Bag {
  41. private Object[] mas ;
  42. protected int cur_size;
  43. public Object[] getMas() {
  44. return mas;
  45. }
  46.  
  47. public Bag(int n) {
  48. mas=new Object [n];
  49. }
  50.  
  51.  
  52. public int getCur_size() {
  53. return this.cur_size;
  54. }
  55.  
  56. public boolean isEmpty() {
  57. return this.cur_size == 0;
  58. }
  59. public void add(Object num) {
  60. int count = -1;
  61. boolean flag = false;
  62. int r = (int) Math.round(Math.random() * (mas.length - 1));
  63. while (!flag) {
  64. for (int k = 0; k < mas.length; k++) {
  65. if (mas[k] == null) {
  66. if(r == k) {
  67. mas[k]=num;
  68. System.out.println("Предмет " + mas[k] + " добавлен в мешок");
  69. flag = true;
  70. break;
  71. }
  72. } else count++;
  73. }
  74. r = (int) Math.round(Math.random() * (mas.length - 1));
  75. if (mas.length - 1 == count) {
  76. System.out.println("Мешок полон");
  77. break;
  78. }
  79. }
  80. }
  81.  
  82. public Object remove() {
  83. boolean flag = false;
  84. Object tmp=null;
  85. if (mas.length == 0) return tmp;
  86. while (!flag) {
  87. int r = (int) Math.round(Math.random() * (mas.length - 1));
  88. for (int k = 0; k < mas.length; k++) {
  89. if (mas[k] != null && r == k) {
  90. tmp = mas[k];
  91. System.out.println("Предмет " + mas[k] + " удален");
  92. mas[k]=null;
  93. flag = true;
  94. break;
  95. }
  96. }
  97. }
  98. return tmp;
  99. }
  100.  
  101. public final Object retElem(int i) {
  102. return mas[i];
  103. }
  104.  
  105. public final int retSize() {
  106. return mas.length;
  107. }
  108.  
  109. public void show() {
  110. for (Object i : mas) {
  111. if(i!=null)
  112. System.out.print(i + " ");
  113. }
  114. System.out.println();
  115. }
  116. }
  117.  
  118. class PairBag {
  119. private final Bag bag;
  120. public final Bag getBag() {return bag;}
  121. public PairBag(int n) {
  122. bag=new Bag(n);
  123. }
  124.  
  125. public int getCur_size() {
  126. return this.bag.getCur_size();
  127. }
  128.  
  129. public void add(Object num,Object num1) {
  130. int count = -1;
  131. boolean flag = false;
  132. Pair pair1 = Pair.make_pair(num,num1);
  133. int r = (int) Math.round(Math.random() * (bag.getMas().length - 1));
  134. while (!flag) {
  135. for (int k = 0; k < bag.getMas().length; k++) {
  136. if (bag.getMas()[k] == null) {
  137. if(r==k) {
  138. bag.getMas()[k]=pair1;
  139. System.out.println("Пара значений ");
  140. pair1.show();
  141. System.out.print("добавлена в мешок");
  142. System.out.println();
  143. flag = true;
  144. break;
  145. }
  146. } else count++;
  147. }
  148. r = (int) Math.round(Math.random() * (bag.getMas().length - 1));
  149. if (bag.getMas().length - 1 == count) {
  150. System.out.println("Мешок полон");
  151. break;
  152. }
  153. }
  154. }
  155. public Object remove() {
  156. boolean flag = false;
  157. Pair tmp = new Pair();
  158. if (bag.getMas().length == 0) return tmp;
  159. while (!flag) {
  160. int R = (int) Math.round(Math.random() * (bag.getMas().length - 1));
  161. for (int k = 0; k < bag.getMas().length; k++) {
  162. if (bag.getMas()[k] != null && R == k) {
  163. tmp = (Pair)bag.getMas()[k];
  164. System.out.println("Пара значений ");
  165. ((Pair<?, ?>) bag.getMas()[k]).show();
  166. System.out.print(" удалена");
  167. bag.getMas()[k]=null;
  168. flag = true;
  169. break;
  170. }
  171. }
  172. }
  173. return tmp;
  174. }
  175. public final Object retElem(int i) {
  176. return bag.getMas()[i];
  177. }
  178. public final int retSize(){return this.bag.retSize();}
  179. public void show() {
  180. for (int i=0;i<bag.getMas().length;i++) {
  181. if(bag.getMas()[i]!=null)
  182. System.out.print(bag.getMas()[i] + " ");
  183. }
  184. System.out.println();
  185. }
  186. }
  187.  
  188. class GPairBag<T1, T2> {
  189. private PairBag pairBag;
  190.  
  191. public GPairBag(int length) {
  192. this.pairBag = new PairBag(length);
  193. }
  194.  
  195. public int retSize() {
  196. return this.pairBag.retSize();
  197. }
  198.  
  199. public int getCur_size() {
  200. return this.pairBag.getCur_size();
  201. }
  202.  
  203. public void add(T1 value1, T2 value2) {
  204. this.pairBag.add(value1, value2);
  205. }
  206.  
  207. public void remove() {
  208. this.pairBag.remove();
  209. }
  210.  
  211. public Pair<T1, T2> ret(int i) {
  212. return (Pair<T1, T2>) this.pairBag.retElem(i);
  213. }
  214. }
  215.  
  216. class GenericPairBag<T1,T2>{
  217. ArrayList<Pair<T1,T2>> mas;
  218. public GenericPairBag(int n) {
  219. mas=new ArrayList<>(n);
  220. for(int i=0;i<n;i++){
  221. mas.set(i,null);
  222. }
  223. }
  224. protected GenericPairBag(){};
  225. public boolean isEmpty() {
  226. return this.mas.isEmpty();
  227. }
  228. public void add(T1 num,T2 num1) {
  229. int count = -1;
  230. boolean flag = false;
  231. Pair pair1 = Pair.make_pair(num,num1);
  232. int r = (int) Math.round(Math.random() * (mas.size() - 1));
  233. while (!flag) {
  234. for (int k = 0; k < mas.size(); k++) {
  235. if (mas.get(k) == null) {
  236. if(r==k) {
  237. mas.set(k,pair1);
  238. System.out.println("Пара значений ");
  239. pair1.show();
  240. System.out.print("добавлена в мешок");
  241. System.out.println();
  242. flag = true;
  243. break;
  244. }
  245. } else count++;
  246. }
  247. r = (int) Math.round(Math.random() * (mas.size() - 1));
  248. if (mas.size() - 1 == count) {
  249. System.out.println("Мешок полон");
  250. break;
  251. }
  252. }
  253. }
  254. public Pair remove() {
  255. boolean flag = false;
  256. Pair tmp = new Pair();
  257. if (mas.size() == 0) return tmp;
  258. while (!flag) {
  259. int R = (int) Math.round(Math.random() * (mas.size() - 1));
  260. for (int k = 0; k < mas.size(); k++) {
  261. if (mas.get(k) != null && R == k) {
  262. tmp = mas.get(k);
  263. System.out.println("Пара значений ");
  264. mas.get(k).show();
  265. System.out.print(" удалена");
  266. mas.set(k,null);
  267. flag = true;
  268. break;
  269. }
  270. }
  271. }
  272. return tmp;
  273. }
  274. public final Pair retElem(int i) {
  275. return mas.get(i);
  276. }
  277. public final int retSize(){return mas.size();}
  278. public void show() {
  279. for (int i=0;i<mas.size();i++) {
  280. if(mas.get(i)!=null)
  281. System.out.print(mas.get(i) + " ");
  282. }
  283. System.out.println();
  284. }
  285. }
  286.  
  287. class Team {
  288. private Bag bag;
  289. private GenericPairBag<String, String> genericPairBag;
  290. private int amount;
  291. private int round;
  292.  
  293. public Team(int n) {
  294. if ((n & n - 1) != 0) {
  295. System.out.println("Введено недопустимое значение. Установленно значение по умолчанию - 8\n");
  296. this.bag = new Bag(8);
  297. } else {
  298. this.bag = new Bag(n);
  299. }
  300.  
  301. this.genericPairBag = new GenericPairBag();
  302. this.amount = this.bag.retSize();
  303. this.round = 1;
  304. }
  305.  
  306. public void RES() {
  307. this.SET();
  308.  
  309. while(this.bag.getCur_size() != 1) {
  310. this.GET();
  311. this.event();
  312. }
  313.  
  314. System.out.println("Победитель: " + this.bag.remove().toString());
  315. }
  316.  
  317. private void SET() {
  318. if (this.bag.isEmpty()) {
  319. for(int i = 0; i < this.amount; ++i) {
  320. this.bag.add("'Команда " + (i + 1) + "'");
  321. }
  322. }
  323.  
  324. }
  325.  
  326. private void GET() {
  327. while(!this.bag.isEmpty()) {
  328. String str1 = (String)this.bag.remove();
  329. String str2 = (String)this.bag.remove();
  330. this.genericPairBag.add(str1, str2);
  331. }
  332.  
  333. }
  334.  
  335. private void event() {
  336. System.out.println("------------------------------------------------");
  337. if (this.genericPairBag.retSize() == 1) {
  338. System.out.println("Финал");
  339. } else {
  340. System.out.println("Раунд " + this.round);
  341. }
  342.  
  343. for(; !this.genericPairBag.isEmpty(); System.out.println()) {
  344. Pair cur = this.genericPairBag.remove();
  345. PrintStream var10000 = System.out;
  346. Object var10001 = cur.getFirst();
  347. var10000.println("Битва между " + var10001 + " и " + cur.getSecond());
  348. int point = (int)(Math.random() * 2.0D);
  349. if (point == 0) {
  350. this.bag.add(cur.getFirst());
  351. System.out.println("Победила " + cur.getFirst());
  352. } else {
  353. this.bag.add(cur.getSecond());
  354. System.out.println("Победила " + cur.getSecond());
  355. }
  356. }
  357.  
  358. ++this.round;
  359. System.out.println("------------------------------------------------");
  360. System.out.println();
  361. }
  362.  
  363. private void event2() {
  364. if (this.genericPairBag.retSize() == 1) {
  365. System.out.println("Финал");
  366. } else {
  367. System.out.println("Раунд " + this.round);
  368. }
  369.  
  370. for(; !this.genericPairBag.isEmpty(); System.out.println()) {
  371. Pair cur = this.genericPairBag.remove();
  372. PrintStream var10000 = System.out;
  373. Object var10001 = cur.getFirst();
  374. var10000.println("Битва между " + var10001 + " и " + cur.getSecond());
  375. Scanner IN = new Scanner(System.in);
  376. int point = IN.nextInt();
  377. if (point == 1) {
  378. this.bag.add(cur.getFirst());
  379. System.out.println("Победила " + cur.getFirst());
  380. } else {
  381. this.bag.add(cur.getSecond());
  382. System.out.println("Победила " + cur.getSecond());
  383. }
  384. }
  385.  
  386. ++this.round;
  387. System.out.println();
  388. }
  389. }
  390.  
  391. class DList<T1, T2> {
  392.  
  393. private ArrayList<T1> m_arr;
  394. private ArrayList<ArrayList<T2>> m_list_arr;
  395.  
  396. public DList() {
  397. m_arr = new ArrayList<>();
  398. m_list_arr = new ArrayList<>();
  399. }
  400.  
  401. public void addList(T1 list_id, ArrayList<T2> arr) {
  402. if (!m_arr.contains(list_id)) //if the id is not the list1
  403. {
  404. m_arr.add(list_id); //add id to the list1
  405. m_list_arr.add(arr); //add the list to the m_list_arr
  406. } else {
  407. int ind = m_arr.indexOf(list_id); //if the id is already int the list1, find index of that id
  408.  
  409. m_list_arr.add(ind, arr); //add the list to the respective list in m_list_arr
  410. }
  411. }
  412.  
  413. public void deleteList(int i) {
  414. if (i >= 0) {
  415. if (i < m_arr.size()) {
  416. m_arr.remove(i);
  417. m_list_arr.remove(i);
  418. } else {
  419. System.out.println("Индекс должен быть меньше чем " + m_arr.size());
  420. }
  421. } else {
  422. System.out.println("Индекс не должен быть отрицательным");
  423. }
  424. }
  425.  
  426. public void deleteList(T1 id) {
  427. if (m_arr.contains(id)) //if m_arr contains the id, find its index and delete it
  428. {
  429. int ind = m_arr.indexOf(id);
  430. m_arr.remove(ind);
  431. m_list_arr.remove(ind);
  432. } else {
  433. System.out.println("Здесь нет такого элемента");
  434. }
  435.  
  436. }
  437.  
  438. public ArrayList<T2> getList(int i) {
  439. ArrayList<T2> temp_list = null;
  440.  
  441. if (i >= 0) {
  442. if (i < m_arr.size()) {
  443. temp_list = m_list_arr.get(i);
  444. } else {
  445. System.out.println("Индекс должен быть меньше чем " + m_arr.size());
  446. }
  447. } else {
  448. System.out.println("Индекс должен быть неотрицательным");
  449. }
  450.  
  451. return temp_list;
  452. }
  453.  
  454. public ArrayList<T2> getList(T1 id) {
  455. ArrayList<T2> temp_list = null;
  456.  
  457. if (m_arr.contains(id)) {
  458. temp_list = m_list_arr.get(m_arr.indexOf(id));
  459. } else {
  460. System.out.println("Здесь нет элемента с таким значением");
  461. }
  462.  
  463. return temp_list;
  464. }
  465. }
  466.  
  467. class AWM {
  468.  
  469. private Integer[] coins;
  470.  
  471. public AWM(Integer[] arr_coins) {
  472. coins = arr_coins;
  473. }
  474.  
  475. public ArrayList<Integer> GetMoneySet(int amount_money) {
  476. int sum = 0;
  477. ArrayList<Integer> arr_coins = new ArrayList<>();
  478.  
  479. Arrays.sort(coins);
  480.  
  481. System.out.println("After sorting: ");
  482. System.out.println(Arrays.toString(coins));
  483.  
  484. int temp = amount_money;
  485.  
  486. for (int j = coins.length - 1; j >= 0; j--) {
  487. if (temp - coins[j] >= 0)
  488. {
  489. arr_coins.add(coins[j]);
  490. temp -= coins[j];
  491. }
  492. }
  493.  
  494. return arr_coins;
  495. }
  496. }
  497.  
  498. class Person {
  499. private String firstName;
  500. private String lastname;
  501. private int age;
  502.  
  503. public Person(String firstName, String lastname) {
  504. this.firstName = firstName;
  505. this.lastname = lastname;
  506. }
  507.  
  508. public Person(String firstName, String lastname, int age) {
  509. this.firstName = firstName;
  510. this.lastname = lastname;
  511. this.age = age;
  512. }
  513.  
  514. public String getFirstName() {
  515. return this.firstName;
  516. }
  517.  
  518. public String getLastname() {
  519. return this.lastname;
  520. }
  521.  
  522. public int getAge() {
  523. return this.age;
  524. }
  525.  
  526. public void view() {
  527. System.out.println("First name: " + this.firstName);
  528. System.out.println("Last name: " + this.lastname);
  529. System.out.println("Age: " + this.age);
  530. }
  531. }
  532.  
  533. abstract class HashFunction<K> {
  534. protected final int size;
  535.  
  536. public abstract int hash(K var1);
  537.  
  538. public HashFunction(int size) {
  539. this.size = size;
  540. }
  541. }
  542. class StrHashFunction<K> extends HashFunction<K> {
  543. public StrHashFunction(int size) {
  544. super(size);
  545. }
  546.  
  547. public int hash(K key) {
  548. int p = 53,p_pow=1;
  549. Integer hash=0;
  550. char[] arr = key.toString().toCharArray();
  551. for(var i:arr){
  552. hash += (i - 'a' + 1) * p_pow;
  553. p_pow *= p;
  554. }
  555.  
  556. return hash%size;
  557. }
  558. }
  559. class HashTable<K, T> {
  560. private final ArrayList<ArrayList<Pair<K, T>>> table;
  561. private final StrHashFunction<K> func;
  562. private final int size;
  563.  
  564. public HashTable(int n) {
  565. this.size = n;
  566. this.func = new StrHashFunction(this.size);
  567. this.table = new ArrayList(this.size);
  568.  
  569. for(int i = 0; i < n; ++i) {
  570. this.table.add(new ArrayList());
  571. }
  572.  
  573. }
  574.  
  575. public T find(K key) {
  576. int index = this.func.hash(key);
  577. Iterator var3 = (this.table.get(index)).iterator();
  578.  
  579. Pair l;
  580. do {
  581. if (!var3.hasNext()) {
  582. return null;
  583. }
  584.  
  585. l = (Pair)var3.next();
  586. } while(!l.getFirst().equals(key));
  587.  
  588. return (T) l.getSecond();
  589. }
  590.  
  591. public void add(K key, T value) {
  592. int hash = this.func.hash(key);
  593. (this.table.get(hash)).add(new Pair(key, value));
  594. }
  595.  
  596. public boolean delete(K key) {
  597. int index = this.func.hash(key);
  598. T del = this.find(key);
  599. if (del == null) {
  600. return false;
  601. } else {
  602. (this.table.get(index)).remove(key);
  603. return true;
  604. }
  605. }
  606. }
  607. public class Main {
  608.  
  609. public static void main(String[] args) {
  610.  
  611. Pair.make_pair(5, "s");
  612. Pair pair=new Pair<>();
  613. pair.setFirst(1);
  614. pair.setSecond("cat");
  615. pair.show();
  616. System.out.println();
  617. Bag bag = new Bag(5);
  618. bag.add(5);
  619. bag.add(1);
  620. bag.add(52);
  621. bag.add(-6);
  622. bag.add(3);
  623. bag.add(3);
  624. bag.add(4);
  625. bag.add(8);
  626. System.out.println(bag.retSize());
  627. System.out.println();
  628. System.out.println(bag.retElem(1));
  629. System.out.println();
  630. bag.show();
  631. bag.remove();
  632. bag.show();
  633. bag.remove();
  634. bag.show();
  635. bag.remove();
  636. bag.show();
  637.  
  638.  
  639. PairBag pairbag = new PairBag(5);
  640. pairbag.add(7, "cats");
  641. pairbag.add(5, "dogs");
  642. pairbag.add(10, "parrots");
  643. pairbag.add(2, "bears");
  644. pairbag.add(1, "tiger");
  645. pairbag.show();
  646.  
  647.  
  648. DList list=new DList<>();
  649. ArrayList<Integer> list1=new ArrayList<>();
  650. list1.add(1);
  651. list1.add(2);
  652. list1.add(5);
  653. list1.add(10);
  654.  
  655. list.addList(15,list1);
  656. }
  657. }
  658.  
  659.  
  660.  
  661.  
Advertisement
Add Comment
Please, Sign In to add comment