华为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 分钟前
【数据结构】堆的应用
c语言·数据结构
间歇性努力持续性发呆的野生快乐选手1 小时前
栈的性质(进栈,出栈,访问)
数据结构·c++
m0_547486662 小时前
《数据结构与算法》全套PPT课件2026(中国海洋大学)
数据结构·算法
旖旎夜光2 小时前
LeetCode 904:水果成篮(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
我变成萤火虫4 小时前
河南萌新联赛2026第(三)场:郑州轻工业大学
数据结构·c++·算法·贪心算法·stl·深度优先·哈希算法
白狐_7984 小时前
408数据结构第7章:B树①——基础概念与结构
数据结构·b树
Suxing95 小时前
C语言基础分享:从“租房”到“拆迁”,C语言动态内存管理
java·数据结构·算法
LuminousCPP5 小时前
单链表专题(四)-刷题复盘篇-LeetCode 138 随机链表复制|原地拷贝法突破复杂指针操作
数据结构·笔记·算法·leetcode·链表
逆境不可逃6 小时前
【LeetCode 912】排序数组——随机化快速排序详解
数据结构·算法·排序算法
c238566 小时前
《算法武林谱:四大排序神功与二分寻宝术全解》
数据结构·算法·排序算法