华为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));
    }
}
相关推荐
无敌最俊朗@13 小时前
C++ 序列容器深度解析:vector、deque 与 list
开发语言·数据结构·数据库·c++·qt·list
Da Da 泓13 小时前
LinkedList模拟实现
java·开发语言·数据结构·学习·算法
jinmo_C++17 小时前
数据结构_ 二叉树线索化:从原理到手撕实现
数据结构
钮钴禄·爱因斯晨19 小时前
数据结构|图论:从数据结构到工程实践的核心引擎
c语言·数据结构·图论
向前阿、20 小时前
数据结构从入门到实战————栈
c语言·开发语言·数据结构·程序人生
洲覆21 小时前
Redis 64字节分界线与跳表实现原理
数据结构·数据库·redis·缓存
im_AMBER21 小时前
数据结构 02 线性表
数据结构·算法
-雷阵雨-21 小时前
数据结构——包装类&&泛型
java·开发语言·数据结构·intellij-idea
gsfl1 天前
redis常见数据结构及其编码方式
数据结构·redis
im_AMBER1 天前
数据结构 01 线性表
数据结构·学习