虽然我对递归的理解没有任何问题,但我似乎不能理解汉诺塔问题的递归解决方案。以下是来自的代码
procedure Hanoi(n: integer; source, dest, by: char);
Begin
if (n=1) then
writeln('Move the plate from ', source, ' to ', dest)
else begin
Hanoi(n-1, source, by, dest);
writeln('Move the plate from '
下面是我使用递归求解汉诺塔的Java代码:
/**here is a stack of N disks on the first of three poles (call
them A, B and C) and your job is to move the disks from pole A to pole B without
ever putting a larger disk on top of a smaller disk.*/
public class Hanoi {
public static void main(String[] args) {
p
我是Scala编程新手。我的目标是实现一个解决汉诺塔问题的尾递归程序。我相信它可以通过这样的递归来实现:
// Implementing a recursive function for Towers of Hanoi,where the no of disks is taken as 'n', 'from' being the Start Peg, 'to' being the End Peg, and 'via' being Intermediate Peg
def move(n: Int, from: Int, to: I
我知道递归是一种在函数本身中调用函数的技术。但下面的代码让我感到困惑,它是如何在第一次递归之后完成cout部分的:
(这段代码解决了汉诺塔之谜)
#include <iostream>
using namespace std;
void move_rings(int n, int src, int dest, int other);
int main(void)
{
int rings;
cout << "Number of Rings: ";
cin >> r
我试着在一个解决汉诺塔难题的程序中显示移动次数,但它一直显示不止一次,如下面的输出所示
Input the number of rings: 2
Towers of Hanoi with 2 rings
1
3
3
我希望它只显示数字3一次,而不是1,3,3。下面是我的代码(对于我的赋值,我必须声明一个静态的int计数)。
import java.util.Scanner;
public class Towers
{
static int count;
public static void doTowers(
int n, // Num
我用javascript做了一个小程序来解决汉诺塔的问题。我使用了3个塔的DIVs,并给第一个塔一个黑色,其余的我给了白色。当我想要程序交换2个元素时,它基本上交换了适当DIVs的属性。代码运行得很好,但我希望每个步骤都是可见的,在当前状态下,它从头到尾只是一道闪光。我试着为每个步骤使用一个按钮,但它不起作用,所以setTimeout()。(为我糟糕的英格兰道歉:c )
var from = 1;
var to = 2;
var help = 3;
function swap(from, to){
while (...){
if (...){
我有一个文件系统的dict表示,如下所示。
{
"en": {
'file_name_1': [{meta representation of file_name_1}] or False,
{'folder_name': {
'index': [{meta repr. of the index}] or False
'sub_file_1': [{meta repr. of file_name_1}] or False,
def hanoi(n,f,v,t):
if n == 0:
pass
else:
hanoi(n-1,f,t,v)
print(f"Move disc from {f} to {t}.")
hanoi(n-1,v,f,t)
hanoi(3,"a","b","c")
我在研究递归,我搞不懂这个“河内塔”函数。很好,但我不明白它到底在做什么。例如,函数打印的指令之一是:
将阀瓣从c移动到b。
但在我看来{f}只能是"a“还是"b"
我必须设计一个递归函数来应用于一个赋值(对于这个赋值,我不允许使用关于河内塔的标准数学库。我偶然发现了下面的代码,我认为这是一个很好的分配工作的代码,但是它不可能运行(n > 30),因为它太慢了:
#include <stdio.h>
#include <stdlib.h>
int TOH(int,char,char,char);
int main()
{
int n;
printf("\nEnter number of disks:");
scanf("%d",&n);
int c =
def hanoi(n,src,dsc,aux):
if n == 1:
print_move(src,dsc)
else:
hanoi(n-1,src,aux,dsc)
print_move(src,dsc)
hanoi(n-1,aux,dsc,src)
def print_move(src,dsc):
print("Move the top disk from",src,"to",dsc)
我希望更改上面的汉诺塔代码,以输出一个移动元组,当按顺序执行时,将通过辅助磁极将
我是OCaml的新手。我写了这段关于河内塔的代码。
let rec hanoi (a,b,c) n =
if n <> 0 then begin
hanoi (a,c,b) (pred n);
Printf.printf "%i %i\n" a b;
hanoi (c,b,a) (pred n)
end;;
我打印: a=origin塔和b=destination塔。
我想这样做:
hanoi : 'a * 'a * 'a > int > ('a * '
我使用MATLAB中的以下函数,按log(2^n)步骤(所有长度为n的二进制数),按升序计算长度为n的所有二进制向量。
A = compV([0;1],n);
function [O] = compV(O,n)
j = length(O);
if n > 1
O1 = [zeros(j,1) O];
O2 = [ones(j,1) O];
O = [O1;O2];
O = compV(O,n-1);
public class han {
public static void main(String[] args) {
hanoi(5, "A", "B", "C");
}
private static void hanoi (int n, String from, String other, String to) {
if (n==0){
return;
}
if (n>0){
hanoi(n-
所以我一直在试着找出为什么这不起作用。基本上,我尝试编写一个递归函数,它将在控制台中显示一个漂亮的文本金字塔。
用户首先输入高度,然后输入用于创建金字塔的符号,并在此处调用函数:
//_pyramidHeight is 10
//The second int is to specify the beginning width, which should be the point at the top.
pyramidLine(_pyramidHeight, 1);
我创建的函数如下:
void pyramidLine (int _height, int _width)
{
fo
导出了一种用于打印金字塔的算法,该算法使用所有可能的组合,使最轻和最小的人躺在金字塔上,最重和最大的人作为基座。
例如:
A人80公斤140厘米
B人90公斤150厘米
人C 100公斤160克
D 100公斤170厘米
人E 150公斤180厘米
人F 160公斤185厘米
A
B C
D E F
注:身高和体重比其他人大的人不能躺在该人的顶上,不是每个人都必须成为金字塔结构的一部分。