找出两个人是否在相同次数的跳跃后相遇

两个人从两个不同的点p1和p2开始比赛。他们在一次跳跃中跑完s1和s2米。找出他们是否会在相同次数的跳跃后在某个点相遇。 例如:

null
Input : p1 = 6, s1 = 3,         p2 = 8, s2 = 2Output : YesExplanation: 6->9->12             8->10->12They meet after two jumps.Input : p1 = 4, s1 = 4,         p2 = 8, s2 = 2Output : YesExplanation: 4->8->12             8->10->12Input : p1 = 0, s1 = 2,         p2 = 5, s2 = 3Output : NoInput : p1 = 42, s1 = 3,         p2 = 94, s2 = 2Output : Yes

A. 简单解决方案 就是让他们一个接一个地跳。每次跳跃后,看看它们是否在同一点上。 一 有效解决方案 基于以下事实: 由于起点总是不同的,如果满足以下条件,它们就会满足。 (1) 速度不一样 (2) 速度差除以初始点之间的总距离。

C++

// C++ program to find any one of them
// can overtake the other
#include<bits/stdc++.h>
using namespace std;
// function to find if any one of them can
// overtake the other
bool sackRace( int p1, int s1, int p2, int s2){
// Since starting points are always
// different, they will meet if following
// conditions are met.
// (1) Speeds are not same
// (2) Difference between speeds divide the
//     total distance between initial points.
return ( (s1 > s2 && (p2 - p1) % (s1 - s2) == 0) ||
(s2 > s1 && (p1 - p2) % (s2 - s1) == 0));
}
// driver program
int main()
{
int p1 = 4, s1 = 4, p2 = 8, s2 = 2;
sackRace(p1, s1, p2, s2)? cout << "Yes" :
cout << "No" ;
return 0;
}


JAVA

// java program to find any one of them
// can overtake the other
import java.util.Arrays;
public class GFG {
// function to find if any one of
// them can overtake the other
static boolean sackRace( int p1, int s1,
int p2, int s2)
{
// Since starting points are
// always different, they will
// meet if following conditions
// are met.
// (1) Speeds are not same
// (2) Difference between speeds
// divide the total distance
// between initial points.
return ( (s1 > s2 && (p2 - p1) %
(s1 - s2) == 0 ) ||
(s2 > s1 && (p1 - p2)
% (s2 - s1) == 0 ));
}
public static void main(String args[])
{
int p1 = 4 , s1 = 4 , p2 = 8 , s2 = 2 ;
if (sackRace(p1, s1, p2, s2))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
// This code is contributed by Sam007.


Python3

# python program to find any one of them
# can overtake the other
# function to find if any one of them can
# overtake the other
def sackRace(p1, s1, p2, s2):
# Since starting points are always
# different, they will meet if following
# conditions are met.
# (1) Speeds are not same
# (2) Difference between speeds divide the
#     total distance between initial points.
return ( (s1 > s2 and (p2 - p1) % (s1 - s2) = = 0 )
or (s2 > s1 and (p1 - p2) % (s2 - s1) = = 0 ))
# driver program
p1 = 4
s1 = 4
p2 = 8
s2 = 2
if (sackRace(p1, s1, p2, s2)):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by Sam007


C#

// C# program to find any one of them
// can overtake the other
using System;
class GFG {
// function to find if any one of
// them can overtake the other
static bool sackRace( int p1, int s1,
int p2, int s2)
{
// Since starting points are
// always different, they will
// meet if following conditions
// are met.
// (1) Speeds are not same
// (2) Difference between speeds
// divide the total distance
// between initial points.
return ( (s1 > s2 && (p2 - p1) %
(s1 - s2) == 0) ||
(s2 > s1 && (p1 - p2)
% (s2 - s1) == 0));
}
// Driver code
public static void Main()
{
int p1 = 4, s1 = 4, p2 = 8,
s2 = 2;
if (sackRace(p1, s1, p2, s2))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
// This code is contributed by Sam007.


PHP

<?php
// PHP program to find any one of them
// can overtake the other
// function to find if any one of
// them can overtake the other
function sackRace( $p1 , $s1 , $p2 , $s2 )
{
// Since starting points are always
// different, they will meet if following
// conditions are met.
// (1) Speeds are not same
// (2) Difference between speeds divide the
//     total distance between initial points.
return (( $s1 > $s2 && ( $p2 - $p1 ) % ( $s1 - $s2 ) == 0) ||
( $s2 > $s1 && ( $p1 - $p2 ) % ( $s2 - $s1 ) == 0));
}
// Driver Code
$p1 = 4;
$s1 = 4;
$p2 = 8;
$s2 = 2;
if (sackRace( $p1 , $s1 , $p2 , $s2 ))
echo "Yes" ;
else
echo "No" ;
// This code is contributed by Sam007
?>


Javascript

<script>
// JavaScript program to find any one of them
// can overtake the other
// function to find if any one of
// them can overtake the other
function sackRace(p1, s1, p2, s2)
{
// Since starting points are
// always different, they will
// meet if following conditions
// are met.
// (1) Speeds are not same
// (2) Difference between speeds
// divide the total distance
// between initial points.
return ( (s1 > s2 && (p2 - p1) %
(s1 - s2) == 0) ||
(s2 > s1 && (p1 - p2)
% (s2 - s1) == 0));
}
// Driver Code
let p1 = 4, s1 = 4, p2 = 8, s2 = 2;
if (sackRace(p1, s1, p2, s2))
document.write( "Yes" );
else
document.write( "No" );
// This code is contributed by susmitakundugoaldanga.
</script>


输出:

Yes

本文由 维沙尔·库马尔·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享