Position Of A Man
A man from origin can move in below direction,
10 Mts right in first turn,
20 Mts up in second turn,
30 Mts left in third turn,
40 Mts down in fourth turn,
50 Mts right in fifth turn,
10 Mts right in sixth turn,
20 Mts up in seventh turn,
30 Mts left in eighth turn,
40 Mts down in ninth turn,
50 Mts right in tenth turn and so on
Write a program to print the position of man from origin in nth turn.
where 2<=n <= 1000.
n = no of turns.
Input Format:
Take integer from stdin.
Output Format:
print the position of man from origin.
Example Input:
3
Output:
-20 20
Explanation
- In each turn the step size increases by 10. Also, the turns move from right, up, left, down and right then the same steps keeps repeating
- In our case, man starts from (0,0) --> (10,0) --> (10,20) --> (-20,20)
- Hence answer is (-20,20)
SOLUTION:
n=int(input())
x=0
y=0
i=1
while i<=n:
t=i%5 #since after 5 turns for 6th turn we have to start like 1st turn
if(t==1):
x=x+10 #first turn add 10 means to travel towrads right
if(t==2):
y=y+20 #second turn add 20 to travel towards up
if(t==3):
x=x-30 #3rd turn travel 30 towards left so subtract
if(t==4):
y=y-40 #4th to travel down 40 mts so subtract
if(t==0):
x=x+50 #5th to travel 50 towrads right so add
i=i+1
print(x,y)
NOTE: during left and right VALUES wont be changing in Y values
and During UP and DOWN Values wont Changes in X values
Comments
Post a Comment