Home > Software engineering >  Can help to verify the non-recursive completely solve the problem of Hanoi tower c code?
Can help to verify the non-recursive completely solve the problem of Hanoi tower c code?

Time:11-25

I use math, find out how many steps, what's the date column to date pillars, each step can be determined directly, and then I wrote this program verification, my mathematical ideas are right,
Blog has been audit, I can send the BBS,

//helloworld. CPP: Defines the entry point for the console application.
//
//by Chen ink fairy 2019-07-18
//no recursive XieHan north tower
# include "stdafx. H"
#include
Int h [34];//in order to facilitate understanding, 0 element no. 33 plate
Int a [4] [34].
Int b [4];
Int jihao;
Int dijici;

Void printH ()
{
//system (" CLS ");
for(int i=1; i<=34; I++)
{
Printf (" % d: % d \ t ", I, a [3] [I]);
}

}
Bool jiancha (int * h)//check below are small number
{
Int d [4];
D [1]=0;
D [2]=0;
D [3]=0;
For (int j=1; J<4. J++)
{
for(int i=1; i<34. I++)
{
[j] [I] the if (a==0 | | a [j] [I] & gt; A [j]] [I - 1)
{

}
The else
{
Printf (" error a [% d] [% d]=% d a [% d] [% d]=% d ", j, I - 1, a [j]] [I - 1, j, I, a [j] [I]);
The scanf (" % d ");
return false;
}
}
}
return true;
}
Bool shunxu (int zhuzi)
{
for(int i=1; i{
If (a [zhuzi] [I]==34 - I)
{
}
The else
{
return false;
}
}
return true;
}
Bool chenggong (int n, int zhuzi)//n number plates are moved to 3
{

Int d=0;
Int t=34;
For (int I=33; I> 33 - n. I -)
{
If (a [zhuzi] [34 and I]!=I - n + 1)
{
return false;

}
}

return true;

}
Int jc3 ()//test 3 column plates have several
{

Int d=0;
Int t=34;
For (int I=33; I> 0; I -)
{
[I] if (h==3)
{
D=d + 1;
T=I;
}
}
Return d;
}
Int jc1 ()//test 1 column plates have several
{

Int d=0;
Int t=34;
For (int I=33; I> 0; I -)
{
[I] if (h==1)
{
D=d + 1;
T=I;
}
}
Return d;
}
Void initH ()
{
for(int i=0; i<34. I++)
{
H [I]=1;
A [1] [I]=I;
A [2] [I]=0;
A [3] [I]=0;
}
B [1]=34;
B [2]=1;
B [3]=1;
}
Int jc2 ()//test 2 column from 33 plates have several
{

Int d=0;
Int t=34;
For (int I=33; I> 0; I -)
{
[I] if (h==2)
{
D=d + 1;
T=I;
}
}
Return d;
}
Int getTop (int zhuzi)
{
Int d=0;
Int t=0;
for(int i=1; i<34. I++)
{
If (h==[I] zhuzi & amp; & I> T)
{

T=I;
}
}
return t;
}
Bool jiou (int s)
{
If (s % 2==0)
{
return true;
}
The else
{
return false;
}
}
Void change (int, int, yuan int mubiao)
{
If (h==[I] yuan)
{
H [I]=mubiao;
A [mubiao] [mubiao] [b]=I;
B=b [mubiao] [mubiao] + 1;
[yuan] a [yuan] [b]=0;
B=b [yuan] - [yuan] 1;
Printf (" h] [% d: % d - & gt; % d ", I, yuan, mubiao);
}
The else
{
Printf (" Error h [% d]=% d ", I, h [I]);
The scanf (" % d ");
}
Jiancha (h);
}
Int FastLog2 (int x)
{
Float fx.
Unsigned long ix, exp;

Fx=(float) x;
Ix=* (unsigned long *) & amp; Fx.
Exp=(ix & gt;> 23) & amp; 0 XFF.

Return exp - 127;
}
Int chu2 (int n, int cishu)
{
For (int j=1; J<=cishu; J++)
{
N=n/2;
}
Return n.
}
Void jihaopan (int n)
{
Int I=1;
Int yuan=n;
While (1)
{
If (n % 2==1)
{
Jihao=I;
Dijici=chu2 (yuan, I) + 1;
break;
}
N=n/2;
I=I + 1;
}

//printf (" how many times have number plate % d, % d ", jihao, dijici);
}
Int main (int arg c, char * argv [])
{
Printf (" Hanoi! \n");
Int times=0;
InitH ();
Int ji=1;
for(int i=1; i<5559060534555523; I++)
{
Jihaopan (I);
If (jiou (jihao)==false)
{
Int TMP.
TMP=dijici % 3;
If (TMP==1)
{
Change (getTop (1), 1, 3);
}
Else if (TMP==2)
{
Change (getTop (3), 3, 2);
}
The else
{
Change (getTop (2), 2, 1);
}
}
The else
{
Int TMP.
TMP=dijici % 3;
If (TMP==1)
{
Change (getTop (1), 1, 2);
}
Else if (TMP==2)
{
Change (getTop (2), 2, 3);
}
The else
{
Change (getTop (3), 3, 1);
}
}
If (chenggong (33, 3))
{
Printf (" SSSSS ");
break;
}
}



return 0;
}

CodePudding user response:

The number of plates must be odd, even I also calculate, plate is odd or even number, total move in a different direction,

CodePudding user response:

Suppose plate for odd number, total goal from 1 to 3 columns,
Odd number plate moving order cycle is 1 - & gt; 3 3 - & gt; 2-2 & gt; 1
Even number plate 1 - & gt; 2-2 & gt; 3 3 - & gt; 1

The first step n is the number plate first visit mobile?
Assuming the top plate 1, increases downwards,

N % 2=1 1 n/2 + 1 mobile
N % 2=0 (n/2) % 2=1 2 plate moves (n/2/2) + 1
So on

According to the odd number plate even number plate and mobile number to take more than 3, pillars can be figured out what's the date to the date column

CodePudding user response:

With a standard type recursive bai comparing to the Hanoi program calculating

CodePudding user response:

Has been a success,
This is to download address

https://github.com/corivsky/corivsky

CodePudding user response:

nullnullnullnullnullnullnullnull
  • Related