从数字中删除前导/尾随数字的最佳方法是什么?

时间:2022-01-14 21:28:46

If I have a number like 12345, and I want an output of 2345, is there a mathematical algorithm that does this? The hack in me wants to convert the number to a string, and substring it. I know this will work, but I'm sure there has to be a better way, and Google is failing me.

如果我有一个像12345这样的数字,并且我想要一个2345的输出,那么有一个数学算法吗?我的黑客想要将数字转换为字符串,并将其子串。我知道这会有效,但我确信必须有更好的方法,谷歌让我失望。

Likewise, for 12345, if I want 1234, is there another algorithm that will do that? The best I can come up with is Floor(x / 10^(n)), where x is the input and n is the number of digits to strip, but I feel like there has to be a better way, and I just can't see it.

同样,对于12345,如果我想要1234,是否还有另一种算法可以做到这一点?我能想到的最好的是Floor(x / 10 ^(n)),其中x是输入,n是要剥离的数字的数量,但我觉得必须有更好的方法,我只是可以看不到。

8 个解决方案

#1


Python 3.0:

>>> import math
>>> def remove_most_significant_digit(n, base=10):
...     return n % (base ** int(math.log(n, base)))
...
>>> def remove_least_significant_digit(n, base=10):
...     return int(n // base)
...
>>> remove_most_significant_digit(12345)
2345
>>> remove_least_significant_digit(12345)
1234

#2


In the first case, don't you just want

在第一种情况下,你不只是想要

n % 10000

i.e. the modulus wrt. 10000 ?

即模量wrt。 10000?

For your second case, if you're using integer arithmetic, just divide by 10. You might want to do this in a more 'explicit' fashion by modding with 10 to get the last digit, subtract and then divide (think of a shift in base 10).

对于你的第二种情况,如果你使用整数运算,只需要除以10.你可能希望以更“明确”的方式做这个,通过用10修改得到最后一位数,减去然后除(想想一个转变)在基地10)。

#3


Yes, the modulus operator (%) which is present in most languages, can return the n last digits:

是的,大多数语言中存在的模数运算符(%)可以返回n个最后数字:

12345 % 10^4 = 12345 % 10000 = 2345

12345%10 ^ 4 = 12345%10000 = 2345

Integral division (/ in C/C++/Java) can return the first n digits:

积分除法(/在C / C ++ / Java中)可以返回前n位数:

12345 / 10^4 = 12345 / 10000 = 1

12345/10 ^ 4 = 12345/10000 = 1

#4


Converting to a string, and then using a substring method will ultimately be the fastest and best way, since you can just strip characters instead of doing math.

转换为字符串,然后使用子字符串方法最终将是最快和最好的方法,因为您可以删除字符而不是数学。

If you really don't want to do that though, you should use modulus (%), which gives the remainder of a division. 11 % 3 = 2, because 3 can only go into 11 three times (9). The remainder is then 2. 41 % 10 = 1, because 10 can go into 41 four times (40). The remainder is then 1.

如果你真的不想这样做,你应该使用模数(%),它给出了除法的余数。 11%3 = 2,因为3只能进入11次3次(9)。剩下的是2. 41%10 = 1,因为10可以进入41次4次(40)。剩下的就是1。

For stripping the first digits, all you would have to do is mod the tens value that you want to get rid of. For stripping two digits from 12345, you should modulus by 1000. 1000 goes into 12345 twelve times, then the remainder will be 345, which is your answer. You would just need to find a way to find the tens value of the last digit you were trying to strip. Use x % (10^(n)), where x is input, and n is the lowest digit you want to strip.

对于剥离第一个数字,您所要做的就是修改您想要摆脱的十位数值。对于从12345剥离两位数,你应该模数为1000. 1000进入12345十二次,然后余数将是345,这是你的答案。你只需要找到一种方法来找到你试图去掉的最后一个数字的十进制值。使用x%(10 ^(n)),其中x是输入,n是要剥离的最低位。

For stripping the last digits, your way works just fine. What's easier than a quick formula like that?

为了剥离最后的数字,你的方式工作得很好。什么比这样的快速公式更容易?

#5


