Showing posts with label COJ. Show all posts
Showing posts with label COJ. Show all posts

Wednesday, September 10, 2014

COJ 1626. Adding Reversed Numbers



This task is a simple implementation task. Juts keep the number in an array digit by digit so that you can calculate the reversed number easily. The numbers are small so this means that we won't have to do the long number sum.


DOWNLOAD THE FULL SOURCE CODE



Sunday, September 7, 2014

COJ 1143. To and Fro

This is an implementation task so we need to think the easiest and the most understandable method. I suggest trying to recover the matrix then the string itself. We can do it in the following way. The first element of the given string S[0] is the first element of an array A[1][1] . Now we need to move according to the direction, and every time write the current char in the current spot, whenever we reached the end (or the beginning) of the array we need to flip the direction, get down to the next row and keep on going. Check out the source code for a better understanding.


       FULL SOURCE CODE

COJ 1457. Baseball Tournament

The number of distinct pairs of N people is ( N*(N-1) )  /2 here is why
suppose we have all the members numbered from 1 to N.
1,2,3...N.
So, one can be a pair( can play a game) with 2,3,4,...N.  Total N-1 games
2 can be pair with all starting from 3 . with 3,4,5,...N. Total N-2
........
..........
and N-1 can be a pair with N. total 1 pair.
So the result is, 1+2+3+..+N-1 which has a good formula of  ( (N-1)*(N-1+1) ) /2
The final answer is the answer above multiplied with the given parameter K.


       FULL SOURCE CODE

Thursday, September 4, 2014

COJ 1179. Optimal Parking

  The task might not be very clear on the statement part or maybe it is left up to the reader to understand. Anyway, the task wants the best coordinate to park where the distance which we walk would be minimal, and the distance means, getting out of the car, entering every store and coming back to the car. No matter where we park our car the distance walked will be minimal if we get to the beginning (or to the end) of the line and start visiting stores one after another until we have reached the other end of the line and then we also need to add the distance which takes from the other end of the line to our car. The constraints are only from 0 to 99 so we can just try out every single coordinate and calculate with brute force method.


      FULL SOURCE CODE

Wednesday, September 3, 2014

COJ 1808. Hamming Distance

We only need to input the 2 strings then loop over and see if elements on the same index match or not.


       FULL SOURCE CODE

Tuesday, September 2, 2014

COJ 1328. CAVerage

We only need to sum up all the numbers, then divide on N and then check if it is smaller than every element or not.


       DOWNLOAD THE FULL SOURCE CODE

Thursday, August 28, 2014

COJ 2148. Quadratic Equations

The equation has a solution if b^2-4*ac>=0.


       FULL SOURCE CODE

COJ 2091. Counting Task

There are many ways you can solve it. You can can take every element get it's code and mark it in a boolean array or you can simply use C++ map to mark the characters directly.


       FULL SOURCE CODE

Wednesday, August 27, 2014

COJ 1324. Snow White

There are only 9 number so the easiest approach is taking every single possibility and check. There are different ways but the easiest method is using 7 for loops.


       FULL SOURCE CODE

Tuesday, August 26, 2014

COJ 1237. Mean Median Problem

There are only 3 cases possible
1. (A+B+C)/3=A
2. (A+B+C)/3=B
3. (A+B+C)/3=C

from each we can get
1. C=2*A-B
2. C=2*B-A
3. C=(A+B)/2

You can make a function which checks the answer for 3 numbers and run it 3 times on all of the cases above and pick the minimum.


       FULL SOURCE CODE

COJ 2620. Aaaaaaaah

Output one 'A' then 4*N 'a' s and then one more 'h'.


       FULL SOURCE CODE

Monday, August 25, 2014

COJ 1219. Bounty Hunter

This task can be a little confusing because of the second parameter which is the distance from home. You may easily ignore it, there is nothing specified in task which wants us to use it, and more to that it says that who cares if we walk much, the important thing is to get paid as much as possible SO we only need to sort the array and start taking deals from the most expensive until our bullets run out.


       FULL SOURCE CODE

COJ 1273. Domino Factory

The most important thing is to correctly calculate the number of domino figures. So how do we do it?
Suppose we have numbers from 0 to N, how many distinct pairs we can make?
0-1,0-2.0-3.......,0-N
which gives N+1 pairs
so now let's consider all the new dominoes with 1
1-1,1-2,1-3,...1-N
total of N pairs
than for 2 it is
2-2,2-3,2-4,....2-N
N-1 pairs
and so on until the last one which is 0 .
So the total number of pairs of number from 0 to N is the sum of numbers from 1 to N+1 which has a good formula ( (N+1)*(N+2) )/2. The answer will be the sum of number from 1 to N+1 multiplied with the 2 sides of domino.


       FULL SOURCE CODE

COJ 1101. Binaries Palindromes

The constraints are a little high. It would be better if the answers have been calculated before reading data. Check whether the number from 1 to 200000 are binary palindromes and keep the answers/ And then input data and output answer.


       FULL SOURCE CODE

COJ 2205. Counting Ones

The constraints are very low, up to 1000 so we can do it with simple implementation, take every number and divide it to 2 and every time add the answer and the output the final result.


       FULL SOURCE CODE

Sunday, August 24, 2014

COJ 1573. Just Another Easy Problem

The task consists of 2 parts, conversion and check. First of all conversion. If we have a hex number and want to convert it to decimal we have to do the following. We need to multiply every digit with the power of it's index base 16 counting from the right to left. For example we have a number ABCD , after converting to decimal it will be 13*(16^0) + 12* (16^1) + 11*(16^2) + 10* (16^3).  Now let's check for the divisibility. The sum of numbers form 1 to N is N*(N+1) /2 and it is easy to notice that when N is odd then the result is divisible otherwise not.


       FULL SOURCE CODE

COJ 1839. A Funny Task

This task may sound a little weird, because even if we have even number of oranges, after passing the first guard we will be down to odd number and we might have a little confusion when giving the half and 3 more, but you just skip this and write the answer as it should be ,   (((N+3)*2+3)*2+3)*2 . 


       FULL SOURCE CODE

COJ 1842. Distance of Manhattan

The statement of the problem already gives everything, you only need to input 4 numbers and output the sum of absolute differences of the first third and second forth numbers.


       FULL SOURCE CODE

COJ 1118. The Drunk Jailer

Due to it's small constraints we can easily implement this algorithm. N is up to 100 which makes it very easy to solve using brute force.


       FULL SOURCE CODE

COJ 1445. What's Next?


The task itself is a very easy task. A very simple implementation just check if the difference between the first two equals to the difference between the second two elements. There is a little part to take care of. when one element equals to 0 we might end up dividing on 0 when we are checking if the progression is geometrical. Use this pseudocode

if(a2-a1==a3-a2)
print "arithemitc"
else
print "gemoetric"
Don't check for geometric progression

       FULL SOURCE CODE