代码高尔夫:沙漏

发布于 2024-08-09 16:35:47 字数 1103 浏览 6 评论 0原文

挑战

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

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

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

_____
\   /
 \ /
 / \
/___\

尺寸 3 将添加更多的线条,使灯泡能够容纳更多的“沙子”。

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

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

沙子是从外向内抽的,如果百分比结果是偶数,则右侧优先。

测试用例

Input:
    3 71%
Output:
    _______
    \x  xx/
     \xxx/
      \x/
      / \
     /   \
    /__xx_\

Input:
    5 52%
Output:
    ___________
    \         /
     \xx   xx/
      \xxxxx/
       \xxx/
        \x/
        / \
       /   \
      /     \
     /  xxx  \
    /xxxxxxxxx\

Input:
    6 75%
Output:
     _____________
     \x         x/
      \xxxxxxxxx/
       \xxxxxxx/
        \xxxxx/
         \xxx/
          \x/
          / \
         /   \
        /     \
       /       \
      /         \
     /_xxxxxxxxx_\

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

The challenge

The shortest code by character count to output an hourglass according to user input.

Input is composed of two numbers: First number is a greater than 1 integer that represents the height of the bulbs, second number is a percentage (0 - 100) of the hourglass' capacity.

The hourglass' height is made by adding more lines to the hourglass' bulbs, so size 2 (the minimal accepted size) would be:

_____
\   /
 \ /
 / \
/___\

Size 3 will add more lines making the bulbs be able to fit more 'sand'.

Sand will be drawn using the character x. The top bulb will contain N percent 'sand' while the bottom bulb will contain (100 - N) percent sand, where N is the second variable.

'Capacity' is measured by the amount of spaces () the hourglass contains. Where percentage is not exact, it should be rounded up.

Sand is drawn from outside in, giving the right side precedence in case percentage result is even.

Test cases

Input:
    3 71%
Output:
    _______
    \x  xx/
     \xxx/
      \x/
      / \
     /   \
    /__xx_\

Input:
    5 52%
Output:
    ___________
    \         /
     \xx   xx/
      \xxxxx/
       \xxx/
        \x/
        / \
       /   \
      /     \
     /  xxx  \
    /xxxxxxxxx\

Input:
    6 75%
Output:
     _____________
     \x         x/
      \xxxxxxxxx/
       \xxxxxxx/
        \xxxxx/
         \xxx/
          \x/
          / \
         /   \
        /     \
       /       \
      /         \
     /_xxxxxxxxx_\

Code count includes input/output (i.e full program).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

怀里藏娇 2024-08-16 16:35:47

C/C++,令人沮丧的 945 个字符......

将输入作为参数:
a.out 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") ;}}

...以及我们人类的解密版本:

#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);
            }
        }
        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") ;
    }
}

C/C++, a dismal 945 characters...

Takes input as parameters:
a.out 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") ;}}

...and the decrypted version of this for us mere humans:

#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);
            }
        }
        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") ;
    }
}
孤独岁月 2024-08-16 16:35:47

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

第二行和第三行之间需要显式换行。

并在新的 Acme::AsciiArtinator< 的帮助下/a> 模块:

$S=-int((1-.01*pop)*($N=pop
)                         *
 $                       N
  )                     +
   $                   N
    *$N;(        ${B},$
     F,${x})=qw(\\ / x
      );while($N){;/l
       ater/g;$S-=$s
        =$S>++$r?$r
         :$S;'than
          you';@o
           =(" "
            x--
            $ N
           .   $
          B     .
         x       x
        (         $
       v           =
      $             s
     /               2
    )     .$"x($t=    $
   r++-$s).x x($w=$v+.5)
  .$F,@o,$"x$N.$F.($^=$N?
 $":_)x$w.x x$t.$^x$v.$B);
$,=$/}print$^x++$r,@o;think

Perl, 191 char

205 199 191 chars.

$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

Explicit newline required between the 2nd and 3rd lines.

And with help of the new Acme::AsciiArtinator module:

