程序填空题:Sum of N-Narcissistic Number
An $n$-digit number that is the sum of the $n$-th powers of its digits is called an $n$-narcissistic number. For example, $153$ is a $3$-narcissistic number since $153=1^3+5^3+3^3$, and $1634$ is a $4$-narcissistic number since $1634=1^4+6^4+3^4+4^4$.
Please complete the following program that prints the sum of all $n$-narcissistic numbers.
```c++
#include
long long cost[10], ans;
int cnt[10], t[10], n;
void dfs(int rest, int now, long long current) {
if (rest == 0) {
long long temp = current;
for (int i = 0; i < 10; i++)
t[i] = 0;
while (temp > 0) {
++t[temp % 10];
temp /= 10;
}
bool flag = 1;
for (int i = 0; i < 10; i++)
if (cnt[i] != t[i]) {
flag = 0;
break;
}
if (flag) {
;
}
return;
}
if () {
return;
}
for (cnt[now] = 0; cnt[now] <= rest; cnt[now]++)
dfs(rest - cnt[now], now + 1, );
= 0;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < 10; i++) {
cost[i] = 1;
for (int j = 0; j < n; j++)
cost[i] *= i;
}
dfs();
printf("%lld\n",ans);
return 0;
}
```
答案:
第1空:ans += current ||
第2空:now > 9 ||
第3空:current + cost[now] * cnt[now] ||
第4空:cnt[now] ||
第5空:n, 0, 0 ||
Please complete the following program that prints the sum of all $n$-narcissistic numbers.
```c++
#include
long long cost[10], ans;
int cnt[10], t[10], n;
void dfs(int rest, int now, long long current) {
if (rest == 0) {
long long temp = current;
for (int i = 0; i < 10; i++)
t[i] = 0;
while (temp > 0) {
++t[temp % 10];
temp /= 10;
}
bool flag = 1;
for (int i = 0; i < 10; i++)
if (cnt[i] != t[i]) {
flag = 0;
break;
}
if (flag) {
;
}
return;
}
if () {
return;
}
for (cnt[now] = 0; cnt[now] <= rest; cnt[now]++)
dfs(rest - cnt[now], now + 1, );
= 0;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < 10; i++) {
cost[i] = 1;
for (int j = 0; j < n; j++)
cost[i] *= i;
}
dfs();
printf("%lld\n",ans);
return 0;
}
```
答案:
第1空:ans += current ||
第2空:now > 9 ||
第3空:current + cost[now] * cnt[now] ||
第4空:cnt[now] ||
第5空:n, 0, 0 ||