说明小数


12

给定十进制形式的<float>, <precision>,您将绘制浮点数小数部分(即小数)的图形表示。例子:

  1. 输入:6.75, 4,输出:

    6||| 7
     ---- 
    

    6.75(输入中的第一个数字)是要解释的数字,4(输入中的第二个数字)是管道下方的破折号。 67底数6.75,是天花板6.75。管道数是decimal part of first input number * second input number

  2. 输入:10.5, 6,输出:

    10|||   11
      ------
    
  3. 输入:20.16, 12,输出

    20||          21
      ------------
    

    .16 实际需要1.92根管,但是由于我无法绘制1.92根管,因此我将其最高拉至2。

  4. 输入:1.1, 12,输出:

    1|           2
     ------------
    

    .1 在这种情况下为1.2根管道,因此将其设置为1根管道。

  5. 此外,边缘情况。输入:(5, 4即数字为整数),输出:

    5    6
     ----
    

  • 要说明的数字是正浮点数,仅受您的语言能力的限制。
  • 精度数是一个偶数整数,大于2(即,最低要求精度为4)。也可以任意大。
  • > =将n.5个管道四舍五入到n + 1(即1.5被四舍五入为2,2.5被四舍五入为3)。<n.5个管道四舍五入为n(即1.4 近似为1,2.4近似为2)。
  • 如果您的语言更方便,则可以将输入作为数组,例如[6.75, 4]。如果您以相反的顺序(即)接受输入[4, 6.75],请在您的答案中进行指定。

您能否确切说明所需的输出格式是什么?
isaacg '16

@isaacg我展示了四个示例输出。什么不清楚?
nicael

似乎发现了一些极端情况。例如5.0 4:输入是从56还是从45,还是可以接受的?输入1.25 2:它有0或1 |s,为什么?(即四舍五入规则是什么)?输入的第一个数字必须为正吗?它的最大精度和大小是多少?输入中的第二个数字是否必须为正数?如果为负,我们是否会倒退?
彼得·泰勒

@彼得澄清。
nicael '16

我认为您没有涵盖四舍五入规则。
彼得·泰勒

Answers:


6

CJam,32个字节

l~1md@:X*mo'|*XSe]1$)NW$s,S*'-X*

精度取第一位,小数点第二位,以空格分隔。

运行所有测试用例。

说明

l~   e# Read input and evaluate, pushing precision and decimal on the stack.
1md  e# Divmod 1, separating the decimal into integer and fractional part.
@:X  e# Pull up precision, store in X.
*mo  e# Multiply precision by fractional part and round.
'|*  e# Push that many vertical bars.
XSe] e# Pad with length X with spaces.
1$)  e# Copy integer part and increment.
N    e# Push linefeed.
W$   e# Copy integer part.
s,   e# Get number of digits as length of string representation.
S*   e# Push that many spaces, to indent the hyphens correctly.
'-X* e# Push X hyphens.

是的,似乎工作正常。
nicael

4

Mathematica,119个字节

