2021-01-21 summary

2021-01-21 summary

This afternoon, I took a closer look at Yjx's summary about the Tower of Hanoi. The simulation and summary of the operation of the Tower of Hanoi are really in place. Quoted below.

In summary, for an N-story Hanoi Tower, first complete a (N-1)-story Hanoi Tower on the B-pillar, then put n on the C-pillar, and finally complete a (N-1) on the B-pillar The tower of Hanoi, a[N]=2 * a[N-1]+1. (Core conclusion 1)
In order to make the total number of moves as small as possible, the number of moves from 1 to n-1 should be as few as possible. Therefore, if conditions permit, you can move the large disk to the destination first, which may make the n-1 stack move less once, saving a lot of operations; but if the n-1 layer repeatedly operates multiple groups in the process of operating the large disk, the operations will increase. This process is difficult to judge specifically, so we try to classify and compare, first operate according to the basic idea from big to small, and then operate according to the idea of ​​first shifting to big, compare which operation is less, and then output this one separately The operation process is fine.
A Tower of Hanoi problem can sometimes be divided into multiple tower operations. In order to make the overall number of operations of a Tower of Hanoi as few as possible, the number of operations on the part with the largest number of layers should be as few as possible. (Core conclusion 2)

In addition, if I hadn't read the blog of classmate yjx, I'm afraid I haven't found out that the question about the new Hanoi Tower is actually incomplete and lacks discussion. Thank you for your reminder here.
In the process of doing the questions recently, I found that a lot of previous knowledge (such as the high precision that will be used later) I have almost forgotten, so this afternoon I will review it with questions like the Tower of Hanoi.

Regarding the question about the Tower of Hanoi, if it is a simple question to find the minimum number of steps and the data range is relatively large, the following two traditional methods cannot be AC, only partial points can be obtained.

//基本汉诺塔问题 (递归法)
#include<bits/stdc++.h>
using namespace std;
int ans=0;
void cwy(int n,char a,char c,char b)
//函数表示共n片圆盘,将A上的圆盘借助B,移动至C
//(第一个int为圆盘数,第二个char为圆盘所在,
//第三个char为目标柱子,第四个char为借助的柱子)
{
    
    
	if(n==0) return;
	cwy(n-1,a,b,c);
	ans++;//若想知道具体的移动方法,
	//也就是变成了前几天的双色汉诺塔题,就在这里加上:
	//printf("%d %c %c\n",n,a,c);
	cwy(n-1,b,c,a);
}
int main() {
    
    
	int n;
	scanf("%d",&n);
	cwy(n,'A','B','C');
	printf("%d",ans);//这里输出最少步数; 
	return 0;
}
//基本汉诺塔问题(数学法) 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	long long n,ans=0;
	scanf("%lld",&n);
	ans=pow(2,n)-1;//直接用地球人都知道的公式;
	printf("%lld",ans); 
	return 0;
} 

So in order to deal with the problem of too large numbers at this time, you need:
high precision

#include<bits/stdc++.h>
using namespace std;
char a[100001];
int n,len=1;//这里的len是用来记录长度的,因为我是正着算,倒着输出,
//为了避免输出前面一大堆空格,所以用len记一下长度;
void cwy(){
    
    
	for(int i=1;i<=len;i++){
    
    
		a[i]*=2;//由于我们知道是二的n次方,所以像竖式乘法一样,每位乘二就行了;
	}
	for(int i=1;i<=len;i++){
    
    
		if(a[i]>9){
    
    
			a[i+1]++;//满十进一位;
			a[i]-=10;
		}
	}
	if(a[len+1]>0){
    
    
		len++;
	}
}
int main()
{
    
    
	scanf("%d",&n);
	a[1]=1;
	for(int i=1;i<=n;i++){
    
    
		cwy();
	}
	for(int i=len;i>1;i--){
    
    
		printf("%d",a[i]);
	}
	printf("%d",a[1]-1);
	return 0;	
} 

Like this, you can AC.

Guess you like

Origin blog.csdn.net/chenweiye1/article/details/112970652