华为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));
    }
}
相关推荐
云泽80812 分钟前
数据结构之单链表和环形链表的应用(二)-
数据结构
dragoooon341 小时前
[优选算法专题二滑动窗口——串联所有单词的子串]
数据结构·c++·学习·算法·leetcode·学习方法
Pocker_Spades_A3 小时前
论文精读(五):面向链接预测的知识图谱表示学习方法综述
人工智能·链表·知识图谱
阿让啊9 小时前
C语言strtol 函数使用方法
c语言·数据结构·c++·单片机·嵌入式硬件
superlls12 小时前
(算法 哈希表)【LeetCode 349】两个数组的交集 思路笔记自留
java·数据结构·算法
Ripple1231213 小时前
数据结构:顺序表与链表
数据结构·链表
一个响当当的名号14 小时前
B树,B+树,B*树(无代码)
数据结构·b树
古译汉书16 小时前
嵌入式铁头山羊stm32-ADC实现定时器触发的注入序列的单通道转换-Day26
开发语言·数据结构·stm32·单片机·嵌入式硬件·算法
野犬寒鸦16 小时前
力扣hot100:相交链表与反转链表详细思路讲解(160,206)
java·数据结构·后端·算法·leetcode
GalaxyPokemon16 小时前
LeetCode - 1089. 复写零
数据结构