我的首次 递归 ,写了好久 ,特放上来纪念
—纯原创
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.