Demo entry 6662979

d

   

Submitted by anonymous on Nov 28, 2017 at 21:27
Language: C++. Code size: 2.3 kB.

#include <iostream>

using namespace std;

struct date {
	int month;
	int day;
};

//Initialize the possible birthdays of Cheryl and boolean values of their months and days
struct date birthday[10] = { {5, 15}, {5, 16}, {5, 19}, {6, 17}, {6, 18}, {7, 14}, {7, 16}, {8, 14}, {8, 15}, {8, 17} };
bool months[4] = { true, true, true, true }, days[6] = { true, true, true, true, true, true };

//Figure out if the month or day only exists once
bool once(int x, char kind) {
	int num = 0;
	if (kind == 'm') {
		for (date d : birthday)
			if (d.month - 5 == x && days[d.day - 14])
				num++;
	}
	else {
		for (date d : birthday)
			if (d.day - 14 == x && months[d.month - 5])
				num++;
	}

	if (num == 1)
		return true;
	else
		return false;
}


//After Knowing imposible day(month) of a date, update its month(day) boolean value
void check(char kind) {
	if (kind == 'm') {
		for (int i = 0; i < 4; i++)
			for (date d : birthday)
				if (d.month - 5 == i && !days[d.day - 14])
					months[i] = false;
	}
	else {
		for (int i = 0; i < 6; i++) {
			if (days[i]) {
				days[i] = false;
				for (date d : birthday)
					if (d.day - 14 == i && months[d.month - 5])
						days[i] = true;
			}
		}
	}
}

int main() {
	/*Accoring to Albert, if Bernard who only knows the day isn't sure about the date,
	the birthday must be in the month which has at least two possible dates.*/
	for (int i = 0; i < 6; i++)
		if (once(i, 'd'))
			days[i] = false;

	/*Months with impossible days become impossible.*/
	check('m');

	/*After knowing the possible months, Bernard figures out the date with the day,
	which means the day can only exist once among the remaining dates.*/
	for (int i = 0; i < 6; i++)
		if (!once(i, 'd'))
			days[i] = false;

	/*Finally Albert figures out the date, which means the date is in the month that only has that single date.*/
	for (int i = 0; i < 4; i++)
		if (!once(i, 'm'))
			months[i] = false;

	/*Remaining days with all impossible dates become inpossible*/
	check('d');

	cout << "Cheryl's birthday is ";

	for (int i = 0; i < 4; i++)
		if (months[i])
			cout << i + 5 << ' ';
	for (int i = 0; i < 4; i++)
		if (days[i])
			cout << i + 14 << endl;

    return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).