When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

I_love_to_eat's blog

By I_love_to_eat, history, 6 years ago, translation, In English

I was trying to solve 320A - Magic Numbers and encountered a funny thing about codeforces. While this code ran okay

char c = getchar();
while (c != '\n')
{
...
}

my solution was giving me WA1, while running fine on my computer

int main()
{
	char c = getchar();
	while (c != EOF)
	{
		if (c != '1')
		{
			cout << "NO";
			return 0;
		}

		while (c == '1')
			c = getchar();

		if (c == '4')
		{
			c = getchar();

			if (c == '1') {
				continue;
			} else if (c == '4'){
				c = getchar();
				continue;
			} else if (c == EOF) {
				break;
			} else {
				cout << "NO";
				return 0;
			}
		} else if (c == EOF) {
			break;
		} else {
			cout << "NO";
			return 0;
		}
	}

	cout << "YES";

	return 0;
}

Maybe, it's the newline char at the end of the output? No 33100199.

So I numbered the NO'es to understand which one is malfunctioning: 33100283. And it was the second one. EOF looked suspicious, as I somehow remembered it could result in undefined behaviour, so hoped, that codeforces tests end with a new line character and it passed 33100350.

But still, my guess about EOF was a true speculation, googling didn't give me anything sensible, so can anybody explain what was the problem?

  • Vote: I like it
  • +10
  • Vote: I do not like it

| Write comment?