用散列包围字符串


24

我已经在Python中做到了这一点,但似乎可以将其缩短很多:

txt = input("Type something.. ")
c = "#"
b = " "
print(c * (len(txt) + 4))
print(c, b * len(txt), c)
print(c, txt, c)
print(c, b * len(txt), c)
print(c * (len(txt) + 4))

因此,如果用户键入:

Hello World

程序打印:

###############
#             #
# Hello World #
#             #
###############


最少的字节数会获胜-当然,答案可以用任何语言编写。




1
输入的字符串将不包含换行符,对吗?
flodel

2
@ edc65我不同意,这个挑战有很大的不同
Beta Decay

10
我建议至少等待一个星期再接受答案。虽然如果您打算提交更短的提交内容,是否计划更新接受的答案并不重要,但是会有人抱怨早日接受答案,甚至投票否决。如果已经有一个答案,也将有些人对发布答案不感兴趣。
马丁·恩德

Answers:


17

CJam,22个 20字节

qa{4/3*' +f+W%z}8/N*

在这里测试。

说明

如何将二维字符网格包装在一层空格(或其他任何字符)中?正确:四次,您在每行上添加一个空格,然后将网格旋转90度。这就是我在这里做的八次旋转:四个代表空格,四个代表#

qa      e# Read the input and wrap it in an array, to make it a 2D grid.
{       e# Execute this block for each value from 0 to 7.
  4/3*  e#   Divide by 4, multiply by 3. Gives 0 for the first four iterations and
        e#   and 3 for the other four.
  ' +   e#   Add the result to a space character (space + 3 == #).
  f+    e#   Append this to each line of the grid.
  W%z   e#   Reverse the lines, then transpose the grid - together these rotate it.
}8/
N*      e# Join the lines of the grid by newlines.

1
多么好的解决方案!
Joshpbarron 2015年

1
我喜欢这样的解释:-D
John Dvorak

13

vim,28 27击键

I# <esc>A #<esc>Y4PVr#G.kwv$3hr kk.

假设输入是在当前打开的文件中作为一行文本提供的。

说明:

I# <esc>        put a "#" and space at the beginning of the line
A #<esc>        put a space and "#" at the end of the line
Y4P             copy the line 4 times
Vr#             replace the entirety of the first line with "#"s
G.              do the same for the last line
kwv$3hr<space>  replace middle of the fourth line with spaces
kk.             do the same for the second line

也可以将其作为“程序”运行,如下所示:

echo 'Hello World' | vim - '+exe "norm I# \<esc>A #\<esc>Y4PVr#G.kwv$3hr kk."'

这有点令人费解,但它可以工作。


1
这是最好的答案,因为它的工作方式与我的想法相同。
tchrist

12

pb -89个字节

v[4]w[Y!-1]{b[35]^}w[B!0]{t[B]vvv>>b[T]^^^<}v>>>w[Y!4]{b[35]v}w[X!0]{b[35]^[Y]b[35]v[4]<}

这就是铅的挑战!并不是说它对这种挑战或其他任何事物具有竞争力。它仍然是一种可怕的高尔夫语言。但是,与其他挑战相比,用pb解决此类挑战要容易得多。由于pb将其输出视为2D画布并能够写入任何坐标,因此涉及文本定位/在文本周围绘图(即此挑战)的所有操作都可以非常直观地处理。

观看运行情况:

该可视化是使用pb解释器pbi的开发版本创建的。蓝色背景的线是Y=-1,程序启动时将存储输入。红色背景的矩形是画笔的当前位置。黄色背景的矩形位于将ascii字符32(空格)显式写入画布的任何位置。没有此背景的任何空格实际上都具有值0,该值将转换为空格。

这是我在编写代码时所用注释的代码,以及与主题相关的部分标题;)

################################
#                              #
# Handle first column oddities #
#                              #
################################
v[4]           # Start from Y=4 and go up (so we land on input afterwords)
w[Y!-1]{       # While we're on the visible part of the canvas
    b[35]^         # Write "#", then go up
}

#########################
#                       #
# Insert text of output #
#                       #
#########################

w[B!0]{        # For each character of input
    t[B]           # Save input char in T
    vvv>>          # Down 3 + right 2 = where text part of output goes
    b[T]^^^<       # Write T and go to next char
}

###############################
#                             #
# Handle last column oddities #
#                             #
###############################

v>>>           # Go to Y=0, X=(X of last text output's location + 2)
w[Y!4]{        # Until we reach the last line of output
    b[35]v         # Draw "#", then go down
}

