忘了是谁给我发的了,邮件我删了.只好在这里回了
有人说很难看懂,所以今天我研究了一会,把大家好理解的发上来
#include<iostream>
using namespace std;
void shuchu(int m,int n)
{
for(int t=n,y=t&1,c=m;y-1;t=t>>1,y=t&1,c--);
switch((c&1)*3+(t>>1)%3) //这里最厉害
{
case 0:cout<<n<<":A-B\n";break;
case 1:cout<<n<<":B-C\n";break;
case 2:cout<<n<<":C-A\n";break;
case 3:cout<<n<<":A-C\n";break;
case 4:cout<<n<<":C-B\n";break;
case 5:cout<<n<<":B-A\n";break;
}
}
void main()
{
int c=1,m,n=1;
cout<<"Input the number of the pale! 0-30"<<endl;
cin>>m;
cout<<"Move"<<m<<"pales:"<<endl;
for(c<<=m;n<c;n++)
shuchu(m,n);
}
我修改过的:
#include<iostream>
using namespace std;
int fn(int c,int m);
void shuchu(int m,int n)
{
for(int t=n,y=t%2,c=m;y-1;t/=2,y=t%2,c--);
switch((c%2)*3+(t/2)%3)
{
case 0:cout<<n<<":A-B\n";break;
case 1:cout<<n<<":B-C\n";break;
case 2:cout<<n<<":C-A\n";break;
case 3:cout<<n<<":A-C\n";break;
case 4:cout<<n<<":C-B\n";break;
case 5:cout<<n<<":B-A\n";break;
}
}
void main()
{
int m,n=1;
cout<<"Input the number of the pale! 0-30"<<endl;
cin>>m;
cout<<"Move"<<m<<"pales:"<<endl;
for(int c=fn(c=1,m);n<c;n++)
shuchu(m,n);
}
int fn(int c,int m)
{
for(int n=0;n<m;n++)
c*=2;
return c;
}
不过,这个的思维我真佩服,我怎么也想不到
[休闲]小小解析一下代码