Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6. import java.util.NoSuchElementException;
  7. import java.util.Queue;
  8.  
  9.  
  10. public class card_trick {
  11.     public static int count(int N){
  12.         // Vasiot kod tuka
  13.         ArrayQueue<Integer> spil = new ArrayQueue<Integer>(51);
  14.         ArrayStack<Integer> drugite = new ArrayStack<Integer>(7);
  15.         int brojac = 0;
  16.        
  17.         // Polnenje na spilot
  18.         for(int i = 1; i <= 51; i++) {
  19.             spil.enqueue(i);
  20.         }
  21.        
  22.        
  23.         while(true) {
  24.             for(int i=1; i <= 7; i++) {
  25.                 int x = spil.dequeue();            
  26.                 drugite.push(x);
  27.             }
  28.             brojac++;
  29.             for(int i=1; i <= 7; i++) {
  30.                 int a = spil.dequeue();
  31.                 int b = drugite.pop();
  32.                 spil.enqueue(b);
  33.                 spil.enqueue(a);
  34.             }
  35.            
  36.             if(drugite.isEmpty()&&spil.peek() == N)
  37.                 break;
  38.         }
  39.        
  40.        
  41.         return brojac;
  42.     }
  43.  
  44.     public static void main(String[] args) throws NumberFormatException, IOException {
  45.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
  46.         System.out.println(count(Integer.parseInt(br.readLine())));
  47.     }
  48.  
  49. }
  50.  
  51. class ArrayQueue<E> implements Queue<E> {
  52.  
  53.     // Redicata e pretstavena na sledniot nacin:
  54.     // length go sodrzi brojot na elementi.
  55.     // Ako length > 0, togash elementite na redicata se zachuvani vo elems[front...rear-1]
  56.     // Ako rear > front, togash vo  elems[front...maxlength-1] i elems[0...rear-1]
  57.     E[] elems;
  58.     int length, front, rear;
  59.  
  60.     // Konstruktor ...
  61.  
  62.     @SuppressWarnings("unchecked")
  63.     public ArrayQueue (int maxlength) {
  64.         elems = (E[]) new Object[maxlength];
  65.         clear();
  66.     }
  67.  
  68.     public boolean isEmpty () {
  69.         // Vrakja true ako i samo ako redicata e prazena.
  70.         return (length == 0);
  71.     }
  72.  
  73.     public int size () {
  74.         // Ja vrakja dolzinata na redicata.
  75.         return length;
  76.     }
  77.  
  78.     public E peek () {
  79.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  80.         if (length > 0)
  81.             return elems[front];
  82.         else
  83.             throw new NoSuchElementException();
  84.     }
  85.  
  86.     public void clear () {
  87.         // Ja prazni redicata.
  88.         length = 0;
  89.         front = rear = 0;  // arbitrary
  90.     }
  91.  
  92.     public void enqueue (E x) {
  93.         // Go dodava x na kraj od redicata.
  94.         elems[rear++] = x;
  95.         if (rear == elems.length)  rear = 0;
  96.         length++;
  97.     }
  98.  
  99.     public E dequeue () {
  100.         // Go otstranuva i vrakja pochetniot element na redicata.
  101.         if (length > 0) {
  102.             E frontmost = elems[front];
  103.             elems[front++] = null;
  104.             if (front == elems.length)  front = 0;
  105.             length--;
  106.             return frontmost;
  107.         } else
  108.             throw new NoSuchElementException();
  109.     }
  110.  
  111.     public boolean contains(Object o) {
  112.         // TODO Auto-generated method stub
  113.         return false;
  114.     }
  115.  
  116.     public Iterator iterator() {
  117.         // TODO Auto-generated method stub
  118.         return null;
  119.     }
  120.  
  121.     public Object[] toArray() {
  122.         // TODO Auto-generated method stub
  123.         return null;
  124.     }
  125.  
  126.     public Object[] toArray(Object[] a) {
  127.         // TODO Auto-generated method stub
  128.         return null;
  129.     }
  130.  
  131.     public boolean remove(Object o) {
  132.         // TODO Auto-generated method stub
  133.         return false;
  134.     }
  135.  
  136.     public boolean containsAll(Collection c) {
  137.         // TODO Auto-generated method stub
  138.         return false;
  139.     }
  140.  
  141.     public boolean addAll(Collection c) {
  142.         // TODO Auto-generated method stub
  143.         return false;
  144.     }
  145.  
  146.     public boolean removeAll(Collection c) {
  147.         // TODO Auto-generated method stub
  148.         return false;
  149.     }
  150.  
  151.     public boolean retainAll(Collection c) {
  152.         // TODO Auto-generated method stub
  153.         return false;
  154.     }
  155.  
  156.     public boolean add(Object e) {
  157.         // TODO Auto-generated method stub
  158.         return false;
  159.     }
  160.  
  161.     public boolean offer(Object e) {
  162.         // TODO Auto-generated method stub
  163.         return false;
  164.     }
  165.  
  166.     public E remove() {
  167.         // TODO Auto-generated method stub
  168.         return null;
  169.     }
  170.  
  171.     public E poll() {
  172.         // TODO Auto-generated method stub
  173.         return null;
  174.     }
  175.  
  176.     public E element() {
  177.         // TODO Auto-generated method stub
  178.         return null;
  179.     }
  180. }
  181.  
  182. interface Stack<E> {
  183.  
  184.     // Elementi na stekot se objekti od proizvolen tip.
  185.  
  186.     // Metodi za pristap:
  187.  
  188.     public boolean isEmpty ();
  189.         // Vrakja true ako i samo ako stekot e prazen.
  190.  
  191.     public E peek ();
  192.         // Go vrakja elementot na vrvot od stekot.
  193.  
  194.     // Metodi za transformacija:
  195.  
  196.     public void clear ();
  197.         // Go prazni stekot.
  198.  
  199.     public void push (E x);
  200.         // Go dodava x na vrvot na stekot.
  201.  
  202.     public E pop ();
  203.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  204. }
  205.  
  206. class ArrayStack<E> implements Stack<E> {
  207.  
  208.     // Stekot e pretstaven na sledniot nacin:
  209.     //depth e dlabochinata na stekot, a
  210.     // elems[0...depth-1] se negovite elementi.
  211.     private E[] elems;
  212.     private int depth;
  213.  
  214.     @SuppressWarnings("unchecked")
  215.     public ArrayStack (int maxDepth) {
  216.         // Konstrukcija na nov, prazen stek.
  217.         elems = (E[]) new Object[maxDepth];
  218.         depth = 0;
  219.     }
  220.  
  221.  
  222.     public boolean isEmpty () {
  223.         // Vrakja true ako i samo ako stekot e prazen.
  224.         return (depth == 0);
  225.     }
  226.  
  227.  
  228.     public E peek () {
  229.         // Go vrakja elementot na vrvot od stekot.
  230.         if (depth == 0)
  231.             throw new NoSuchElementException();
  232.         return elems[depth-1];
  233.     }
  234.  
  235.  
  236.     public void clear () {
  237.         // Go prazni stekot.
  238.         for (int i = 0; i < depth; i++)  elems[i] = null;
  239.         depth = 0;
  240.     }
  241.  
  242.  
  243.     public void push (E x) {
  244.         // Go dodava x na vrvot na stekot.
  245.         elems[depth++] = x;
  246.     }
  247.  
  248.  
  249.     public E pop () {
  250.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  251.         if (depth == 0)
  252.             throw new NoSuchElementException();
  253.         E topmost = elems[--depth];
  254.         elems[depth] = null;
  255.         return topmost;
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement