显示树木年轮的年龄


24

介绍

昨天我看到一个生日难题。恭喜!!

同样在这周,我看了电视节目《骨头》中的一集,其中发现一具尸体被埋在树下。为了计算死亡时间,他们计算了年轮。

之所以形成年轮,是因为树木在冬季生长较慢,在夏季生长较快。因此,您可以通过计算环数来计算树的年龄。您还可以看到自然事件,例如雨季或旱季。

在此处输入图片说明

挑战

给定一个整数n >= 1作为输入,编写一个完整的程序以输出树龄轮。

由于环的形状会发生变化,因此请使用三个不同的字符(“ 0”,“ *”,“ +”)来显示气候周期。

1岁

0

2岁

***
*0*
***

3岁

+++++
+***+
+*0*+
+***+
+++++

4岁

0000000
0+++++0
0+***+0
0+*0*+0
0+***+0
0+++++0
0000000

树的大小是边的平方 2*n - 1

获奖

以字节为单位的最短代码获胜。


年龄= 5呢?
蓝色

3
戒指有一个三步循环。('0', '*', '+')所以5年是*
胡安·卡洛斯·奥罗佩萨

@vihan不明白这个问题。
Juan Carlos Oropeza 2015年

@vihan对不起,仍然不明白如何除以二来解决问题。如果您有解决的办法,我可能就不知道了
Juan Carlos Oropeza

大小是每边的面积,周长还是长度?
Beta Decay 2015年

Answers:


6

K5,27个 30 26 25 22字节

"0"{4(|+y,)/x}/"0*+"3!1_!

此方法"0"使用其他字符({4(|+y,)/x})迭代地在所有四个面上“包装”一个核心(以开头)。季节性换行的顺序由模3(3!)顺序确定。使基本情况恰到好处地排列有点奇怪。

编辑:

"0*+"3!u|\:u:t,1_|t:|!

此替代方案可通过从提供的排他范围(!)中反转并在放置项目(t,1_|t:|)后与其自身连接的方式一次构建整个矩形阵列。然后,我们采用笛卡尔乘积最大值(u|\:u:),将整个矩阵取模3(3!),并索引到字符数组中。

实际上:

  "0*+"3!u|\:u:t,1_|t:|!1
,,"0"

  "0*+"3!u|\:u:t,1_|t:|!3
("+++++"
 "+***+"
 "+*0*+"
 "+***+"
 "+++++")

  "0*+"3!u|\:u:t,1_|t:|!5
("*********"
 "*0000000*"
 "*0+++++0*"
 "*0+***+0*"
 "*0+*0*+0*"
 "*0+***+0*"
 "*0+++++0*"
 "*0000000*"
 "*********")

我不知道K,但这是一个完整的程序,而不仅仅是一个函数吗?
Alex A.

它既是一个完整的程序,又是一个功能。这是所谓的“默认定义”的示例。无论如何,这种区别是极其任意的。
JohnE 2015年

11

BBC Basic,93个字节

1I.r:r=r-1:F.i=-r TOr:F.j=-r TOr:p=ABS(i):q=ABS(j):IFp<q TH.p=q
2V.48-(p MOD3)*6MOD7:N.:P.:N.

缩写关键字在这里有很大帮助。在第2行中,我使用VDU命令(等效于C putchar())来打印每个字符。这比高效得多P.MID$("0*+",p MOD3+1,1)

在Mac上,它在BeebEm3中运行:

在此处输入图片说明


您如何创建该gif?
Juan Carlos Oropeza

9
@JuanCarlosOropeza效率不高。我使用QuickTime Player捕获屏幕,使用QuickTime Player 7将视频导出为PNG图像,使用GraphicConverter将其转换为GIF,并使用ezgif.com来优化结果。
吱吱作响的ossifrage

7

CJam,25个字节

q~,_1>W%\+_ff{e>"0*+"=}N*

在这里测试。

说明

q~,       e# Read input N, turn into range [0 1 ... N-1]
_1>       e# Duplicate and cut off the zero.
W%        e# Reverse.
\+        e# Prepend to original range to give [N-1 ... 1 0 1 ... N-1]
_         e# Duplicate
ff{       e# Nested map for each pair of elements in that array.
  e>      e# Take the maximum, i.e. chessboard distance from the centre.
  "0*+"=  e# Select the right character using cyclic indexing into this string.
}
N*        e# Join the lines with line feeds.

5

Matlab,63个字节