I don't think there's any other approach than division for removing trailing numbers. It might be more efficient to do repeated integral division than to cast to a float, perform an exponent, then floor and cast back to an integer, but the basic idea remains the same.

我不认为除去尾随数字之外还有其他方法而不是除法。进行重复积分除法可能比投射到浮点数,执行指数,然后求平面并转换回整数更有效,但基本思想保持不变。

Keep in mind that the operation is nearly identical for any base. To remove one trailing decimal digit, you do / 10. If you had 0b0111 and you wanted to remove one digit, it would have to be /2. Or you could have 0xff / 16 to get 0x0f.

请记住,任何基地的操作几乎相同。要删除一个尾随的十进制数字,你可以执行/ 10.如果你有0b0111而你想要删除一个数字,那么它必须是/ 2。或者你可以让0xff / 16得到0x0f。

#6


You have to realize that numbers don't have digits, only strings do, and how many (and which) digits they have depends entirely on the base (which numbers don't have either). Internally, computers use what amounts to binary strings. So in general, manipulating base 10 digits requires you to convert the number to a string first - or do calculations that are the same you would do when converting it to a string. However, for your specific task of removing leading and trailing digits, these calculations (modulo and integer division) are very simple and much faster than converting the entire number.

你必须意识到数字没有数字,只有字符串,并且它们有多少(和哪些)数字完全取决于基数(这些数字也没有)。在内部,计算机使用的是二进制字符串。因此,通常,操作基数为10的数字需要先将数字转换为字符串 - 或者进行与将其转换为字符串时相同的计算。但是,对于删除前导和尾随数字的特定任务,这些计算(模和整数除法)非常简单,并且比转换整数更快。

#7


i think that converting to string and then remove the first char wont do the trick. i believe that the alg for converting to string is doing the div-mod routine, for optimization you might as well do the div-mod alg by yourself and manipulate it to your needs

我认为转换为字符串,然后删除第一个字符不会做的伎俩。我相信转换为字符串的alg正在执行div-mod例程,为了优化你也可以自己做div-mod alg并根据你的需要操作它

#8


Here is C++ code ... It's not tested.

这是C ++代码......它没有经过测试。

int myPow ( int n , int k ){
int ret = 1;
for (int i=0;i<k;++i) ret*=n;
return ret;
}

int countDigits (int n ){
 int count = 0;
 while ( n )++count, n/=10;
return count;
}

int getLastDigits (int number , int numDigits ){
  int tmp = myPow ( 10 , numDigits );
  return number % tmp;
}

int getFirstDigits (int number, numDigits ){
   int tmp = myPow ( 10, countDigits ( number) - numDigits );
   return nuber / tmp;
}

#1


Python 3.0:

>>> import math
>>> def remove_most_significant_digit(n, base=10):
...     return n % (base ** int(math.log(n, base)))
...
>>> def remove_least_significant_digit(n, base=10):
...     return int(n // base)
...
>>> remove_most_significant_digit(12345)
2345
>>> remove_least_significant_digit(12345)
1234

#2


In the first case, don't you just want

在第一种情况下,你不只是想要

n % 10000

i.e. the modulus wrt. 10000 ?

即模量wrt。 10000?

For your second case, if you're using integer arithmetic, just divide by 10. You might want to do this in a more 'explicit' fashion by modding with 10 to get the last digit, subtract and then divide (think of a shift in base 10).

对于你的第二种情况,如果你使用整数运算,只需要除以10.你可能希望以更“明确”的方式做这个,通过用10修改得到最后一位数,减去然后除(想想一个转变)在基地10)。

#3


Yes, the modulus operator (%) which is present in most languages, can return the n last digits:

是的,大多数语言中存在的模数运算符(%)可以返回n个最后数字:

12345 % 10^4 = 12345 % 10000 = 2345

12345%10 ^ 4 = 12345%10000 = 2345

Integral division (/ in C/C++/Java) can return the first n digits:

积分除法(/在C / C ++ / Java中)可以返回前n位数:

12345 / 10^4 = 12345 / 10000 = 1

12345/10 ^ 4 = 12345/10000 = 1

#4


Converting to a string, and then using a substring method will ultimately be the fastest and best way, since you can just strip characters instead of doing math.

