首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASCII冰淇淋

ASCII冰淇淋
EN

Code Golf用户
提问于 2015-03-02 17:08:25
回答 1查看 1.7K关注 0票数 15

编写一个接受正整数N的程序或函数,打印或返回N×padding艺术字符串,其上半部分是由('s组成的半圆,下半部是由V's组成的向下三角形,空格用作填充。

换句话说,制作一个ASCII冰淇淋筒:(N=17输出)

代码语言:javascript
复制
      (((((      
    (((((((((    
  (((((((((((((  
  (((((((((((((  
 ((((((((((((((( 
 ((((((((((((((( 
(((((((((((((((((
(((((((((((((((((
VVVVVVVVVVVVVVVVV
 VVVVVVVVVVVVVVV 
  VVVVVVVVVVVVV  
   VVVVVVVVVVV   
    VVVVVVVVV    
     VVVVVVV     
      VVVVV      
       VVV       
        V        

示例

下面是N=1到5的输出。注意,对于奇数N,三角形总是较大的一半。

代码语言:javascript
复制
V

((
VV

(((
VVV
 V 

 (( 
((((
VVVV
 VV 

 ((( 
(((((
VVVVV
 VVV 
  V  

这是一个N= 101的巴斯特宾.

下面是一个未使用的Python 3引用实现:

代码语言:javascript
复制
N = int(input())
ic = [[' '] * N for _ in range(N)]
for y in range(N//2):
    for x in range(N):
        if (x - (N - 1) / 2)**2 + (y - (N - 1) / 2)**2 < (N / 2)**2:
            ic[y][x] = '('
for y in range(N//2, N):
    for x in range(y - N//2, N - (y - N//2)):
        ic[y][x] = 'V'
for line in ic:
    print(''.join(line))

详细信息

  • 接受来自stdin、命令行或函数参数的输入。输出到stdout或类似的,否则,如果您编写函数,则可能返回字符串。
  • 圆锥部分应该与所有N的参考实现完全匹配。
  • 冰淇淋部分不需要完全匹配参考实现,只要它显然是一个半圆形状的所有N (这是这样,您不必担心半圆由于舍入误差的微小差异)。
  • 不应有任何不必要的前导空间,但可能有多余的尾随空间。
  • 输出可以选择性地包含尾随换行符。
  • 您可以选择使用其他3个不同的可打印ASCII字符来代替(V和空格。

评分

最短的提交单位字节获胜。平局是最古老的投稿。

EN

回答 1

Code Golf用户

回答已采纳

发布于 2015-03-02 21:54:47

CJam,46字节

在网上试试。

代码语言:javascript
复制
{:Z{Z{Z(2./:R-zYR<):P#YR-zP#+Z2./P#>SP?}/N}fY}

我认为这目前完全模仿了最初的规范,这是我开始生成这个答案时所需要的。可能会通过使数学不太精确到原始规范来节省几个字节,但是在我找到一种方法来保存超过一个或两个字节之前,我将保持原样。

解释

代码语言:javascript
复制
{               "Begin block";
  :Z{             "For each y from 0 to input-1";
    Z{              "For each x from 0 to input-1";
      Z(2./:R         "Calculate the radius as (input-1)/2.0";
      -z              "Calculate the horizontal distance from the center";
      YR<):P          "Calculate the power to raise distances to: (y<radius)+1
                       (This results in Euclidean distance being calculated for
                        the ice cream and Manhattan distance being calculated
                        for the cone)";
      #               "Raise the horizontal distance to the determined power";
      YR-zP#          "Calculate the vertical distance from the center and
                       raise it to the determined power";
      +               "Add the horizontal and vertical distances";
      Z2./P#          "Calculate the solid distance threshold and raise it to
                       the determined power";
      >SP?            "If the solid threshold is exceeded, produce a space;
                       otherwise, produce the determined power digit
                       (This results in ice cream being represented by the
                        digit '2' and the cone by the digit '1')";
    }/              "End x loop";
    N               "Produce a new line";
  }fY             "End y loop";
}               "End block";
票数 7
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/47255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档