打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等於該數本身。 – JAVA編程語言程序開發技術文章

import java.util.Scanner; 
 
 
public class Daffodil { 
    public static void main(String[]args){ 
        Scanner in = new Scanner(System.in); 
        System.out.println("請輸入開始值:"); 
        int a = in.nextInt(); 
        System.out.println("請輸入末尾值:"); 
        int b = in.nextInt(); 
         
        String str = new String(); 
        char[]buff = new char[3]; 
        int plot; 
        for(int i=a;i<b;i++){ 
            plot=0; 
            int temp; 
            buff=(Integer.toString(i)).toCharArray(); 
            for(int j=0;j<3;j++){ 
                temp=Integer.parseInt(String.valueOf(buff[j])); 
                plot+=Math.pow(temp,3); 
            } 
            if(plot==i){ 
                System.out.print(i+" "); 
            } 
        } 
    } 

摘自 djw10962000的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *