Interpretations: Hanoi towers problem (high-precision processing, pressure 20 4)

Subject description
given A, B, C three thin long enough column A column placed in the middle of 2n empty disk, there are n different sizes, each size has two identical discs, Note that these two discs are indiscriminate (following figure shows the case of n = 3). To move these countries now disc cartridge C, may be placed during movement of the B-pillar staging. Claim:

submit

(1) can only be moved a disc;

(2) A, B, C of the three slender columns must be kept small disc is large on the order;

Task: An set to a minimum number of moving 2n discs accomplish the above tasks required for n inputs, the output An.

Input
input file hanoi.in is a positive integer n, the A column placed in 2n discs.

Output
Output file hanoi.out only one row, comprising a positive integer, the above tasks required to complete a minimum number of moving An.

Input Sample
1
Sample Output
2
Tips

For 50% of the data, 1 <= n <= 25

Data for 100%, 1 <= n <= 200

An established and managed to An-1 of recurrence relations.

Ideas sources

//a[1]=2;a[n]=2*a[n-1]+2; 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[22];//四位一格,共80位 
int main()
{
 int n;
 while(cin>>n)
 {
 	memset(a,0,sizeof(a));
 	a[1]=2;
 	for(int i=2;i<=n;i++)
 	{
 	 int c=0;//进位 
 	 for(int j=1;j<=20;j++)
 	 {
 	  a[j]=a[j]*2+c;
	  if(j==1) a[j]+=2;//最低位+2 
	  c=a[j]/10000;
	  a[j]%=10000;	
	 }
	}
	int i=20;
	while(i>1&&!a[i]) i--; //去0 
	cout<<a[i];
	while(--i)
	 printf("%04d",a[i]);
	cout<<endl;
 }
 return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43540515/article/details/91983325