忘记了写代码,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;
}
}
{
/* 把 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;
}
}
解决方案 »
- socket的问题,Linux监听端口
- Java算不算一种丑陋的语言?
- httpclient4 重试机制请教?
- 为什么JAVA APPLET流行不起来 ?
- 在JDK1.1中Thread类定义了suspend()和resume()方法,但是在JDK1.2中已经过时,应使用什么方法来替代之?
- JCreator使用问题
- exception in thread "main" java.lang.NoClassDefFoundError:hello/class
- 请教:用Java函数如何实现文本图片的操作?
- 给钱购买一个jsp购物商城,谁有联系我,或留下QQ,MSN.
- 为什么Window XP装不了JBuilder7?
- 求助哥哥们,java中不可以在两个嵌套的块中声明两个完全同名的变量?
- Applet往服务端写文件的问题,哪位大侠处理过类似问题?
ㄴ얍ㅈㄷㅂ주답저더ㅏㅓ미ㅏㄴ움니ㅏㅓ압저ㅔㄷ재ㅑ벼대ㅑ
퍼ㅑㄹ머ㅏㄴ져ㅑㅛㄷㅂ저ㅏ더자ㅏㅓㄴ아미;ㅏ니암이ㅏㅁ니;아
코렴너암너ㅈ바더쟈ㅂ뎌ㅏㄹ라ㅓ캬ㅐㅕㅁㄴ어ㅏㅣㅏ비;자뎌ㅗ카ㅜ
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()));
}
}
}
}
是牌,
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
}
{
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变量
谢谢各位了