Vous êtes sur la page 1sur 3

Assignment

1. [Easy] Write the atoi funtion. Input should be taken as a string of numbers and converted to an
integer and returned. String "123" should be converted to numerical value. Input string to be
validated to contain only numerals.

2. [Easy] Implement a fast integer multiplication algorithm as follows: Consider two integers ``A
and ``B. Note that B can be written as X.2^0 + X.2^1+... and so on. The value of X can be 0 or 1.
Once B is converted in the above form the multiplication involves addition and shifting operations
alone.
Consider the following example:
5 * 10 , where 10 can be written as [(1 * 2^3) + (0 * 2^2) + (1*2^1) + (0 * 2^0)]
= [(1 * 2^3) + (1*2^1)].
Now the multiplication becomes (5 << 3)+ (5<<1) which gives (40) +(10) = 50. Implement such a
program using bit manipulation technique. Ensure that you take care of overflows. Is this algorithm
really fast? When does is execute really faster than the regular multiplication based algorithm?

3. [Medium] Convert roman letters to number using a C program. The output should be like this.
Give the roman numbers: LX
Equivalent Decimal: 60

Give the roman numbers:XC


Equivalent Decimal: 90

Give the roman numbers: XII


Equivalent Decimal: 12

Note : X = 10, C = 100, L = 50, V=5, D=500, M=1000

3. [Medium] After setting up the area and his toys. Chandu is up for playing his very first game. His
first game is played on a N X N board with some initial stones placed on each cell. He can move
each stone in all four direction i.e up,down, left or right. His target of the game is to move all stones
to any one of the four corners in minimum moves (different stones can be moved to different
corners).
Chandu seeks you again for his help. Your task is to write a program that tell the minimum number

of moves given a particular state of board.


NOTE: One cell may contain more than one stones.
Input:

First line of input contains two integers N and K. Where, N X N is the size of the board and k is the
no of stones present on the board. Then, two lines(x[] and y[]) follow each containing K integers
where ith integer of first line and ith integer of second line represent the x-cordinate and ycoordinate of ith stone respectively. in short, (x[i],y[i]) is where ith stone is placed at.
Output:

Print the minimum number of moves required to move all stone to the corners.
Constraints:

1<=N<=10^6, 1<=k<=10^5
Example
Consider the following line of text
34
2213
2231
First line 3, 4 says that the board size is 3X3 and there are 4 stones present. The four stones are
places in (2,2), (2,2), (1,3) and (3,1).
We move the 2 stones in (2,2) to (0,0) in 2 moves. From (1,3) we can move to (0,3) we need 1
move. Similarly from (3,1) we need to do 1 more move to reach (3,0). Now all the stones are in the
corners. Note that this should be minimal number of moves. The answer for the above test case is
4.
5. [Difficult] Johnny has become addicted to playing the Lights Off game on his new iPhone.
Yesterday he got stuck at a difficult level. He asked you, the talented programmer, for help. Please
write a program to help Johnny solve the Lights Off game, not only for the regular 5x5 board size,
but also for much larger dimensions!
Input
The first line contains t, the number of test cases (about 10). Then t test cases follow.
Each test case has the following form:
The first line contains n, the size of the board (3 n 30).
Then n lines follow, each line contains n characters '0' or '1'. The character in the ith line and
jth column is '0' if the corresponding light is off and '1' if it is on.
Output
For each test case, in the first line, print k, the number of times Johny must press a light. Any valid
solution in which k does not exceed 5000 is accepted.

Then k lines follow, each line containing two numbers i and j (1 i, j n), describing the position
of a light to be pressed. Note that (i,j) means the light in the ith row and jth column; the rows are
numbered 1 to n from top to bottom, and the columns are numbered 1 to n from left to right.
Example
Input:
1
3
000
110
010

Output:
5
11
21
22
32
33

Output details:
The states of the game after pressing each light are:
010 -> 110 -> 100 -> 011 -> 000
-->
000
110
010

(1,1)

110
010
010

(2,1)

010
100
110

(2,2)

000
011
100

(3,2)

000
001
011

(3,3)

000
000
000

Vous aimerez peut-être aussi