四舍五入之前需要加1的小数位数是多少?


10

当四舍五入一个数字时,如果下一位数字是>= 5您加1。例如:

3.1415926535 rounded to 1dp is 3.1
3.1415926535 rounded to 4dp is 3.1416    <-- Note the 5 changed to 6
3.1415926535 rounded to 5dp is 3.14159

3.1415926535 rounded to 9dp is 3.141592654  <-- Note the 3 changed to 4

您面临的挑战是接收一个整数作为输入并输出小数位数,然后在该位数之前四舍五入该数字的平方根-即出现的数字位数之前的小数位数>= 5

整数将在0到100,000之间(含0和100,000),因此对于59752的边缘情况,您需要支持17个小数点(以检查第17位)。

如果您的编程语言无法更改小数点位数,则可以显示“?” 给用户的信息。

例:

Input    Root                     Output

    5 -> 2.23 606797749979     -> 2
   41 -> 6.40312423 743284     -> 8      (Largest gap under 100)
  596 -> 24.4131112314 674     -> 10     (Largest gap under 1000)
59752 -> 244.44222221212112029 -> 16     (Largest gap under 100000)

在理想的正方形上做您想做的事。

这是因此最短的代码获胜。


对于任何有兴趣的人,数字310,617是1,000,000以下的最大数字,在达到数字前有18 >= 5


我们最多需要支持多少个小数位?-没有语言可以无限精确地存储。
蓝色

不确定为什么,但是我得到了SQRT(59752)的17位数字(两种不同的语言)。其他结果正确显示。
乔纳森·里奇·佩平

@ JonathanLeech-Pepin您是否出于某种原因包括了最后一位数字?或者您的程序不支持足够的小数位。
2015年

它的位数永远不会大于5。例如,我的程序将以-1退出
蓝色

@muddyfish很好。
蒂姆(Tim)

Answers:



2

Pyth,13个字节

f<5e@=*QC\d2Z

测试套件

Q等于输入开始。在每个时间步长上Q 100,计算为chr('d')。扎根。采取这个mod10。如果结果大于5,则终止。打印终止索引所花费的迭代次数(0索引)。

详细:

f<5e@=*QC\d2Z
                   Q = eval(input())
f           Z      Filter for the first truthy result over the infinite sequence
                   starting at Z (= 0)
     =*Q           Q *=
        C\d             chr('d') (= 100)
                   ---------------------
    @  Q   2          Q ^ (1/2)
   e                            % 10
 <5               5 <

1

CJam,29 26 28字节

rimqs_'.+'.#)>{'5<}%0#]W'?er

在线尝试。

放一个“?” 如果没有出现该数字可以四舍五入(完美的正方形或太长)。


1

Pyth,22个字节

J`%@Q2 1x.e<\4@Jbr2lJ1

说明

                       - Autoassign Q to evaluated input
   @Q2                 - Get the square root of Q
J`%    1               - Get the stuff after the decimal point and put it in a string. Store in J
         .e      r2lJ  - Create a range between 2 and the length of the string (forget about the 0. bit) and enumerate over it
              @Jb      - Get the current decimal place
           <\4         - Is it bigger than 4
        x            1 - Find the position of the first True value

我绝对可以打高尔夫球。如果输入的数字不大于4,它将输出-1。支持17dp。


1

Javascript,59个字节

f=a=>(a=/\.(.*?)[5-9]/.exec(Math.sqrt(a)),a?a[1].length:'?')

返回?59752,因为JavaScript仅使用双精度。


1

Linux Shell,52个字节

dc -e'34k?vp'|cut -d. -f2|sed 's/.[5-9\s].*//'|wc -m

我尝试了一个纯粹的 dc解决方案,但失败了。精度可调(第一个数字)。

由于OP善意地指出“您可以在完美的正方形上做您想做的事”,在这种情况下,此解决方案输出的精度为+1,在这种情况下为35。


1

Mathematica 60字节

(Position[Drop@@RealDigits[N[Sqrt@#,99]],x_/;x>4][[1,1]]-1)&

(Position[Drop@@RealDigits[N[Sqrt@#, 99]], x_ /; x > 4][[1, 1]] - 1) &[59752]

16


您可以删除周围的空格Apply
LegionMammal978

谢谢。字节数保持不变,因为我没有计算这些空格。
DavidC 2015年

-2

Ruby,46个字节

这可能无效,因为它只能容纳16位数字。

p (gets.to_i**0.5).to_s.split('.')[1]=~/[5-9]/

59752的输出是什么?
蒂姆(Tim)

nil,因为整个字符串中没有超过4的数字。它可能取决于红宝石版本。
MegaTom,2015年

它需要支持59752-因此需要17 dps
蒂姆
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.