$S=-int((1-.01*pop)*($N=pop
)                         *
 $                       N
  )                     +
   $                   N
    *$N;(        ${B},$
     F,${x})=qw(\\ / x
      );while($N){;/l
       ater/g;$S-=$s
        =$S>++$r?$r
         :$S;'than
          you';@o
           =(" "
            x--
            $ N
           .   $
          B     .
         x       x
        (         $
       v           =
      $             s
     /               2
    )     .$"x($t=    $
   r++-$s).x x($w=$v+.5)
  .$F,@o,$"x$N.$F.($^=$N?
 $":_)x$w.x x$t.$^x$v.$B);
$,=$/}print$^x++$r,@o;think
离旧人 2024-08-16 16:35:47

Golfscript - 136 个字符(适合推文)

请确保在输入的 % 之后不要有换行符
例如
$ echo -n 3 71%|./golfscript.rb hourglass.gs

您可以像这样设置沙漏动画:

$ for((c=100;c>=0;c-- ));do echo -n "15 $c%"|./golfscript.rb hourglass.gs;echo;sleep 0.1;done;

Golfscript - 136 个字符
确保保存时不要在末尾添加额外的换行符,否则会打印额外的数字

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

Golfscript - 144 Chars

);' ':|/(~:^.*:X
 ;0=~100.@-X*\/
  X'x':x*'_':&
   @*+:s;&&&+
    ^*n^,{:y
     |*.[92
      ]+{s
       [)
       \#
      :s;]
     }:S~^(
    y-,{;S\+
   S+.}%;'/'+
  +\+}%.{&/|*}
 %\-1%{-1%x/|*&
/x*}%)|/&[*]++n*

工作原理
首先做最上面一行下划线,即2n+1
创建沙漏的上半部分,但使用“_”字符而不是空格,因此对于 3 71%,我们将拥有。

\x__xx/
 \xxx/
  \x/

通过将“_”替换为“”来完成上半部分,但保存副本以生成下半部分

通过反转整个内容来创建下半部分

  /x\
 /xxx\
/xx__x\

将所有“x”替换为“ ”,然后将“_”替换为“x” '

  / \
 /   \
/  xx \

最后将底行中的 ' ' 替换为 '_'

  / \
 /   \
/__xx_\

Roundabout 但对我来说,代码比尝试同时生成两半更短

Golfscript - 136 Chars (Fits in a Tweet)

Be sure not to have a newline after the % for the input
eg
$ echo -n 3 71%|./golfscript.rb hourglass.gs

You can animate the hourglass like this:

$ for((c=100;c>=0;c--));do echo -n "15 $c%"|./golfscript.rb hourglass.gs;echo;sleep 0.1;done;

Golfscript - 136 Chars
Make sure you don't save it with an extra newline on the end or it will print an extra number

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

Golfscript - 144 Chars

);' ':|/(~:^.*:X
 ;0=~100.@-X*\/
  X'x':x*'_':&
   @*+:s;&&&+
    ^*n^,{:y
     |*.[92
      ]+{s
       [)
       \#
      :s;]
     }:S~^(
    y-,{;S\+
   S+.}%;'/'+
  +\+}%.{&/|*}
 %\-1%{-1%x/|*&
/x*}%)|/&[*]++n*

How it works
First do the top line of underscores which is 2n+1
Create the top half of the hourglass, but use '_' chars instead of spaces, so for the 3 71% we would have.

\x__xx/
 \xxx/
  \x/

Complete the top half by replacing the "_" with " " but save a copy to generate the bottom half

The bottom half is created by reversing the whole thing

  /x\
 /xxx\
/xx__x\

Replacing all the 'x' with ' ' and then then '_' with 'x'

  / \
 /   \
/  xx \

Finally replace the ' ' in the bottom row with '_'

  / \
 /   \
/__xx_\

Roundabout but for me, the code turned out shorter than trying to generate both halves at once

农村范ル 2024-08-16 16:35:47

Python,213 个字符

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

Python, 213 char

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
却一份温柔 2024-08-16 16:35:47

Rebmu:188 个字符

