public static void printNarcissisticNumber() { for (int i = 100; i <= 999; i++) { int ones = i % 10; int tens = (i % 100) / 10; int hundreds = i / 100; if (i == ones * ones * ones + tens * tens * tens + hundreds * hundreds * hundreds ) { System.out.println(i); } } }
求余运算符的运算规则
a%b=a-(a/b)b 5%3=5-(5/3)3=2
5%-3=5-(5/-3)-3=2 -5%3=-5-(-5/3)3=-2
-5%-3=-5-(-5/-3)-3=-2 如果操作数中有浮点数则采用的规则为 a%b=a-(bq),这里q=int(a/b)
5.2%3.1=5.2-13.1=2.1 5.2%-3.1=5.2-(-1)(-3.1)=2.1
-5.2%3.1=-5.1-(-1)3.1=-2.1 -5.2%-3.1=-5.1-(-1)(-3.1)=-2.1