我编写了这个C程序,用于在跟踪路径时将多个磁盘(n)从peg A移动到peg C. 但是,我不知道如何/在何处进行计数调用/增量,以便在结尾处跟踪并打印出总移动次数.任何意见,将不胜感激.(最初我在TOH函数中打印它,这不起作用所以我删除了printf(..)行)我改变了变量,这样可以提高可读性; 但是,计数输出很远.对于板数= 3,计数= 239.对于板4的数量,计数= 130,431
#include <stdio.h>
int TOH(int,char,char,char);
int main()
{
int n;
printf("\nEnter number of plates:");
scanf("%d",&n);
int c = TOH(n,'A','C','B');
printf("\n");
printf("Total number of moves = %d \n ", c);
return 0;
}
int TOH(int n,char first,char third,char second)
{
int count;
if(n>0){
count=TOH(n-1, first, second, third);
printf("Move disk %d from peg %c to peg %c\n", n, first, third);
count++;
count+= TOH(n-1, second, third, first);
}
return count;
}
Run Code Online (Sandbox Code Playgroud) 为什么我的程序不能找到我的主要课程?我认为你不需要其余的parse()函数来理解什么是错误的......让我知道
package help;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class help {
ArrayList<Character> StringList = new ArrayList<Character>();
static char[] data;
String val;
public void main(String[] args){
InputStreamReader ISR = new InputStreamReader (System.in);
BufferedReader BR = new BufferedReader(ISR);
try{
int sCurrentChar;
while ((sCurrentChar = BR.read()) != -1) {
parse((char) sCurrentChar);
}
} catch(IOException e){
e.printStackTrace();
}
}
public void parse(char x){
boolean done =false;
int state =0;
Run Code Online (Sandbox Code Playgroud)