###########################
#                         #
# Loop to finish Y=0, Y=4 #
#                         #
###########################

w[X!0]{        # Until we've gone all the way left
    b[35]^[Y]      # Print "#" at Y=4, go to Y=0
    b[35]v[4]      # Print "#" at Y=0, go to Y=4
    <              # Move left, printing until output is complete
}

3
Not that it's competitive... It's still a horrible golf language.我这么说,但现在我只能并列最后。我期待最后的彻底。:D
Undergroundmonorail

9

Brainfuck-156字节

++++++++++>,[>>+++++[<+++++++>-],]<....[.---<<]>>+++>>+++.---.[.>>]<<.+++.[<]>.>>+++.---.<[.>>]<<<.>>.---[<]>.--->>+++.---[.>>]<<..+++.---[<]>[+++.>>]<<....

这可能是可打高尔夫球的。在某些地方,我不知道将某个值存储在某个位置以供重用或重新制作/从磁带上的其他位置获取它会更好。我没有做工作去弄清楚它,而是没有这样做。:D

有评论:

++++++++++>              Place a 10 (\n) at the beginning of the tape

,[>>+++++[<+++++++>-],]  Place a byte of input; place a 35 (#); repeat until end of input

<....                    Print the last cell (35; #) 4 times

[.---<<]                 Print every noninput cell on the tape in reverse (one # for each byte of input; then \n)
                         After printing each cell; decrease it by 32

>>+++                    Increase the 7 back up to 10

>>+++.---.               Increase a 32 (space) back up to 35 (#); print it; put it back to 32 and print again

[.>>]                    Print every 32 on the tape (one for each byte of input)

<<.+++.                  Print the last space again; increase it by 3 and print the resulting #

[<]>.                    Go to the beginning of the tape and print the \n

>>+++.---.               Increase a 32 (space) back up to 35 (#); print it; put it back to 32 and print again

<[.>>]                   Print every byte of input

<<<.>>.---               Print a space; then print a 35 (#} that was left behind earlier; Set it back to 32 after

[<]>.---                 Go to the beginning of the tape and print the \n; decrease by 3

>>+++.---                Set a space to #; print it; set it back to space

[.>>]                    Print all spaces

<<..+++.---              Print a space twice more; set it to #; print it again; set it back to space

[<]>                     Go to the "newline" (currently a 7 instead of 10)

[+++.>>]                 Increase by 3; print; do the same for all spaces (to set them to # before printing)

<<....                   Print the last # 4 more times

5

K,21个字节

4(|+"#",)/4(|+" ",)/,

征服字符串,在字符串的所有四个侧面添加一个空格,然后在字符串的每个侧面添加一个八面体。实际上:

  4(|+"#",)/4(|+" ",)/,"Hello."
("##########"
 "#        #"
 "# Hello. #"
 "#        #"
 "##########")

适用于OK,Kona和K5。

一个字符的长度内有很多变体,这些变体消除了上面的冗余,但是即使我们只需要执行两次“包装”操作,也似乎没有任何变化:

{x{|+x," #"y}/&4 4},:
{x{|+x,y}/,/4#'" #"},:
{x{|+x,y}/" #"@&4 4},:
{|+x,y}/[;" #"@&4 4],:

3

Pyth,31个字节

Js[K\#*d+2lzKb*K+4lz)_Jjd[KzK)J

多亏了发表评论的人对如何进一步打高尔夫球的提示,我确实不太了解(可能)讲的语言。


2
几个提示:"#"等于\#。如果必须串联许多对象,最好使用,j""[然后再加上一个等于的对象列表jk[
orlp 2015年

1
更多提示。s[等于jk[字符串数组。另外,您也可以K像一样即时分配jk[K\#,只需删除初始分配即可。分配lzJ没有帮助,如果只用了两次,所以保存J别的东西。在这里,如果您进行交换ZJ可以删除=。最后,您可以J即时分配。代码如下所示:_Js[K\#*d+2lzKb*K+4lz)jd[KzK)J
PurkkaKoodari 2015年

3

Python 3,88个字节

谢谢@WorldSEnder

s=" ";n=s+input()+s
b=len(n)
h="#";x=h*(b+2);y=h+s*b+h;z="\n"
print(x+z+y+z+h+n+h+z+y+z+x)

I / O示例:

This is a test
##################
#                #
# This is a test #
#                #
##################

s=" "一开始我总共节省了1个字节。
WorldSEnder

为什么用;btw代替换行?两者都应该是一个字节,对不对?
JeromeJ

3
@JeromeJ我知道这没什么用,但是如果放在一行上它看起来会更短;)
Beta Decay

3

Perl,43 76字节

按指定转换每个文本输入行:

s/.*/($x=("#"x(4+($z=length))))."\n".($y="#"." "x(2+$z)."#\n")."# $& #\n$y$x"/e

例如:

echo surround a long string with pounds | 
perl -ple's/.*/($x=("#"x(4+($z=length))))."\n".($y="#"." "x(2+$z)."#\n")."# $& #\n$y$x"/e' 
######################################
#                                    #
# surround a long string with pounds #
#                                    #
######################################

以下是查看实际操作的方法:

perl -MO=Deparse,-p,-q,-x9 -ple '($x=("#"x(4+($z=length))))."\n".($y="#"." "x(2+$z)."#\n")."# $& #\n$y$x";'
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined(($_ = <ARGV>))) {
    chomp($_);
    (((($x = ('#' x (4 + ($z = length($_))))) . "\n") . ($y = (('#' . (' ' x (2 + $z))) . "#\n"))) . (((('# ' . $&) . " #\n") . $y) . $x));
}
continue {
    (print($_) or die((('-p destination: ' . $!) . "\n")));
}
-e syntax OK

所以更像这样:

((  
      (($x =  ('#' x (4 + ($z = length($_))))) . "\n")
    .  ($y = (('#' . (' ' x (2 + $z))) . "#\n"))
  )  
    .  (((('# ' . $&) . " #\n") . $y) . $x)
)   

字符串前后应有一排空格填充。看看问题的示例输出...。但是很高兴看到您提交...尤其是在Perl中:D
rayryeng-恢复Monica 2015年

@rayryeng我很难过,没人在Perl中提交。
tchrist

老实说,我很惊讶。当然,这似乎是一个适合的问题:)。
rayryeng-恢复莫妮卡2015年

@rayryeng在下一版本中已修复。:)
tchrist

1
实际上,现在我考虑了一下,如果将所有内容拆分为单独的语句,则可以将它们简单地移到替换项之外,然后放下e修饰符...但是,如果这样做,您最好也完全放掉替换项:$x="#"x(4+($z=y!!!c));$y="#".$"x(2+$z)."#\n";$_="$x\n$y# $_ #\n$y$x"。请使用实际的换行符代替,\n它只有65个字节,外加2个-lp
ThisSuitIsBlackNotNot

2

JavaScript(ES6),73

大量使用模板字符串,这两个换行符很重要并已计数。

在任何符合EcmaScript 6的浏览器(FireFox和最新的Chrome,也许是Safari)中测试运行以下代码段。

f=s=>(z=c=>`*${c[0].repeat(s.length+2)}*
`)`*`+z` `+`* ${s} *
`+z` `+z`*`

// Less golfed

U=s=>(
  z=c=>'*' + c.repeat(s.length+2) + '*\n',
  z('*') + z(' ') + '* ' + s + ' *\n' + z(' ') + z('*')
)

// TEST

O.innerHTML=f('Hello world!')
<pre id=O></pre>

这比我从其他挑战中得出的第一次尝试要短得多:

f=s=>(q=(c,b,z=c.repeat(b[0].length))=>[z,...b,z].map(r=>c+r+c))('*',q(' ',[s])).join`\n`

不为我(金丝雀)跑步。
mınxomaτ

@minxomat什么是金丝雀?
edc65

它适用于Chrome 45.0.2454.85 m,Windows 7
edc65

Canary始终是最新的Chrome版本。我的Chrome稳定版也无法使用。编辑:虽然在FireFox中工作。
mınxomaτ

(将z副作用分配给全局是否合法?)
Neil

2

Python 2,74

s='# %s #'%input()
n=len(s)
b='\n#'+' '*(n-2)+'#\n'
print'#'*n+b+s+b+'#'*n

接受报价,例如 "Hello World"

  • 第三行是输入中的输入# _ #
  • 第二和第四行 b# #具有空间,用换行包围任一侧采取所有四个换行符的照顾的权利数量。
  • 第一和第五行是 #乘以输入的长度

线被连接并打印。


2

MATLAB,93 91字节

不是最漂亮的,但可以完成工作。

t=[32 input('','s') 32];m='#####'.';n=repmat('# ',numel(t),1)';disp([m [n;t;flipud(n)] m])

代码说明

步骤1

t=[32 input('','s') 32];

从STDIN中读取一个字符串,并在其中插入一个前导和尾随的空格。32是空格的ASCII代码,并且以字符串类型在输入中读取,将32s合并为空格。

第2步

m='#####'.';

在列向量中声明5个哈希符号的字符数组。

步骤三

n=repmat('# ',numel(t),1)'

创建一个2行的字符矩阵,该矩阵首先由哈希符号填充,然后由空格填充。字符数是输入字符串的长度加上2,以便我们可以容纳字符串前后的空间。

第四步

disp([m [n;t;flipud(n)] m])

我们将把所有东西拼凑在一起。我们将第一列5个散列,然后放置中心部分,然后放置另一列5个散列。中心部分由在步骤#3中创建的2行字符矩阵,输入字符串本身(具有尾部和前导空格)组成,后跟2行字符矩阵,但取反。

示例运行

>> t=[32 input('','s') 32];m='#####'.';n=repmat('# ',numel(t),1)';disp([m [n;t;flipud(n)] m])
This is something special for you
#####################################
#                                   #
# This is something special for you #
#                                   #
#####################################
>> t=[32 input('','s') 32];m='#####'.';n=repmat('# ',numel(t),1)';disp([m [n;t;flipud(n)] m])
Hello World
###############
#             #
# Hello World #
#             #
###############
>> t=[32 input('','s') 32];m='#####'.';n=repmat('# ',numel(t),1)';disp([m [n;t;flipud(n)] m])
I <3 Code Golf StackExchange!
#################################
#                               #
# I <3 Code Golf StackExchange! #
#                               #
#################################

2

Perl 5.14 +,57 56个字节

perl -lpe '$_=join"#
#",($_=" $_ ",y// /cr,"#".y//#/cr)[2,1,0..2]'

54个字节+ 2个字节-lp(如果输入未以换行符结尾,-l则可以删除以节省一个字节)。

接受STDIN上的输入:

$ echo Hello World | perl -lpe '$_=join"#
#",($_=" $_ ",y// /cr,"#".y//#/cr)[2,1,0..2]'
###############
#             #
# Hello World #
#             #
###############

怎么运行的

该程序的核心是列表切片:

($_=" $_ ",y// /cr,"#".y//#/cr)[2,1,0..2]'

这提供了一种紧凑的方式来存储输出的三个唯一行(边界框的前两行与后两行相同,仅是镜像的)。对于输入字符串foo,切片的结果将是:

index   value
--------------
  2    "######"
  1    "     "
  0    " foo "
  1    "     "
  2    "######"

将这些值与一起加入#\n#我们的盒子。

请注意,r要对音译运算符使用非破坏性修饰符,需要Perl 5.14+ y///


2

PHP, 95 93字节

不完全是出色的或类似的东西,但实际上很有趣!

$l=strlen($s=" $argv[1] ");printf("#%'#{$l}s#
#%1\${$l}s#
#$s#
#%1\${$l}s#
#%1\$'#{$l}s#",'');

不完全是漂亮,也不是什么,但是效果很好!


感谢@Titus节省2个字节。


您可以使用$argv代替$_GET和来保存两个字节-r
泰特斯(Titus)

1

C ++,198字节

#include <iostream>
#include <string>
int i;int main(){std::string t,n,o;std::getline(std::cin,o);t="\n#";o="# "+o+" #";for(;i<o.size();i++){n+="#";if(i>1)t+=" ";}t+="#\n";std::cout<<n+t+o+t+n;}

我第一次在Codegolf刺手,虽然我学会了C ++可能不是打高尔夫球的最佳语言,但我还是觉得自己做得很体面(?)。

不打高尔夫球

#include <iostream>
#include <string>

int i;                              //globals default to a value of 0

int main()
{
    std::string t, n, o;
    std::getline(std::cin, o);

    t = "\n#";                      // t needs hashes at start and end, places hash at start here
    o = "# " + o + " #";            // put hash at each end of input

    for(; i < o.size(); i++) {
        n += "#";                   // fills n with hashes
        if(i > 1) {
            t += " ";               // fill t with spaces between hashes, has two fewer spaces than n has hashes
        }
    }
    t += "#\n";                     // puts final hash at end of t

    std::cout << n + t + o + t + n; // final output
}

n,o和t分别代表完全散列的线,输入(在每个末端带有散列)以及输入和散列线之间的线。


1

> <>106字节

我觉得> <>可能不是最好的语言,但是我走得太远了,不能放弃并且不发布它。第*4行结尾处的假定为空格。您不喜欢这段代码看起来多么怪异吗?在线尝试。

<v?(0:i
v>~" ## "}}l:::
>"#"o1-:?!v02.>~a"#"oo
"-2ooa"#"~<.31v!?:-1o" 
7v?=3loroo"#"a<.4
.>";^"e3pa2p093

这是一个除了方向转换器之外没有其他任何版本的版本,它可以使您了解指针如何移动(请注意,我省略了“ teleport”语句,即.)。

方向流:

<v
v>
>         v   >
          <   v 
 v            <
 >           

说明

我对堆栈的可视化将基于输入 input。> <>是一种二维语言,因此在执行其下面的代码时(在此代码中),请注意指针在行之间移动的位置<>v^中主要用于更改方向)。我将从指针开始的地方开始我的解释。注意,随着指针在第五行之后向后移动,将重复两行。

我总是对> <>感到很酷的地方是它能够修改自己的源代码,并且我在此程序中使用了它。第3行和第4行可重复使用,以通过修改每个字符来打印最后两行。

第1行:输入回路

<v?(0:i
<        change direction to left
   (0:i  checks if input is less than 0 (no input defaults to -1)
 v?      change direction to down if so

堆: [-1,t,u,p,n,i]


第2行:生成第三行输出

v>~" ## "}}l:::  
 >~" ## "}}      remove -1 (default input value) from stack and pads with # and spaces
           l:::  push 4 lengths of padded input

堆: [9,9,9,9,#, ,t,u,p,n,i, ,#]


第3行:打印输出的第一行

>"#"o1-:?!v02.>~a"#"oo
>"#"o                   print "#"
     1-                 subtract 1 from length (it's at the top of the stack)
       :?!v             move down if top of stack is 0

堆: [0,9,9,9,#, ,t,u,p,n,i, ,#]

输出:

#########

第4行:打印输出的第二行

"-2ooa"#"~<.31v!?:-1o"*
 -2ooa"#"~<              pops 0, prints newline, "#", then decrements length by 2
"                   o"*  prints space (* is supposed to be space char)
                  -1     decrements top of stack
           .31v!?:       changes direction to down if top of stack is 0, else jumps back to "

堆: [0,9,9,#, ,t,u,p,n,i, ,#]

输出(*代表空格):

#########
#*******

第5行:打印输出的第三行

7v?=3loroo"#"a<.4
        oo"#"a<    prints "#",newline
       r           reverses stack
7v?=3lo        .4  outputs until stack has 3 values, then changes direction to down

堆: [9,9,0]

输出:

#########
#       #
# input #

第6行:设置为输出第四行和第五行

.>";^"e3pa2p093
 >";^"           push ";",then "^"
      e3p        place "^" as the fifteenth character on line 4
         a2p     place ";" as the eleventh character on line 3
            0    push a value (value doesn't matter -- it will be deleted)
.            93  jump to the tenth character on line 4

堆: [0,9,9,0]


第4行:打印输出的第四行

"-2ooa"#"~<.31^!?:-1o"*
   ooa"#"~<              delete 0 (unnecessary value pushed), then print newline,"#"
 -2                      subtract two from value on top of stack (length)
"          .31^!?:-1o"*  print space until top of stack is 0, then change direction to up

堆: [0,9,0]

输出(*代表空格):

#########
#       #
# input #
#*******

第三行:打印输出的最后一行

"#"o1-:?!;02.>~a"#"oo
             >~a"#"oo  pop top of stack, print "#", newline
"#"o1-:?!;02.          print "#" until top of stack is 0, then terminate

堆: [0,0]

输出:

#########
#       #
# input #
#       #
#########

1

PHP,93 91字节

$b=str_pad("",$e=strlen($s=" $argv[1] "));echo$h=str_pad("",2+$e,"#"),"
#$b#
#$s#
#$b#
$h";

从命令行参数获取输入;转义空格或使用单引号。用运行-r


1

Pyke(非竞争性),6个字节

.X".X#

在这里尝试!

Pyke是在挑战之后写的,因此是非竞争性的。

.X"    - surround string in spaces
   .X# - surround string in hashes

.X接受一个字符串和一个字符串常量arg,并用该组字符将字符串包围。常数arg最多可以包含8个字符,并且对字符串的环绕方式有不同的影响。


1

05AB1E,26字节,不竞争

g4+'#ש,¹' .ø'#.ø,®,

在线尝试!

编辑:哇!没注意到这是一个古老的挑战!抱歉!


1

C#-142字节(方法主体为104)

class P{static void Main(string[]a){for(int i=0;++i<6;)System.Console.Write("#{0}#\n",i==3?$" {a[0]} ":new string(" #"[i%2],a[0].Length+2));}}

取消高尔夫:

class P
{
    static void Main(string[] a)
    {
        for (int i = 0; ++i < 6;)
            System.Console.Write("#{0}#\n", i == 3 ? $" {a[0]} " : new string(" #"[i%2], a[0].Length + 2));
    }
}

0

PowerShell,84 82字节

$l=$input.length+4;$p='#'*$l;$s=' '*$l;$p,$s,"  $input  ",$s,$p-replace'^ | $','#'

0

Lua,90个字节

a=arg[1]h="#"s=" "t="\n"..h c=h:rep(#a+4)..t..s:rep(#a+2)..h..t..s print(c..a..c:reverse())

0

Ruby,83个字节

我想它可能会进一步打高尔夫球,但是由于还没有Ruby的答案,所以这里是:

s=ARGV[0]
n=s.size
r="#"*(n+4)
t="\n#"+" "*(n+2)+"#\n"
puts r+t+"\n# "+s+" #\n"+t+r

0

球拍172字节

(λ(s)(let((n(string-length s))(p #\space)(g(λ(l c)(make-string l c))))(display(string-append
(g(+ 4 n)#\#)"\n#"(g(+ 2 n)p)"#\n# "s" #\n#"(g(+ 2 n)p)"#\n"(g(+ 4 n)#\#)))))

取消高尔夫:

(define (f s)
  (let ((n (string-length s))
        (p #\space)
        (g (λ (l c) (make-string l c))))
    (display (string-append (g (+ 4 n) #\#)
                            "\n#"
                            (g (+ 2 n) p)
                            "#\n# "
                            s
                            " #\n#"
                            (g (+ 2 n) p)
                            "#\n"
                            (g (+ 4 n) #\#)
                            ))))

测试:

(f "This is a test" )

输出:

##################
#                #
# This is a test #
#                #
##################

0

C#,116110字节

s=>{string r="\n".PadLeft(s.Length+5,'#'),p="#".PadRight(s.Length+3,' ')+"#\n";return r+p+"# "+s+" #\n"+p+r;};

取消高尔夫:

s=>
{
    string r = "\n".PadLeft(s.Length + 5, '#'),         //the first line, made of '#'s
        p = "#".PadRight(s.Length + 3, ' ') + "#\n";    //the second line
    return r + p + "# " + s + " #\n" + p + r;           //output is simmetrical
};

初始版本:

s=>{int l=s.Length;string r=new string('#',l+4)+"\n",p="#"+new string(' ',l+2)+"#\n";return r+p+"# "+s+" #\n"+p+r;};

完整的测试用例程序:

using System;

namespace SurroundStringWithHashes
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string,string>f= s=>{int l=s.Length;string r=new string('#',l+4)+"\n",p="#"+new string(' ',l+2)+"#\n";return r+p+"# "+s+" #\n"+p+r;};

            Console.WriteLine(f("Hello World"));
            Console.WriteLine(f("Programming Puzzles & Code Golf"));
        }
    }
}

使用var代替string
伊西(Yytsi)

在这种情况下无济于事,因为我有2个字符串,每个var关键字仅允许一个声明。
adrianmp

糟糕,没有看到第二个:D
Yytsi '16

0

C(gcc)165字节

f(*s){i,j;c='#';for(j=0;j<5;j++){if(j==0||j==4){for(i=0;i<strlen(s)+2;i++)printf("#");}else if(j==2) printf("#%s#",s);else printf("#%*c",(strlen(s)+1),c);puts("");

非高尔夫版本

void  f(char *s)
{
    int i,j;
    char c='#';

    for(j=0;j<5;j++)
    { 
       if(j==0||j==4)
       { 
         for(i=0;i<strlen(s)+2;i++)
           printf("#");
       }
       else
        if(j==2)
         printf("#%s#",s);
      else
        printf("#%*c",(int)(strlen(s)+1),c);

   puts("");
}

0

SmileBASIC,73个字节

LINPUT S$L=LEN(S$)+2O$="#"+" "*L+"#
T$="#"*(L+2)?T$?O$?"# ";S$;" #
?O$?T$
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.