【问题标题】:How to solve arithmetic dictionary problem?如何解决算术字典问题?
【发布时间】: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) &lt;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


【解决方案1】:

这里有一个提示:

令 dp(d, l, s) 表示第一个数字 = d 的数字个数(d 也可以为 0),数字的长度

尝试找出递归。您如何使用它来解决问题?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2021-09-06
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多