Language agnostic 高尔夫代码:沙漏 挑战

Language agnostic 高尔夫代码:沙漏 挑战,language-agnostic,code-golf,rosetta-stone,Language Agnostic,Code Golf,Rosetta Stone,根据用户输入输出沙漏的最短字符数代码 输入由两个数字组成:第一个数字是表示灯泡高度的大于1的整数,第二个数字是沙漏容量的百分比(0-100) 沙漏的高度是通过向沙漏灯泡添加更多线来确定的,因此尺寸2(最小可接受尺寸)将为: _____ \ / \ / / \ /___\ 尺寸3将增加更多的线条,使灯泡能够容纳更多的“沙子” 将使用字符x绘制沙子。顶部灯泡将包含N%的“沙子”,而底部灯泡将包含(100-N)%的沙子,其中N是第二个变量 “容量”是通过沙漏包含的空间量()来衡量的。如果百分

根据用户输入输出沙漏的最短字符数代码

输入由两个数字组成:第一个数字是表示灯泡高度的大于1的整数,第二个数字是沙漏容量的百分比(0-100)

沙漏的高度是通过向沙漏灯泡添加更多线来确定的,因此尺寸2(最小可接受尺寸)将为:

_____
\   /
 \ /
 / \
/___\
尺寸3将增加更多的线条,使灯泡能够容纳更多的“沙子”

将使用字符
x
绘制沙子。顶部灯泡将包含N%的“沙子”,而底部灯泡将包含(100-N)%的沙子,其中N是第二个变量

“容量”是通过沙漏包含的空间量(
)来衡量的。如果百分比不准确,则应四舍五入

沙子是从外到内抽取的,如果百分比结果相等,则以右侧为准

测试用例

代码计数包括输入/输出(即完整程序)。

Perl,191字符 205 199 191查尔

$S=-int((1-.01*pop)*($N=pop)*$N)+$N*$N;$S-=$s=$S>++$r?$r:$S,
$\=$/.$"x$N."\\".x x($v=$s/2).$"x($t=$r++-$s).x x($w=$v+.5)."/$\
".$"x$N."/".($^=$N?$":_)x$w.x x$t.$^x$v."\\"while$N--;print$^x++$r
第2行和第3行之间需要显式换行符

在新模块的帮助下:

C/C++,一个令人沮丧的945个字符。。。 将输入作为参数: a、 出局5.52%

#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
#define p printf

int h,c,*l,i,w,j,*q,k;const char*
 z;int main(int argc,char**argv)
  {h=atoi(argv[1]);c=(h*h*atoi(
   argv[2])+99)/100;l=new int[
    h*3];for(q=l,i=0,w=1;i<h;
     i++,c=(c-w)&~((c-w)>>31
      ),w+=2)if(c>=w){*q++=
       0;*q++ =0;* q++=w;}
        else {*q++=(c+1)/
         2;*q++=w-c;*q++
          =c/2;}p("_");
           for(i=0;i<h
            ;i ++)p (
             "__");p
              ("\n"
               );q
                =
               l+h
              *3-1;
             for (i=
            --h;i>=0;
           i--){p("%*"
          "s\\",h-i,"")
         ; z= "x\0 \0x";
        for(k=0;k<3;k++,q
       --,z+=2)for(j=0;j<*
      q;j++)p(z);q-=0;p("/"
     "\n");}q=l;for(i=0;i<=h
    ;i++){z =i==h? "_\0x\0_":
   " \0x\0 ";p("%*s/",h-i,"");
  for(k=0;k<3;k++,q++,z+=2)for(
 j=0;j<*q;j++)p(z);p("\\\n") ;}}
