[Pratice]猜生日(书中实例)

原理:

通过5个集合来确定二进制的每一个位,从而确定5位二进制数对应的十进制(2^5-1 = 31)


#include <iostream>
#include <cstdlib>
using namespace std;

char set[][53] = {
	"16 17 18 19\n 20 21 22 23\n 24 25 26 27\n 28 29 30 31",
	" 8  9 10 11\n 12 13 14 15\n 24 25 26 27\n 28 29 30 31",
	" 4  6  5  7\n 12 13 14 15\n 20 21 22 23\n 28 29 30 31",
	" 2  3  6  7\n 10 11 14 15\n 18 19 22 23\n 26 27 30 31",
	" 1  3  5  7\n  9 11 13 15\n 17 19 21 23\n 25 27 29 31",
	};

int main(int argc, char const *argv[])
{
	int record = 0;
	bool value = 0;

	for (int i = 0; i < 5; ++i)
	{
		cout << set[i] << endl << endl;
		cout << "Is your birthday in the set?" << endl;
		cout << "	Please enter 1 or 0 :" << endl;
		cin >> value;

		system("cls");		

		record <<= 1;
		record |= value;
	}

	cout << "Your birthday : " << record << endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值