How does Java PTA calculate the number of daffodils in 3 to 7 digits
This article mainly introduces how Java PTA calculates the number of daffodils in 3 to 7 digits. It is very detailed and has a certain reference value. Interested friends must finish reading it!
PTA calculates the number of daffodils in 3 to 7 digits
Import java.util.Scanner;public class Main {public static void main (String [] args) {Scanner in = new Scanner (System.in); int n = in.nextInt (); int min = (int) Math.pow (10, n-1); int max = (int) Math.pow (10, n); int [] pow = new int [10]; pow [1] = 1; for (int I = 2; I)
< pow.length; i++) { pow[i] = (int) Math.pow(i, n); } for (int i = min; i < max; i++) { int sum = 0; for (int j = 10; j < max; j *= 10) { sum += pow[i / j % 10]; } sum += pow[i % 10]; if (sum == i) { System.out.println(i); } } }}水仙花数的Java实现 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:"水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。 要求输出所有在m和n范围内的水仙花数import java.util.ArrayList;import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int start = input.nextInt(); int end = input.nextInt();// 系统输入两个整数 ArrayList out = flower(start, end);// 调用方法 if (out.isEmpty()) {// 判断是否为空,按格式输出no。 System.out.println("no"); } else { for (int i = 0; i < out.size(); i++) {// 按格式输出 if (i != out.size() - 1) {// 最后一个不需要输出空格 System.out.print(out.get(i) + " "); } else { System.out.print(out.get(i)); } } } } public static ArrayList flower(int startNumber, int endNumebr) { ArrayList flowerNumber = new ArrayList(); int j = 0; for (int i = startNumber; i < endNumebr; i++) { /* * 若满足条件放入数组 条件是分别取出3个位的数的3次方相加==原数 */ if ((i == Math.pow(Math.floor(i / 100), 3) + Math.pow(Math.floor(i % 100 / 10), 3) + Math.pow(i % 100 % 10, 3)) && i >= startNumber & & I