Skip to content

Commit

Permalink
Create ShortestDistance.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinandan323 authored Oct 26, 2021
2 parents 1d69400 + cb51d21 commit cca9ca8
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ShortestDistance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
using namespace std;
int main()
{
char ch;
int x=0,y=0;
cin>>ch;
while(ch!='\n')
{
switch(ch)
{
case 'N': y+=1; break;
case 'S': y-=1;break;
case 'E': x+=1;break;
case 'W': x-=1; break;
}
ch=cin.get();
}
cout<<"Displacement is :"<<x<<" "<<y<<endl;
if(x >=0 && y>=0)
{
for(int i=1;i<=x;i++)
cout<<"E";
for(int j=1;j<=y;j++)
cout<<"N";
}
else if(x <=0 && y>=0)
{
for(int i=1;i<=-x;i++)
cout<<"W";
for(int j=1;j<=y;j++)
cout<<"N";
}
else if(x <=0 && y<=0)
{
for(int i=1;i<=-x;i++)
cout<<"W";
for(int j=1;j<=-y;j++)
cout<<"S";
}
else if(x >=0 && y<=0)
{
for(int i=1;i<=x;i++)
cout<<"E";
for(int j=1;j<=-y;j++)
cout<<"S";
}
else
cout<<"Stay here ! Don't move;";
}

0 comments on commit cca9ca8

Please sign in to comment.