华为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));
    }
}
相关推荐
立志成为大牛的小牛1 小时前
数据结构——四十二、二叉排序树(王道408)
数据结构·笔记·程序人生·考研·算法
摇滚侠8 小时前
StreamAPI,取出list中的name属性,返回一个新list
数据结构·list
是苏浙11 小时前
零基础入门C语言之C语言实现数据结构之单链表经典算法
c语言·开发语言·数据结构·算法
橘颂TA12 小时前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++
迷途之人不知返13 小时前
数据结构之,栈与队列
数据结构
MOONICK15 小时前
数据结构——哈希表
数据结构·哈希算法·散列表
FMRbpm16 小时前
链表5--------删除
数据结构·c++·算法·链表·新手入门
努力学习的小全全18 小时前
【CCF-CSP】05-01数列分段
数据结构·算法·ccf-csp
遗憾是什么.19 小时前
数据结构 -- 栈
数据结构·算法·链表
liuhuapeng030419 小时前
GetMapping自动截取List<String>字符
数据结构·windows·list