2014년 1월 1일 수요일

자바 큐를 이용한 피보나치 수열, 자바알고리즘, 자바자료구조[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육]

자바 큐를 이용한 피보나치 수열, 자바알고리즘, 자바자료구조[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육] 



자바 큐를 이용한 피보나치 수열, 자바알고리즘, 자바자료구조
 
import java.io.*;
interface Queue  {  
           public boolean isEmpty();
     public boolean isFull();
           public Object getFrontElement();
           public Object getRearElement();
           public void put(Object theObject);
           public Object remove();
}
public class ArrayQueue implements Queue
     {
           // data members
          int front;          // one counterclockwise from first element
          int rear;           // position of rear element of queue
          Object [] queue;    // element array
         
          public ArrayQueue(int initialCapacity)
         {
               if (initialCapacity < 1)
                    throw new IllegalArgumentException
                      ("initialCapacity must be >= 1");
               queue = new Object [initialCapacity + 1];
               // default front = rear = 0
         }   
   
       public boolean isEmpty() {
      return front == rear;
       }
   
       public boolean isFull() {
      return (rear+1) % queue.length == front;
       }
      
       public Object getFrontElement() {
           if (isEmpty())  return null;
           else return queue[(front + 1) % queue.length];
      }
     
      public Object getRearElement() {
          if (isEmpty())  return null;
          else return queue[rear];
     }
   
     public void put(Object theElement)  {
          // increase array length if necessary
          if ((rear + 1) % queue.length == front) {
            // double array size
            // code to double array size comes here
         }
         // put theElement at the rear of the queue
         rear = (rear + 1) % queue.length;
         queue[rear] = theElement;
     }   


      public Object remove()  {
           if (isEmpty())
               return null;
           front = (front + 1) % queue.length;
          Object frontElement = queue[front];
          queue[front] = null;  // enable garbage collection
          return frontElement;
      }
  }
 
 
class FiboWithQueue {
 void fibo(int n) {
  int[] fib = new int[n];
  int a, b;
  ArrayQueue queue = new ArrayQueue(2);
  fib[0] = fib[1] = 1;
  queue.put(new Integer(1));  // fib(0)
  queue.put(new Integer(1));   //fib(1)
  for(int i=2; i<n; i++) {
   a = ((Integer)queue.getFrontElement()).intValue();
   b = ((Integer)queue.getRearElement()).intValue();
   fib[i] = a + b;
   queue.remove();
   queue.put(new Integer(fib[i]));
  }
  //------------ 수열 출력
  for(int i=0; i<fib.length; i++) {
   System.out.println(fib[i] + " ");
  }
 }
 public static void main(String[] args) {
  if (args.length < 1) {
   System.out.println("Usage : java FiboWithQueue number");
   System.exit(1);
  }
  int n = Integer.parseInt(args[0]);
  FiboWithQueue f = new FiboWithQueue();
  f.fibo(n);
 }
}
 
<li tabindex="0" class="ui-state-default ui-corner-top ui-tabs-active ui-state-active" role="tab" aria-selected="true" aria-controls="tabs-1" aria-labelledby="ui-id-1">JAVA</li><li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-2" aria-labelledby="ui-id-2">ORACLE</li><li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-3" aria-labelledby="ui-id-3">iPhone/Android</li><li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-4" aria-labelledby="ui-id-4">.NET</li><li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-5" aria-labelledby="ui-id-5">표준웹/HTML5</li><li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-6" aria-labelledby="ui-id-6">채용/취업무료교육</li><li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-7" aria-labelledby="ui-id-7">초보자(재학생)코스</li>

댓글 없음:

댓글 쓰기