Martina312

Трик со карти

Nov 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.NoSuchElementException;
  5.  
  6. class SLLNode<E> {
  7. protected E element;
  8. protected SLLNode<E> succ;
  9.  
  10. public SLLNode(E elem, SLLNode<E> succ) {
  11. this.element = elem;
  12. this.succ = succ;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return element.toString();
  18. }
  19. }
  20.  
  21. interface Stack <E>{
  22. public boolean isEmpty(); //dali stekot e prazen
  23.  
  24. public E peek(); // go vrkja elementot na vrvot bez da go izbrishe
  25.  
  26. public void clear(); // go brishe stekot
  27.  
  28. public void push(E x); //dodava element na vrvot
  29.  
  30. public E pop(); //brishe element od vrvot i ni go vrakja istiot
  31. }
  32.  
  33. class ArrayStack<E> implements Stack<E> {
  34. private E [] elems; //niza od elementite vo stekot so indeksi [0,depth-1]
  35. private int depth; //dlabocina na stekot odnosno kolku elementi ke ima
  36.  
  37. public ArrayStack(int maxDepth){
  38. elems=(E[]) new Object [maxDepth]; //maxDepth e maksimalnata golemina na nizata!!! Razlicno od depth shto e momentalna golemina!!!
  39. depth = 0; //inicijalno bidejki nemame element depth e 0
  40. }
  41.  
  42.  
  43. @Override
  44. public boolean isEmpty() {
  45. return (depth==0); //ako e 0, vrakja true - stekot nema elementi, ako ne stekot ima elementi i vrakja true
  46. }
  47.  
  48. @Override
  49. public E peek() {
  50. if(depth==0)
  51. throw new NoSuchElementException();
  52. return elems[depth-1]; //ke go vrati posledniot
  53. }
  54.  
  55. @Override
  56. public void clear() {
  57. for(int i=0;i<depth;i++){
  58. elems[i]=null;
  59. }
  60. depth=0;
  61. }
  62.  
  63. @Override
  64. public void push(E x) {
  65. elems[depth]=x;
  66. depth++;
  67. }
  68.  
  69. @Override
  70. public E pop() {
  71. if(depth==0)
  72. throw new NoSuchElementException();
  73. E topmost=elems[--depth];
  74. elems[depth]=null;
  75. return topmost;
  76.  
  77. /*
  78. E topmost=elems[depth-1];
  79. elems[depth-1]=null;
  80. depth--;
  81. return topmost;*/
  82. }
  83. }
  84.  
  85. interface Queue<E>{
  86. public boolean isEmpty(); // proveruva dali redicata e prazna
  87.  
  88. public int size(); // ja vrakja dolzinata na redicata
  89.  
  90. public void clear(); // ja prazni redicata
  91.  
  92. public E peek(); // ni go dava elementot koj se naogja na pocetokot -front,glava
  93.  
  94. public void enqueue(E x); // dodava element na krajot na redicata
  95.  
  96. public E dequeue(); //go brishe elementot na pocetok na redicata i ni ja vrakja negovata vrednost
  97. }
  98.  
  99. class LinkedQueue<E> implements Queue<E> {
  100. private SLLNode<E> front, rear; //front e link do prviot jazel od redicata, rear e link do posledniot jazel od redicata
  101. private int length; //goleminata na redicata
  102.  
  103. public LinkedQueue(){
  104. clear();
  105. }
  106.  
  107. @Override
  108. public boolean isEmpty() {
  109. return (length==0);
  110. }
  111.  
  112. @Override
  113. public int size() {
  114. return length;
  115. }
  116.  
  117. @Override
  118. public void clear() {
  119. front=rear=null;
  120. length=0;
  121. }
  122.  
  123. @Override
  124. public E peek() {
  125. if(front==null)
  126. throw new NoSuchElementException();
  127. return front.element;
  128. }
  129.  
  130. @Override
  131. public void enqueue(E x) {
  132. SLLNode<E> latest = new SLLNode<>(x, null); //pravime nov jazol koj ke go sodrzi xi ke pokazuva na nula
  133. if (rear != null) { //ako ne stanuva zbor za prv element
  134. rear.succ = latest;
  135. rear = latest;
  136. } else {
  137. front = rear = latest; // ako e prv element vo listata voedno e i posleden pa i rear i front ke pokazuvaat na nego
  138. }
  139. length++;
  140. }
  141.  
  142. @Override
  143. public E dequeue() {
  144. if(front!=null){ //ako redicata ima elementi
  145. E frontmost=front.element;
  146. front=front.succ; //noviot front e front successor
  147. if (front == null) { //ama ako naredniot front e null, t.e ako se vadelo eden element a redicata imala samo eden element
  148. rear=null; //znaci deka i rear e null t.e redicata e prazna!!!
  149. }
  150. length--;
  151. return frontmost;
  152. }
  153. else throw new NoSuchElementException();
  154. }
  155. }
  156.  
  157. public class card_trick {
  158.  
  159.  
  160. public static int count(int N){
  161.  
  162. LinkedQueue<Integer> shpil = new LinkedQueue<Integer>();
  163. ArrayStack<Integer> stek = new ArrayStack<Integer>(7);
  164. int brojac = 0;
  165. boolean flag = false;
  166.  
  167. for(int i=1;i<52;i++) {
  168. shpil.enqueue(i);
  169. }
  170.  
  171. while (flag == false) {
  172. for (int i = 0; i < 7; i++) {
  173. stek.push(shpil.dequeue());
  174. }
  175.  
  176. while (!stek.isEmpty()) {
  177. shpil.enqueue(stek.pop());
  178. shpil.enqueue(shpil.dequeue());
  179. }
  180. brojac++;
  181. if (shpil.peek() == N) {
  182. return brojac;
  183. }
  184. flag = false;
  185. }
  186. return 0;
  187.  
  188. }
  189.  
  190.  
  191. public static void main(String[] args) throws NumberFormatException, IOException {
  192. BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
  193. System.out.println(count(Integer.parseInt(br.readLine())));
  194. }
  195.  
  196. }
Add Comment
Please, Sign In to add comment