プログラミング法則3


/****************************************************************** 

       6   :1/2,3/5,4/7,6/10,8/13,9/15 ,     

   n (n<2000)  。 

  :             ,                  

 ,  2 ,3/5  5 = 3+2  ,    4/7  7 = 4+3; 

          ,       ,   ,     (      

  )        ,       ,         ,     

   ,                         。    

     ,      ,       ,          ,   

       ,      ,                  , 

  a[0] = 1,     ;a[2] = 2,     ; 

*******************************************************************/ 

  

/*******************************************************************************
Copyright (c)                    。



   : Test_6_2.java 

  :        3
  :    
  :2013/10/21

*******************************************************************************/


import java.util.Scanner; 

class Test_6_2{ 

         //           

         void fullNumberArray(int a[], int n){ 

                   a[0] = 1; 

                   a[1] = 2;//    

                   int t; 

                   //           

                   for(int i = 2;i < 2 * n;i = i + 2) 

                   { 

                            //      

                            t = a[i -2] + 1;//       

                            //         ,     t    ; 

                            int j = i -1; 

                            while(true) 

                            { 

                                     if(t > a[j])  break;//         t,    ,   

                                     //       t,t 1,         t 

                                     if(t == a[j])  {t ++;j = i - 1;} 

                                     else j = j - 2;//      

                            } 

                            a[i] = t;//   

                            a[i+1] = a[i] + i/2 + 1;//   

                   } 

         }    

         

         public static void main(String []args){ 

                   Test_6_2 test = new Test_6_2(); 

                   Scanner sc = new Scanner(System.in); 

                   System.out.println("Please input n:"); 

                   int n = sc.nextInt(); 

                   int [] a = new int [2 * n + 10];//      ,         

                   test.fullNumberArray(a,n); 

                   //        

                   for(int i = 0; i < 2 * n;i = i + 2) 

                            { 

                                     System.out.print(a[i] + "/"  + a[i+1] + " "); 

                                     if((i + 2) % 20 == 0)  System.out.println();//    10  

                            } 

         } 

}