给定数字N,输出从1到n的所有Munchhausen数字。
简介:慕尼黑(Münchhausen)数字是一个等于其位数乘以每个数字的幂的数字的总和。它类似于自恋数字。
例如:
3435 = 3 3 + 4 4 + 3 3 + 5 5
也可以将其视为Münchhausen数,因为当1升到幂1时本身就是1。
由于数字3435可以表示为数字的每个数字的总和,当数字的每个数字升为等于数字本身的幂时,即(((3升为3的幂)+(4升为4的幂) )+(3升到3的幂)+(5升到5的幂))将输出相同的数字,即3435,则该数字可以称为Münchhausen数。
例子:
Input : 500
Output : 1
One is the only Münchhausen Number smaller
than or equal to 500.
Input : 5000
Output : 1 3435
1 and 3435 are the only Münchhausen Numbers smaller
than or equal to 5000.
我们对每个i介于0到9之间的可能数字i预先计算i的幂次幂。在对这些值进行预先计算之后,我们遍历每个小于n的数字的所有数位,并计算提高到幂数的位数之和。
C++
// C++ code for Münchhausen Number
#include
using namespace std;
// pwr[i] is going to store i raised to
// power i.
unsigned pwr[10];
// Function to check out whether
// the number is Münchhausen
// Number or not
bool isMunchhausen(unsigned n) {
unsigned sum = 0;
int temp = n;
while (temp) {
sum += pwr[(temp % 10)];
temp /= 10;
}
return (sum == n);
}
void printMunchhausenNumbers(int n)
{
// Precompute i raised to power i for every i
for (int i = 0; i < 10; i++ )
pwr[i] = (unsigned)pow( (float)i, (float)i );
// The input here is fixed i.e. it will
// check up to n
for (unsigned i = 1; i <= n; i++)
// check the integer for Münchhausen Number,
// if yes then print out the number
if (isMunchhausen(i))
cout << i << "\n";
}
// Driver Code
int main() {
int n = 10000;
printMunchhausenNumbers(n);
return 0;
}
Java
// Java code for Munchhausen Number
import java.io.*;
import java.util.*;
class GFG {
// pwr[i] is going to store i raised to
// power i.
static long[] pwr;
// Function to check out whether
// the number is Munchhausen
// Number or not
static Boolean isMunchhausen(int n) {
long sum = 0l;
int temp = n;
while (temp>0) {
int index= temp%10;
sum =sum + pwr[index];
temp /= 10;
}
return (sum == n);
}
static void printMunchhausenNumbers(int n)
{
pwr= new long[10];
// Precompute i raised to
// power i for every i
for (int i = 0; i < 10; i++ )
pwr[i] = (long)Math.pow( (float)i, (float)i );
// The input here is fixed i.e. it will
// check up to n
for (int i = 1; i <= n; i++)
// check the integer for Munchhausen Number,
// if yes then print out the number
if (isMunchhausen(i)==true)
System.out.println(i );
}
public static void main (String[] args) {
int n = 10000;
printMunchhausenNumbers(n);
}
}
// This code is contributed by Gitanjali.
Python3
# Python 3 code for
# Münchhausen Number
import math
# pwr[i] is going to
# store i raised to
# power i.
pwr = [0] * 10
# Function to check out
# whether the number is
# Münchhausen Number or
# not
def isMunchhausen(n) :
sm = 0
temp = n
while (temp) :
sm= sm + pwr[(temp % 10)]
temp = temp // 10
return (sm == n)
def printMunchhausenNumbers(n) :
# Precompute i raised to
# power i for every i
for i in range(0, 10) :
pwr[i] = math.pow((float)(i), (float)(i))
# The input here is fixed
# i.e. it will check up to n
for i in range(1,n+1) :
# check the integer for
# Münchhausen Number, if
# yes then print out the
# number
if (isMunchhausen(i)) :
print( i )
# Driver Code
n = 10000
printMunchhausenNumbers(n)
# This code is contributed by Nikita Tiwari.
C#
// C# code for Munchhausen Number
using System;
class GFG {
// pwr[i] is going to store i
// raised to power i.
static long[] pwr;
// Function to check out whether
// the number is Munchhausen
// Number or not
static bool isMunchhausen(int n)
{
long sum = 0;
int temp = n;
while (temp > 0) {
int index = temp % 10;
sum = sum + pwr[index];
temp /= 10;
}
return (sum == n);
}
static void printMunchhausenNumbers(int n)
{
pwr = new long[10];
// Precompute i raised to
// power i for every i
for (int i = 0; i < 10; i++)
pwr[i] = (long)Math.Pow((float)i, (float)i);
// The input here is fixed i.e.
// it will check up to n
for (int i = 1; i <= n; i++)
// check the integer for Munchhausen Number,
// if yes then print out the number
if (isMunchhausen(i) == true)
Console.WriteLine(i);
}
// Driver Code
public static void Main()
{
int n = 10000;
printMunchhausenNumbers(n);
}
}
// This code is contributed by vt_m.
PHP
输出:
1
3435
注意:如果采用定义0 ^ 0 = 0,则恰好有四个Münchhausen数字:0、1、3435和438579088 [来源:MathWorld]