1到10罗马数字_从罗马到现代

1到10罗马数字

We’re so used to our regular numbers — 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 — that we forget we haven’t always had those. There were quite a few numeral systems in the history of mankind, but most of them are no longer in use. However, Roman numerals are still in (sort of) use, especially if you have a grandfather’s clock and want to know the time. 😁

我们已经习惯了常规数字-1,2,3,4,5,6,7,8,8,9,10-我们忘记了我们并不总是拥有这些数字。 人类历史上有很多数字系统 ,但是其中大多数不再使用。 但是, 罗马数字仍在使用中(尤其是在其中),尤其是如果您有祖父的时钟并想知道时间的话。 😁

Most of us have been taught how to read Roman numerals as kids and we do that almost instinctively. Here’s a primer how to read them:

我们大多数人都学会了如何从小就读罗马数字,而我们几乎是本能地做到这一点。 这是入门阅读方法:

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

罗马数字由七个不同的符号表示: IVXLCDM

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, 2 is written as II in Roman numeral — just two one's added together. 12 is written as XII, which is X + II. 27 is written as XXVII, which is XX + V + II.

例如,2用罗马数字表示为II仅将两个加在一起。 12写成XII ,即X + II 。 27表示为XXVII ,即XX + V + II

Roman numerals are usually written largest to smallest from left to right. However, as numbers increase, at some point, it makes more sense to subtract the lower number from the larger number. For example, 9 is not VIIII but IX,40 is not XXXX but XV, etc. It’s easy to explain that to a fellow human, but how can we explain that to a machine? We need to write a fool-proof algorithm to do that!

罗马数字通常从左到右从大到小书写。 但是,随着数字的增加,在某个时候,从较大的数字中减去较低的数字更有意义。 例如,9不是VIIII而是IX, 40不是XXXX而是XV ,等等。很容易向同伴解释,但是我们如何向机器解释呢? 我们需要编写一个简单的算法来做到这一点!

First, let’s just translate Roman numerals to regular numbers in the order they appear. For example, XLVII would be 10, 50, 5, 1, 1. To do that we’ll use a for loop, and we’ll be checking if the current numeral in the string (s) is one of the Roman numerals. For example, if it’s I, we’ll be adding 1 to the array of numbers that we created beforehand, and if it’s X, we’ll be adding 10.

首先,让我们按照出现的顺序将罗马数字转换为常规数字。 例如,XLVII将是10、50、5、1、1。为此,我们将使用一个for循环,并检查字符串中的当前数字是否为罗马数字之一。 例如,如果是I ,则将1添加到预先创建的数字数组中;如果是X ,则将添加10。

Here’s how this loop will look like:

这是这个循环的样子:

In our example, our number is XLVII. We know in our hearts that the number is 47, but how can we logically explain to the machine that [10, 50, 5, 1, 1] is, in fact, 47?

在我们的示例中,我们的号码是XLVII 。 我们内心知道数字是47,但是我们如何从逻辑上向机器解释[10, 50, 5, 1, 1]实际上是47?

The core idea is that in Roman numerals, the number on the left should be larger than the one on the right.

核心思想是在罗马数字中,左边的数字应大于右边的数字。

So what we can do is look at the current number and the next number, and if the current number is less than the next number, it’ll be subtracted from the next number. In our example, 10 is less than 50, so we’ll subtract 10 from 50 and we’ll get 40.

因此,我们可以做的是查看当前数字和下一个数字,如果当前数字小于下一个数字,将从下一个数字中减去该数字。 在我们的示例中,10小于50,因此我们从50减去10,得到40。

So let’s iterate through the array and check the value of numbers that are next to each other. Let’s use a while loop for that.

因此,让我们遍历数组并检查彼此相邻的数字的值。 让我们使用一个while循环。

Let’s remove the first number of the array with splice() and then compare its value to the number that’s now first in the array. We’ll continue doing that until we don’t have anything left in the array of numbers.

让我们使用splice()删除数组的第一个数字,然后将其值与数组中第一个数字进行比较。 我们将继续这样做,直到数字数组中没有剩余任何内容为止。

If the removed number is larger than the next one, or if it’s the very last number, we just add it to the total. I do firstNumber[0] because splice returns an array, and I need the first and only element there.

如果删除的数字大于下一个,或者它是最后一个数字,我们将其添加到总数中。 我执行firstNumber [0]是因为splice返回一个数组,我需要那里的第一个也是唯一的元素。

If the removed number is less than the next number, we subtract it from the total.

如果删除的数字小于下一个数字,则从总数中减去它。

And in the end, we’ll just return total!

最后,我们只返回总数!

Here’s how the full code looks like:

完整的代码如下所示:

翻译自: https://medium.com/weekly-webtips/from-roman-to-modern-times-a1d54c0c9a65

1到10罗马数字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值