两个系列的第一个碰撞点

给定五个数字a、b、c、d和n(其中a、b、c、d、n>0)。这些值代表两个系列的n项。由这四个数字组成的两个系列是b,b+a,b+2a…。b+(n-1)a和d,d+c,d+2c…。。d+(n-1)c 当两个级数在任何一点上的总和变得完全相同时,这两个级数将发生冲突。打印碰撞点。

null
Example:Input : a = 20, b = 2,         c = 9, d = 19,         n = 100Output: 82Explanation: Series1 = (2, 22, 42, 62, 82, 102...)  Series2 = (28, 37, 46, 55, 64, 73, 82, 91..) So the first collision point is 82.   

A. 幼稚的方法 就是在两个不同的数组中计算这两个序列,然后通过运行两个嵌套循环来检查每个元素是否冲突 时间复杂性 :O(n*n) 辅助空间 :O(n) 有效的方法 上述问题的解决办法是: *生成第一个系列的所有元素。让当前元素为x。 *如果x也是第二个系列的一个元素,那么应该满足以下条件。 …..a) x应大于或等于第二系列的第一个元素。 …..a) x和第一个元素之间的差应该可以被c整除。 *如果满足上述条件,则第i个值为所需的汇合点。 以下是上述问题的实施情况:

C++

// CPP program to calculate the colliding
// point of two series
#include<bits/stdc++.h>
using namespace std;
void point( int a, int b, int c, int d, int n)
{
int x , flag = 0;
// Iterating through n terms of the
// first series
for ( int i = 0; i < n; i++)
{
// x is i-th term of first series
x = b + i * a;
// d is first element of second
// series and c is common difference
// for second series.
if ((x - d) % c == 0 and x - d >= 0)
{
cout << x << endl ;
flag = 1;
break ;
}
}
// If no term of first series is found
if (flag == 0)
{
cout << "No collision point" << endl;
}
}
// Driver function
int main()
{
int a = 20 ;
int b = 2 ;
int c = 9;
int d = 19;
int n = 20;
point(a, b, c, d, n);
return 0;
}
// This code is contributed by  'saloni1297'.


JAVA

// Java program to calculate the colliding
// point of two series
import java.io.*;
class GFG
{
static void point( int a, int b, int c, int d, int n)
{
int x , flag = 0 ;
// Iterating through n terms of the
// first series
for ( int i = 0 ; i < n; i++)
{
// x is i-th term of first series
x = b + i * a;
// d is first element of second
// series and c is common difference
// for second series.
if ((x - d) % c == 0 && x - d >= 0 )
{
System.out.println( x ) ;
flag = 1 ;
break ;
}
}
// If no term of first series is found
if (flag == 0 )
{
System.out.println ( "No collision point" );
}
}
// Driver function
public static void main (String[] args)
{
int a = 20 ;
int b = 2 ;
int c = 9 ;
int d = 19 ;
int n = 20 ;
point(a, b, c, d, n);
}
}
// This code is contributed by vt_m


python

# Function to calculate the colliding point
# of two series
def point(a, b, c, d, n):
# Iterating through n terms of the
# first series
for i in range (n):
# x is i-th term of first series
x = b + i * a
# d is first element of second
# series and c is common difference
# for second series.
if (x - d) % c = = 0 and x - d > = 0 :
print x
return
# If no term of first series is found
else :
print "No collision point"
# Driver code
a = 20
b = 2
c = 9
d = 19
n = 20
point(a, b, c, d, n)


C#

// C# program to calculate the colliding
// point of two series
using System;
class GFG {
static void point( int a, int b, int c,
int d, int n)
{
int x, flag = 0;
// Iterating through n terms of the
// first series
for ( int i = 0; i < n; i++) {
// x is i-th term of first series
x = b + i * a;
// d is first element of second
// series and c is common difference
// for second series.
if ((x - d) % c == 0 && x - d >= 0) {
Console.WriteLine(x);
flag = 1;
break ;
}
}
// If no term of first series is found
if (flag == 0) {
Console.WriteLine( "No collision point" );
}
}
// Driver function
public static void Main()
{
int a = 20;
int b = 2;
int c = 9;
int d = 19;
int n = 20;
point(a, b, c, d, n);
}
}
// This code is contributed by vt_m


PHP

<?php
// PHP program to calculate
// the colliding point of
// two series
function point( $a , $b , $c , $d , $n )
{
$x ; $flag = 0;
// Iterating through
// n terms of the
// first series
for ( $i = 0; $i < $n ; $i ++)
{
// x is i-th term
// of first series
$x = $b + $i * $a ;
// d is first element of
// second series and c is
// common difference for
// second series.
if (( $x - $d ) % $c == 0 and
$x - $d >= 0)
{
echo $x ;
$flag = 1;
break ;
}
}
// If no term of first
// series is found
if ( $flag == 0)
{
echo "No collision po$" ;
}
}
// Driver Code
$a = 20 ;
$b = 2 ;
$c = 9;
$d = 19;
$n = 20;
point( $a , $b , $c , $d , $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript program to calculate
// the colliding point of
// two series
function point(a, b, c, d, n)
{
let x ;
let flag = 0;
// Iterating through
// n terms of the
// first series
for (let i = 0; i < n; i++)
{
// x is i-th term
// of first series
x = b + i * a;
// d is first element of
// second series and c is
// common difference for
// second series.
if ((x - d) % c == 0 &&
x - d >= 0)
{
document.write(x);
flag = 1;
break ;
}
}
// If no term of first
// series is found
if (flag == 0)
{
document.write( "No collision po" );
}
}
// Driver Code
let a = 20 ;
let b = 2 ;
let c = 9;
let d = 19;
let n = 20;
point(a, b, c, d, n);
// This code is contributed by _saurabh_jaiswal.
</script>


输出:

82

时间复杂度:O(n) 本文由 闪烁巴贾杰 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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