【华为OD-E卷 - 工号不够用了怎么办 100分(python、java、c++、js、c)】
题目
3020年,空间通信集团的员工人数突破20亿人,即将遇到现有工号不够用的窘境。
现在,请你负责调研新工号系统。继承历史传统,新的工号系统由小写英文字母(a-z)和数字(0-9)两部分构成。
新工号由一段英文字母开头,之后跟随一段数字,比如”aaahw0001″,”a12345″,”abcd1″,”a00″。
注意新工号不能全为字母或者数字,允许数字部分有前导0或者全为0。
但是过长的工号会增加同事们的记忆成本,现在给出新工号至少需要分配的人数X和新工号中字母的长度Y,求新工号中数字的最短长度Z。
输入描述
- 一行两个非负整数 X Y,用数字用单个空格分隔。 0< X <=2^50 – 1 0< Y <=5
输出描述
- 输出新工号中数字的最短长度Z
用例
用例一:
输入:
260 1
- 1
输出:
1
- 1
用例二:
输入:
26 1
- 1
输出:
1
- 1
用例三:
输入:
2600 1
- 1
输出:
2
- 1
python解法
- 解题思路:
# 读取输入的两个整数 x 和 y
x, y = map(int, input().split())
# 定义函数,找到满足条件的最小 z 值
def find_min_z(x, y):
z = 1 # 初始化 z 为 1
# 循环查找,直到满足条件 26^y * 10^z >= x
while 26**y * 10**z < x:
z += 1 # 如果条件不满足,增加 z
return z # 返回满足条件的最小 z 值
# 输出结果
print(find_min_z(x, y))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
java解法
- 解题思路
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取输入的 x 和 y
long x = scanner.nextLong(); // x 是目标值
int y = scanner.nextInt(); // y 是指数
// 计算满足条件的最小 z
long minDigits = calculateMinDigits(x, y);
System.out.println(minDigits);
}
/**
* 计算满足条件的最小 z 值
* @param x 目标值
* @param y 指数
* @return 满足条件的最小 z 值
*/
private static long calculateMinDigits(long x, int y) {
// 计算 26^y,表示组合数
double combinations = Math.pow(26, y);
// 如果 x 小于或等于组合数,返回最小值 1
if (x <= combinations) {
return 1;
}
// 计算 log10(x / combinations) 并向上取整
return (long) Math.ceil(Math.log10(x / combinations));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
C++解法
- 解题思路
更新中
- 1
C解法
#include
#include
int main() {
double total_employees; // 员工总数(目标值)
int letters_count; // 用于标识的字母数量
// 读取输入的员工总数和字母数量
scanf("%lf %d", &total_employees, &letters_count);
// 计算 26^letters_count,表示所有字母组合数
double combinations = pow(26, letters_count);
// 计算所需的最小数字位数
// 如果 combinations >= total_employees,则 log10(total_employees / combinations) 会是负数
double required_digits = log10(total_employees / combinations);
// 向上取整以确保满足条件
int min_digits = (int) ceil(required_digits);
// 如果 min_digits <= 0,返回 1(确保至少需要一位数字)
printf("%d\n", min_digits > 0 ? min_digits : 1);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
JS解法
更新中
- 1
注意:
如果发现代码有用例覆盖不到的情况,欢迎反馈!会在第一时间修正,更新。
解题不易,如对您有帮助,欢迎点赞/收藏
评论记录:
回复评论: