USACO BRHS10A Problem 'fizzbuzz' Analysis

by Damon Doucet

This problem doesn't require anything fancy; simply loop through each line of input and determine whether or not each line is as it should be. When you find the line that is incorrect, you've found half of your answer (the other half is easily computed if we've gotten this far).

Here's my solution:

#include <fstream>
using namespace std;

ifstream fin("fizzbuzz.in");
ofstream fout("fizzbuzz.out");

int N, A, B;
string Find(int i);

int main() {
	int i; string check;
	fin >> N >> A >> B;

	for (i = 1; i <= N; i++) {
		fin >> check;
		if (check != Find(i))
			break;
	}

	fout << i << "\n" << Find(i) << "\n";
}

string inttostring(int i) {
	string ret = "";
	do {
		ret = char(i % 10 + '0') + ret;
		i /= 10;
	} while (i > 0);
	return ret;
}

string Find(int i) {
	if (i % A > 0 && i % B > 0) return inttostring(i);

	if (i % A == 0) {
		if (i % B == 0) return "FizzBuzz";
		else return "Fizz";
	}
	else return "Buzz";
}