查詢由數字字串組成的陣列的最大公約數 (GCD)


在本文中,我們將深入探討一個與陣列和字串操作相關的有趣問題。我們今天要研究的問題是“查詢由數字字串組成的陣列的最大公約數 (GCD)”。這個問題是磨練您在字串操作、陣列和數論方面技能的好方法。

問題陳述

給定一個字串陣列,其中每個字串表示一個正整數,我們的任務是找到所有這些整數的最大公約數 (GCD)。

方法

我們將每個字串轉換為整數,並計算所有這些整數的 GCD。為了計算 GCD,我們可以使用歐幾里得演算法,這是一種有效查詢兩個數的 GCD 的方法。

示例

以下是解決問題的程式:

#include <stdio.h>
#include <stdlib.h>

// Function to calculate the greatest common divisor (GCD) using Euclidean algorithm
int gcd(int a, int b) {
   if (b == 0) {
      return a;
   }
   return gcd(b, a % b);
}

// Function to find the GCD of an array of integers
int findGCD(int arr[], int size) {
   int result = arr[0];
    
   // Loop through the array and update the GCD with each element
   for (int i = 1; i < size; i++) {
      result = gcd(result, arr[i]);
   }
    
   return result;
}

int main() {
   int arr[] = {42, 84, 126, 168};
   int size = sizeof(arr) / sizeof(arr[0]);
   int result = findGCD(arr, size);
   printf("The GCD of the array is: %d\n", result);
   return 0;
}

輸出

The GCD of the array is: 42
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int gcd(int a, int b) {
   if (b == 0) {
      return a;
   }
   return gcd(b, a % b);
}

int findGCD(vector<string>& arr) {
   int result = stoi(arr[0]);
   
   for(int i = 1; i < arr.size(); i++) {
      result = gcd(result, stoi(arr[i]));
   }
   
   return result;
}

int main() {
   vector<string> arr = {"42", "84", "126", "168"};
   int result = findGCD(arr);
   cout << "The GCD of the array is: " << result << endl;
   return 0;
}

輸出

The GCD of the array is: 42
import java.util.Arrays;

public class GCDProgram {
   // Function to calculate the greatest common divisor (GCD) using Euclidean algorithm
   public static int gcd(int a, int b) {
      if (b == 0) {
         return a;
      }
      return gcd(b, a % b);
   }
   // Function to find the GCD of an array of integers
   public static int findGCD(int[] arr) {
      int result = arr[0];

      // Loop through the array and update the GCD with each element
      for (int i = 1; i < arr.length; i++) {
         result = gcd(result, arr[i]);
      }

      return result;
   }

   public static void main(String[] args) {
      int[] arr = {42, 84, 126, 168};
      int result = findGCD(arr);
      System.out.println("The GCD of the array is: " + result);
   }
}

輸出

The GCD of the array is: 42
# Function to calculate the greatest common divisor (GCD) using Euclidean algorithm
def gcd(a, b):
   if b == 0:
      return a
   return gcd(b, a % b)

# Function to find the GCD of a list of integers
def find_gcd(arr):
   result = int(arr[0])
    
   # Loop through the list and update the GCD with each element
   for i in range(1, len(arr)):
      result = gcd(result, int(arr[i]))
    
   return result

if __name__ == "__main__":
   arr = ["42", "84", "126", "168"]
   result = find_gcd(arr)
   print("The GCD of the array is:", result)

輸出

The GCD of the array is: 42

帶測試用例的解釋

讓我們取字串陣列 {"42", "84", "126", "168"}。

當我們將此陣列傳遞給 findGCD 函式時,它首先將第一個字串轉換為整數,並將其儲存在 result 變數中。

然後它迭代其餘的陣列,將每個字串轉換為整數,並將 result 更新為 result 和當前整數的 GCD。

對於給定的陣列,所有整數的 GCD 是 42。因此,函式將返回 42。

結論

這個問題演示了我們如何操作字串並使用數論來解決 C++ 中的複雜問題。這是一個練習您的 C++ 編碼技能並瞭解歐幾里得演算法以查詢兩個數的 GCD 的極好問題。

更新於:2023年10月20日

瀏覽量:354

開啟你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.