📌  相关文章
📜  通过跳跃两个给定的长度来达到数字

📅  最后修改于: 2021-04-25 00:09:08             🧑  作者: Mango

给定整数kd1d2和整数数组arr [] 。从数字k开始,您可以进行大小为d1d2的跳跃,即,从k开始的所有可能移动都是:

  • k + d1k – d1
  • k + d2k – d2

任务是从k进行任意数量的跳转,找到任意数组中的数字,并且给定的跳转大小为d1d2 。对于每个数字,如果可到达则打印1 ,否则打印0
例子:

方法:k可以跳到d1d2的任何数字x都将具有x = k +(i * d1)+(j * d2)的形式,其中ij是整数。
d1d2的GCD为gcd 。由于gcd同时划分了d1d2 。因此我们可以写成d1 = m1 * gcdd2 = m2 * gcd其中m1m2是整数
并且x = k + gcd *(i * m1 + j * m2)= k + M * gcd
因此,从k可以到达的任何数字x都应满足(x – k)%gcd = 0
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#include 
using namespace std;
 
// Function that returns the vector containing the
// result for the reachability of the required numbers
void reachTheNums(int nums[], int k, int d1, int d2, int n)
{
    int i, ans[n] = { 0 };
    int gcd = __gcd(d1, d2);
 
    for (i = 0; i < n; i++) {
        int x = nums[i] - k;
 
        // If distance x is coverable
        if (x % gcd == 0)
            ans[i] = 1;
        else
            ans[i] = 0;
    }
 
    for (i = 0; i < n; i++)
        cout << ans[i] << " ";
}
 
// Driver code
int main()
{
    // Numbers to be checked for reachability
    int nums[] = { 9, 4 };
    int n = sizeof(nums) / sizeof(nums[0]);
    // Starting number K
    int k = 8;
 
    // Sizes of jumps d1 and d2
    int d1 = 3, d2 = 2;
 
    reachTheNums(nums, k, d1, d2, n);
 
    return 0;
}


Java
// Java implementation of the approach
 
import java.io.*;
class GFG {
// Recursive function to return gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0 
        if (a == 0)
          return b;
        if (b == 0)
          return a;
        
        // base case
        if (a == b)
            return a;
        
        // a is greater
        if (a > b)
            return __gcd(a-b, b);
        return __gcd(a, b-a);
    }
 
 
     
 
// Function that returns the vector containing the
// result for the reachability of the required numbers
static void reachTheNums(int nums[], int k, int d1, int d2, int n)
{
    int i;
    int ans[] = new int[n];
    int gcd = __gcd(d1, d2);
 
    for (i = 0; i < n; i++) {
        int x = nums[i] - k;
 
        // If distance x is coverable
        if (x % gcd == 0)
            ans[i] = 1;
        else
            ans[i] = 0;
    }
 
    for (i = 0; i < n; i++)
        System.out.print(ans[i] + " ");
}
 
// Driver code
 
 
    public static void main (String[] args) {
        // Numbers to be checked for reachability
    int nums[] = { 9, 4 };
    int n =nums.length;
    // Starting number K
    int k = 8;
 
    // Sizes of jumps d1 and d2
    int d1 = 3, d2 = 2;
 
    reachTheNums(nums, k, d1, d2, n);
    }
}
 
// This code is contributed by inder_verma..


Python3
# Python3 implementation of the approach
import math as mt
 
# Function that returns the vector
# containing the result for the reachability
# of the required numbers
def reachTheNums(nums, k, d1, d2, n):
 
    ans = [0 for i in range(n)]
 
    gcd = mt.gcd(d1, d2)
 
    for i in range(n):
        x = nums[i] - k
 
        # If distance x is coverable
        if (x % gcd == 0):
            ans[i] = 1
        else:
            ans[i] = 0
 
    for i in range(n):
        print(ans[i], end = " ")
 
# Driver code
 
# Numbers to be checked for
# reachability
nums = [9, 4]
n = len(nums)
 
# Starting number K
k = 8
 
# Sizes of jumps d1 and d2
d1, d2 = 3, 2
 
reachTheNums(nums, k, d1, d2, n)
 
# This code is conteibuted
# by mohit kumar 29


C#
// C# implementation of the above approach
 
using System ;
 
class GFG {
     
    // Recursive function to return gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0
        if (a == 0)
        return b;
        if (b == 0)
        return a;
         
        // base case
        if (a == b)
            return a;
         
        // a is greater
        if (a > b)
            return __gcd(a-b, b);
             
        return __gcd(a, b-a);
    }
 
 
     
 
    // Function that returns the vector containing the
    // result for the reachability of the required numbers
    static void reachTheNums(int []nums, int k, int d1, int d2, int n)
    {
        int i;
        int []ans = new int[n];
        int gcd = __gcd(d1, d2);
     
        for (i = 0; i < n; i++) {
            int x = nums[i] - k;
     
            // If distance x is coverable
            if (x % gcd == 0)
                ans[i] = 1;
            else
                ans[i] = 0;
        }
     
        for (i = 0; i < n; i++)
            Console.Write(ans[i] + " ");
    }
 
    // Driver code
    public static void Main () {
        // Numbers to be checked for reachability
    int []nums = { 9, 4 };
    int n =nums.Length;
    // Starting number K
    int k = 8;
 
    // Sizes of jumps d1 and d2
    int d1 = 3, d2 = 2;
 
    reachTheNums(nums, k, d1, d2, n);
    }
    // This code is contributed by Ryuga
}


PHP


Javascript


输出:
1 1