📜  通过重复搜索 X*K,在给定向量中不存在 X 的最小值

📅  最后修改于: 2022-05-13 01:56:05.304000             🧑  作者: Mango

通过重复搜索 X*K,在给定向量中不存在 X 的最小值

给定一个向量vec和整数XK ,任务是每次在向量中找到X时,用XK的乘积替换X。返回向量中不存在的最终产品。

Naive Approach:这个问题可以通过在向量中反复找X来解决,每次找到X都用X*K代替。

请按照以下步骤了解如何:

  1. 遍历数组并检查数组中是否存在 X。
  2. 如果找到,将 X 替换为 X*K,然后重复步骤 1。
  3. 如果没有找到,则中断循环。
  4. 在循环结束时,返回 X 的最终值。

下面是上述方法的实现:

C++
// C++ program to implement the above approach
 
#include 
using namespace std;
 
// Function to check if X*K is present
// in the vector or not repeatedly
int findProductTillExist(vector& vec, int x, int k)
{
    // Calculating the size of the vector vec
    int n = vec.size();
 
    // Since we have to keep checking
    // and repeating the process we take
    // a condition which always holds
    while (true) {
 
        // Get an iterator which checks
        // if the X exists in the vector
        // or not by using the find function
        auto it = find(vec.begin(), vec.end(), x);
 
        // If the iterator does not point
        // to end it means x is present
        // Hence multiply X by K
        if (it != vec.end()) {
            x *= k;
        }
 
        // If x is present in vector
        // break the loop
        else
            break;
    }
 
    // Return the final value of x
    return x;
}
 
// Driver code
int main()
{
    vector vec = { 1, 2, 6, 10 };
    int x = 2, k = 3;
 
    cout << findProductTillExist(vec, x, k) << endl;
}


Java
// Java code for the above approach
class GFG {
  static int find(int[] vec, int x) {
    int index = -1;
    for (int i = 0; i < vec.length; i++) {
      if (vec[i] == x) {
        index = i;
      }
    }
    return index;
  }
 
  // Function to check if X*K is present
  // in the vector or not repeatedly
  static int findProductTillExist(int[] vec, int x, int k) {
 
    // Calculating the size of the vector vec
    int n = vec.length;
 
    // Since we have to keep checking
    // and repeating the process we take
    // a condition which always holds
    while (true) {
 
      // Get an iterator which checks
      // if the X exists in the vector
      // or not by using the find function
      int it = find(vec, x);
 
      // If the iterator does not point
      // to end it means x is present
      // Hence multiply X by K
      if (it != -1) {
        x *= k;
      }
 
      // If x is present in vector
      // break the loop
      else
        break;
    }
 
    // Return the final value of x
    return x;
  }
 
  // Driver code
  public static void main(String args[]) {
    int[] vec = { 1, 2, 6, 10 };
    int x = 2, k = 3;
 
    System.out.println(findProductTillExist(vec, x, k));
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# python3 program to implement the above approach
 
# Function to check if X*K is present
# in the vector or not repeatedly
def findProductTillExist(vec, x, k):
 
    # Calculating the size of the vector vec
    n = len(vec)
 
    # Since we have to keep checking
    # and repeating the process we take
    # a condition which always holds
    while (True):
 
        # Get an iterator which checks
        # if the X exists in the vector
        # or not by using the find function
        it = x in vec
 
        # If the iterator does not point
        # to end it means x is present
        # Hence multiply X by K
        if (it):
            x *= k
 
        # If x is present in vector
        # break the loop
        else:
            break
 
    # Return the final value of x
    return x
 
# Driver code
if __name__ == "__main__":
 
    vec = [1, 2, 6, 10]
    x, k = 2, 3
 
    print(findProductTillExist(vec, x, k))
 
# This code is contributed by rakeshsahni


C#
// C# code for the above approach
using System;
class GFG {
  static int find(int[] vec, int x)
  {
    int index = -1;
    for (int i = 0; i < vec.Length; i++) {
      if (vec[i] == x) {
        index = i;
      }
    }
    return index;
  }
 
  // Function to check if X*K is present
  // in the vector or not repeatedly
  static int findProductTillExist(int[] vec, int x, int k)
  {
 
    // Calculating the size of the vector vec
    int n = vec.Length;
 
    // Since we have to keep checking
    // and repeating the process we take
    // a condition which always holds
    while (true) {
 
      // Get an iterator which checks
      // if the X exists in the vector
      // or not by using the find function
      int it = find(vec, x);
 
      // If the iterator does not point
      // to end it means x is present
      // Hence multiply X by K
      if (it != -1) {
        x *= k;
      }
 
      // If x is present in vector
      // break the loop
      else
        break;
    }
 
    // Return the final value of x
    return x;
  }
 
  // Driver code
  public static void Main()
  {
    int[] vec = { 1, 2, 6, 10 };
    int x = 2, k = 3;
 
    Console.WriteLine(findProductTillExist(vec, x, k));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
18

时间复杂度: O(N 2 )
辅助空间: O(1)