1 条题解

  • 0
    @ 2021-6-14 23:25:17

    C++ :

    #include<iostream>
    #include<cmath>
    using namespace std;
    int flag[70][70];
    int main()
    {
    	int i,j,k,n;
    	cin>>n;
    	int p=pow(2,n); 
    	for (j=1;j<=p-1;j++)
    	{
    		cout<<"<"<<j<<">";
    		int a[70]={0},cur=1,last;
    		for (cur=1;cur<=p;cur++)
    		{
    			if (cur&1)
    				{
    				for (i=1;i<=p;i++) 
    					if (a[i]==0) 
    					{
    						cout<<i<<"-";a[i]=1;last=i;break;
    					}
    		       }
    			else 
    			{
    			   for(i=1;i<=p;i++) 		
    				if (a[i]==0 && flag[last][i]==0)
    				{
    					if (cur!=p)cout<<i<<",";
    					else cout<<i;
    					a[i]=1; flag[last][i]=1;
    					break;
    				} 
    			}
    		}
    		cout<<endl;
    	}
    	return 0;
    }
    

    Pascal :

    var i,j,k,m,n,half,s:integer;
    a:array [1..1000,1..1000] of integer;
    b:array[1..1000]of boolean;
    begin
        read(m);
        n:=1;
        for i:=1 to m do n:=n*2;
        k:=1;a[1,1]:=1;half:=1;
        while k<=m do
         begin
             for i:=1 to half do
              for j:=1 to half do
               a[i,j+half]:=a[i,j]+half;
             for i:=1 to half do
              for j:=1 to half do
               begin
                  a[i+half,j]:=a[i,j+half];
                  a[i+half,j+half]:=a[i,j];
               end;
              half:=half*2;
              k:=k+1;
         end;
        for i:=2 to n do
         begin
             s:=0;
             write('<',i-1,'>');
             fillchar(b,sizeof(b),true);
             for j:=1 to n do
              if b[j] then
               begin
                  write(j,'-',a[j,i]);
                  b[j]:=false;
                  b[a[j,i]]:=false;
                  inc(s);
                  if s<>n div 2 then write(',');
               end;
             writeln;
         end;
    end.
    
    
    • 1

    信息

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