忘记了写代码,I am so sorry......void fun(int n, char A = 'A', char B = 'B', char C='C')
{
  /* 把 n 个盘子从 A 移动到 B,可以借助与 C */
  /* 这就形成了一个递归的过程 */  static stepCount = 0 ;  if ( n > 0 ) {
    fun(n-1, A, C, B);
    cout << "从 " << A << " 移动最顶的盘子到 " << B << endl;
    stepCount++;
    fun(n-1, C, B, A);
  } else {
    cout << "共移动 " << stepCount << " 次" << endl;
  }
}

解决方案 »

  1.   

    I am sorry......偶看错题目了 ……我做的是把盘子从 A 移到 B,你的题目是从 A 移到 C不过反正 B 和 C 都是空的柱子,数学上是等价的,换一下  BC 的位置就可以了。
      

  2.   

    汗……在 Java 版发 C 代码,会不会被人打?
      

  3.   

    &#12603;&#45264;&#50612;&#12623;&#12596;&#50573;&#12616;&#12599;&#12616;&#45908;&#51105;&#45908;&#12623;&#12616;&#48519;&#12616;&#12610;&#45908;&#12623;&#12610;&#51200;&#12623;&#47196;&#12629;&#12619;
    &#12596;&#50573;&#12616;&#12599;&#12610;&#51452;&#45813;&#51200;&#45908;&#12623;&#12627;&#48120;&#12623;&#12596;&#50880;&#45768;&#12623;&#12627;&#50517;&#51200;&#12628;&#12599;&#51116;&#12625;&#48316;&#45824;&#12625;
    &#54140;&#12625;&#12601;&#47672;&#12623;&#12596;&#51256;&#12625;&#12635;&#12599;&#12610;&#51200;&#12623;&#45908;&#51088;&#12623;&#12627;&#12596;&#50500;&#48120;;&#12623;&#45768;&#50516;&#51060;&#12623;&#12609;&#45768;;&#50500;
    &#53076;&#47156;&#45320;&#50516;&#45320;&#12616;&#48148;&#45908;&#51144;&#12610;&#45964;&#12623;&#12601;&#46972;&#12627;&#52908;&#12624;&#12629;&#12609;&#12596;&#50612;&#12623;&#12643;&#12623;&#48708;;&#51088;&#45964;&#12631;&#52852;&#12636;
      

  4.   

    f(n) = 2*f(n-1) + 1
      

  5.   

    public class Card {
        private int suit = UNASSIGNED;
        private int number = UNASSIGNED;    public final static int UNASSIGNED = -1;    public final static int DIAMONDS = 1;
        public final static int CLUBS = 2;
        public final static int HEARTS = 3;
        public final static int SPADES = 4;    public final static int ACE = 1;
        public final static int KING = 13;    public Card(int number, int suit) {
            if (isValidNumber(number)) {
                this.number = number;
            } else {
                //Error
            }        if (isValidSuit(suit)) {
                this.suit = suit;
            } else {
                //Error
            }
        }    public int getSuit() {
            return suit;
        }    public int getNumber() {
            return number;
        }    public static boolean isValidNumber(int number) {
            if (number >= ACE && number <= KING) {
                return true;
            } else {
                return false;
            }
        }
        public static boolean isValidSuit(int suit) {
            if (suit >= DIAMONDS && suit <= SPADES) {
                return true;
            } else {
                return false;
            }
        }    public static String numberToString(int number) {
            String result = "";
            switch (number) {
                case ACE: result =  "Ace"; break;
                case 2: result = "Two"; break;
                case 3: result = "Three"; break;
                case 4: result = "Four"; break;
                case 5: result = "Five"; break;
                case 6: result = "Six"; break;
                case 7: result = "Seven"; break;
                case 8: result = "Eight"; break;
                case 9: result = "Nine"; break;
                case 10: result = "Ten"; break;
                case 11: result = "Jack"; break;
                case 12: result = "Queen"; break;
                case KING: result = "King"; break;
                case UNASSIGNED: result = "Invalid Number"; break;
            }
            return result;
        }    public static String suitToString(int suit) {
            String result = "";
            switch (suit) {
                case DIAMONDS: result = "Diamonds"; break;
                case CLUBS: result = "Clubs"; break;
                case HEARTS: result = "Hearts"; break;
                case SPADES: result = "Spades"; break;
                case UNASSIGNED: result = "Invalid Suit"; break;
            }
            return result;
        }
    }
    可惜不是这个
    public class Deck {    public static int numSuits = 4;
        public static int numCardsPerSuit = 13;
        public static int numCards = numSuits * numCardsPerSuit;    private Card[][] cards;    public Deck() {
            cards = new Card[numSuits][numCardsPerSuit];
            for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
                for (int number = Card.ACE; number <= Card.KING; number++) {
                    cards[suit-1][number-1] = new Card(number, suit);
                }
            }
        }
        public Card getCard(int suit, int number) {
            return cards[suit-1][number-1];
        }
    }
    这是什么你猜
    public class DisplayDeck {
        public static void main(String[] args) {
            Deck deck = new Deck();
            for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
                for (int number = Card.ACE; number <= Card.KING; number++) {
                    Card card = deck.getCard(suit, number);
                    System.out.println(card.numberToString(card.getNumber())
                                       + " of " 
                                       + card.suitToString(card.getSuit()));
                }
            }
        }
    }
    是牌,
      

  6.   

    integer i;public void hhh(integer n;char a,b,c){
    if(n>0){
            hhh(n-1,a,c,b);
            i = i +1 ;
            jTextArea1.append(String.valueof(i)+':  Move '+a+' to '+c);
            hhh(n-1,b,a,c);
    }}public void get_count(integer n){
    char a,b,c;
    a='A';  b='B'; c='C';
    i = 0;
    hhh(n,a,b,c);
    //得出总次数i
    }
      

  7.   

    此问题我已经解决,java代码如下:public class HanNuoTa
    {
    public static void hanoi(int n,String start,String middle,String end)
    {
    //终止条件,移动一个盘子
    if (n==1)
    System.out.println("Move " + start + " To " + end);
    //将n-1个盘子移到"middle",将第一个盘子移到"end"
    //再将n-1个盘子从"middle"移到"end"
    else
    {
    hanoi(n-1,start,end,middle);
    System.out.println("Move " + start + " to " + end);
    hanoi(n-1,middle,start,end);
    }
    }

    public static void main(String[] args)
    {
    //盘子个数和各个柱子的名字
    int n = 3;
    String start = "start";
    String middle = "middle";
    String end = "end";
    //输入盘子的个数和n个盘子的移动方法
    /*System.out.print("Enter the numbers of disks: ");
    try
    {
    n = System.in.read();
    }
    catch(Exception ex)
    {
    System.out.println("The number is wrong: " + ex);
    }
                      */
    System.out.println("The solution for n = " + n + " :");
    hanoi(n,start,middle,end);
    }

    }运行程序可看出具体的移动过程,n可以修改,也可以由键盘输入,如果要知道次数,可加一个static变量
    谢谢各位了