1 条题解

  • 0
    @ 2021-6-14 23:29:40

    C :

    #include <stdio.h>
    #include <math.h>
    struct list
    {
    	int left;
    	int data;
    	char ch;
       int right;
    	};
    struct list tree[2050];
    
    void print(int root)
    {
    	if(root!=-1)
    	  {
    		  print(tree[root].left);
    		  print(tree[root].right);
    		  printf("%c",tree[root].ch);
    		  }
    	}
    main()
    {
    	
    	int i=1,n;
    	//FILE *fin=fopen("fbi.in","r");
    	scanf("%d",&n);
    	int front,len,node;
    	front=pow(2,n);
    	len=pow(2,n+1);
    	node=pow(2,n-1);
    	  
    	
    	
    	for(i=front;i<len;i++)
    	 {
    		 scanf("%1d",&tree[i].data);
    		 tree[i].left=-1;
    		 tree[i].right=-1;
    		 if(tree[i].data==1)
    		   tree[i].ch='I';
    		 else
    		   tree[i].ch='B';
    		 }
    		/*for(i=1;i<len;i++)
    		 printf("%c ",tree[i].ch);
    		 printf("\n");*/
        int temp=n-1;
        while(temp>=0)
          {
    	      for(i=pow(2,temp);i<pow(2,temp+1);i++)
    	        {
    		        tree[i].left=2*i;
    		        tree[i].right=2*i+1;
    		        if(tree[2*i].ch=='B' && tree[2*i+1].ch=='B')
    		        tree[i].ch='B';
    		        else if(tree[2*i].ch=='I' && tree[2*i+1].ch=='I')
    		        tree[i].ch='I';
    		        else tree[i].ch='F';
    		        }
    	      temp--;
    	      }
    	   print(1);
    	  
    	}
    

    C++ :

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int m;
    char str[3333];
    char put[3] = {'B', 'I' ,'F'};
    int dfs(int sx,int ex)
    {
        int a,b;
        if(sx==ex)
        {
            printf("%c", put[str[sx] - '0']);
            return str[sx] - '0';
        }
        a=dfs(sx,(sx+ex)/2);
        b=dfs((ex+sx)/2+1,ex);
        if(a == b)
            {
            printf("%c", put[a]);
            return a;
            }
        else
            {
            cout<<'F';
           return 2;
           }
    }
    int main()
    {
        scanf("%d",&m);
        scanf("%s",str);
        dfs(0,strlen(str)-1);
        cout<<endl;
        return 0;
    }
    
    

    Pascal :

    var
       n,i,j:longint;
       s:ansistring;
       a:array[0..100]of longint;
       p:array[0..2050,0..2050]of char;
       f:array[0..2050,0..2050]of boolean;
    procedure dfs(t,x:longint);
    var
       i:longint;
    begin
     if t>n+1 then exit;
     for i:=x to a[t] do
      begin
       if p[t,i]=' ' then exit;
       if f[t,i] then continue;
       write(p[t,i]);f[t,i]:=true;
       if i mod 2=0 then
        begin
         if p[t,i]=p[t,i-1]then p[t+1,i div 2]:=p[t,i] else p[t+1,i div 2]:='F';
         dfs(t+1,i div 2);
        end;
      end;
    end;
    begin
     readln(n);
     a[n+1]:=1;
     for i:=n downto 1 do a[i]:=a[i+1]*2;
     for i:=1 to a[1] do
      for j:=1 to n do p[j,i]:=' ';
     readln(s);
     for i:=1 to a[1] do
      if s[i]='1' then p[1,i]:='I' else p[1,i]:='B';
     dfs(1,1);
    end.
    
    • 1