Code Chef “Yet Another Number Game” Problem Solved With PHP (Code: NUMGAME)
by Nishant Arora on 1/12/2011I love spending time on solving coding problems rather than watching tv or playing video games dunno why, but I love it, just an hour ago I started reading this problem here. And had developed a solution to the said problem:
| 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 | <?php $ip = fopen('php://stdin', "r"); $op = fopen('php://stdout',"w"); $test_cases = trim(fgets($ip)); $c = 0; while($c < $test_cases){ $turn = 1; $case = trim(fgets($ip)); while($case>2){ $limit = $case/2; $a=2; while(($a < $limit)){ $factor = $case/$a; if(floor($factor) == $factor){ break; }else{ $factor = 1; } $a++; } $case = $case - $factor; $turn++; } while($case>1){ $case--; $turn++; } if(($turn % 2) == 0){ fwrite($op, sprintf("%s\n","ALICE")); }else{ fwrite($op, sprintf("%s\n","BOB")); } $c++; } ?> |
I was amazed because while testing on the IDE here, and it was working all so fine, but when I submitted the solution to the codechef bot, it just gave me a time limit exceeded error. My jaws dropped when the error continued even after I had optimized the code pretty well. But as the say, you miss out things just right in front of you, thats it, I missed out an important pattern, the script was generating just even and odd outputs, in a formula not much known, study the test cases 1-10 and you will just say… AH-HA!…
So the solution simply becomes:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php $ip = fopen('php://stdin', "r"); $op = fopen('php://stdout',"w"); $test_cases = trim(fgets($ip)); $c = 0; while($c < $test_cases){ $turn = 1; $case = trim(fgets($ip)); if(($case % 2) == 0){ fwrite($op, sprintf("%s\n","ALICE")); }else{ fwrite($op, sprintf("%s\n","BOB")); } $c++; } ?> |
Easy Simple Slick!
Happy to solve it!
I see not many developers use PHP to get things done at CodeChef, maybe my findings helps others
Incoming search terms:
yet another number game | alice codechef | yet another number game solution | solution of yet another game in c | solution for problems on codechef | solution for numgame codechef | php and number game | numgame codechef solution in c | numgame codechef | codechef solutions to problems | code chef solutions | yet another number game solution algorithm | Some Other Random Posts: | |






