1 条题解

  • 0
    @ 2024-12-24 9:54:28

    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;
    }
    
    
    • 1

    信息

    ID
    1093
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者