In the recent Code Jam qualification round on problem B:
http://code.google.com/codejam/contest/1460488/dashboard , I got the small test case correct but the large test case wrong.
I went back to try and find the problem after the round ended and spotted 2 serious errors in my code, that had not affected the output for the small test case.
Here is the code, I will describe the errors below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include<iostream>
using namespace std;
int main()
{
unsigned int tests;
short int googlers, surprising, best;
short int total[100];
cin >> tests;
for(int i = 1; i <= tests; i++) //loops for each test case
{
cin >> googlers >> surprising >> best; //number of dancers, number of surprising scores, best score
for(int a=0; a < googlers; a++)
cin >> total[a];
int number = 0; //number that can be equal or greater than best
for(int a=0; a < googlers; a++) //loops for each score
{
bool found = false;
bool possible = false;
//---first try without surprising---//
for(int first = best; first <= 10 && !found; first++){ //**THIS LINE**
for(int second=first-1; second <= first && !found; second++){
for(int third=first-1; third <= first && !found; third++){
if(first + second + third == total[a]){ //if you can make the score...
if(first >= 0 && second >= 0 && third >= 0){ //... and none of them are illegal
possible = true;
found = true;
}
}
}
}
}
//---if failed without surprising try with---//
if((!found) && (surprising != 0)) //if an answer hasn't been found and there are any surprising left
{
for(int first = best; first <= 10; first++){ //**THIS LINE**
for(int second=first-2; second <= first; second++){
for(int third=first-2; third <= first; third++){
if(first + second + third == total[a]){
if(first >= 0 && second >= 0 && third >= 0){
surprising--; //surprising has been used so decrease accordingly
possible = true;
}
}
}
}
}
}
if(possible) //if it could be greater / equal to best...
number++; //increase total
}
cout << "Case #" << i << ": ";
cout << number;
if(i < tests) //to ensure that no endline is printed at the end
cout << endl;
}
return 0;
}
|
The first error was in each FOR loop that I have marked with
**THIS LINE**. Initially, I continued the loop while the first number was lower than the total. However, I now realize that this is wrong, as no number can exceed 10.
This did not affect the result for the small test case, as I don't think any of the
best values were above 10 anyway.
Once I fixed it however, my program output was not the same as before, so I looked for more errors:
The second error is in the line marked
//...and none of them are illegal and the matching line in the second try including surprising scores. After fixing the original problem I was faced with the issue that my program allowed values to be under 0, also illegal.
Once I fixed these 2 errors, the output went back to the original but as with the old code, it gets the practice version of the problem incorrect.
I've tried for ages looking for the error, but I can't find it. Can someone please show me where I am going wrong?