rJ N 0% rN Wad1mpJ2 S{ \x/ }D0 Hc&[ 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{_}Ls+W2 h1tiVsb1n -1 chRVs{_}hLceVn1

它与此处较短的解决方案具有竞争力,尽管它实际上以“天真的”方式解决了问题。或多或少它是在做“沙物理”,而不是利用对称性或旋转矩阵或任何东西。

H 定义了一个用于打印半个沙漏的函数,您向该函数传递一个数字,该数字是在开始打印“x”字符之前要打印的空格数。如果您在上半部分,则沙串是通过交替附加到头部和尾部来构造的。如果您位于底部,它会跳到字符串的中间来选择插入源。评论源位于:

http://github.com/hostilefork/rebmu /blob/master/examples/hourglass.rebmu

但 Rebmu 的真正技巧是它是一种精简的方言,不会破坏其宿主语言 (Rebol) 的任何解析规则。您可以通过在中间注入普通代码将其变成世界末日可视化,只要您使用小写代码即可:

>> rebmu [rJ 生日:至今(问“你什么时候出生?”)n:(2012 年 12 月 21 日 - 现在/日期)/(2012 年 12 月 21 日 - 生日)Wad1mpJ2 S{ \x/ }D0 Hc ~[u[Ze?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{_}Ls+W2h1tiVsb1n -1 chRVs{_}hLceVn1]

Input Integer: 10
When were you born? 23-May-1974
_____________________
\                   /
 \                 /
  \               /
   \             /
    \           /
     \         /
      \       /
       \x  xx/
        \xxx/
         \x/
         / \
        /   \
       /  xx \
      /xxxxxxx\
     /xxxxxxxxx\
    /xxxxxxxxxxx\
   /xxxxxxxxxxxxx\
  /xxxxxxxxxxxxxxx\
 /xxxxxxxxxxxxxxxxx\
/xxxxxxxxxxxxxxxxxxx\

噢不! :)

(注意:我能够编写和调试 Rebmu 程序的一个主要原因是我可以随时中断普通编码以使用现有的调试工具等。)

Rebmu: 188 chars

rJ N 0% rN Wad1mpJ2 S{ \x/ }D0 Hc&[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{_}Ls+W2 h1tiVsb1n -1 chRVs{_}hLceVn1

It's competitive with the shorter solutions here, though it's actually solving the problem in a "naive" way. More or less it's doing the "sand physics" instead of exploiting symmetries or rotating matrices or anything.

H defines a function for printing a half of an hourglass, to which you pass in a number which is how many spaces to print before you start printing "x" characters. If you're on the top half, the sand string is constructed by alternating appends to the head and the tail. If you're on the bottom it picks the insertion source by skipping into the middle of the string. Commented source available at:

http://github.com/hostilefork/rebmu/blob/master/examples/hourglass.rebmu

But the real trick up Rebmu's sleeve is it's a thin dialect that doesn't break any of the parsing rules of its host language (Rebol). You can turn this into a Doomsday visualization by injecting ordinary code right in the middle, as long you code in lowercase:

>> rebmu [rJ birthday: to-date (ask "When were you born? ") n: (21-dec-2012 - now/date) / (21-dec-2012 - birthday) Wad1mpJ2 S{ \x/ }D0 Hc~[u[Ze?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{_}Ls+W2h1tiVsb1n -1 chRVs{_}hLceVn1]

Input Integer: 10
When were you born? 23-May-1974
_____________________
\                   /
 \                 /
  \               /
   \             /
    \           /
     \         /
      \       /
       \x  xx/
        \xxx/
         \x/
         / \
        /   \
       /  xx \
      /xxxxxxx\
     /xxxxxxxxx\
    /xxxxxxxxxxx\
   /xxxxxxxxxxxxx\
  /xxxxxxxxxxxxxxx\
 /xxxxxxxxxxxxxxxxx\
/xxxxxxxxxxxxxxxxxxx\

O noes! :)

(Note: A major reason I'm able to write and debug Rebmu programs is because I can break into ordinary coding at any point to use the existing debugging tools/etc.)

梦途 2024-08-16 16:35:47

哈斯克尔。 285 个字符。 (无副作用!)

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

使用例如 x 5 50 运行

Haskell. 285 characters. (Side-effect-free!)

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

Run with e.g. x 5 50

养猫人 2024-08-16 16:35:47

到目前为止,C++ 答案是 592 个字符,仍然具有合理的格式。

#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";
    }
}

如果我决定忘记合理地格式化它,我可以将其低至 531

#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";}}

A c++ answer, is 592 chars so far, still having reasonable formatting.

#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";
    }
}

If i decide to just forget formatting it reasonably, i can get it as low as 531:

#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";}}
懒的傷心 2024-08-16 16:35:47

Bash: 639 - 373 个字符

我想我应该尝试一下 bash(还没有看到太多的代码高尔夫)。 (我的版本:GNU bash,版本 3.2.48(1)-release (i486-pc-linux-gnu)

基于 Mobrule 很好的 Python 答案

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

从命令行启动,例如:./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/\/\\\\//}"

Bash: 639 - 373 characters

I thought I would give bash a try (haven't seen much code-golfing in it). (my version: GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu))

Based on Mobrule's nice python answer.

Optimizations must still be available, so all suggestions are welcome!

Start from the command line, e.g. : ./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/\/\\\\//}"
够钟 2024-08-16 16:35:47

爪哇; 661 个字符

public class M{public static void main(String[] a){int h=Integer.parseInt(a[0]);int s=(int)Math.ceil(h*h*Integer.parseInt(a[1])/100.);r(h,h-1,s,true);r(h,h-1,s,false);}static void r(int h,int c,int r,boolean 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)r(h,c-1,r,true);p(u,z,t?'x':((c==0)?'_':' '),t?' ':'x',t,c,true);if(!t)r(h,c-1,r,false);}static void p(int s,int n,char o,char i,boolean t,int p,boolean 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?'/':'\\';System.out.println(z);}}

我需要找到一套更好的高尔夫球杆。

Java; 661 characters

public class M{public static void main(String[] a){int h=Integer.parseInt(a[0]);int s=(int)Math.ceil(h*h*Integer.parseInt(a[1])/100.);r(h,h-1,s,true);r(h,h-1,s,false);}static void r(int h,int c,int r,boolean 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)r(h,c-1,r,true);p(u,z,t?'x':((c==0)?'_':' '),t?' ':'x',t,c,true);if(!t)r(h,c-1,r,false);}static void p(int s,int n,char o,char i,boolean t,int p,boolean 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?'/':'\\';System.out.println(z);}}

I need to find a better set of golf clubs.

旧街凉风 2024-08-16 16:35:47

PHP-361

<?$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));?>

