MOOC清华《程序设计基础》第2章:猜数游戏(from 1 to 50)

//******************************************* 
//程序名:GuessNumber(from1to50) 
//功  能:玩家心中想1到50中的一个数,
//        给出6张卡片,让玩家说出哪几张
//        卡片上有这个数,然后计算机便能
//        判断出是哪个数 
//编制人:刘聪 
//时  间:2017年5月26日
//******************************************* 

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int n, Binary, bit, counter, judge, Number;
	int DecimalToBinary(int n);
	n = 0;
	Binary = 0;
	bit = 0;
	counter = 0;
	judge = 0;
	Number = 0;
	
	cout<<"猜数游戏开始!请在心中默想一个1到50之间的整数,"<<endl
		<<"请看以下0#到5#,共6张卡片,是否有您所想的数字。"<<endl
		<<"若有,请说“1”;若无,请说“0”。"<<endl;
		
	cout<<endl<<"卡片  0#:"<<endl;
	for(n = 1;n <= 50; n++)
	{
		Binary = DecimalToBinary(n);
		bit = Binary % 10 / 1;
		if(bit == 1) 
		{
			cout<<n<<'\t';
		    counter++;
		}
		if(counter == 5)
		{
			cout<<endl;
			counter = 0;
		}
        if((n == 50) && (counter != 0)) cout<<endl;
		if(n == 50) counter = 0;
	}
	cout<<"这张表有你想的数字吗?:";
	cin>>judge;
	if(judge == 1) 
	{
	    Number = 1 * 1 + Number;
	    cout<<endl;
	}
	else 
	{
		if(judge == 0) 
		{
			Number = 1 * 0 + Number;
			cout<<endl;
		}
	    else cout<<"输入错误。"; 
	}
	
	cout<<endl<<"卡片  1#:"<<endl;
	for(n = 1;n <= 50; n++)
	{
		Binary = DecimalToBinary(n);
		bit = Binary % 100 / 10;
		if(bit == 1) 
		{
			cout<<n<<'\t';
		    counter++;
		}
		if(counter == 5)
		{
			cout<<endl;
			counter = 0;
		}
        if((n == 50) && (counter != 0)) cout<<endl;
		if(n == 50) counter = 0;
	}
	cout<<"这张表有你想的数字吗?:";
	cin>>judge;
	if(judge == 1) 
	{
	    Number = 2 * 1 + Number;
	    cout<<endl;
	}
	else 
	{
		if(judge == 0) 
		{
			Number = 2 * 0 + Number;
			cout<<endl;
		}
	    else cout<<"输入错误。"; 
	}
	
	cout<<endl<<"卡片  2#:"<<endl;
	for(n = 1;n <= 50; n++)
	{
		Binary = DecimalToBinary(n);
		bit = Binary % 1000 / 100;
		if(bit == 1) 
		{
			cout<<n<<'\t';
		    counter++;
		}
		if(counter == 5)
		{
			cout<<endl;
			counter = 0;
		}
	    if((n == 50) && (counter != 0)) cout<<endl;
		if(n == 50) counter = 0;
	}
	cout<<"这张表有你想的数字吗?:";
	cin>>judge;
	if(judge == 1) 
	{
	    Number = 4 * 1 + Number;
	    cout<<endl;
	}
	else 
	{
		if(judge == 0) 
		{
			Number = 4 * 0 + Number;
			cout<<endl;
		}
	    else cout<<"输入错误。"; 
	}
	
	cout<<endl<<"卡片  3#:"<<endl;
	for(n = 1;n <= 50; n++)
	{
		Binary = DecimalToBinary(n);
		bit = Binary % 10000 / 1000;
		if(bit == 1) 
		{
			cout<<n<<'\t';
		    counter++;
		}
		if(counter == 5)
		{
			cout<<endl;
			counter = 0;
		}
		if((n == 50) && (counter != 0)) cout<<endl;
		if(n == 50) counter = 0;
	}
	cout<<"这张表有你想的数字吗?:";
	cin>>judge;
	if(judge == 1) 
	{
	    Number = 8 * 1 + Number;
	    cout<<endl;
	}
	else 
	{
		if(judge == 0) 
		{
			Number = 8 * 0 + Number;
			cout<<endl;
		}
	    else cout<<"输入错误。"; 
	}
	
	cout<<endl<<"卡片  4#:"<<endl;
	for(n = 1;n <= 50; n++)
	{
		Binary = DecimalToBinary(n);
		bit = Binary % 100000 / 10000;
		if(bit == 1) 
		{
			cout<<n<<'\t';
		    counter++;
		}
		if(counter == 5)
		{
			cout<<endl;
			counter = 0;
		}
		if((n == 50) && (counter != 0)) cout<<endl;
		if(n == 50) counter = 0;
	}
	cout<<"这张表有你想的数字吗?:";
	cin>>judge;
	if(judge == 1) 
	{
	    Number = 16 * 1 + Number;
	    cout<<endl;
	}
	else 
	{
		if(judge == 0) 
		{
			Number = 16 * 0 + Number;
			cout<<endl;
		}
	    else cout<<"输入错误。"; 
	}
	
	cout<<endl<<"卡片  5#:"<<endl;
	for(n = 1;n <= 50; n++)
	{
		Binary = DecimalToBinary(n);
		bit = Binary % 1000000 / 100000;
		if(bit == 1) 
		{
			cout<<n<<'\t';
		    counter++;
		}
		if(counter == 5)
		{
			cout<<endl;
			counter = 0;
		}
		if((n == 50) && (counter != 0)) cout<<endl;
		if(n == 50) counter = 0;
	}
	cout<<"这张表有你想的数字吗?:";
	cin>>judge;
	if(judge == 1) 
	{
	    Number = 32 * 1 + Number;
	    cout<<endl;
	}
	else 
	{
		if(judge == 0) 
		{
			Number = 32 * 0 + Number;
			cout<<endl;
		}
	    else cout<<"输入错误。"; 
	}
	
	cout<<"您所猜的数字是:"<<Number<<endl;
	
	return 0;
}

int DecimalToBinary(int n)
{
	int quotient, remainder, counter, Binary, i;
	counter = 0;
	quotient = 0;
	remainder = 0;
	Binary = 0;
	i = 0;
	while((quotient = n / 2 ) > 0)
	{
		remainder = n % 2;
		n = quotient;
		counter++;
		if(remainder == 1)
		{
			for(i = counter; i > 1; i--)
		        remainder = remainder * 10;
		    Binary = remainder + Binary;
		}
	}
	remainder = 1;
	counter++;
	for(i = counter; i > 1; i--)
	    remainder = remainder * 10;
	Binary = remainder + Binary;
	return Binary;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值