首页 > 试题广场 >

水仙花数

[问答题]

水仙花数


题目描述:

春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。
现在要求输出所有在m和n范围内的水仙花数。

输入

输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

输出

对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。

样例输入

100 120
300 380

样例输出

no
370 371

importsys
a = [153,370,371,407]
fori in sys.stdin.readlines():
    a0, a1 = map(int, i.strip().split())
    res = []
    forj in range(a0, a1 +1):
        ifj in a: res.append(j)
    print("no"ifnot reselse" ".join(map(str, res)))
编辑于 2018-03-23 12:43:53 回复(0)
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int start = sc.nextInt();
			int end = sc.nextInt();
			boolean flag = false;
			if (100 <= start && start <= end && end <= 999) {
				for (int i = start; i <= end; i++) {
					int a = i / 100 % 10;
					int b = i / 10 % 10;
					int c = i % 10;
					int sum = a * a * a + b * b * b + c * c * c;
					if (i == sum) {
						flag = true;
						System.out.print(i + " ");
					}
				}
				if (flag == false) {
					System.out.print("no");
				}
				System.out.println();
			}
		}
		sc.close();
	}
}
发表于 2017-08-11 15:06:55 回复(0)
int m; int n; int count = 0;//记录水仙花数个数  Scanner scanner = new Scanner(System.in);
m = scanner.nextInt();
n = scanner.nextInt(); for(int i = m; i <= n; i++){ int cou100; int cou10; int cou1; //分别获取百位,十位,个位的数字  cou100 = i / 100;
    cou10 = (i % 100) / 10;
    cou1 = i % 10; if (cou1*cou1*cou1 + cou10*cou10*cou10 + cou100*cou100*cou100 == i){
        System.out.print(i + " ");
        count++;
    }
} if (count == 0){
    System.out.println("no");
}

发表于 2017-08-10 21:12:21 回复(0)

def shuiXian(m,n):
     a=[]
     for i in range (m,n):
        a1 = i/100
        a2 = i/10%10
        a3 = i%100%10
         if i == int(a1*a1*a1+a2*a2*a2+a3*a3*a3):
            a.append(i)
    return a
x,y=input().split()
shuiXian(x, y)

 
编辑于 2020-12-01 15:03:56 回复(0)
import java.util.Scanner;

public class MyTest01 {


public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext()) {
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        if(n<m) {
            System.out.println("no");
            continue;
        }
        boolean flag = true;
        for (; m <= n; n++) {
            if(n==getNum(n)) {
                System.out.println(n+"\t");
                flag = false ;
            }
        }
        if(flag) {
            System.out.println("no");
        }
    }
}
/**
 * 获取三个数字的立方
 * @param m
 * @return
 */
public static int getNum(int m) {
    if(m<10) {
        return m*m*m;
    }else {
        return (m%10)*(m%10)*(m%10) + getNum(m/10);
    }
}
}
发表于 2017-08-15 15:40:07 回复(0)