转换为字符串,然后使用子字符串方法最终将是最快和最好的方法,因为您可以删除字符而不是数学。

If you really don't want to do that though, you should use modulus (%), which gives the remainder of a division. 11 % 3 = 2, because 3 can only go into 11 three times (9). The remainder is then 2. 41 % 10 = 1, because 10 can go into 41 four times (40). The remainder is then 1.

如果你真的不想这样做,你应该使用模数(%),它给出了除法的余数。 11%3 = 2,因为3只能进入11次3次(9)。剩下的是2. 41%10 = 1,因为10可以进入41次4次(40)。剩下的就是1。

For stripping the first digits, all you would have to do is mod the tens value that you want to get rid of. For stripping two digits from 12345, you should modulus by 1000. 1000 goes into 12345 twelve times, then the remainder will be 345, which is your answer. You would just need to find a way to find the tens value of the last digit you were trying to strip. Use x % (10^(n)), where x is input, and n is the lowest digit you want to strip.

对于剥离第一个数字,您所要做的就是修改您想要摆脱的十位数值。对于从12345剥离两位数,你应该模数为1000. 1000进入12345十二次,然后余数将是345,这是你的答案。你只需要找到一种方法来找到你试图去掉的最后一个数字的十进制值。使用x%(10 ^(n)),其中x是输入,n是要剥离的最低位。

For stripping the last digits, your way works just fine. What's easier than a quick formula like that?

为了剥离最后的数字,你的方式工作得很好。什么比这样的快速公式更容易?

#5


I don't think there's any other approach than division for removing trailing numbers. It might be more efficient to do repeated integral division than to cast to a float, perform an exponent, then floor and cast back to an integer, but the basic idea remains the same.

我不认为除去尾随数字之外还有其他方法而不是除法。进行重复积分除法可能比投射到浮点数,执行指数,然后求平面并转换回整数更有效,但基本思想保持不变。

Keep in mind that the operation is nearly identical for any base. To remove one trailing decimal digit, you do / 10. If you had 0b0111 and you wanted to remove one digit, it would have to be /2. Or you could have 0xff / 16 to get 0x0f.

请记住,任何基地的操作几乎相同。要删除一个尾随的十进制数字,你可以执行/ 10.如果你有0b0111而你想要删除一个数字,那么它必须是/ 2。或者你可以让0xff / 16得到0x0f。

#6


You have to realize that numbers don't have digits, only strings do, and how many (and which) digits they have depends entirely on the base (which numbers don't have either). Internally, computers use what amounts to binary strings. So in general, manipulating base 10 digits requires you to convert the number to a string first - or do calculations that are the same you would do when converting it to a string. However, for your specific task of removing leading and trailing digits, these calculations (modulo and integer division) are very simple and much faster than converting the entire number.

你必须意识到数字没有数字,只有字符串,并且它们有多少(和哪些)数字完全取决于基数(这些数字也没有)。在内部,计算机使用的是二进制字符串。因此,通常,操作基数为10的数字需要先将数字转换为字符串 - 或者进行与将其转换为字符串时相同的计算。但是,对于删除前导和尾随数字的特定任务,这些计算(模和整数除法)非常简单,并且比转换整数更快。

#7


i think that converting to string and then remove the first char wont do the trick. i believe that the alg for converting to string is doing the div-mod routine, for optimization you might as well do the div-mod alg by yourself and manipulate it to your needs

我认为转换为字符串,然后删除第一个字符不会做的伎俩。我相信转换为字符串的alg正在执行div-mod例程,为了优化你也可以自己做div-mod alg并根据你的需要操作它

#8


Here is C++ code ... It's not tested.

这是C ++代码......它没有经过测试。

int myPow ( int n , int k ){
int ret = 1;
for (int i=0;i<k;++i) ret*=n;
return ret;
}

int countDigits (int n ){
 int count = 0;
 while ( n )++count, n/=10;
return count;
}

int getLastDigits (int number , int numDigits ){
  int tmp = myPow ( 10 , numDigits );
  return number % tmp;
}

int getFirstDigits (int number, numDigits ){
   int tmp = myPow ( 10, countDigits ( number) - numDigits );
   return nuber / tmp;
}