n=input('')-1;x='0*+';t=abs(-n:n);x(mod(bsxfun(@max,t,t'),3)+1)

例:

>> n=input('')-1;x='0*+';t=abs(-n:n);x(mod(bsxfun(@max,t,t'),3)+1)
5
ans =
*********
*0000000*
*0+++++0*
*0+***+0*
*0+*0*+0*
*0+***+0*
*0+++++0*
*0000000*
*********

5

Python 2,83字节

I=n=input()
while I+n-1:I-=1;i=abs(I);w=("O*+"*n)[i:n];print w[::-1]+w[0]*2*i+w[1:]

逐行打印。每行分为三部分:

  • 左循环部分,包括第一个重复的char。
  • 重复中心部分
  • 正确的自行车零件。

对于n=4

0    000000    
0+    ++++    0
0+*    **    +0
0+*0        *+0
0+*    **    +0
0+    ++++    0
0    000000    

我们反向生成左侧部分w,将其克隆为最后一个字符2*i时间,然后添加没有第一个字符的原始版本。


5

Python 2,83字节

n=input()
R=range(1-n,n)
for i in R:print''.join('0*+'[max(i,-i,j,-j)%3]for j in R)

如果我们将树视为坐标网格,则处的符号(i,j)max(abs(i),abs(j))%3或等效地确定max(i,-i,j,-j)%3。对于每一行i,我们加入并打印该行中的符号。


您可以通过将range语句直接放在第三行来缩短此时间。
伊桑·布劳

@EthanBrouwer我使用了R两次,它的长度超过5个字符,因此作业胜出。
xnor 2015年

感动!我只看到第一个。我的错。:)
Ethan Brouwer

5

Pyth,23个字节

VK+_StQUQsm@"0*+"eS,dNK

在线尝试:演示

说明:

VK+_StQUQsm@"0*+"eS,dNK   implicit: Q = input number
    StQ                   the list [1, 2, ..., Q-1]
   _                      reverse it [Q-1, ..., 2, 1]
       UQ                 the list [0, 1, ..., Q-1]
  +                       combine them [Q-1, ..., 1, 0, 1, ..., Q-1]
 K                        and store in K
VK                        for each N in K:
          m           K      map each element d in K to:
                 eS,dN          the maximum of d and N
           @"0*+"               and pick the corresponded char (modulo 3)
         s                   join the chars to a string and print

3

MATLAB,80 78 73字节

感谢Luis Mendo帮助我剃除5个字节!

A=eye(2*input('')-1);a='0*+';a(mod(bwdist(A.*rot90(A),'chessboard'),3)+1)

>> A=eye(2*input('')-1);a='0*+';a(mod(bwdist(A.*rot90(A),'chessboard'),3)+1)

5

ans =

*********
*0000000*
*0+++++0*
*0+***+0*
*0+*0*+0*
*0+***+0*
*0+++++0*
*0000000*
*********

取消高尔夫和代码说明

%// Accepts an integer n from the user and creates a 2*n - 1 x 2*n - 1 identity matrix
A=eye(2*input('')-1);

%// Creates an array of three characters to print each level of the ring
a='0*+';

%// By taking the identity matrix and element-wise multiplying with its 90 degree rotated 
%// version of itself, this creates a zero matrix except for the centre most
%// value, which is 1
%// This takes the distance transform via the chessboard / Chebyshev distance
%// from the centre element
%// This mirrors what "level" each square would be at
%// 1: https://en.wikipedia.org/wiki/Distance_transform
%// 2: https://en.wikipedia.org/wiki/Chebyshev_distance
b = bwdist(A.*rot90(A),'chessboard');

%// Because each level cycles through each of the characters in the
%// character array a, we need to perform a mod operation so that
%// all of the values cycle from 1 to 3
%// This changes the distance transform output so that we range
%// from 1 to 3 instead
c = mod(b,3) + 1;

%// The values in the matrix c correspond exactly to the locations
%// we need to sample from the array a and we display our result
a(c)

次音符

bwdist是图像处理工具箱中一部分的功能,只能在MATLAB中运行。八度(IIRC)bwdist尚未实现,因此无法在八度中运行。


您可以保存一些字节:使用eye逐个元素的rot90'ed版本并将其相乘以生成“种子”矩阵:I=eye(2*input('')-1);a='0*+';a(mod(bwdist(I.*rot90(I),'chessboard'),3)+1)
Luis Mendo

太酷了!感谢@LuisMendo
rayryeng-恢复莫妮卡

2

Python 2,134字节

def l(x,c=1):
 p="\n\x1b[%d"%c;d=p+";%dH"%c
 if x:s=x*2-1;d+=(p+"G").join(["0*+"[(x+1)%3]*s]*s)+l(x-1,c+1)
 return d
print l(input())

2

Perl,118个字节

还有更多工作要做,但现在还只是基本版本。现在具有美味的额外规格依从性。

for$i(0..($-=<>-1)){substr$a[$_],$i,$}=2*($--$i)+1,(0,'*','+')[($--$i)%3]x$}for$i..$-}$,=$/;print@a,reverse@a[0..$--1]

用法:

perl -e 'for$i(0..($-=<>-1)){substr$a[$_],$i,$}=2*($--$i)+1,(0,'*','+')[($--$i)%3]x$}for$i..$-}$,=$/;print@a,reverse@a[0..$--1]' <<< 9
+++++++++++++++++
+***************+
+*0000000000000*+
+*0+++++++++++0*+
+*0+*********+0*+
+*0+*0000000*+0*+
+*0+*0+++++0*+0*+
+*0+*0+***+0*+0*+
+*0+*0+*0*+0*+0*+
+*0+*0+***+0*+0*+
+*0+*0+++++0*+0*+
+*0+*0000000*+0*+
+*0+*********+0*+
+*0+++++++++++0*+
+*0000000000000*+
+***************+
+++++++++++++++++

1

Matlab 92

input('')-1;x=ones(2*n+1,1)*abs(-n:n);z=mod(max(x,x'),3);z(z>1)=2;z(z<1)=7;disp([z+41,''])

1

Sed,277252个字符

(251个字符的代码+ 1个字符的命令行选项。)

期望以一元格式输入。

:m
s/1/0/
s/1/*/
s/1/+/
tm
h
s/^/:/
:r
s/(.*):(.)/\2\1:/
tr
s/://
G
s/\n.//
h
:
/^(.)\1*$/ba
s/(.)(.)(\2*)\1/\1:\2\3:\1/
:c
s/(:_*)[^_](.*:)/\1_\2/
tc
:u
s/(.)(:\1*)_/\1\2\1/
tu
s/://g
H
b
:a
g
s/[^\n]+/:/
:f
s/(.*):(\n[^\n]+)/\2\1:/
tf
s/://
G
s/\n//

样品运行:

bash-4.3$ sed -rf treering.sed <<< 1
0

bash-4.3$ sed -rf treering.sed <<< 11
***
*0*
***

bash-4.3$ sed -rf treering.sed <<< 111
+++++
+***+
+*0*+
+***+
+++++

bash-4.3$ sed -rf treering.sed <<< 1111
0000000
0+++++0
0+***+0
0+*0*+0
0+***+0
0+++++0
0000000

0

JavaScript(ES6),114

使用警告输出-比例字体错误,结果丑陋。在下面的代码段中,警报重定向到被剪切的正文,从而获得更好的结果。反引号内的换行符很重要且已计算在内。

测试在Firefox中运行该代码段。

/* Redefine alert for testing purpose */ alert=x=>O.innerHTML=x;

[...'*+0'.repeat(n=prompt()-1)].map((c,i)=>i<n?b=[z=c.repeat(i-~i),...b,z].map(r=>c+r+c):0,b=[0]);alert(b.join`
`)
<pre id=O></pre>


我尝试运行代码片段,但是什么也没有发生。不知道那是因为我在chrome中打开堆栈溢出吗?
Juan Carlos Oropeza

@JuanCarlosOropeza也许就是那个。我已经写过:Test running the snippet in Firefox但是显然我只是在开玩笑,Chrome(Chrome的无版本)不符合EcmaScritpt 6,缺少=>功能。
edc65

@JuanCarlosOropeza我必须纠正自己。Chrome的最新版本具有箭头功能,但不了解扩展运算符...。距离ES6仍很远
edc65

0

Ruby,85个字符

puts (m=0..(n=gets.to_i-1)*2).map{|i|m.map{|j|"0*+"[[(i-n).abs,(j-n).abs].max%3]}*""}

样品运行:

bash-4.3$ ruby -e 'puts (m=0..(n=gets.to_i-1)*2).map{|i|m.map{|j|"0*+"[[(i-n).abs,(j-n).abs].max%3]}*""}' <<< 4
0000000
0+++++0
0+***+0
0+*0*+0
0+***+0
0+++++0
0000000


0

C,138字节

j,k,l;t(i){l=2*i;char*c=calloc(l,l);memset(c,10,l*(l-2));for(;k<i;++k)for(j=k;j<l-1-k;)memset(c+j++*l+k,"+0*"[(i-k)%3],l-2*k-1);puts(c);}

函数t采用一个整数参数-年龄。

取消高尔夫(具有main轻松运行以上功能的功能):

#include "stdlib.h" /* calloc - only necessary for 64-bit system */
j,k,l;t(i)
{
    l=2*i;
    char*c=calloc(l,l);
    memset(c,10,l*(l-2)); /* fill with '\n' */
    for(;k<i;++k)for(j=k;j<l-1-k;)memset(c+j++*l+k,"+0*"[(i-k)%3],l-2*k-1);
    puts(c);
}

main(int c,char**v)
{
    t(atoi(v[1]));
}

stdlib.h可能有必要在某些系统上,因为没有它的未申报函数的返回类型calloc将默认为int。由于intchar*的大小不一定相同,因此可以将无效的指针写入c。在大多数32位系统都char*int具有相同的大小,但是这不是64位系统真。

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.