Skip to content

Commit 8d15de9

Browse files
authored
Create 2698. Find the Punishment Number of an Integer
1 parent 24765c7 commit 8d15de9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int solve(int sum, int n){
4+
int s = 0;
5+
int x = n;
6+
int m = 1;
7+
while(n){
8+
s=(n%10)*m + s;
9+
n/=10;
10+
m*=10;
11+
if(s+solve(sum-s,n)==sum){
12+
// cout<<sum<<endl;
13+
return sum;
14+
}
15+
}
16+
return x;
17+
}
18+
int punishmentNumber(int n) {
19+
int s = 0;
20+
for(int i = 1 ; i <= n ; ++i){
21+
if(solve(i,i*i)==i){
22+
// cout<<i<<endl;
23+
s+=(i*i);
24+
}
25+
}
26+
return s;
27+
}
28+
};

0 commit comments

Comments
 (0)