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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值