# 算法學習：

（上面是思維導圖）

``````#include<stdio.h>
#include <windows.h>
//while、do while 、goto、for、遞歸

void digui(int shang,int xia,int zhong,int num)
{
zhong = (shang + xia) / 2;
if (shang <= xia)
{
if (num == zhong)
{
printf("遞歸:find.%d\n", zhong);
return;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}

}
return;
}

int main()
{
int a[1024];
for (int i = 0; i<1024; i++) {
a[i] = i;
//printf("%d\n",a[i]);
}
int shang = 0;
int xia = 1023;
int zhong = (shang + xia) / 2;
int num = 102;
while (shang <= xia)//一、while
{
zhong = (shang + xia) / 2;
if (num == zhong)
{
printf("while:find.%d\n", zhong);
break;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);

}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}

}
printf("==================\n");
printf("==================\n");
printf("==================\n");

shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 102;
do {//二、do while
zhong = (shang + xia) / 2;
if (num == zhong)
{
printf("do while:find.%d\n", zhong);
break;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}

} while (xia>=shang);

printf("==================\n");
printf("==================\n");
printf("==================\n");

shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 988;
AAA://三、goto
zhong = (xia + shang) / 2;
if (num == zhong)
{
printf("goto:find.%d\n", zhong);
goto BBB;
}
else if (num > zhong)
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}

if (shang<=xia)
{
goto AAA;
}
BBB:

printf("==================\n");
printf("==================\n");
printf("==================\n");

shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 288;
for (int i; shang <= xia ;zhong = (shang + xia) / 2)
{
if (num == zhong)
{
printf("for:find.%d\n", zhong);
break;
}
else if (num < zhong)
{
xia = zhong - 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
else
{
shang = zhong + 1;
printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
}
}

printf("==================\n");
printf("==================\n");
printf("==================\n");

shang = 0;
xia = 1023;
zhong = (shang + xia) / 2;
num = 288;

digui(shang,xia,zhong,num); //遞歸

getchar();
getchar();
Sleep(5000);

return 0;
}``````