1-100中的有趣数,和特殊回文数,水仙花数。
2条回答 默认 最新
- Yangyufan95 2021-12-19 10:52关注
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+"个这样的数:"); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录