首頁  >  文章  >  後端開發  >  檢查將數組元素連接形成的數字是否為哈希德數

檢查將數組元素連接形成的數字是否為哈希德數

WBOY
WBOY轉載
2023-08-25 20:17:06751瀏覽

檢查將數組元素連接形成的數字是否為哈希德數

在這個問題中,我們給了整數陣列。我們需要將所有元素組合成一個整數並檢查它是否是哈沙德數。

在我們繼續解決方案之前,讓我們先了解哈爾沙德數。所有的數都是哈爾沙德數,它們可以被它們的數字總和整除。例如,12是哈爾沙德數,因為12可以被3整除,而3是1 2的和。

為了解決這個問題,我們可以將所有的陣列元素相加,然後檢查結果是否是一個Harshad數。

問題陳述——我們給了一個整數陣列。我們需要將所有元素組合成一個數字,並檢查組合數​​字是否為哈沙德數。

範例

輸入– arr = {1, 35, 69, 60};

輸出-是

解釋 - 結果數字 1356960 可以被它的和整除。

輸入   # arr = {1, 65, 78 , 1}

輸出 – 否

說明 – 合併後的數字 165781 不能被 28 整除。

輸入  – arr = {1, 44}

輸出-是

解釋——144 能被 9 整除。

方法 1

這種方法將所有陣列元素合併為一個字串。然後,我們將使用stoi()方法將合併的字串轉換為整數。之後,我們可以使用模運算子來檢查數字是否可以被其各位數字之和整除。

演算法

  • 定義「組合」字串變數並使用空字串對其進行初始化。

  • 迭代整數數組。使用 to_string() 方法將數字轉換為字串。之後,將其附加到“組合”變數中。

  • 定義變數‘sum’並將其初始化為零,用於儲存數字的總和。

  • 遍歷組合字串,並儲存每個數字的總和。

  • 使用stoi()方法將組合的字串轉換為整數。之後,對整數進行取模運算,並根據結果傳回布林值。

範例

#include <iostream>
#include <vector>
using namespace std;

// function to check whether the number formed by concatenating the elements of the array is a Harshad number or not
bool isHarshadNumber(vector<int> array){
   // store the concatenated number
   string combined = "";
   // Iterate over the array
   for (auto num : array){
      // Concatenate the string
      combined += to_string(num);
   }
   // Stores the sum of digits
   int sum = 0;
   // Calculate sum of digits
   for (int i = 0; i < combined.length(); i++)
      sum += (combined[i] - '0');
   // Check if n is divisible by the sum
   return stoi(combined) % sum == 0;
}
int main(){
   // Input
   vector<int> arr{1, 35, 69, 60};
   if (isHarshadNumber(arr))
      cout << "Yes, the number formed by concatenating the array element is a Harshad number";
   else
      cout << "No, the number formed by concatenating the array element is not a Harshad number";
   return 0;
}

輸出

Yes, the number formed by concatenating the array element is a Harshad number

時間複雜度 - O(N),因為我們遍歷字串。

空間複雜度 - O(1),因為我們不使用額外的空間。

方法2

在這個方法中,我們將對組合整數的每個小塊執行模運算,並檢查大整數是否能被其和整除。

演算法

  • 定義「組合」字串變數。

  • 迭代整數數組,將所有整數組合並儲存到‘combined’變數中。

  • 將數字總和儲存在「sum」變數中

  • #使用循環遍歷「組合」字串。

  • 定義‘current’變數並初始化為零

  • #將‘current’變數乘以10,並加上目前的數字值。然後,將結果值儲存在‘current’變數中。

  • 對‘current’和sum進行模運算。

  • 當循環的所有迭代完成時,如果「目前」變數的值為零,則傳回 true。如果目前變數的值不為零,則傳回 false。

範例

#include <iostream>
#include <vector>
using namespace std;

// function to check whether the number formed by concatenating the elements of the array is a Harshad number or not
bool isHarshadNumber(vector<int> array){
   // store the concatenated number
   string combined = "";
   // Iterate over the array
   for (auto num : array){
      // Concatenate the string
      combined += to_string(num);
   }
   // Stores the sum of digits
   int sum = 0;
   // Calculate the sum of digits
   for (int i = 0; i < combined.length(); i++)
      sum += (combined[i] - '0');
   // to store the current integer
   int current = 0;
   for (int i = 0; i < combined.size(); i++) {
      // Calculate the current integer by multiplying 10 and adding the current digit
      current = current * 10 + (combined[i] - '0');
      // Check if the current integer is divisible by the sum
      current %= sum;
   }
   return current == 0;
}
int main(){
   // Input
   vector<int> arr{1, 35, 69, 0};
   if (isHarshadNumber(arr))
      cout << "Yes, the number formed by concatenating the array element is a Harshad number";
   else
      cout << "No, the number formed by concatenating the array element is not a Harshad number";
   return 0;
}

輸出

No, the number formed by concatenating the array element is not a Harshad number

時間複雜度 - O(N)

空間複雜度 - O(1)

結論

我們學習了兩種不同的方法來解決問題。第一種方法僅在數組包含較少元素時使用,因為 stoi() 方法在將字串轉換為整數時有一些限制。第二種方法是通用的,可以用於N個陣列元素。

以上是檢查將數組元素連接形成的數字是否為哈希德數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除