华为OD机考 - 水仙花数 Ⅰ(2025B卷 100分)

java 复制代码
import java.util.*;
public static Integer get(int count,int c){
    if(count<3||count>7){
        return -1;
    }
    //存储每位数的最高位......最低位
    int[] arr =new int[count];
    List<Integer> res = new ArrayList<>();
    for(int i=(int) Math.pow(10,count-1);i<(int) Math.pow(10,count);i++){
        getArr( i,arr);
        int sum = 0;
        for(int j = 0;j<arr.length;j++){
            sum+= Math.pow(arr[j],arr.length);
        }
        if(sum == i){
            res.add(i);
        }
    }
    //System.out.println(res);
    if(res.size()<c){
        return res.get(res.size()-1)*c;
    }
    return res.get(c);
}

public static int[] getArr(int i,int[] arr){
    int count = 0,j=arr.length;
    while(count<arr.length){
         arr[count] = i/ ((int) Math.pow(10,j-1));
         i = i- arr[count]* ((int) Math.pow(10,j-1));
         j--;
         count++;
    }
    return arr;
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // 注意 hasNext 和 hasNextLine 的区别
    while (in.hasNextLine()) { // 注意 while 处理多个 case
        int count = in.nextInt();
        int c = in.nextInt();
        System.out.println(get(count, c));
    }
}
相关推荐
琢磨先生David5 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_454245035 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
岛雨QA5 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc5 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
wefg15 天前
【算法】单调栈和单调队列
数据结构·算法
岛雨QA5 天前
图「Java数据结构与算法学习笔记12」
数据结构·算法
czxyvX5 天前
020-C++之unordered容器
数据结构·c++
岛雨QA5 天前
多路查找树「Java数据结构与算法学习笔记11」
数据结构·算法
AKA__Zas5 天前
初识基本排序
java·数据结构·学习方法·排序