猜数字游戏 + 二分法查找 + 杨辉三角形

//猜数字//


#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include


void rule()
{
printf("系统会在1至100中随机选择一个数字,玩家可以按照要求猜这个数字,猜对为止,祝您游戏愉快\n");
}


void menu()
{
printf("进行游戏请选1.   查看规则请选2.  退出游戏请选0.\n ");
}


int main()
{
int flag = 1;
printf("*欢迎进行猜数字游戏*\n");
do
{
menu();
printf("请选择>:");
scanf("%d", &flag);
switch (flag)
{
case 0:
break;
case 1:
{
 int ret = 0;
 srand((unsigned)time(NULL));
 ret = rand() % 100;
 while (1)
 {
 int num = 0;
 scanf("%d", &num);
 if (numret)
 printf("你猜大了\n");
 else
 {
 printf("恭喜你,猜对了\n");
 break;
 }


 }
 printf("正确答案为:%d\n",ret);


}
break;
case 2:
rule();
}
} while (flag);
return 0;
}




//二分法查找//


#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 


int bin_search(int arr[],int key, int left, int right)
{
int mid = (left+right)>>1;
while(left <= right)
{
int mid = left-(left-right)/2;
if(arr[mid] < key)
{
left = mid+1;
}
else if(arr[mid] > key)
{
right = mid-1;
}
else
return mid;
}
return -1;
}
int  main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int key = 0;
printf("请输入需要查找的数字\n");
scanf("%d", &key);
int left = 0;
int right = sizeof(arr)/sizeof(arr[0])-1;
int ret = bin_search(arr, key, left, right);
if (ret != -1)
{
printf("查找到了,存在%d\n", arr[ret]);
}
else
printf("未查找到所输入数字\n");
system("pause");
return 0;
}




//杨辉三角形//


#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 


int main()
{
int i, j;
int arr[10][10] = { 0 };
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
{
if (j == 0)
arr[i][j] = 1;
else
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
printf("%5d  ", arr[i][j]);
printf("\n");
}
system("pause");
return 0;
}


你可能感兴趣的:(TEST)