方案理解
我应该编写一个方案函数(数字计数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这只是一个解释问题,但我会说你会采用数字的十进制表示形式,并计算 6、4 或 9 的总位数。例如:
现在明白了吗?
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:
Get it now?
一种解释 - 例如:
给定
678799391
,4
的位数为0
,1
的位数>6 和3
代表9
。出现次数的总和为0 + 1 + 3 = 4
。One interpretation - example:
Given
678799391
, the number of digits would be0
for4
,1
for6
and3
for9
. The sum of the occurences would be0 + 1 + 3 = 4
.将整个数字转换为列表并单独检查每个数字。
我相信你能明白这是怎么回事。它非常粗糙,我不确定它是否真的惯用,但它应该有效。
这个想法是将数字转换为列表,检查第一个数字,如果是六、四或九,则递归调用转换回字符串 + 1 的数字列表的 cdr 上的过程...
Convert the whole number to a list and check each one individually.
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...
如果您不使用列表,则可以使用 10 的模
%
并将整数/
除以 10。以下是递归解决方案:
If you are not using lists, you can work with modulo
%
of 10 and dividing whole numbers/
with 10.Below is the recursive solution :
首先,我们必须了解问题的意思:它要求你编写一个程序,计算数字 4、6 或 9 在另一个输入的数字中出现的次数。例如,输入
10345
应返回1
。让我们看看为什么:10345
的数字是1
、0
、3
、4
> 和5
。我们必须问,“4
、6
或9
出现了多少次?”嗯,10345
中没有6
或9
。然而,有一个4
。因此,该过程应返回1
。另一个例子:
(digit-count 14289)
让我们像以前一样将其分解。
14289
的数字为1
、4
、2
、8
和 <代码>9。没有6
。然而,有1
和9
。多少?有一个1
和一个9
。由于存在两个(总共)所需的数字(所需的数字为4
、6
和9
),(digit -count 14289)
应返回2
。更多示例:
(digit-count 144)
-->2
(有两个4
)(位数1)
-->0
(没有4
、6
或9
)(位数 1262)
-->1
(有一个6
)现在,让我们开始定义。我们可以利用
appearances
函数,它接受两个输入并返回第一个输入在第二个输入中出现的次数。例如:
(appearances 'a 'amsterdam)
返回2
,因为amsterdam
中有两个a
。使用
appearances
,这是我们的定义(最后!):该函数返回 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 return1
. Let's see why:The digits of
10345
are1
,0
,3
,4
, and5
. We have to ask, "How many times do4
,6
, or9
show up?" Well, there are no6
's or9
's in10345
. However, there is one4
. Therefore, the procedure should return1
.Another example:
(digit-count 14289)
Let's break it up like we did before. The digits of
14289
are1
,4
,2
,8
, and9
. There are no6
's. There are, however,1
's and9
's. How many? There is one1
and one9
. Since there are two (total) of the desired digits present (desired digits are4
,6
, and9
),(digit-count 14289)
should return2
.Some more examples:
(digit-count 144)
-->2
(there are two4
's)(digit-count 1)
-->0
(there are no4
's,6
's, or9
's)(digit-count 1262)
-->1
(there is one6
)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)
returns2
because there are twoa
's inamsterdam
.Using
appearances
, here is our definition (finally!):This function returns the sum of the
appearances
of 4, theappearances
of 6, and theappearances
of 9. Please feel free to reply with any feedback or questions!