变种水仙花数 - Lily Number:把任意的数字,从中间拆分成两个数字,比如1461 可以拆分成(1和461),(14和61),(146和1),如果所有拆分后的乘积之和等于自身,则是一个Lily Number。
例如:
655 = 6 * 55 + 65 * 5
1461 = 1*461 + 14*61 + 146*1
求出 5位数中的所有 Lily Number。
变种水仙花数 - Lily Number:把任意的数字,从中间拆分成两个数字,比如1461 可以拆分成(1和461),(14和61),(146和1),如果所有拆分后的乘积之和等于自身,则是一个Lily Number。
例如:
655 = 6 * 55 + 65 * 5
1461 = 1*461 + 14*61 + 146*1
求出 5位数中的所有 Lily Number。
无
一行,5位数中的所有 Lily Number,每两个数之间间隔一个空格。
import java.util.Scanner; public class Main { public static void main(String[] args) { int sum1 = 0,sum2 = 0,n = 10,m = 0; for (int i = 10000; i <= 99999; i++) { int tmp = i; while (n < 100001){ //循环控制次数 m = tmp % n; //m是右边的数 tmp = tmp/n; //tmp是左边的数 sum1 = m * tmp; //sum1是每次的乘积 n = n*10; // n控制位数 sum2 += sum1; //sum2每次加和 tmp = i; //初始化 } if (i == sum2){ System.out.print(i + " "); } n = 10; sum2 = 0; } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { for (int i = 10000; i <= 99999; i++) { if(lily(i)) { System.out.print(i+" "); } } } static boolean lily(int x) { int sum = 0; int y = x; String xStr = String.valueOf(x); for (int i = 1; i <= 4; i++) { int head = Integer.parseInt(xStr.substring(0, i)); int tail = Integer.parseInt(xStr.substring(i)); sum += head * tail; } if (sum == y ) { return true ; } return false; } }
import java.util.Scanner; import java.util.ArrayList; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { ArrayList<Integer> array=new ArrayList<Integer>(); for(int i=10000;i<=99999;i++){ int a=(i/10000)*(i%10000); int b=(i/1000)*(i%1000); int c=(i/100)*(i%100); int d=(i/10)*(i%10); if(i==(a+b+c+d)){ array.add(i); } } for(int i=0;i<array.size();i++){ System.out.print(array.get(i)+" "); } } }
public class Main{ public static void main(String[] args){ for(int i=10000;i<100000;i++){ if(lilyNum(i)) System.out.printf("%d ",i); } } public static boolean lilyNum(int x){ int count=0; for(int i=10;i<=10000;i*=10){ count+=(int)(x%i)*(x/i); } return count==x?true:false; } }
import java.util.*; public class Main { public static void main(String[] arg) { for (int i = 10000 ; i < 100000; i++) { int a = i / 10000; int a1 = (i - a * 10000) * a; int b = i / 1000; int b1 = (i - b * 1000) * b; int c = i / 100; int c1 = (i - c * 100) * c; int d = i / 10; int d1 = (i % 10) * d; int num = a1 + b1 + c1 + d1; if ( num == i) { System.out.printf(i+" "); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //while(scanner.hasNext()){ for(int i = 10000;i < 100000;i++){ int a = (i / 10000) * (i % 10000); int b = (i / 1000) * (i % 1000); int c = (i / 100) * (i % 100); int d = (i / 10) * (i % 10); if(i == (a + b + c + d)){ System.out.print(i + " "); } } //} } }
/** * @Title: 变种水仙花 * @Remark: 变种水仙花数 - Lily Number:把任意的数字,从中间拆分成两个数字,比如1461 可以拆分成(1和461),(14和61),(146和1),如果所有拆分后的乘积之和等于自身,则是一个Lily Number。 * @Author: ijunfu * @Version: 1.0.0 * @Date: 2022-03-18 */ public class Main { public static void main(String[] args) { for(int i=10000; i<99999; i++) { int sum = 0; String numStr = String.valueOf(i); for(int j=1; j<5; j++) { int prefix = Integer.parseInt(numStr.substring(0, j)); int suffix = Integer.parseInt(numStr.substring(j)); sum += prefix * suffix; } if(i == sum) System.out.print(i + " "); } } }
//很容易就可以看懂的代码 import java.util.*; public class Main{ public static void main(String[] args) { for (int i = 10000;i < 100000;i++) { int sum = 0; String s = Integer.toString(i); for (int j = 1; j < 5; j++) { int pre = Integer.parseInt(s.substring(0,j)); int end = Integer.parseInt(s.substring(j,s.length())); sum+= pre*end; } if (sum == i){ System.out.print(i+" "); } } } }
public class Main{ public static void main(String[] args){ for(int i=10000;i<=99999;i++){ if(isLilyNumber(i)){ System.out.print(i+" "); } } } public static boolean isLilyNumber(int n){ //23456 int sum = n%10000*(n/10000)+n%1000*(n/1000)+n%100*(n/100)+n%10*(n/10); if(n==sum){ return true; } return false; } }
public class Main{ public static void main(String[] args){ int num = 10000; int a; int b; while (num < 99999){ int sum = 0; for (int i = 0; i < 4; i++){ // 先转换成字符串,再分别分割求和 a = Integer.parseInt(String.valueOf(num).substring(0, 4 - i)); b = Integer.parseInt(String.valueOf(num).substring(4 - i)); sum += a * b; } if (sum == num){ System.out.print(num + " "); } num++; } } }
public class Main { public static void main(String[] args) { int a,b,c,d,e; int r1,r2,r3,r4; for (int i = 10000; i < 99999; i++) { r1=(i/10000)*(i%10000); r2=(i/1000)*(i%1000); r3=(i/100)*(i%100); r4=(i/10)*(i%10); if(i==(r1+r2+r3+r4)){ System.out.print(i); System.out.print(" "); } }Java的通过代码全是直接输出 真丢人