P1760 Babel Tower of Hanoi

Topic background

· A small road Babel direct Adventures Part IV

Title Description

In your help, small A successfully collected valuable data, he finally came to the legendary sky road connection through the Tianshan Mountains. But this is still some distance from the road the sky, but suddenly discovered that he had no A small map! ! ! But fortunately, he found a chest at the foot of the mountain. Judgments based on experience (A small experienced it?), Which should be on the map! On the chest, there are three columns and n discs in a column. A little after a long judgment, that this is the hanoi tower! (It should be pondering). But the move will take time, so A must be small to accomplish this task by making life extension medicine. Now, you tell him he needs how many steps to complete, so that he made enough life extension medicine .. Time 1s.

Input Format

A number n, there are n disks represents

Output Format

A number s, s represents the steps required.

Sample input and output

# 1 Input
31
Output # 1
2147483647

# 2 Input
15
Output # 2
32767

Instructions / tips
for all data n <= 15000
easily practiced hand the title Oh!

Tower of Hanoi movement times calculated: f (n) = 2 ^ n-1
code is as follows:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<cstdio>
using namespace std;
#pragma warning(disable:4996)
int n, l, i, a[10000];

void p()
{
	for (int i = 1; i <= l; i++)a[i] *= 2;//每一位都*2
	for (int i = 1; i <= l; i++)//各位是否>9,是则进位
	{
		if (a[i] > 9)
		{
			a[i + 1]++;
			a[i] -= 10;
		}
	}
	if (a[l + 1] > 0)l++;//高位进位,长度+1
}

int main()
{
	cin >> n;
	a[1] = 1;
	l = 1;//答案长度为1
	for (i = 0; i < n; i++)p();//求2^n
	for (i = l; i > 1; i--)cout << a[i];//打印高位
	cout << a[1] - 1;//末位减1
	return 0;
}
Published 53 original articles · won praise 18 · views 10000 +

Guess you like

Origin blog.csdn.net/YUEXILIULI/article/details/103971751