ZZULIOJ 1096: 水仙花数(函数专题),Java

1096: 水仙花数(函数专题)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int m=cin.nextInt();
            int n=cin.nextInt();
            int flag=1;
            for(int i=m;i<=n;i++)
            {
                if(narcissus(i)==1)
                {
                    if(flag==1)
                    {
                        System.out.printf("%d", i);
                        flag=0;
                    }
                    else System.out.printf(" %d",i);
                }
            }
            if(flag==1) System.out.println("no");
            else System.out.println();
        }
    }
    public static int narcissus(int n)
    {
        int d1=n%10;
        int d2=n/10%10;
        int d3=n/100;
        if(n==d1*d1*d1+d2*d2*d2+d3*d3*d3) return 1;
        else return 0;
    }
}

你可能感兴趣的:(#,Java刷题)