duanshai4484 2011-06-11 04:28
浏览 13
已采纳

找到位置的因素

I know question title seems quite 'un-understandable', but I don't know how to write question title for this particular question.

Question: I want to find factor for position.

Let me clear you with an example.

Value          Factor
[Available]    [Have to find out] 
----------------------------------
1               10
3               10
9               10
10              10
11              10
25              10
50              10
75              10
99              10

100             100
101             100
105             100
111             100
127             100
389             100
692             100
905             100
999             100

1000            1000
1099            1000
1111            1000
4500            1000
6825            1000
7789            1000
9999            1000

10000           10000
10099           10000
51234           10000
98524           10000
99999           10000

100000          100000

and so on.

I hope you understand what I mean to get.

  • 写回答

4条回答 默认 最新

  • douza1373 2011-06-11 04:40
    关注

    Assuming that the first three values should be 1 (as noted by Asaph), then you just need to use all that logarithm stuff you learned in school:

    pow(10, floor(log10($n)))
    

    So, how does this work? The base-10 logarithm of a number x is the y such that 10^y = x (where ^ stands for exponentiation). This gives us the following:

    log(  1) 0
    log( 10) 1
    log(100) 2
    ...
    

    So the log10 of a number between 1 and 10 will be between 0 and 1, the log10 of a number between 10 and 100 will be between 1 and 2, etc. The floor function will give you the integer part of the logarithm (we're only dealing with non-negative values here so there's no need to worry about which direction floor goes with negative values) so floor(log10()) will be 0 for for anything between 1 and 10, 1 for anything between 10 and 100, etc. Now we have how many factors of ten we need so a simple pow(10, ...) gives us the final result.

    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?