[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>问题:求100到999之间的 3位 水仙花数。/ n位水仙花数
<>水仙花数定义:水仙花数的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
*
<>3位水仙花数
方法一:(其中用的while循环)
public class Narcissus { public static void main(String[] args) { int num = 100
; int a[] = new int[3]; System.out.print("3位的水仙花数有:\t"); while (num <= 999) {
//用的while循环 int sum = 0; a[0] = num / 100; a[1] = num / 10 % 10; a[2] = num % 10
; for (int i = 0; i < 3; i++) { sum += Math.pow(a[i], 3); //相当于 sum = sum +
(int) Math.pow(a[i], 3); } if (num == sum) { System.out.print(num + "\t"); } num
++; } } }
方法二:(通俗易解版)(其中用的for循环)
public class Narcissus { public static void main(String[] args) { //
1.通过for循环获取[100, 999]之间所有的正整数 for (int i = 100; i < 1000; i++) { // i = 153
//用的for循环 // 2.获取i的个位数、十位数和百位数。 int bit1 = i % 10; int bit2 = i % 100 / 10; int
bit3= i / 100; // 3.1*1*1 + 5*5*5+ 3*3*3 == 153 if (bit1 * bit1 * bit1 + bit2 *
bit2* bit2 + bit3 * bit3 * bit3 == i) { System.out.println(i); } } } }
*
<>n位水仙花数
import java.util.Scanner; public class Narcissus { public static void main(
String[] agrs) { System.out.print("指定最大位数N:"); Scanner input = new Scanner(
System.in); int N = input.nextInt(); input.close(); for (int i = 3; i <= N; i++)
{ int a[] = new int[i]; int num = (int) Math.pow(10, i - 1) + 1; System.out.
print(i + "位的水仙花数有:\t"); while (num <= Math.pow(10, i)) { //num表示取值范围, 数字位数为i
int sum = 0; for (int j = 0; j < i; j++) a[j] = (int) (num / Math.pow(10, j) %
10); for (int j = 0; j < i; j++) sum = sum + (int) Math.pow(a[j], i); if (num ==
sum) System.out.print(num + "\t"); num++; } System.out.print("\n"); } } }