数组范围查询以查找最大阿姆斯特朗数并更新
数组范围查询是数据结构的一个新兴领域。在此查询中,我们将随机元素设置为数组,并给出了通用查询问题以有效地解决数据结构问题。阿姆斯特朗数是其数字立方和。例如,0、1、153、370、371 和 407 是阿姆斯特朗数。
让我们举个例子来理解阿姆斯特朗数
示例 1 - 给定数字为 371,检查该数字是否为阿姆斯特朗数。
3*3*3 + 7*7*7 + 1*1*1 = 371
因此,这是一个阿姆斯特朗数。
示例 2 - 给定数字为 121,检查该数字是否为阿姆斯特朗数。
1*1*1 + 2*2*2 + 1*1*1 = 9
因此,这不是一个阿姆斯特朗数。
在本文中,我们将解决数组范围查询以查找具有更新的最大阿姆斯特朗数。
语法
Vector<object_type> variable_name;
这是一种在程序中声明向量的方法。
算法
我们将从名为“bits/stdc++.h”的头文件开始。
我们正在创建一个名为“isArmstrong”的函数定义,它将 n 作为参数来检查该数字是否为阿姆斯特朗数。
以下几点了解阿姆斯特朗数的操作 -
将值“0”存储到“sum”变量中,该变量稍后将用于对每个具有幂的数字进行加法。
然后将“n”存储在变量“temp”中。此临时变量将在 while 循环中用于检查阿姆斯特朗数的条件。
接下来,我们将值“0”存储在变量“digits”中,该变量稍后将找到每个数字的幂。
现在开始主函数并初始化变量“arr[]”以设置给定的数组元素。
我们使用第一个 for循环打印数组元素。
初始化名为“armstrong”的向量变量,该变量将在 if 语句中满足条件,以使用预定义函数pushback()查找阿姆斯特朗数列表。
然后我们使用第二个 for循环迭代数组长度索引,并且在此循环下,if-else 语句已用于根据阿姆斯特朗数或非阿姆斯特朗数查找数组元素列表。
为了更新数组范围查询,我们初始化名为“newNumber”的变量以存储新的数组元素,该元素将通过使用 if-else 语句检查阿姆斯特朗数的验证。
接下来,将 0 存储到变量“maxArmstrong”中,该变量跟踪数组元素中的最大阿姆斯特朗数。
继续使用第三个 for循环,它迭代阿姆斯特朗元素长度。在此循环内,if 语句已用于查找最大阿姆斯特朗数。
然后使用最后一个 for循环迭代满足阿姆斯特朗数的以下数组元素,并打印所有阿姆斯特朗数。
最后,我们使用“maxArmstrong”变量打印最大阿姆斯特朗数。
示例
在此程序中,我们将查找具有更新的最大阿姆斯特朗数。
#include <bits/stdc++.h>
using namespace std;
// Function to check if a number is an Armstrong number or not
bool isArmstrong(int n) {
int sum = 0;
int temp = n;
int digits = 0;
while (temp > 0) {
digits++;
temp /= 10;
}
temp = n;
while (temp > 0) {
int digit = temp % 10;
sum += pow(digit, digits);
temp /= 10;
}
return sum == n;
}
int main() {
int arr[] = {0, 123, 1, 19, 12, 153, 370};
int a = sizeof(arr) / sizeof(arr[0]);
cout<<"The given array element:";
for(int m = 0; m < a; m++) {
cout<<arr[m]<<" ";
}
// Vector to store Armstrong numbers
vector<int> armstrongs;
// Check each element of the array if it's an Armstrong number or not
cout<<"\nThe element found to be Non-Armstrong number\n";
for (int i = 0; i < a; i++) {
if (isArmstrong(arr[i])) {
armstrongs.push_back(arr[i]);
} else {
cout << arr[i] << endl;
}
}
// Add a new number to the array and check if it's an Armstrong number or not
int newNumber = 1278;
cout<<"The newly added number\t"<<newNumber;
if (isArmstrong(newNumber)) {
cout << " : Armstrong number" << endl;
armstrongs.push_back(newNumber);
} else {
cout << " : Non-Armstrong number" << endl;
}
// Find the maximum Armstrong number in the array
int maxArmstrong = 0;
for (int i = 0; i < armstrongs.size(); i++) {
if (armstrongs[i] > maxArmstrong) {
maxArmstrong = armstrongs[i];
}
}
cout << "The following array element satisfied for Armstrong Number: ";
for (int i = 0; i < armstrongs.size(); i++) {
cout << armstrongs[i] << " ";
}
cout << endl;
cout << "The maximum Armstrong number in the array is: " << maxArmstrong << endl;
return 0;
}
输出
There are 3 array element whose setbits are in a multiple of KThe given array element:0 123 1 19 12 153 370 The element found to be Non-Armstrong number 123 19 12 The newly added number 1278 : Non-Armstrong number The following array element satisfied for Armstrong Number: 0 1 153 370 The maximum Armstrong number in the array is: 370
结论
我们探讨了数组范围查询的概念,以查找具有更新的最大阿姆斯特朗数。我们了解了如何将给定的数组元素过滤成阿姆斯特朗数和非阿姆斯特朗数的组合。从现有数组元素中删除非阿姆斯特朗数后,我们简单地打印了满足阿姆斯特朗类型的数组元素的结果,并在其中找到最大值。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP