@yexiaoqi 2022-05-27T03:17:22.000000Z 字数 810 阅读 296

# 水仙花数

刷题 华为机试

输入：3
0



输入：9
1



public class Main {    static int[] min = {100,1000,10000,100000,1000000};    static int[] max = {999,9999,99999,999999,9999999};    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            int n = sc.nextInt();            int m = sc.nextInt();            if(n<3 || n>7){                System.out.println(-1);            } else {                int count = -1;//因为m从0开始，所以count初始化为-1                for (int i=min[n-3]; i<=max[n-3]; i++) {                    char[] arr = (i+"").toCharArray();                    int sum = 0;                    for (char c : arr) {                        int cnum = Integer.parseInt(c+"");                        sum += Math.pow(cnum, n);                    }                    if (i==sum && ++count==m) {                        System.out.println(i);                        break;                    }                    if (i == max[n - 3] && m > count)                        System.out.println(i * m);                }            }        }    }}

• 私有
• 公开
• 删除