#34718: 阿姆斯壯數c++


anyiliu5604@gmail.com (an-yi Liu)

學校 : 臺北市立松山高級中學
編號 : 197699
來源 : [36.224.219.150]
最後登入時間 :
2023-09-05 23:35:57
a040. 阿姆斯壯數 | From: [36.224.217.221] | 發表日期 : 2023-04-10 18:15

得到輸入位數,即可解決
#include <iostream>
#include <math.h>
using namespace std;

 

int len(int n){
int ans = 0;
while(n != 0){
n /= 10;
ans++;
}
return ans;
}

 

int main(){
int first = 0;
int last = 0;
int count = 0;
cin >> first >> last;
for(int i = first; i <= last; i++){
int sum = 0;
int num = i; // use a separate variable to iterate through the digits of i
int* arr = new int[len(i)];
for(int j = 0; j < len(i); j++){
arr[j] = num % 10;
sum = sum + pow(arr[j], len(i));
num = num / 10;
}
if(sum == i){
cout << sum << " ";
count++;
}
delete[] arr; // free the memory allocated for the array
}
if (count == 0){
cout << "none" << endl;
}
return 0;
}
 
ZeroJudge Forum