C++ 检查由连接所有数组元素形成的数字是否为哈夏德数
在这个问题中,我们给定了一个整数数组。我们需要将所有元素组合为一个整数,并检查它是否是一个哈夏德数。
在我们解决这个问题之前,让我们先了解一下哈夏德数。所有的数字都是哈夏德数,它们可以被它们的数字之和整除。例如,12是哈夏德数,因为12可以被3(即1+2)整除。
为了解决这个问题,我们可以将所有数组元素组合起来,然后检查结果数字是否是哈夏德数。
问题陈述 -我们给定一个整数数组。我们需要将所有元素组合成一个数字,并检查组合的数字是否是哈夏德数。
示例
输入 - arr = {1, 35, 69, 60};
输出 - YES
解释 - 结果数字1356960可以被它的数字之和整除。
输入 - arr = {1, 65, 78, 1}
输出 - No
解释 - 组合数字165781不能被28整除。
输入 - arr = {1, 44}
输出 - YES
解释 - 144可以被9整除。
方法1
这个方法将把所有数组元素组合成一个字符串。然后,我们将使用stoi()方法将组合字符串转换为整数。然后,我们可以使用模运算符来检查数字是否可以被它的数字之和整除。
步骤
- 定义’combined’字符串变量,并用空字符串初始化它。
-
遍历整数数组。使用to_string()方法将数字转换为字符串。然后,将它附加到’combined’变量上。
-
定义’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”字符串变量。
- 遍历整数数组,将所有整数组合起来,并将它们存储到“combined”变量中。
- 将数字的总和存储在“sum”变量中。
- 使用循环遍历“combined”字符串。
- 定义“current”变量并初始化为零。
- 将“current”变量乘以10,然后加上当前的数字值。然后将计算结果存储在“current”变量中。
- 将“current”与总和进行模操作。
- 当循环的所有迭代完成后,如果“current”变量的值为零,则返回true。如果“current”变量的值不为零,则返回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个数组元素。
极客笔记