1-100中的有趣数,和特殊回文数,水仙花数。
public static void main(String[] args) {
int x = 0;
for (int i = 0; i < 2021; i++) {
int n = i;
for (int j = 1; j <= Math.sqrt(n); j++) {
if((int)Math.pow(j, 2)+(int)Math.pow((int)Math.sqrt(n-(int)Math.pow(j, 2)), 2)==n
&&j>=(int)Math.sqrt(n-(int)Math.pow(j, 2))){
// System.out.println(j+" "+(int)Math.sqrt(n-(int)Math.pow(j, 2)));
x++;
//有的数有多种表示方法,例如 25 = 5 * 5 + 0 * 0 = 3 * 3 + 4 * 4,在算答案时只算一次,所以跳出本次循环
break;
}
}
}
System.out.println("在 1 到 2021 中有多少"+x+"个这样的数:");
}
public class A {
public static void main(String[] args) {
int res = 0;
for1:for (int i = 1; i <= 2021; i++) {
for (int j = 0; j * j <= i; j++) {
for (int k = j; k * k <= i; k++) {
if (j * j + k * k == i) {
res ++;
System.out.println(i + "=" + j + "*" + j + "+" + k + "*" + k);
continue for1;
}
}
}
}
System.out.println("共有" + res + "个");
}
}