经典的汉诺塔问题? 不必在内存中明确保留塔的状态?恐怕必须用三个全局变量纪录三根针的状态 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是俺的一个TC2.0的源程序,希望对你能有所帮助。#include "conio.h"#include "key.h"#define A 0#define B 1#define C 2#define FLOORCOLOR BLUEint steps=0;char num=0;float music123[]={130.8,146.8,164.8,174.6,196.0,220.0,246.9,261.7,293.7,329.6,349.2,392,440,493.9};char storeycolor[9]={ LIGHTGREEN,LIGHTBLUE,LIGHTCYAN,LIGHTRED, LIGHTMAGENTA,YELLOW,MAGENTA,CYAN,GREEN };static struct tower{ char x,y,top,storey[9];}Tower[3],*p;void drawblock(char x,char y,char n,char blocksort,char color){ int i; gotoxy(x,y); textcolor(color); for(i=0;i<n;i++) cprintf("%c",blocksort);}void GetStorey(void){ char inputerror=1,n; while(inputerror) { gotoxy(25,3); textcolor(LIGHTBLUE); cprintf("Please input Hanoi height:"); n=getch(); if(n==51&&num>0X30&&num<0X3B) break; n-=0x30; if(n<1||n>10) { inputerror=1; gotoxy(25,4);textcolor(RED); cprintf("Input error,please input again."); } else { inputerror=0; textcolor(LIGHTGREEN); gotoxy(52,3); num+=n; cprintf("%2d",num); } }}void InitDemo(void){ int i; Tower[B].x=40-num; Tower[B].y=12+num; Tower[A].x=Tower[B].x-num*2-8; Tower[A].y=Tower[B].y; Tower[C].x=Tower[B].x+num*2+8; Tower[C].y=Tower[B].y; for(i=0;i<num;i++) Tower[A].storey[i]=(num-i)*2; Tower[A].top=num; for(i=0;i<3;i++) drawblock(Tower[i].x-2,Tower[i].y+1,num*2+4,223,FLOORCOLOR); for(i=0;i<num;i++) { drawblock(Tower[A].x+i*1,Tower[A].y-i,(num-i)*2,219,storeycolor[num-i-1]); sound((int)music123[i]*2); delay(3000); nosound(); } gotoxy(25,4); cprintf(" "); gotoxy(25,5); cprintf("Press any key start to demo. "); gotoxy(1,1); getch(); textcolor(LIGHTCYAN+128); gotoxy(25,5); cprintf(" Press ESC to exit. ");}void DemoOver(void){ gotoxy(25,5); cprintf(" Demo Over "); gotoxy(1,1); textcolor(LIGHTGRAY); getch();}void DELAY(void){ static scale=200; int i,j; for(i=0;i<100;i++) for(j=0;j<scale;j++) if(bioskey(1)) switch(bioskey(0)) { case UP: scale=abs(scale-20); break; case DOWN: scale+=20; break; case ESC: nosound(); exit(0); }}void Move(char getone,char putone){ char width; p=&Tower[getone]; width=p->storey[p->top-1]; drawblock(p->x+num-width/2,p->y-p->top+1,width,' ',0); p->storey[p->top-1]=0; p->top--; p=&Tower[putone]; drawblock(p->x+num-width/2,p->y-p->top,width,219,storeycolor[width/2-1]); p->top++; p->storey[p->top-1]=width; steps++; gotoxy(30,4); textcolor(LIGHTGREEN); cprintf("Total Steps:%5d",steps); gotoxy(1,1); sound((int)music123[p->top-1]); DELAY(); nosound();}void Hanoi(char n,char one,char two,char three){ if(n==1) Move(one,three); else { Hanoi(n-1,one,three,two); Move(one,three); Hanoi(n-1,two,one,three); }}main(){ system("cls"); gotoxy(32,1); cprintf("H A N I O"); GetStorey(); InitDemo(); Hanoi(num,A,B,C); DemoOver();} ---------------------》对话框中同时显示两副位图的问题《---------------------- MFC如何判断菜单的某一项项被点击? 类里面如何使用定时器? 第二个子窗口大小不变 SetWindowPos,马上散分。。。 请教一个文件操作的问题?急!!!! ODBC编程的困惑 native event简介 希望结识想学好VC的朋友 一个关于播放语音 的问题! 我用DirectX编了一个小东西,程序很简单,可是有一个Blt调用老是莫名其妙的出错,有谁愿意帮帮我,我把code mail给你! alt的问题? 谁有bmp转成GIF的VC源码?
#include "conio.h"
#include "key.h"
#define A 0
#define B 1
#define C 2
#define FLOORCOLOR BLUE
int steps=0;
char num=0;
float music123[]={130.8,146.8,164.8,174.6,196.0,220.0,246.9,261.7,293.7,329.6,349.2,392,440,493.9};
char storeycolor[9]={ LIGHTGREEN,LIGHTBLUE,LIGHTCYAN,LIGHTRED,
LIGHTMAGENTA,YELLOW,MAGENTA,CYAN,GREEN };
static struct tower{ char x,y,top,storey[9];}Tower[3],*p;
void drawblock(char x,char y,char n,char blocksort,char color)
{ int i;
gotoxy(x,y); textcolor(color);
for(i=0;i<n;i++) cprintf("%c",blocksort);
}void GetStorey(void)
{ char inputerror=1,n;
while(inputerror)
{ gotoxy(25,3); textcolor(LIGHTBLUE);
cprintf("Please input Hanoi height:");
n=getch();
if(n==51&&num>0X30&&num<0X3B) break;
n-=0x30;
if(n<1||n>10)
{ inputerror=1; gotoxy(25,4);textcolor(RED);
cprintf("Input error,please input again.");
}
else
{ inputerror=0; textcolor(LIGHTGREEN); gotoxy(52,3);
num+=n; cprintf("%2d",num);
}
}
}
void InitDemo(void)
{ int i;
Tower[B].x=40-num; Tower[B].y=12+num;
Tower[A].x=Tower[B].x-num*2-8; Tower[A].y=Tower[B].y;
Tower[C].x=Tower[B].x+num*2+8; Tower[C].y=Tower[B].y;
for(i=0;i<num;i++) Tower[A].storey[i]=(num-i)*2;
Tower[A].top=num;
for(i=0;i<3;i++) drawblock(Tower[i].x-2,Tower[i].y+1,num*2+4,223,FLOORCOLOR);
for(i=0;i<num;i++)
{ drawblock(Tower[A].x+i*1,Tower[A].y-i,(num-i)*2,219,storeycolor[num-i-1]);
sound((int)music123[i]*2);
delay(3000);
nosound();
}
gotoxy(25,4); cprintf(" ");
gotoxy(25,5); cprintf("Press any key start to demo. "); gotoxy(1,1);
getch(); textcolor(LIGHTCYAN+128);
gotoxy(25,5); cprintf(" Press ESC to exit. ");
}
void DemoOver(void)
{ gotoxy(25,5); cprintf(" Demo Over "); gotoxy(1,1);
textcolor(LIGHTGRAY); getch();
}
void DELAY(void)
{ static scale=200;
int i,j;
for(i=0;i<100;i++)
for(j=0;j<scale;j++)
if(bioskey(1))
switch(bioskey(0))
{ case UP: scale=abs(scale-20); break;
case DOWN: scale+=20; break;
case ESC: nosound(); exit(0);
}
}
void Move(char getone,char putone)
{ char width;
p=&Tower[getone]; width=p->storey[p->top-1];
drawblock(p->x+num-width/2,p->y-p->top+1,width,' ',0);
p->storey[p->top-1]=0; p->top--;
p=&Tower[putone];
drawblock(p->x+num-width/2,p->y-p->top,width,219,storeycolor[width/2-1]);
p->top++; p->storey[p->top-1]=width;
steps++;
gotoxy(30,4); textcolor(LIGHTGREEN); cprintf("Total Steps:%5d",steps);
gotoxy(1,1);
sound((int)music123[p->top-1]);
DELAY();
nosound();
}
void Hanoi(char n,char one,char two,char three)
{ if(n==1) Move(one,three);
else
{ Hanoi(n-1,one,three,two);
Move(one,three);
Hanoi(n-1,two,one,three);
}
}
main()
{ system("cls");
gotoxy(32,1); cprintf("H A N I O");
GetStorey();
InitDemo();
Hanoi(num,A,B,C);
DemoOver();
}