-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharmstrongNumber1134.cpp
More file actions
59 lines (47 loc) · 970 Bytes
/
armstrongNumber1134.cpp
File metadata and controls
59 lines (47 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
The k-digit number N is an Armstrong number if and only if the k-th power of each digit sums to N.
Given a positive integer N, return true if and only if it is an Armstrong number.
Example 1:
Input: 153
Output: true
Explanation:
153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3.
Example 2:
Input: 123
Output: false
Explanation:
123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36.
Note:
1 <= N <= 10^8
*/
class Solution {
public:
bool isArmstrong(int N) {
int n = N;
vector<int>num;
while (n) {
num.emplace_back(n%10);
num/=10;
}
int count = num.size();
for (int i:num) {
N-=pow(i,count);
}
return N==0;
}
};
//the fatest method
class Solution
{
public:
bool isArmstrong(int N)
{
int sum = 0;
int d = 0;
for (int n = N; n; n /= 10)
++d;
for (int n = N; n; n /= 10)
sum += pow(n % 10, d);
return sum == N;
}
};