Simple guess number game, two-point search, Yang Hui's triangle

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.