#包括
#包括
#包括
#定义p printf
inth,c,*l,i,w,j,*q,k;常量字符*
Zint main(int argc,字符**argv)
{h=atoi(argv[1]);c=(h*h*atoi(
argv[2])+99)/100;l=新整数[
h*3];对于(q=l,i=0,w=1;i>31
),w+=2)如果(c>=w){*q++=
0;*q++=0;*q++=w;}
else{*q++=(c+1)/
2、 *q++=w-c;*q++
=c/2;}p(“”);
对于(i=0;i=0;
i--{p(“%*”
“s\\”,h-i,”)
;z=“x\0\0x”;
用于(k=0;kJava;661个字符)

[代码>公共类公众类公众类M{公众类公众类公众类M{公众类公众类公众类公众类公众类公众类公众类公众类公众类M{公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众静态静态无效无效主要(字符串[[字符串[[字符串[]a]a]a a a a a]a a]a)公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类公众类false);}静态空p(int s,int n,char,chi,布尔t,int p,布尔d){int q= n/2+(t t& &(f% 2=0))1:0;int e=q+f;string z=;;int j;(j=0;j<p>C++答案,是<强> 592 < /强>字符,至今仍有合理的格式。< /P>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef string S;
typedef int I;
typedef char C;
I main(I,C**v){
    I z=atoi(v[1]),c=z*z,f=ceil(c*atoi(v[2])/100.);
    cout<<S(z*2+1,'_')<<'\n';
    for(I i=z,n=c;i;--i){
        I y=i*2-1;
        S s(y,' ');
        C*l=&s[0];
        C*r=&s[y];
        for(I j=0;j<y;++j)
            if(n--<=f)*((j&1)?l++:--r)='x';
        cout<<S(z-i,' ')<<'\\'<<s<<"/\n";
    }
    for(I i=1,n=c-f;i<=z;++i){
        I y=i*2-1;
        S s(y,'x');
        C*l=&s[0];
        C*r=&s[y];
        for(I j=0;j<y;++j)
            if(n++<c)*(!(j&1)?l++:--r)=(i==z)?'_':' ';
        cout<<S(z-i,' ')<<'/'<<s<<"\\\n";
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
typedef字符串S;
类型定义int I;
typedef-charc;
I干管(I,C**v){
I z=atoi(v[1]),c=z*z,f=ceil(c*atoi(v[2])/100);
coutPython-272个字符
Golfscript-136个字符(适合于推特)
确保输入的%后面没有换行符
例如
$echo-n3 71%|/golfscript.rb hourglass.gs

可以按如下方式设置沙漏的动画:

$for((c=100;c>=0;c--);执行echo-n“15$c%”./golfscript.rb hourglass.gs;echo;睡眠0.1;完成;

高尔夫脚本-136个字符
请确保在结尾处没有额外的换行符,否则它将打印额外的数字

);' ': /(~:
;0=~100.@-
.**\/:t;'_':&&
*.n
,{:y *.'\\'+{[&'x':x]0t(:t>=}:S~
(y-,{;S\+S+.}%;'/'++\+}%.{&/ *}%\-1%{-1%x/ *&/x*}%) /&[*]++n*    
Golfscript-144个字符

);' ':|/(~:^.*:X ;0=~100.@-X*\/ X'x':x*'_':& @*+:s;&&&+ ^*n^,{:y |*.[92 ]+{s [) \# :s;] }:S~^( y-,{;S\+ S+.}%;'/'+ +\+}%.{&/|*} %\-1%{-1%x/|*& /x*}%)|/&[*]++n* 通过将“u”替换为“”完成上半部分,但保存一份副本以生成下半部分

下半部分是通过颠倒整个过程来创建的

  /x\
 /xxx\
/xx__x\
将所有的“x”替换为“”,然后将“\u”替换为“x”

  / \
 /   \
/  xx \
最后,将最下面一行中的“”替换为‘’

  / \
 /   \
/__xx_\

迂回但对我来说,代码比试图同时生成两半代码要短

Exabyte18的java转换为C#,655字节:

public class M {public static void Main(){int h = Convert.ToInt32(Console.ReadLine());
int s = Convert.ToInt32(h * h * Convert.ToInt32(Console.ReadLine()) / 100);r(h,h-1,s,true);
r(h,h-1,s,false);Console.ReadLine();}static void r(int h, int c, int r, bool t){
if(c<0) return;int u=2*(h-c)-1;if (t&&c==h-1)p(2*h+1,0,'_','_',true,0,false);
int z=r>=u?u:r; r-=z;if (t)M.r(h,c-1,r,true); p(u,z,t?'x':((c==0)?'_':' '), t?' ':'x',t,c,true);
if(!t)M.r(h,c-1,r,false);}static void p(int s, int n, char o, char i, bool t, int p, bool d)
{int f=(s-n);int q=n/2+(!t&&(f%2==0)?1:0);int e=q+f;string z="";int j;for(j=0;j<p+4;j++) z+=" ";if(d)z+=t?'\\':'/';
for (j=0;j<s;j++) z+=(j>=q&&j<e)?i:o; if(d)z+=t?'/':'\\';Console.WriteLine(z);}}
public类M{public static void Main(){int h=Convert.ToInt32(Console.ReadLine());
int s=Convert.ToInt32(h*h*Convert.ToInt32(Console.ReadLine())/100);r(h,h-1,s,true);
r(h,h-1,s,false);Console.ReadLine();}静态void r(int h,int c,int r,bool t){
如果(c=u?u:r;r-=z;如果(t)M.r(h,c-1,r,真);p(u,z,t?'x':((c==0?),“u':”,t?“”:'x',t,c,真);
if(!t)M.r(h,c-1,r,false);}静态空p(int s,int n,char o,char i,bool t,int p,bool d)
{intf=(s-n);intq=n/2+(!t&&&(f%2==0)?1:0);inte=q+f;string z=“”;intj;for(j=0;jPython,213字符)
Bash:639-373个字符
我想我会尝试一下bash(没有看到太多的代码打高尔夫球)。(我的版本:GNUBash,版本3.2.48(1)-发行版(i486 pc linux GNU))

基于

优化必须仍然可用,因此欢迎所有建议

从命令行开始,例如:
/hourglass.sh 7 34%

function f () { for i in `seq $1`;do printf "$2";done; }
N=$1;S=$[$1*$1-$1*$1*$[100-${2/\%/}]/100]
b='\';o=$b;n="\n";r=1;while [ $N -gt 0 ];do
N=$[N-1];z=" ";s=$r;[ $N -eq 0 ]&& z=_;[ $S -lt $r ]&& s=$S
S=$[S-s];t=$[r-s];v=$[s/2];w=$[s-v];r=$[r+2]
o=$n`f $N " "`$b`f $v x;f $t " ";f $w x`/$o$b$n`f $N " "`/`f $w "$z";f $t x;f $v "$z"`$b
done;f $r _;echo -e "${o/\/\\\\//}"
Ruby,297254(压缩后)

使用ruby-a-pf.rb运行这两个函数

n,p = $F.map{|i|i.to_i}
r="\n"
y=''
g,s,u,f,b=%w{x \  _ / \\}
$> << u*2*n+u+r     # draw initial underbar line
a=u
c=100.0/n/n         # amount of sand a single x represents
e = 100.0           # percentage floor to indicate sand at this level
n.times{ |i|
  d=2*n-1-2*i       # number of spaces at this level
  e-= c*d           # update percentage floor
  x = [((p - e)/c+0.5).to_i,d].min
  x = 0 if x<0
  w = x/2           # small half count
  z = x-w           # big half count
  d = d-x           # total padding count
  $> << s*i+b+g*w+s*d+g*z+f+r
  y=s*i+f+a*z+g*d+a*w+b+r+y
  a=s
}
$_=y
n,p=$F.map{| i | i.to_i}
r=“\n”
y=''
g、 s,u,f,b=%w{x\\\\\/\\}
$>Haskell.285个字符。(无副作用!)

使用PHP-361运行,例如
x5 50

<?$s=$argv[1];$x='str_pad';$w=$s*2-1;$o[]=$x('',$w+2,'_');
$r=$s*ceil($w/2);$w=$r-($r*substr($argv[2],0,-1)/100);$p=0;
$c=-1;while($s){$k=$s--*2-1;$f=$x($x('',min($k,$w),' '),$k,'x',2);
$g=$x($x('',min($k,$w),'x'),$k,' ',2);$w-=$k;$o[]=$x('',$p)."\\$f/";
$b[]=$x('',$p++)."/$g\\";}$b[0]=str_replace(' ','_',$b[0]);
krsort($b);echo implode("\n",array_merge($o,$b));?>
:188个字符
rJ N 0%rN-Wad1mpJ2 S{\x/}d0hc&[u[Z=~wA Qs^RTkW[isEL0c[skQdvK2][eEV?kQ[tlQ]]pcSeg--B0[eZ1 5]3]prRJ[si^DspSCsQfhS]eZ1[S+DcA+wMPc2no]]Va |[mpAj**2]prSI^w{{uu}Ls+W2 h1tiVsb1n-1 chrv

虽然它实际上是以一种“幼稚”的方式解决问题,但它与这里较短的解决方案相比具有竞争力。它或多或少是在做“沙物理”,而不是利用对称性或旋转矩阵或任何东西

H
定义了一个用于打印半个沙漏的函数,在开始打印“x”之前,您可以向沙漏传递一个数字,即要打印多少个空格字符。如果您位于上半部分,则沙字符串由头和尾的交替附加构成。如果您位于下半部分,则通过跳到字符串中间来拾取插入源。注释源可在以下位置获得:

< Rebmu >的真正技巧是,它是一个瘦的方言,不会破坏它的宿主语言的任何语法规则(ReBOL)。你可以通过在中间注入普通代码,把它变成一个末日可视化,只要你用小写代码:

>rebmu[rJ生日:至今(问“你什么时候出生的?”)n:(2012年12月21日-现在/日期)/(2012年12月21日-生日)Wad1mpJ2 S{
);' ':|/(~:^.*:X
 ;0=~100.@-X*\/
  X'x':x*'_':&
   @*+:s;&&&+
    ^*n^,{:y
     |*.[92
      ]+{s
       [)
       \#
      :s;]
     }:S~^(
    y-,{;S\+
   S+.}%;'/'+
  +\+}%.{&/|*}
 %\-1%{-1%x/|*&
/x*}%)|/&[*]++n*
\x__xx/
 \xxx/
  \x/
  /x\
 /xxx\
/xx__x\
  / \
 /   \
/  xx \
  / \
 /   \
/__xx_\
public class M {public static void Main(){int h = Convert.ToInt32(Console.ReadLine());
int s = Convert.ToInt32(h * h * Convert.ToInt32(Console.ReadLine()) / 100);r(h,h-1,s,true);
r(h,h-1,s,false);Console.ReadLine();}static void r(int h, int c, int r, bool t){
if(c<0) return;int u=2*(h-c)-1;if (t&&c==h-1)p(2*h+1,0,'_','_',true,0,false);
int z=r>=u?u:r; r-=z;if (t)M.r(h,c-1,r,true); p(u,z,t?'x':((c==0)?'_':' '), t?' ':'x',t,c,true);
if(!t)M.r(h,c-1,r,false);}static void p(int s, int n, char o, char i, bool t, int p, bool d)
{int f=(s-n);int q=n/2+(!t&&(f%2==0)?1:0);int e=q+f;string z="";int j;for(j=0;j<p+4;j++) z+=" ";if(d)z+=t?'\\':'/';
for (j=0;j<s;j++) z+=(j>=q&&j<e)?i:o; if(d)z+=t?'/':'\\';Console.WriteLine(z);}}
N,p=map(int,raw_input()[:-1].split())
S=N*N-N*N*(100-p)/100
_,e,x,b,f,n=C='_ x\/\n'
o=""
r=1
while N:N-=1;z=C[N>0];s=min(S,r);S-=s;t=r-s;v=s/2;w=s-v;r+=2;o=n+e*N+b+x*v+e*t+x*w+f+o+n+e*N+f+z*w+x*t+z*v+b
print _*r+o
function f () { for i in `seq $1`;do printf "$2";done; }
N=$1;S=$[$1*$1-$1*$1*$[100-${2/\%/}]/100]
b='\';o=$b;n="\n";r=1;while [ $N -gt 0 ];do
N=$[N-1];z=" ";s=$r;[ $N -eq 0 ]&& z=_;[ $S -lt $r ]&& s=$S
S=$[S-s];t=$[r-s];v=$[s/2];w=$[s-v];r=$[r+2]
o=$n`f $N " "`$b`f $v x;f $t " ";f $w x`/$o$b$n`f $N " "`/`f $w "$z";f $t x;f $v "$z"`$b
done;f $r _;echo -e "${o/\/\\\\//}"
n,p = $F.map{|i|i.to_i}
r="\n"
y=''
g,s,u,f,b=%w{x \  _ / \\}
$> << u*2*n+u+r     # draw initial underbar line
a=u
c=100.0/n/n         # amount of sand a single x represents
e = 100.0           # percentage floor to indicate sand at this level
n.times{ |i|
  d=2*n-1-2*i       # number of spaces at this level
  e-= c*d           # update percentage floor
  x = [((p - e)/c+0.5).to_i,d].min
  x = 0 if x<0
  w = x/2           # small half count
  z = x-w           # big half count
  d = d-x           # total padding count
  $> << s*i+b+g*w+s*d+g*z+f+r
  y=s*i+f+a*z+g*d+a*w+b+r+y
  a=s
}
$_=y
m,p=$F.map{|i|i.to_i}
q=m*m-m*m*(100-p)/100
_,e,x,b,f=%w{_ \  x \\ /}
n="\n"
o=''
r=1
while m>0
m-=1
z=m>0?e:_
s=q<r ?q:r
q-=s
t=r-s
v=s/2
w=s-v
r=r+2
o=n+e*m+b+x*v+e*t+x*w+f+o+n+e*m+f+z*w+x*t+z*v+b
end
$_=_*r+o
x n c=h s++'\n':reverse(h(flip s)) where h s=r w '-'++s '+' b(w-2)0 p;w=(t n);p=d(n*n*c)100
s x n i o p|i>0='\n':l++s x n(i-2)(o+1)(max(p-i)0)|True=[] where l=r o b++'\\':f d++r(i#p)n++f m++'/':r o b;f g=r(g(i-(i#p))2)x
b=' '
r=replicate
t n=1+2*n
d=div
(#)=min
m=(uncurry(+).).divMod
<?$s=$argv[1];$x='str_pad';$w=$s*2-1;$o[]=$x('',$w+2,'_');
$r=$s*ceil($w/2);$w=$r-($r*substr($argv[2],0,-1)/100);$p=0;
$c=-1;while($s){$k=$s--*2-1;$f=$x($x('',min($k,$w),' '),$k,'x',2);
$g=$x($x('',min($k,$w),'x'),$k,' ',2);$w-=$k;$o[]=$x('',$p)."\\$f/";
$b[]=$x('',$p++)."/$g\\";}$b[0]=str_replace(' ','_',$b[0]);
krsort($b);echo implode("\n",array_merge($o,$b));?>
Input Integer: 10
When were you born? 23-May-1974
_____________________
\                   /
 \                 /
  \               /
   \             /
    \           /
     \         /
      \       /
       \x  xx/
        \xxx/
         \x/
         / \
        /   \
       /  xx \
      /xxxxxxx\
     /xxxxxxxxx\
    /xxxxxxxxxxx\
   /xxxxxxxxxxxxx\
  /xxxxxxxxxxxxxxx\
 /xxxxxxxxxxxxxxxxx\
/xxxxxxxxxxxxxxxxxxx\