找到小于x的10的最大幂的最快方法 [英] Fastest way to find the largest power of 10 smaller than x

查看:95
本文介绍了找到小于x的10的最大幂的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种快速的方法来找到小于给定数的10的最大幂?

Is there any fast way to find the largest power of 10 smaller than a given number?

此刻,我正在使用此算法,但是只要看到它,我的内心就会死亡:

I'm using this algorithm, at the moment, but something inside myself dies anytime I see it:

10**( int( math.log10(x) ) ) # python
pow( 10, (int) log10(x) )   // C

对于每个问题,我都可以实现一个简单的log10pow函数,但是我仍然想知道十进制数字是否有些魔术.

I could implement simple log10 and pow functions for my problems with one loop each, but still I'm wondering if there is some bit magic for decimal numbers.

推荐答案

另一种算法是:

i = 1;
while((i * 10) < x)
    i *= 10;

这篇关于找到小于x的10的最大幂的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