TCS Coding Question 1 | Our hoary culture had several great persons ….
Problem Statement
Our hoary culture had several great persons since time immemorial and king vikramaditya’s nava ratnas (nine gems) belongs to this ilk.They are named in the following shloka:
Among these, Varahamihira was an astrologer of eminence and his book Brihat Jataak is recokened as the ultimate authority in astrology.
He was once talking with Amarasimha,another gem among the nava ratnas and the author of Sanskrit thesaurus, Amarakosha.
Amarasimha wanted to know the final position of a person, who starts from the origin 0 0 and travels per following scheme.
Scheme
- He first turns and travels 10 units of distance
- His second turn is upward for 20 units
- Third turn is to the left for 30 units
- Fourth turn is the downward for 40 units
- Fifth turn is to the right(again) for 50 units
… And thus he travels, every time increasing the travel distance by 10 units.
Test Cases
Case 1
- Input : 3
- Expected Output :-20 20
Case 2
- Input: 4
- Expected Output: -20 -20
Case 3
- Input : 5
- Expected Output : 30 -20
Case 4
- Input : 7
- Expected Output : 90 -20
C
C++
Java
Python
Perl
C
#include <stdio.h> #include <stdlib.h> int main() { int n; scanf("%d", &n); char c = 'R'; int x = 0, y = 0; int distance = 10; while(n) { switch(c) { case 'R': x = x + distance; c = 'U'; distance = distance + 10; break; case 'U': y = y + distance; c = 'L'; distance = distance + 10; break; case 'L': x = x - distance; c = 'D'; distance = distance + 10; break; case 'D': y = y - distance; c = 'A'; distance = distance + 10; break; case 'A': x = x + distance; c = 'R'; distance = distance + 10; break; } n--; } printf("%d %d",x,y); return 0; }
C++
#include <iostream> using namespace std; int main() { int n; cin >> n; char c = 'R'; int x = 0, y = 0; int distance = 10; while(n) { switch(c) { case 'R': x = x + distance; c = 'U'; distance = distance + 10; break; case 'U': y = y + distance; c = 'L'; distance = distance + 10; break; case 'L': x = x - distance; c = 'D'; distance = distance + 10; break; case 'D': y = y - distance; c = 'A'; distance = distance + 10; break; case 'A': x = x + distance; c = 'R'; distance = distance + 10; break; } n--; } cout << x << " " << y <<endl; return 0; }
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCase = sc.nextInt(); getDistance(testCase); } public static void getDistance(int a) { int distance = 10; int x = 0; int y = 0; char ch = 'R'; while(a > 0) { switch(ch) { case 'R': x = x + distance; ch = 'U'; distance = distance+10; break; case 'U': y = y + distance; ch = 'L'; distance = distance + 10; break; case 'L': x = x - distance; ch = 'D'; distance = distance + 10; break; case 'D': y = y - distance; ch = 'A'; distance = distance + 10; break; case 'A': x = x + distance; ch = 'R'; distance = distance + 10; break; } a--; } System.out.println(x+ " , "+y); } }
Python
n = int(input()) c = 'R' dis = 10 x,y=0,0 for i in range(n): if c=='R': x=x+dis c='U' dis=dis+10 elif c=='U': y=y+dis c='L' dis=dis+10 elif c=='L': x=x-dis c='D' dis=dis+10 elif c=='D': y=y-dis c='A' dis=dis+10 elif c=='A': x=x+dis c='R' dis=dis+10 print(x,y)
Perl
$n=<>; $n=$n*1; $c=0; $dis=10; $x=0; $y=0; for(my $i=0;$i<$n;$i++) { if($c==0) { $x=$x+$dis; $c=1; $dis=$dis+10; } elsif($c==1) { $y=$y+$dis; $c=2; $dis=$dis+10; } elsif($c==2) { $x=$x-$dis; $c=3; $dis=$dis+10; } elsif($c==3) { $y=$y-$dis; $c=4; $dis=$dis+10; } elsif($c==4) { $x=$x+$dis; $c=0; $dis=$dis+10; } } print $x; print " "; print $y;
Login/Signup to comment
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int num = obj.nextInt();
int j = 10;
int x = 0, y = 0;
int a = 0, b = 0;
for (int i = 1; i <= num; i++) {
if (i % 2 != 0 && a + 2 == i) {
x = x – j;
a++;
} else if (i % 2 != 0) {
x = x + j;
a++;
}
if (i % 2 == 0 && b + 3 == i) {
y = y – j;
b++;
} else if (i % 2 == 0) {
y = y + j;
b++;
}
j += 10;
}
System.out.println(x + "," + y);
}
}
public static void main(String args[]{
System.out.println(“Enter input”);
(This is missing in java program)
C++ implementation
#include
using namespace std;
int main(){
int t;
cin>>t;
int n = 5, dis = 10;
int x = 0 , y = 0, j = 0;
for(int i=0; i<t; i++, j++){
if(j == 5){
j = 0;
}
if(j == 0){
x += dis;
dis += 10;
}else if(j == 1){
y += dis;
dis += 10;
}else if(j == 2){
x -= dis;
dis += 10;
}else if(j == 3){
y -= dis;
dis += 10;
}else{
x += dis;
dis += 10;
}
}
cout<<x<<" "<<y;
return 0;
}
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press “Run” button to compile and execute it.
*******************************************************************************/
#include
using namespace std;
int main(){
int n,x,y;
cin>>n;
x=0,y=0;
for(int i=1; i<=n; i++){
if(i%4==1){
x=x+(10*i);
}
if(i%4==2){
y=y+(10*i);
}
if(i%4==3){
x=x-(10*i);
}
if(i%4==0){
y=y-(10*i);
}
}
cout<<x<<" "<<y<<" ";
}
#include
using namespace std;
int main()
{
int d, n;
int x, y;
cin >> n;
for( x = y = 0, d = 10 ; d <= n*10 ; d += 10 )
{
switch( d%40 )
{
case 10 : x += d; break;
case 20 : y += d; break;
case 30 : x -= d; break;
case 0 : y -= d; break;
}
}
cout << x << ' ' << y;
return 0;
}
I have solved in this way 😉
#include
using namespace std;
int right(int a,int b,int c)
{
a = a+c;
b = b;
return a;
}
int upward(int a,int b,int c)
{
b = b+c;
return b;
}
int left(int a,int b,int c)
{
a = a-c;
b = b;
return a;
}
int downward(int a,int b,int c)
{
b = b-c;
a = a;
return b;
}
int main()
{
int T;
cout<<"enter the number of test cases want to print"<>T;
int x {0};
int y {0};
int i = 1;
int p {0};
while(T)
{
if(T>=i)
{
p = p+10;
x = right(x,y,p);
}
else
{
T = false;
}
++i;
if(T>=i)
{
p = p+10;
y = upward(x,y,p);
}
else
{
T = false;
}
++i;
if(T>=i)
{
p = p+10;
x = left(x,y,p);
}
else
{
T = false;
}
++i;
if(T>=i)
{
p = p+10;
y = downward(x,y,p);
}
else
{
T = false;
}
++i;
if(T>=i)
{
p = p+10;
x = right(x,y,p);
}
else
{
T = false;
}
++i;
}
cout<<x<<" "<<y<<endl;
}
THIS IS BEST CODE FOR THE PROBLEM
#include
using namespace std;
int main() {
int n;
cin>>n;
int x=0,y=0,k=10;
for(int i=1;i<=n;i++){
if(i%5==1)
x=x+k;
else if(i%5==2)
y=y+k;
else if(i%5==3)
x=x-k;
else if(i%5==4)
y=y-k;
else
x=x+k;
k+=10;
}
cout<<x<<" "<<y;
}
x,y=0,0
n = int(input())
if n==1:
x += 10
elif n==2:
x += 10
y +=20
elif n==3:
x += 10
y +=20
x -= 30
elif n==4:
x += 10
y +=20
x -= 30
y -= 40
elif n==5:
x += 10
y +=20
x -= 30
y -= 40
x += 50
elif n==6:
x += 10
y +=20
x -= 30
y -= 40
x += 50
x += 60
elif n==7:
x += 10
y +=20
x -= 30
y -= 40
x += 50
x += 60
y += 70
print(f'{x} {y}’)
Much Simpler way
/**
* prepCodin1
*/
import java.util.Scanner;
public class prepCodin1 {
public static void getDistance(int a) {
int x = 0;
int y = 0;
for (int i = 0; i < a; i++) {
if ((i) % 5 == 0) {
x = x + (10 * (i + 1));
} else if ((i) % 5 == 1) {
y = y + (10 * (i + 1));
} else if ((i) % 5 == 2) {
x = x – (10 * (i + 1));
} else if ((i) % 5 == 3) {
y = y – (10 * (i + 1));
} else {
x = x + (10 * (i + 1));
}
}
System.out.println(x + " , " + y);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
getDistance(testCase);
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x=0;int y=0;int d=10;
for(int i=1;i<=n;i++){
if(i%4==1){
x=x+d;
d=d+10;
}else if(i%4==2){
y=y+d;
d=d+10;
}else if(i%4==3){
x=x-d;
d=d+10;
}else if(i%4==0){
y=y-d;
d=d+10;
}
}
System.out.println(x+" "+y);
}
}
python code is wrong
n-1
n = int(input())
if n % 4 == 0:
x = -(n * 5)
y = x
elif n % 4 == 1:
x = (n + 1) * 5
y = -x + 10
elif n % 4 == 2:
x = n * 5
y = x + 10
elif n % 4 == 3:
x = -(n + 1) * 5
y = -x
print((x, y))