标题: 关于打印水仙花数的程序,大家看看还能不能优化? [打印本页] 作者: 刘文秀 时间: 2016-7-27 22:26 标题: 关于打印水仙花数的程序,大家看看还能不能优化? public static void main(String[] args) {
int sum = 0;
for (int a = 1;a <= 100 ;a++ ) {
//sum = sum + a; //求1-10的和用
if (a % 2 == 0) {
sum = a + sum;
} //求偶数和用
if (a % 2 != 0) {
sum = sum + a;
} //求奇数和用
}
System.out.println(sum);
//打印水仙花数
for (int a = 100;a <= 999 ;a++ ) {
int ge = a % 10;
int shi = a / 10 % 10;
int bai = a / 10 / 10 % 10;
if (ge*ge*ge + shi*shi*shi + bai*bai*bai == a) {
System.out.println(a);
}
}
//统计水仙花数
int num = 0;
for (int a = 100;a <= 999 ;a ++ ) {
int ge = a % 10;
int shi = a /10 % 10;
int bai = a /10 / 10 % 10;
if (ge*ge*ge + shi*shi*shi + bai*bai*bai == a) {
num++;
}
}
System.out.println(num);