华为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));
    }
}
相关推荐
让我们一起加油好吗2 小时前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
路灯中转站2 小时前
数据结构C语言版(什么是数据类型)
数据结构
凌冰_2 小时前
数据结构 - 队列(五)
数据结构·算法·软件工程
让我们一起加油好吗3 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
让我们一起加油好吗4 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
YLCHUP16 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴17 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
凌冰_1 天前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏1 天前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组
djjjx.1 天前
Mysql数据库基础篇(十一):索引的常见操作、聚簇与非聚簇索引、为什么其他数据结构不能作为索引的底层结构
数据结构