📌  相关文章
📜  查找数字是否是AP的一部分,其第一个元素和不同之处已给出

📅  最后修改于: 2021-05-05 00:13:21             🧑  作者: Mango

给定三个非负整数a,d和x。在此,a是第一个元素,d是AP(算术级数)的差。我们需要确定x是否为给定AP的一部分。
例子 :

Input : a = 1, d = 3, x = 7
Output : Yes
7 is part of given AP, 1 + 3 + 3 = 7

Input : a = 10, d = 0, x = 10
Output : Yes

首先,在d = 0的情况下,如果a = x else答案为No,则应输出Yes。对于非零d,如果x属于序列x = a + n * d,其中n为非负整数,则仅当( n – a)/ c为非负整数。

C++
// C++ program to check if x exist
// or not in the given AP.
#include 
using namespace std;
 
// returns yes if exist else no.
bool isMember(int a, int d, int x)
{
 
    // If difference is 0, then x must
    // be same as a.
    if (d == 0)
        return (x == a);
 
    // Else difference between x and a
    // must be divisible by d.
    return ((x - a) % d == 0 && (x - a) / d >= 0);
}
 
// Driver code.
int main()
{
    int a = 1, x = 7, d = 3;
    if (isMember(a, d, x))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to check if x exist
// or not in the given AP.
class GFG {
 
    // returns yes if exist else no.
    static boolean isMember(int a, int d, int x)
    {
 
        // If difference is 0, then x must
        // be same as a.
        if (d == 0)
            return (x == a);
 
        // Else difference between x and a
        // must be divisible by d.
        return ((x - a) % d == 0 && (x - a) / d >= 0);
    }
 
    // Driver code.
    public static void main(String args[])
    {
        int a = 1, x = 7, d = 3;
        if (isMember(a, d, x))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Nikita Tiwari


Python3
# Python3 code to check if x exist
# or not in the given AP.
 
def isMember(a, d, x):
     
    # If difference is 0, then x
    # must be same as a.
    if d == 0:
        return x == a
     
    # Else difference between x
    # and a must be divisible by d.
    return ((x - a) % d == 0 and
        int((x - a) / d) >= 0)
 
# Driver code
a = 1
x = 7
d = 3
 
if isMember(a, d, x):
    print( "Yes")
else:
    print("No")
 
# This code is contributed by "Abhishek Sharma 44"


C#
// C# program to check if x exist
// or not in the given AP.
using System;
class GFG {
 
    // returns yes if exist else no.
    static bool isMember(int a, int d, int x)
    {
        // If difference is 0, then x must
        // be same as a.
        if (d == 0)
            return (x == a);
 
        // Else difference between x and a
        // must be divisible by d.
        return ((x - a) % d == 0 && (x - a) / d >= 0);
    }
 
    // Driver code.
    public static void Main()
    {
        int a = 1, x = 7, d = 3;
        if (isMember(a, d, x))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.


PHP
= 0);
}
 
// Driver code.
$a = 1; $x = 7; $d = 3;
if (isMember($a, $d, $x))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by aj_36
?>


Javascript


输出 :

Yes