PHP - 361

<?$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));?>
晨曦慕雪 2024-08-16 16:35:47

Python - 272 个字符

X,p=map(int,raw_input()[:-1].split())
k=X*X;j=k*(100-p)/100
n,u,x,f,b,s='\n_x/\ '
S=list(x*k+s*j).pop;T=list(s*k+u*(2*X-j-1)+x*j).pop
A=B=""
for y in range(X):
 r=S();q=T()
 for i in range(X-y-1):r=S()+r+S();q+=T();q=T()+q
 A+=n+s*y+b+r+f;B=n+s*y+f+q+b+B
print u+u*2*X+A+B

Python - 272 chars

X,p=map(int,raw_input()[:-1].split())
k=X*X;j=k*(100-p)/100
n,u,x,f,b,s='\n_x/\ '
S=list(x*k+s*j).pop;T=list(s*k+u*(2*X-j-1)+x*j).pop
A=B=""
for y in range(X):
 r=S();q=T()
 for i in range(X-y-1):r=S()+r+S();q+=T();q=T()+q
 A+=n+s*y+b+r+f;B=n+s*y+f+q+b+B
print u+u*2*X+A+B
戴着白色围巾的女孩 2024-08-16 16:35:47

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);}}

Exabyte18's java converted to C#, 655 bytes:

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);}}
甜嗑 2024-08-16 16:35:47

Ruby, 297 254(压缩后)

使用 ruby -a -p f.rb

n,p = $F.map{|i|i.to_i}
r="\n"
y=''
g,s,u,f,b=%w{x \  _ / \\}
gt; << 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
  
gt; << 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

Ruby, 211

运行两者这是 mobrule 在 Ruby 中的杰作。 (仍然没有最后的换行符。:-)

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

Ruby, 297 254 (after compression)

Run both with ruby -a -p f.rb

n,p = $F.map{|i|i.to_i}
r="\n"
y=''
g,s,u,f,b=%w{x \  _ / \\}
gt; << 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
  
gt; << 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

Ruby, 211

This is mobrule's tour de force, in Ruby. (And still no final newline. :-)

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文