Two Policemen Catching a Thief
Policeman1 and Policeman2 along with a thief are standing on X-axis. The integral value of their location on the X-axis is passed as the input.
Both the policemen run at the same speed and they start running at the same time to catch the thief.
- The program must print Police1 if Policeman1 will catch the thief first.
- The program must print Police2 if Policeman2 will catch the thief first.
- The program must print Both if Policeman1 and Policeman2 will reach the thief at the same time.
Both the policemen run at the same speed and they start running at the same time to catch the thief.
- The program must print Police1 if Policeman1 will catch the thief first.
- The program must print Police2 if Policeman2 will catch the thief first.
- The program must print Both if Policeman1 and Policeman2 will reach the thief at the same time.
Input Format:
The first line will contain the value of N which represents the number of test cases to be passed as the input.
Next N lines will contain the integral location of Policeman1, Policeman2 and the thief each separated by a space.
The first line will contain the value of N which represents the number of test cases to be passed as the input.
Next N lines will contain the integral location of Policeman1, Policeman2 and the thief each separated by a space.
Output Format:
The first line will contain one of the following values - Police1, Police2, Both
The first line will contain one of the following values - Police1, Police2, Both
Constraints:
1 <= N <= 20
1 <= N <= 20
Example Input/Output 1:
Input:
3
1 2 3
1 3 2
2 1 3
Input:
3
1 2 3
1 3 2
2 1 3
Output:
Police2
Both
Police1
Police2
Both
Police1
Example Input/Output 1:
Input:
2
-2 2 0
10 5 -1
Input:
2
-2 2 0
10 5 -1
Output:
Both
Police2Both
Code:
#include
<iostream>
#include<stdlib.h>
using
namespace std;
int
main(int argc, char** argv)
{
int
n,p1,p2,t,i;
cin>>n;
for(i=0;i<n;i++)
{
cin>>p1>>p2>>t;
if(abs(p1-t)>abs(p2-t))
cout<<"Police2\n";
else if(abs(p1-t)<abs(p2-t))
cout<<"Police1\n";
else
cout<<"Both\n";
}
}
Please do comment If u have any Queries!
int n=sc.nextInt();
ReplyDeleteint i;
sc.nextLine();
String s[]=new String[n];
for(i=0;isubb1)
{
System.out.println("Police2");
}
else if(subb1>subb)
{
System.out.println("police1");
}
else if(subb==subb1)
{
System.out.println("BOTH");
}
}
}
}
int n=sc.nextInt();
ReplyDeleteint i;
sc.nextLine();
String s[]=new String[n];
for(i=0;isubb1)
{
System.out.println("Police2");
}
else if(subb1>subb)
{
System.out.println("police1");
}
else if(subb==subb1)
{
System.out.println("BOTH");
}
}
}
}
#include
ReplyDelete#include
int main()
{
int n,i,p1,p2,t;
scanf("%d",&n);
for(i=0;iabs(p2-t))
{
printf("\nPolice2");
}
else if(abs(p1-t)<abs(p2-t))
{
printf("\nPolice1");
}
else
{
printf("\nBoth");
}
}
return 0;
}