FBI的後續根遍歷---遞歸思想

我的首次 遞歸 ,寫了好久 ,特放上來紀念
—純原創
package new50;

import java.util.Scanner;

public class newFBI {

static int n;
static int k;
static int[] N;
static int x;
static String s;
static String[][] ss;

public static void main(String[] args) {
	Scanner Scan=new Scanner(System.in);
    while(Scan.hasNext()){
    	 n=Integer.parseInt(Scan.nextLine());
    	 N=new int[n+1];
    	 s=Scan.nextLine();
    	 ss=new String[n+1][2];
    	  x=-1;
    	  k=0;
    	if(n==0) {judge(s); System.out.println();} 
    	else 
    	recursion();
    }
}//main

static void recursion(){
	boolean hasTwo = false; int p=100;
	for(int i=0;i<=n-1;i++)
		if(N[i]==2)
		{
			hasTwo=true;  p=i; 
			break;
		}
   if(!hasTwo){
	   x++; judge(""+s.charAt(x));
	   N[0]++;
	   ss[0][k++]=""+s.charAt(x);
	   if(k==2) k=0;
	   
   }//if
   else {
	   N[p]=0;
	   N[p+1]++;
	   if(N[p+1]==1)
	   {
		   ss[p+1][0]=ss[p][0]+ss[p][1];
		   judge(ss[p+1][0]);
	   }
	   else 
	   {
		   ss[p+1][1]=ss[p][0]+ss[p][1];
		   judge(ss[p+1][1]);
	   }
	   
   }//else
   if(p+1!=n){
	   recursion(); return;
   }
   else { System.out.println();   return;}
	
}//recursion

static void judge(String st){
	if(st.contains("0")&&st.contains("1"))
	   System.out.print("F");
	else {
		if(st.contains("0"))
			System.out.print("B");
		else System.out.print("I");
	}
	
}

}

//若存在N[i]=2,則應先處理N[i],使N[i]歸0,並向上進位
//若不存在N[i]=2,則hasTwo=false — !hasTwo=true;
//則 N[i]=0 或 N[i]=1 , 即 N[0]=0 或 1,
//若 N[0]爲 0 或 1,-----因爲 前方不存在 N [i]=2, 那麼 N[i] 就需要通過繼續使 N[0] 繼續增加來達到 成爲 2.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章