JavaScript:261 个字符(函数)
function f(s,w,h){for(y=h,g=s*2;y--;){for(i=0,o=[];i<s;i++)for(x=0,o[i]=[],o[i+s]=[];x<w;x++){o[i][s-i-1+g*x]='/';o[i][s-i+i*2+g*x]='\\';o[i+s][g*x+i]='\\';o[i+s][g+g*x-i-1]='/'}for(a=0,z='';a<g;a++,console.log(z),z='')for(b=0;b<g*w;b++)z+=o[a][b]?o[a][b]:' '}}
JavaScript:281 个字符(带有标准输入/输出的 Rhino 脚本)
a=arguments;s=+a[0];w=+a[1];h=+a[2];for(y=h,g=s*2;y--;){for(i=0,o=[];i<s;i++)for(x=0,o[i]=[],o[i+s]=[];x<w;x++){o[i][s-i-1+g*x]='/';o[i][s-i+i*2+g*x]='\\';o[i+s][g*x+i]='\\';o[i+s][g+g*x-i-1]='/'}for(a=0,z='';a<g;a++,print(z),z='')for(b=0;b<g*w;b++)z+=o[a]?o[a][b]?o[a][b]:' ':' '}
可读犀牛版本:
size = +arguments[0];
width = +arguments[1];
height = +arguments[2];
for (y = 0; y < height; y++) {
o = [];
for (i = 0; i < size; i++) {
// Will draw the top and bottom halves of each diamond row
// in a single pass. Using array o[] to store the data:
o[i] = [];
o[i + size] = [];
for (x = 0; x < width; x++) {
// Draw the top half of the diamond row:
o[i][(size - i - 1) + (size * 2 * x)] = '/';
o[i][(size - i) + (i * 2) + (size * 2 * x)] = '\\';
// Draw the bottom half of the diamond row:
o[i + size][(size * 2 * x) + i] = '\\';
o[i + size][(size * 2) + (size * x * 2) - i - 1] = '/';
}
}
// Output the full diamond row to console from array o[]:
for (a = 0; a < size * 2; a++) {
z = "";
for (b = 0; b < size * 2 * width; b++) {
z += o[a] ? o[a][b] ? o[a][b] : ' ' : ' ';
}
print(z);
}
}
测试用例:
java org.mozilla.javascript.tools.shell.Main diamonds.js 4, 3, 2
/\ /\ /\
/ \ / \ / \
/ \ / \ / \
/ \/ \/ \
\ /\ /\ /
\ / \ / \ /
\ / \ / \ /
\/ \/ \/
/\ /\ /\
/ \ / \ / \
/ \ / \ / \
/ \/ \/ \
\ /\ /\ /
\ / \ / \ /
\ / \ / \ /
\/ \/ \/
java org.mozilla.javascript.tools.shell.Main diamonds.js 2, 6, 1
/\ /\ /\ /\ /\ /\
/ \/ \/ \/ \/ \/ \
\ /\ /\ /\ /\ /\ /
\/ \/ \/ \/ \/ \/
java org.mozilla.javascript.tools.shell.Main diamonds.js 1, 1, 1
/\
\/