An Armstrong number is defined as an N-digit integer such that the sum of its digits is raised to the power of N equals the number itself.
Example:1634 = 14 + 64 + 34 + 44
Please find all Armstrong numbers within the specified range according to the requirements of the problem.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input consists of one line containing two numbers N and M (N 0, M <= 1000000), representing the range to search for Armstrong numbers. | All Armstrong numbers within the given range should be outputted sequentially in ascending order. If none are found, output "none." |
100 999 | 153 370 371 407 |
10 99 | none |
Thought Process
Use a for loop to iterate from N to M and check each number for the Armstrong property. Convert the numbers to strings to perform the Armstrong number check. Store the found Armstrong numbers in an array/vector. If the size of the vector is 0, output "none"; otherwise, output the contents of the vector.
Sample Code-ZeroJudge A040: Armstrong Number
#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int>num;
for (int i = N; i<=M; i++)
{
string str = to_string(i);
int sum = 0;
int len = str.size();
for (int j = 0; j<str.size(); j++)
{
sum += pow(str[j] - '0', len);
}
if (sum == i) num.push_back(i);
}
sort(num.begin(), num.end());
if (num.size() == 0) cout << "none" << endl;
else
{
for (int i = 0; i<num.size(); i++)
{
cout << num[i] << " ";
}
}
}
//ZeroJudge A040
//Dr. SeanXD