华为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));
    }
}
相关推荐
mmmmath_36 小时前
面试题 02.07. 链表相交
算法·链表
sylviiiiiia6 小时前
Leetcode hot100 多数元素/相交链表/反转链表
算法·leetcode·链表
我不会起名字3227 小时前
一天一道算法题(26):栈的简单应用
java·数据结构·python·算法·leetcode·golang·
不会就选b7 小时前
算法日常・每日刷题--<贪心+大根堆>2
数据结构·算法
事圆则缓7 小时前
Java 常见数据结构与 Android 使用场景
android·java·数据结构
白狐_7988 小时前
408 数据结构|外部排序优化:怎么减少时间开销
数据结构·算法
kiracrimson20 小时前
从缓存的角度看链表与线性表的差异
数据结构·链表·缓存
心抵鹊1 天前
归并排序之翻转对(hard)
数据结构·算法
白狐_7981 天前
408 数据结构|红黑树插入:只记两大类
数据结构
Lost of 程序猿1 天前
.NET 线程安全集合与并发数据结构深度实战:从 lock 到无锁
数据结构·安全·.net