方案理解

发布于 2024-09-24 20:16:51 字数 105 浏览 2 评论 0原文

我应该编写一个方案函数(数字计数 n),它接受正整数 n 并计算 n 的位数,即 6、4 或 9。

我无法理解我到底应该做什么,我对“n的数字是6、4或9”感到困惑,这是什么意思?

I am supposed to write a scheme function (digit-count n) that accepts a positive integer n and evaluates to the number of digits of n that are 6, 4, or 9.

I am having trouble understanding what exactly I am supposed to do, I am confused about the "digits of n that are 6, 4 or 9", what does this mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如痴如狂 2024-10-01 20:16:51

这只是一个解释问题,但我会说你会采用数字的十进制表示形式,并计算 6、4 或 9 的总位数。例如:

  • 100 --> 0
  • 4 --> 1
  • 469 --> 3
  • 444 --> 3

现在明白了吗?

This is just an interpretation question, but I would say that you would take the decimal representation of a number, and count the total number of digits that are 6, 4, or 9. For example:

  • 100 --> 0
  • 4 --> 1
  • 469 --> 3
  • 444 --> 3

Get it now?

西瑶 2024-10-01 20:16:51

一种解释 - 例如:

给定 6787993914 的位数为 01 的位数>6 和 3 代表 9。出现次数的总和为0 + 1 + 3 = 4

One interpretation - example:

Given 678799391, the number of digits would be 0 for 4, 1 for 6 and 3 for 9. The sum of the occurences would be 0 + 1 + 3 = 4.

剩一世无双 2024-10-01 20:16:51

将整个数字转换为列表并单独检查每个数字。

(define (number->list x) 
  (string->list (number->string x))

(define (6-4-or-9 x) (cond ((= x 6) true)) ((= x 4) true)) ((= x 9) true))))

(define (count-6-4-9 x) (cond ((6-4-or-9 (car (number->list x)))).......

我相信你能明白这是怎么回事。它非常粗糙,我不确定它是否真的惯用,但它应该有效。

这个想法是将数字转换为列表,检查第一个数字,如果是六、四或九,则递归调用转换回字符串 + 1 的数字列表的 cdr 上的过程...

Convert the whole number to a list and check each one individually.

(define (number->list x) 
  (string->list (number->string x))

(define (6-4-or-9 x) (cond ((= x 6) true)) ((= x 4) true)) ((= x 9) true))))

(define (count-6-4-9 x) (cond ((6-4-or-9 (car (number->list x)))).......

I'm sure you can see where that's going. It's pretty crude, and I'm not sure it's really idiomatic, but it should work.

The idea is is that you convert the number to a list, check to first digit, if it's six, four or nine, recursively call the procedure on the cdr of the number list converted back to a string + 1...

忆离笙 2024-10-01 20:16:51

如果您不使用列表,则可以使用 10 的模 % 并将整数 / 除以 10。

以下是递归解决方案:

(define (digits n)
(if(not (< n 1)) (+ 1 (digits (/ n 10))) 0))

If you are not using lists, you can work with modulo % of 10 and dividing whole numbers / with 10.

Below is the recursive solution :

(define (digits n)
(if(not (< n 1)) (+ 1 (digits (/ n 10))) 0))
一影成城 2024-10-01 20:16:51

首先,我们必须了解问题的意思:它要求你编写一个程序,计算数字 4、6 或 9 在另一个输入的数字中出现的次数。例如,输入 10345 应返回 1。让我们看看为什么:

10345 的数字是 1034 > 和 5。我们必须问,“469 出现了多少次?”嗯,10345 中没有 69。然而,有一个4。因此,该过程应返回1

另一个例子:(digit-count 14289)

让我们像以前一样将其分解。 14289 的数字为 1428 和 <代码>9。没有6。然而,有19。多少?有一个 1 和一个 9。由于存在两个(总共)所需的数字(所需的数字为 469),(digit -count 14289) 应返回 2

更多示例:

(digit-count 144) --> 2(有两个4

(位数1) --> 0(没有 469

(位数 1262) --> 1(有一个6

现在,让我们开始定义。我们可以利用 appearances 函数,它接受两个输入并返回第一个输入在第二个输入中出现的次数。
例如:(appearances 'a 'amsterdam) 返回 2,因为 amsterdam 中有两个 a

使用 appearances,这是我们的定义(最后!):

(define (count469 num)
  (+ (appearances 4 num)
     (appearances 6 num)
     (appearances 9 num)))

该函数返回 4 的 appearances、6 的 appearances 和9 的出场。如有任何反馈或问题,请随时回复!

First, we must understand what the question is asking: It is asking you to write a procedure that counts the number of times the numbers 4, 6, or 9 show up in another inputted number. For instance, inputting 10345 should return 1. Let's see why:

The digits of 10345 are 1, 0, 3, 4, and 5. We have to ask, "How many times do 4, 6, or 9 show up?" Well, there are no 6's or 9's in 10345. However, there is one 4. Therefore, the procedure should return 1.

Another example: (digit-count 14289)

Let's break it up like we did before. The digits of 14289 are 1, 4, 2, 8, and 9. There are no 6's. There are, however, 1's and 9's. How many? There is one 1 and one 9. Since there are two (total) of the desired digits present (desired digits are 4, 6, and 9), (digit-count 14289) should return 2.

Some more examples:

(digit-count 144) --> 2 (there are two 4's)

(digit-count 1) --> 0 (there are no 4's, 6's, or 9's)

(digit-count 1262) --> 1 (there is one 6)

Now, let's start defining. We can take advantage of the appearances function, which takes two inputs and returns how many times the first input appears in the second.
For example: (appearances 'a 'amsterdam) returns 2 because there are two a's in amsterdam.

Using appearances, here is our definition (finally!):

(define (count469 num)
  (+ (appearances 4 num)
     (appearances 6 num)
     (appearances 9 num)))

This function returns the sum of the appearances of 4, the appearances of 6, and the appearances of 9. Please feel free to reply with any feedback or questions!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文