Google Code Jam 2016 – Qualification Round
Here are my solutions
A. Counting Sheep
//
// main.cpp
// Counting Sheep
//
// Copyright © 2016 Robotex. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <set>
int main(int argc, const char * argv[]) {
// insert code here...
if (argc != 2)
{
std::cout << "Pass the test case as argument" << std::endl;
return 1;
}
std::ifstream fin(argv[1]);
std::ofstream fout("output.txt");
unsigned int T;
unsigned int N;
fin >> T;
for (unsigned i = 1; i <= T; ++i)
{
std::set<unsigned int> digits;
unsigned int k = 0;
fin >> N;
if (N > 0)
{
while (digits.size() < 10)
{
++k;
unsigned int digitsToParse = k * N;
unsigned int digitExtracted;
while (digitsToParse != 0)
{
digitExtracted = digitsToParse % 10;
digitsToParse /= 10;
digits.insert(digitExtracted);
}
}
}
fout << "Case #" << i << ": ";
if (digits.size() == 10)
fout << k * N << std::endl;
else
fout << "INSOMNIA" << std::endl;
}
return 0;
}
B. Revenge of the Pancakes
//
// main.cpp
// Revenge of the Pancakes
//
// Copyright © 2016 Robotex. All rights reserved.
//
#include
#include
#include
int main(int argc, const char * argv[]) {
// insert code here...
if (argc != 2)
{
std::cout << "Pass the test case as argument" << std::endl; return 1; } std::ifstream fin(argv[1]); std::ofstream fout("output.txt"); unsigned int T; std::string S; auto flipfun = [] (char c) { return c=='-'?'+':'-'; }; fin >> T;
fin.ignore();
for (unsigned i = 1; i <= T; ++i)
{
std::getline(fin,S);
unsigned int flips = 0;
for (std::string::reverse_iterator it = S.rbegin(); it != S.rend(); ++it)
{
if (*it == '+')
continue;
if (S.front() == '-')
{
std::reverse(it, S.rend());
std::transform(it, S.rend(), it, flipfun);
}
else
{
std::string::reverse_iterator searchit;
for (searchit = it; *searchit!='+'; ++searchit);
std::reverse(searchit, S.rend());
transform(searchit, S.rend(), searchit, flipfun);
}
++flips;
}
fout << "Case #" << i << ": " << flips << std::endl;
}
return 0;
}