Google Code Jam Problem : Store Credit : Solution

For days I have been trying to figure out how to deal with the whole input and output file thing in Google Code Jam Competition. Once I figured it out I thought of making a solution for the question recommended for beginners practicing for gcj – Store Credit. Google recommends this as the first problem for beginners who practice for code jam, and the reason is simple – it seems to be the easiest. You can read the problem here or download the zip file containing the source, input and output files along with the description. Well now we are all set lets go solve it.



Input :

While analyzing the input we can see that the first line of the input gives the number of test cases (N), which is followed by N test cases. For each test case the first line gives the credit (C), next line gives the number of items in the store. These credentials will help us solve the problem easily.

Output :

The output is simple, but should have the Case #x: (where x is the test case number) followed by indices of the items that add up exactly to the credit in the store.

Solution :

We can solve this problem in two ways:
1) Either we make a loop that will take one element of the array and try adding it to all other elements. And the indices of array elements that add up exactly to the credit C will be printed as the solution for the problem followed by the test case number.

2)Or we take an element from the input (which is now in the array) subtract it from the credit and in whichever case there is a positive number as the result we can just check if that number is present in the input as another element if there is one such element we'll print the indices of these two elements as the solution.


The first way is simple to understand so we'll follow it.

Code :

The output file can be made by these steps (Linux)::
1) open the terminal.
2) change the working directory to where the executable is.
3) Type in : ./{name_of_executable} < file-large.in > out.out 4) The above command will save the output to a file named out.out .
Make sure you have the input file in the same location as the executable.
OK, that's it. The code has been checked against Google Code jam solution checker. And the solution was found accurate. The code provided in the site and in the zip file are same and both are optimized for Linux distributions. You may rewrite it easily for a windows or mac system. For any help drop your question in the comments. Thanks for reading.

Share this:

, , , , ,

CONVERSATION

0 comments :

Post a Comment