【发布时间】:2019-12-01 13:30:01
【问题描述】:
给定从 1 到 n 的正整数。
调用 sumdigit (n) 是 n 的位数之和。
调用 STR(n) 作为代表数字 n 的字符串。
按以下顺序排列这些数字: 当且仅当 (1) 或 (2) 满足时,x 位于 y 之前:
- (1) sumdigit (x) <sumdigit (y)
- (2) sumdigit(x)=sumdigit(y) && str(x)<str(y)
例如:
x = 301, y = 221 -> x 在 y 之前(因为 sumdigit (x)
x = 201, y = 30 -> x 在 y 之前(因为 sumdigit (x) = sumdigit (y) 和 str (x)
x = 222, y = 213 -> y 在 x 之前(因为 sumdigit (x) = sumdigit (y) and str (x)> str (y))
给定 n
上述问题有两种查询方式:
- 找到按上述规则排序的范围内的第 k 个数字。 (k
- 在按照上述规则排序的序列中,找出数k的序号。 (k
我可以通过动态规划dp(n, sum)解决条件(1)的问题:数字x的个数有sumdigit (x) = sum and x
->We can count the number of x numbers with sumdigit (x) <sumdigit (k).
The problem I can't solve is the condition (2),
I can't think of a way to count the number of x numbers
with sumdigit(x)=sumdigit(k) and str(x)<str(k)
你能帮我解决条件(2)的问题吗?
我需要你的帮助!
【问题讨论】:
-
@Primusa 我需要你的帮助!
标签: algorithm numbers dynamic-programming