问题描述
求出所有的“水仙花数”。所谓的“水仙花数”,是指一个3位数,其各位数字的立方和等于该数本身。
输入格式
程序使用for循环遍历所有三位数整数,不需要手动输入
输出格式
遇到水仙花数输出
样例输入
无输入
样例输出
例:
153
370
371
407
数据规模和约定
输入数据中每一个数的范围。
例:100~999
代码如下:
Java
public class 水仙花数 {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if (a * a * a + b * b * b + c * c * c == i) {
System.out.println(i);
}
}
}
}
C++
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
for(int i=100;i<1000;i++){
int a = i/100;
int b = i/10%10;
int c = i%10;
if (a*a*a+b*b*b+c*c*c==i)
cout<<i<<endl;
}
return 0;
}