Java練習 SDUT-1239_水仙花數
阿新 • • 發佈:2018-09-26
java mem 等於 clas next() public out pri code
如果給定的範圍內不存在水仙花數,則輸出no;
每個測試實例的輸出占一行。
水仙花數
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
春天是鮮花的季節,水仙花就是其中最迷人的代表,數學上有個水仙花數,是這樣定義的:
“水仙花數”是指一個三位數,它的各位數字的立方和等於其本身,比如:153=13+53+33。
現在要求輸出所有在m和n範圍內的水仙花數。
Input
輸入數據有多組,每組占一行,包括兩個整數m和n(100<=m<=n<=999)。
Output
對於每個測試實例,要求輸出所有在給定範圍內的水仙花數,就是說,輸出的水仙花數必須大於等於m,並且小於等於n,如果有多個,則要求從小到大排列在一行內輸出,之間用一個空格隔開;
每個測試實例的輸出占一行。
Sample Input
100 120
300 380
Sample Output
no
370 371
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int i,n,m,num,a[] = new int[10000]; int f,b[] = new int [10]; num = 0; for(i=100;i<=999;i++) if(judge(i)==1) a[num++] = i; //153 370 371 407; while(cin.hasNext()) { f = 0; n = cin.nextInt(); m = cin.nextInt(); for(i=0;i<num;i++) if(a[i]>=n&&a[i]<=m) b[f++] = a[i]; if(f==0) System.out.println("no"); else for(i=0;i<f;i++) if(i==f-1) System.out.println(b[i]); else System.out.print(b[i]+" "); } cin.close(); } public static int judge(int x) { int a,b,c; a = x / 100; b = x / 10 % 10; c = x % 10; if(a*a*a+b*b*b+c*c*c==x) return 1; return 0; } }
Java練習 SDUT-1239_水仙花數