
在这个问题中,我们给出了整数数组。我们需要将所有元素组合成一个整数并检查它是否是哈沙德数。
在我们继续解决方案之前,让我们先了解一下哈尔沙德数。所有的数都是哈尔沙德数,它们可以被它们的数字之和整除。例如,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 整除。
这种方法将所有数组元素合并为一个字符串。然后,我们将使用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),因为我们不使用额外的空间。
在这种方法中,我们将对组合整数的每个小块执行模运算,并检查大整数是否能被其和整除。
定义“组合”字符串变量。
迭代整数数组,将所有整数组合并存储到‘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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号