Guess number game:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Print_menu ()
{
printf ("******** Please choose: **********\n");
printf ("********1: Start Game ********\n");
printf ("********0: Exiting game ********\n");
}
void Guess_num ()
{
int input=1;
while (input)
{
print_menu ();
scanf ("%d", &input);
switch (input)
{case
1:
{
int ret=0;
printf ("Game start \ n");
Srand ((unsigned) time (NULL));
Ret=rand ()%100;//random number generation range 1-100 while
(1)
{
int num=0;
printf ("Please guess:");
scanf ("%d", &num);
if (Num>ret)
{
printf ("You guessed big");
}
else if (Num<ret)
{
printf ("You guess small \ n");
}
else
{
printf ("Congratulations, guessed right \ n");
break;
}
}
break;
Case 0:
{
printf ("Quit Game \ n");
Exit (0);
}
Default:
printf ("Please enter the correct option \ n");
break;
}
}} int main ()
{
guess_num ();
System ("pause");
return 0;
}
Two-point search:
#include <stdio.h>
#include <stdlib.h>
int bin_search (int arr[],int num,int left,int right)
{ While
(left<=right)
{
int mid=left-(left-right)/2;//mid= (left+right)/2 or mid= (left&right) + ((left^right) >>1)
if (arr [Mid]>num)
{
right=mid-1;
}
else if (arr[mid]<num)
{
left=mid+1
}
else
{return
mid;
}
}
return-1;
}
int main ()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int left=0;
int right= (sizeof (arr)/sizeof (arr[0))-1;
int num=0;
int ret=0;
printf ("Please enter the number you want to find:");
scanf ("%d", &num);
Ret=bin_search (arr,num,left,right);
if (ret!=-1)
{
printf ("Find this number, subscript:%d\n", ret);
}
else
{
printf ("No this number \ n");
}
System ("pause");
return 0;
}
In the above two-point lookup, use mid=left-(left-right)/second instead of mid= (left+right)/2 to prevent data overflow, or to use infrequently used methods (Ieft&right) + (left^right) >>1 ) method to do it.
Yang Hui's triangle:
#include <stdio.h>
#include <stdlib.h>
#define LINE ten
void Print (int arr[][line])
{
int i=0;
for (i=0;i<line;i++)
{
int j=0;
for (j=0;j<=line-i;j++)
{
printf (" ");
}
for (j=0;j<=i;j++)
{
printf ("%3d ", Arr[i][j]);
}
printf ("\ n")
;
}
void Yh_triangle (int arr[][line])
{
int i=0;
for (i=0;i<line;i++)
{
int j=0;
for (j=0;j<=i;j++)
{
if (j==0)
{
arr[i][j]=1
}
if (j==i)
{
arr[i][j]=1;
}
if ((j!=0) && (j<i))
{
arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
}}} Print (arr);
}
int main ()
{
int arr[line][line];
Yh_triangle (arr);
System ("pause");
return 0;
}