这个整数编码如何工作? [英] How does this integer encoding work?
问题描述
在此代码高尔夫问题中,有一个 python answer 将所有整数的长度从1到99编码为英文大写:
In this code golf question, there is a python answer that encodes the lengths of all integers from 1 to 99 in english to a big number:
7886778663788677866389978897746775667552677566755267756675527886778663788677866355644553301220112001
要获得 n
的长度,您只需要计算 3 +(the_big_number /(10 ** n))%10
。这是如何工作的?
To get the length of n
, you just have to calculate 3 + (the_big_number / (10**n)) % 10
. How does this work?
推荐答案
(the_big_number /(10 ^ n))%10
拉出大数字的 n
最低有效位,所以长度只是以零(1 + 3 = 4) ),最左边的九十九(7 + 3 = 10)的长度继续下去。
(the_big_number / (10^n)) % 10
pulls out the n
th least significant digit of the big number, so the lengths are just stored starting with the length of "zero" (1+3=4) at the far right, and following over to the length of "ninety-nine" (7+3=10) at the far left.
最短的英文数字是三个字母(一,二,六,10),所以每个长度存储与三个偏移。 100之前最长的是9 + 3 = 12个字母(例如七十八),所以每个数字都可以存储为一位数字。
The shortest English numbers are three letters ("one", "two", "six", "ten"), so each length is stored with an offset of three. The longest prior to 100 are 9 + 3 = 12 letters (e.g. "seventy-eight"), so each number can be stored as a single digit.
这篇关于这个整数编码如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!