急求折半查找动态过程的图形显示
输入一组有序数字,用折半查找找一个数字,要求用图形显示出过程,用箭头在数字下方显示,有low,mid,high三个注释在箭头下方表示。如:查找3123456789↑↑↑l...
输入一组有序数字,用折半查找找一个数字,要求用图形显示出过程,用箭头在数字下方显示,有low,mid,high三个注释在箭头下方表示。如:查找3
1 2 3 4 5 6 7 8 9
↑ ↑ ↑
low mid high 找到3,位置是3.
在TC中作出,箭头可以用直线代替,可以限定输入为十个数字。很急,希望高手来帮帮忙! 展开
1 2 3 4 5 6 7 8 9
↑ ↑ ↑
low mid high 找到3,位置是3.
在TC中作出,箭头可以用直线代替,可以限定输入为十个数字。很急,希望高手来帮帮忙! 展开
2009-07-01
展开全部
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<string.h>
#define N 10
typedef struct
{
int x,y;
char s;
}list;
int main()
{
int num,i,i1;
int low=0,high=N-1,mid;
int gdriver=VGA,gmode=VGAHI;
int b[N]={05,13,19,21,37,56,64,75,80,88};
list a[N];
initgraph(&gdriver,&gmode,"c:\\tc");
setbkcolor(YELLOW);
delay(90000);
delay(90000);
delay(90000);
delay(90000);
for(i=50;i<501;i=i+50)
{
int j=80;
a[i/50-1].x=i;
a[i/50-1].y=j;
delay(9999);
}
setcolor(RED);
outtextxy(50,80,"05");
delay(9999);
setcolor(RED);
outtextxy(100,80,"13");
delay(9999);
setcolor(RED);
outtextxy(150,80,"19");
delay(9999);
setcolor(RED);
outtextxy(200,80,"21");
delay(9999);
setcolor(RED);
outtextxy(250,80,"37");
delay(9999);
setcolor(RED);
outtextxy(300,80,"56");
delay(9999);
setcolor(RED);
outtextxy(350,80,"64");
delay(9999);
setcolor(RED);
outtextxy(400,80,"75");
delay(9999);
setcolor(RED);
outtextxy(450,80,"80");
delay(9999);
setcolor(RED);
outtextxy(500,80,"88");
delay(9999);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("which number do you want to search\t");
scanf("%d",&num);
if((num<b[0])||(num>b[N-1]))
printf("\nNot exist");
else
{
while(low<=high)
{
mid=(low+high)/2;
if(b[mid]<num)
{
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(BLUE);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(RED);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(BLUE);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(RED);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(BLUE);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(RED);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(YELLOW);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(YELLOW);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(YELLOW);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(YELLOW);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
low=mid+1;
}
else if(b[mid]>num)
{
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(BLUE);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(RED);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(BLUE);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(RED);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(BLUE);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(RED);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(YELLOW);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(YELLOW);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(YELLOW);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(YELLOW);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
high=mid-1;
}
else
{
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(BLUE);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(RED);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(BLUE);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(RED);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(BLUE);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(RED);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
break;
}
}
if(low<=high)
printf("\nThe location is %d\n",mid+1);
else
printf("\nNot exist");
}
system("PAUSE");
return 0;
}
完全自己做的
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<string.h>
#define N 10
typedef struct
{
int x,y;
char s;
}list;
int main()
{
int num,i,i1;
int low=0,high=N-1,mid;
int gdriver=VGA,gmode=VGAHI;
int b[N]={05,13,19,21,37,56,64,75,80,88};
list a[N];
initgraph(&gdriver,&gmode,"c:\\tc");
setbkcolor(YELLOW);
delay(90000);
delay(90000);
delay(90000);
delay(90000);
for(i=50;i<501;i=i+50)
{
int j=80;
a[i/50-1].x=i;
a[i/50-1].y=j;
delay(9999);
}
setcolor(RED);
outtextxy(50,80,"05");
delay(9999);
setcolor(RED);
outtextxy(100,80,"13");
delay(9999);
setcolor(RED);
outtextxy(150,80,"19");
delay(9999);
setcolor(RED);
outtextxy(200,80,"21");
delay(9999);
setcolor(RED);
outtextxy(250,80,"37");
delay(9999);
setcolor(RED);
outtextxy(300,80,"56");
delay(9999);
setcolor(RED);
outtextxy(350,80,"64");
delay(9999);
setcolor(RED);
outtextxy(400,80,"75");
delay(9999);
setcolor(RED);
outtextxy(450,80,"80");
delay(9999);
setcolor(RED);
outtextxy(500,80,"88");
delay(9999);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("which number do you want to search\t");
scanf("%d",&num);
if((num<b[0])||(num>b[N-1]))
printf("\nNot exist");
else
{
while(low<=high)
{
mid=(low+high)/2;
if(b[mid]<num)
{
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(BLUE);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(RED);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(BLUE);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(RED);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(BLUE);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(RED);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(YELLOW);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(YELLOW);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(YELLOW);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(YELLOW);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
low=mid+1;
}
else if(b[mid]>num)
{
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(BLUE);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(RED);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(BLUE);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(RED);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(BLUE);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(RED);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(YELLOW);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(YELLOW);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(YELLOW);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(YELLOW);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
high=mid-1;
}
else
{
delay(90000);
delay(90000);
delay(90000);
delay(90000);
setcolor(BLUE);
line(a[low].x,a[low].y+5,a[low].x,a[low].y+40);
setcolor(RED);
outtextxy(a[low].x,a[low].y+40,"low");
delay(90000);
setcolor(BLUE);
line(a[mid].x,a[mid].y+5,a[mid].x,a[mid].y+40);
setcolor(RED);
outtextxy(a[mid].x,a[mid].y+60,"mid");
delay(90000);
setcolor(BLUE);
line(a[high].x,a[high].y+5,a[high].x,a[high].y+40);
setcolor(RED);
outtextxy(a[high].x,a[high].y+50,"high");
delay(90000);
break;
}
}
if(low<=high)
printf("\nThe location is %d\n",mid+1);
else
printf("\nNot exist");
}
system("PAUSE");
return 0;
}
完全自己做的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询