62,612
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
public class FlowerNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入次方数:");
int n = sc.nextInt();
flower(n);
}
/**
* 判断一个数是否为花朵数,如果是打印出该数
*
* @param n 次方
*/
private static void flower(int n) {
for (int i = (int) Math.pow(10, n - 1); i < (int) Math.pow(10, n); i++) {
String s = String.valueOf(i);
int count = 0;
for (int j = 0; j < s.length(); j++) {
count += (int) Math.pow(Integer.parseInt(s.charAt(j) + ""), n);
}
if (count == i) {
System.out.println(i);
}
}
}
}
public class Test {
public static void main(String[] args) {
for (int i = 100; i < 1000; ++i) {
int n = i;
int sum = 0;
int bits = calculateBits(i);
while (n != 0) {
sum += Math.pow(n % 10, bits);
n /= 10;
}
if (sum == i) {
System.out.println(sum);
}
}
}
public static int calculateBits(int n) {
int count = 0;
do {
n /= 10;
++count;
} while (n != 0);
return count;
}
}
public class Narcissistic
{
public static void main (String args[])
{
System.out.println("Get Narcissistic Number from 100 to 10,000:");
for(int n=100;n<=10000;n++)//Check number from 100 to 10,000
if(isNarcissistic(n))
System.out.print(n+" ");
}
static boolean isNarcissistic(int x)//method to judge whether x is Narcissistic Number or not
{
int digit[ ]=new int[20];//array to stores each digit of x
int sum=0,places=String.valueOf(x).length();//places is the digit of x
int y=x;//copy x to complete the following loop
for(int i=0;i<places;i++){
digit[i]=y%10;
y=y/10;
}
for(int i=0;i<places;i++)
sum+=Math.pow(digit[i],places);
if(x==sum)
return true;
else
return false;
}
}