Skip to content

Commit

Permalink
added more cat-in-the-hat test cases and renamed file
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Jan 28, 2014
1 parent 4f40364 commit a4db921
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 56 deletions.
72 changes: 72 additions & 0 deletions wk1/107-cat-hat/cat-hat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Problem 107 - The Cat in the Hat
*
* Natalie Barillario & Jordan Scales
* for CS370
* Jan 27, 2014
*/
#include <iostream>
#include <cmath>
#include <stdio.h>

int main() {
std::string input;
int i, initialHeight, workingCats;
double n;
double x;
int totalCats, totalHeight, nonWorkingCats;
bool shouldBreak;

while(scanf("%d %d\n", &initialHeight, &workingCats)) {
if (initialHeight == 0 && workingCats == 0) {
break;
}

/* Handle edge case for 1 1 */
if (initialHeight == 1 && workingCats == 1) {
std::cout << 0 << " " << 1 << std::endl;
continue;
}

/**
* Working cats: n^x
* Height of first cat: (n+1)^x
*
* Find x and n
*/
for (n = 1; n <= workingCats; n++) {
shouldBreak = false;
for (x = 1; pow(n+1, x) <= initialHeight; x++) {
if (pow(n, x) == workingCats && pow(n+1, x) == initialHeight) {
shouldBreak = true;
break;
}
}

if (shouldBreak) break;
}

/**
* Compute the non-working cats (totalCats - workingCats)
* Total cats: n^0 + n^1 + ... + n^x
* Non-working Cats: Total cats - n^x
*
* Compute the height of everyone
* (n+1)^x * n^0 + (n+1)^(x-1) * n^1 + ... + (n+1)^1 * n^(x-1) + n^x
*/
totalCats = 0;
totalHeight = 0;

for (i = 0; i <= x; i++) {
totalCats += pow(n, i);
totalHeight += pow(n+1, x-i) * pow(n, i);
}
nonWorkingCats = totalCats - workingCats;

std::cout << nonWorkingCats << " " << totalHeight << std::endl;
}
return 0;
}



56 changes: 0 additions & 56 deletions wk1/107-cat-hat/p107.cpp

This file was deleted.

16 changes: 16 additions & 0 deletions wk1/107-cat-hat/test.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1 1
216 125
5764801 1679616
1024 243
2 1
4 1
1024 1
371293 248832
11 10
1 1
1048576 59049
483736625 481890304
125 64
64 1
81 64
0 0

0 comments on commit a4db921

Please sign in to comment.