Created
May 23, 2019 14:26
-
-
Save LanHao0/9341a4f3e8490836b7fa9527850cab9e to your computer and use it in GitHub Desktop.
n位水仙花数,java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
/** | |
* @Author: LanHao | |
* @Website:https://lanhaoo.club/ | |
* @Created_Date:8:15 PM_5/23/2019 | |
* @Magic_Power_Of_Code! | |
*/ | |
public class water_flower { | |
public static void main (String[] args){ | |
Scanner scanner=new Scanner(System.in); | |
String n=scanner.nextLine(); | |
int new_n=Integer.valueOf(n); | |
//todo 判断是否为数字 | |
String range_left="1"; | |
String range_right="1"; | |
String[] a; | |
ArrayList result1=new ArrayList(); | |
for (int i = 0; i<new_n-1;i++){ | |
range_left=range_left+"0"; | |
} | |
for (int i = 0; i<new_n;i++){ | |
range_right=range_right+"0"; | |
} | |
int new_range_l=Integer.valueOf(range_left); | |
int new_range_r=Integer.valueOf(range_right); | |
System.out.println(new_range_l); | |
System.out.println(new_range_r); | |
for(int ii=new_range_l;ii<new_range_r;ii++){ | |
double final_answer=0; | |
String i=String.valueOf(ii); | |
a=i.split(""); | |
for (int j=0;j<a.length;j++){ | |
int cc=Integer.valueOf(a[j]); | |
final_answer=final_answer+Math.pow(cc,new_n); | |
} | |
if(final_answer==ii){ | |
result1.add(ii); | |
} | |
} | |
System.out.println(result1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment