C语言 二分法 猜价格游戏

/* Author Mr.Long
 * Date 2015年12月2日17:37:47 
 */ 
#include
#include
/*  二分法  */
int main(){
	int price,newprice = 0,count = 0;
	printf("****猜价格游戏****\n");
	printf("设置商品价格(0 - 999):");
	input: 
	scanf("%d",&price);
	if(price <=0 || price >= 999){
		printf("请输入0 - 999 范围内的价格:");
		goto input;
	} 
	system("cls");
	while(newprice != price){
		printf("请输入你猜的价格:");
		scanf("%d",&newprice);
		if(newprice > price){
			printf("->高了\n");
		}else if(newprice < price){
			printf("->低了\n");
		}else if(newprice == price){
			printf("恭喜你,猜对了\n");
			break;
		}
		count++;
	}
	printf("所用步数:%d",count); 
	return 0;
}

你可能感兴趣的:(c/c++)