a=ToString;b=Array;a[c=Floor@#]<>{b["|"&,d=Round[#2#~Mod~1]],b[" "&,#2-d],a[c+1],"
"," "&~b~IntegerLength@c,"-"&~b~#2}&

我尝试过...测试:

In[1]:= a=ToString;b=Array;f=a[c=Floor@#]<>{b["|"&,d=Round[#2#~Mod~1]],b[" "&,#2-d],a[c+1],"\n"," "&~b~IntegerLength@c,"-"&~b~#2}&;

In[2]:= f[6.75, 4]

Out[2]= 6||| 7
         ----

In[3]:= f[10.5, 6]

Out[3]= 10|||   11
          ------

In[4]:= f[20.16, 12]

Out[4]= 20||          21
          ------------

In[5]:= f[1.1, 12]

Out[5]= 1|           2
         ------------

In[6]:= f[5, 4]

Out[6]= 5    6
         ----

您能否提供一个可行的演示,还是不可能?
nicael '16


3

Java中,253个 206 181字节

@Kenney通过内联条件和曾经使用过的变量并整理出冗余变量,节省了47个字节。

@Kenney通过使用三元运算符内联2个循环,再次节省了25个字节。

纯字符串操作:

内联循环版本(181字节):

String m(float f,int p){int g=(int)f,i=0;String h="",q=""+g;int c=q.length();for(;i<c+p;)h+=i++<c?" ":"-";for(i=c;i<p+c;)q+=i++<c+Math.round((f-g)*p)?"|":" ";return q+(g+1)+"\n"+h;}

4循环版本(206字节):

String m(float f,int p){int g=(int)f,i=0;String h="",q=""+g;int c=q.length();for(;i++<c;)h+=" ";for(;i<=c+p;i++)h+="-";for(i=c;i<c+Math.round((f-g)*p);i++)q+="|";for(;i++<p+c;)q+=" ";return q+(g+1)+"\n"+h;}

非高尔夫版本:

String m(float f,int p){
//initialize some useful values, d is the number of pipes needed
int g=(int)f,d=Math.round((f-g)*p),i=0;
String h="",q=""+g;//append the floored value to the pipe string first
int c=q.length();
for(;i<c;i++)h+=" ";//pad hyphen string with spaces for alignment
for(++i;i<=c+p;i++)h+="-";//append hyphens
for(i=c;i<c+d;i++)q+="|";//append pipes
for(;i<p+c;i++)q+=" ";//append spaces for padding
return q+(g+1)+"\n"+h;}//concatenate the strings in order, separating the strings with a UNIX newline, and return it.

ideone.com上的工作示例。完整程序接受STDIN输入为 <float>,<precision>

注意:Java的Math.round(float)回合使用RoundingMode.HALF_UP默认值,这是OP的必需行为。

提供的测试用例的输出与OP提供的匹配。


我希望你不要介意!你忘了删除a(没用过),在233设置,您可以为您节省另外23获得210个字节:更换q.length()b节省13: int g=(int)f, b=(""+g).length(), c=b, i=0;。在的条件下递增所述迭代器for保存6和内联d(使用一次)保存4: int c = b; for(;i++<b;)h+=" "; for(;i++<=b+p;)h+="-"; for(i=c;i<c+Math.round((f-g)*p);i++)q+="|"; for(;i++<p+b;)q+=" ";
肯尼

另外,有人建议使用实际的换行符而不是转义序列,但是由于我在Windows上,所以这是CRLF,因此无论如何\n
都要

不错-是的,也b变得过时了;-)您仍然可以在第二个中为1保存一个字节for(;i++<=c+p;)。您可以在Windows上保存带有Unix行尾的文件,但是不幸的是Java不允许多行字符串 ..
Kenney 2016年

@肯尼,不。我试过了 这会导致连字符未对齐。不管怎么说,Java都不适合这个工作。
Tamoghna Chowdhury

我仅使用2个for循环将其压缩为181个字节:for(;i<c+p;)h+=i++<c?" ":"-";for(i=c;i<p+c;)q+=i++<c+Math.round((f-g)*p)?"|":" ";
Kenney 2016年

3

的Javascript ES6,105个 104字节

(f,p)=>(i=f|0)+("|".repeat(j=(f-i)*p+.5|0)+" ".repeat(p-j))+(i+1)+(`
`+i).replace(/\d/g," ")+"-".repeat(p)

多亏了您如何保存how,因此节省了1个字节?


抱歉,我没有意识到破折号是输出的一部分,我以为它们只是用来可视化空间。
尼尔

(f,p)=>(i=f|0)+("|"[r="repeat"](j=(f-i)*p+.5|0)+" "[r](p-j))+(i+1)+("\n"+i).replace(/\d/g," ")+"-"[r](p)
Mama Fun Roll'1

哦,是的,换成\n实际的换行符。并确保将其包装在模板字符串中。
Mama Fun Roll'1

2

Haskell,113个字节

(%)=replicate.round
s=show
x!y|(n,m)<-properFraction x=[s n,(y*m)%'|',(y-y*m)%' ',s$n+1,"\n",s n>>" ",y%'-']>>=id

用法示例:

*Main> putStrLn $ 20.16 ! 12
20||          21
  ------------

properFraction将小数分割成整数和小数部分。输出是零件列表(初始编号,小节,空格等),这些零件被串联为单个字符串(通过>>=id)。


可以看到在线演示吗?
nicael

@nicael:演示(带有main完整程序的包装)。
nimi 2016年

像看起来一切正常(顺便说一句:测试存在,认为这是一个更方便的编译器)。
nicael '16

2

MATL,49字节

2#1\tYUbiXK*Yo'|'1bX"tnKw-Z"hb1+YUhht4Y2m13*_45+c

使用语言/编译器的6.0.0版。在Matlab或Octave上运行。

以与挑战中相同的顺序获取数字。

例子

>> matl
 > 2#1\tYUbiXK*Yo'|'1bX"tnKw-Z"hb1+YUhht4Y2m13*_45+c
 >
> 20.16
> 12
20||          21
  ------------

>> matl
 > 2#1\tYUbiXK*Yo'|'1bX"tnKw-Z"hb1+YUhht4Y2m13*_45+c
 >
> 5
> 4
5    6
 ----

说明

2#1\       % implicit input 1st number. Separate decimal and integer part
tYU        % duplicate integer part and convert to string
biXK*Yo    % input 2nd number. Copy it. Multiply by decimal part of 1st number and round
'|'1bX"    % row vector of as many '|' as needed
tnKw-Z"    % row vector of as many spaces as needed
h          % concat horiontally
b1+YUhh    % integer part of 1st number plus 1. Convert to string. Concat twice
t4Y2m      % detect numbers in this string
13*_45+c   % transform numbers into spaces, and non-numbers into '|'
           % implicitly display both strings

您有在线口译员吗?
nicael

尚未:-(在Matlab或Octave上运行
Luis Mendo

2

Perl,90个字节

print$f,"|"x($d=.5+($b=pop)*(($a=pop)-($f=0|$a))),$"x(1+$b-$d),$f+1,$/,$"x length$f,"-"x$b

期望输入作为命令行参数。保存在文件中(例如90.pl)并以perl 90.pl 6.75 4

有评论

print $f,                        # floored input (initialized below due to expr nesting)
      "|" x ($d=.5+              # rounded pipe count (`x` operator casts to int)
             +($b=pop)           # second argument  (executed first)
             *( ($a=pop)         # first argument   (executed second)
               -($f=0|$a) )      # minus floored first argument = fractional part
            ),
      $"x(1+$b-$d),              # spaces
      $f+1,                      # floored + 1
      $/,                        # newline
      $"  x length $f,           # 2nd line alignment
      "-" x $b                   # the 'ruler'

1

Stackgoat31 27字节

CFv1%C*D'|^w1P-Y^vHXNY^w'-^

与大多数其他答案相似。我看看我还能打高尔夫球吗。输入可以是逗号分隔,空格分隔或几乎所有分隔的内容。

不参与竞争,因为Stackgoat是在挑战之后制造的

说明

CF   // Input, floored, push to stack
v1%  // Decimal part
C*   // Times second part
D    // Duplicate that result
'|^  // Repeat | by previous number
w    // Second input
1P   // Move # of |'s to the top of stack
-    // Subtract
Y^   // Repeat " " by above number
vH   // Ceil first input
X    // Newline
Z+   // Add to 
N    // Get length of first #
Y^   // Repeat by spaces
w'-  // Repeat - second input times

1

Lua,157个字节

长,但找不到更短的解决方案

function f(d,n)r=""a=math.floor(d)d,s=d-a,a..r for i=1,#s do r=r.." "end for i=1,n do s,r=s..(i-.5>n*d and" "or"|"),r.."-"end s=s..a+1 return s.."\n"..r end

不打高尔夫球

function g(d,n)
  r=""
  a=math.floor(d)
  d,s=d-a,a..r                         -- d now contains its decimal part
  for i=1,#s do r=r.." "end            -- padding the hyphens
  for i=1,n
  do
    s,r=s..(i-.5>n*d and" "or"|"),r.."-"
    -- s is concatenated with a "|" if i-.5>n*d, a space otherwise
  end
  s=s..a+1
  return s.."\n"..r
end

您可以在线测试lua ,以下测试用例可能会有用:)

function f(d,n)r=""a=math.floor(d)d,s=d-a,a..r for i=1,#s do r=r.." "end for i=1,n do s,r=s..(i-.5>n*d and" "or"|"),r.."-"end s=s..a+1 return s.."\n"..r end
print(f(16.75,4))
print(f(5,4))
print(f(20.16,12))

1

C, 233 231字节

#include <stdlib.h>
#include <math.h>
i,n,l;main(c,v)char**v;{double m;l=atol(v[2]);n=(int)(modf(atof(v[1]),&m)*l+0.5);c=printf("%.f",m);for(;i++<l;)putchar(i>n?32:'|');printf("%.f\n",m+1);printf("%*s",c,"");for(;--i;)putchar(45);}

取消高尔夫:

#include <stdlib.h>
#include <math.h>
i,n,l;

main(c,v)
char**v;
{
    double m;
    l=atol(v[2]); /* Get length from command line */
    n=(int)(modf(atof(v[1]),&m)*l+0.5); /* Get number of pipes and lower limit */
    c=printf("%.f",m); /* print lower limit */

    /* print pipes and spaces */
    for(;i++<l;)
            putchar(i>n?32:'|');

    /* print upper limit */
    printf("%.f\n",m+1);

    /* print spaces before dashes */
    printf("%*s",c,"");

    /* print dashes */
    for(;--i;)
            putchar(45);
}

1

Python 3中,116 108个字节

def f(F,P):l=int(F);h,p=str(l+1),int((F-l)*P+.5);l=str(l);print(l+"|"*p+" "*(P-p)+h);print(" "*len(l)+"-"*P)

trinket.io链接

感谢Seeq节省了一些字符。

第一版:

def f(F,P):
 l=int(F)
 h,s,p=str(l+1)," ",int((F-l)*P+.5)
 l=str(l)
 print(l+"|"*p+s*(P-p)+h)
 print(s*len(l)+"-"*P)

非高尔夫版本:

def frac(F,P):
        low = int(F)
        high = low+1
        pipes = int((F-low)*P+.5)
        print(str(low)+"|"*pipes+" "*(P-pipes)+str(high))
        print(" "*len(str(low))+"-"*P)

您能提供一个工作演示吗?
nicael

这个trinket.io链接应该可以正常工作:trinket.io/python/409b1488f8
Jack Brounstein,2016年

实际上,使用空格文字比存储文字要花费更少的字符。您也可以只加入所有行;。您只使用h一次,因此也应该内联它。应该保存一些字符。
seequ

@Seeq很好地抓住了空间字面量。早些时候,我在第二行的末尾打印空白填充。在意识到这是不必要的之后,我没有仔细检查代码以节省费用。这h比较棘手。为了使len最后两行中的串联和函数正常工作,l必须为字符串,因此h需要替换为str(int(l)+1)h转换前进行设置l可以节省一些字符。
杰克·布劳恩斯坦
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.