# 棋盘上的棋子数

14

### 例子

``````STDIN:  3
STDOUT: 2

STDIN:  4
STDOUT: 2

STDIN:  5
STDOUT: 5

STDIN:  6
STDOUT: 6

STDIN:  8
STDOUT: 12
``````

### 规则

• 您提交的内容必须是一个程序或函数等，它需要一个整数并输出或返回板上的棋子数
• 您可以放心地假设输入是非负整数> 2
• 这是，因此字节最少的程序将获胜！
• 请注意，板左下方的正方形始终是深色的。作品只能放在深色方块上
• 你必须整整地占据一排

3

lirtosiast 2015年

5

# Par，8字节

``````✶″½↓┐*½┐
``````

## 说明

``````               ## [implicit: read line]      Example
✶              ## Convert to number           7
″              ## Duplicate                   7 7
½              ## Divide by two               7 3.5    half the board
↓              ## Minus one                   7 2.5    leave one row empty
┐              ## Ceiling                     7 3      a whole number of rows
*              ## Multiply                    21       total number of spaces
½              ## Divide by two               10.5     only the blue squares
┐              ## Ceiling                     11       starts with blue, so round up
``````

12

## 六边形，19字节

``````?({{&2'2':{):!/)'*/
``````

### 说明

``````?({2':{)'*){&2':!:&?':
``````

``````?  Read an integer N from STDIN into edge A.
(  Decrement to get N-1.
{  Move the MP forwards onto edge B.
2  Set the edge to 2.
'  Move the MP backwards onto edge C.
:  Divide edge A by edge B (rounding down) to compute (N-1)/2.
{  Move the MP forwards onto edge A.
)  Increment to restore value of N.
'  Move the MP backwards onto edge B.
*  Multiply edges A and C to compute N*(N-1)/2.
)  Increment to compute N*(N-1)/2 + 1.
{  Move the MP forwards onto edge C.
&  This actually a copy operation, but we use it to reset the edge to zero.
2  Set the edge to 2.
'  Move the MP backwards onto edge A.
:  Divide edge B by edge C to compute (N*(N-1)/2 + 1)/2.
!  Output the result as an integer. We're basically done now.
:  no-op (we already have this value)
&  Copy either B or C into A (doesn't matter).
?  Read a zero (EOF) into A.
'  Move the MP backwards onto an unused cell.
:  Divide some unused cell by A (which is zero), terminating with an error.
``````

{{将内存边缘移动了两次，因此第二行中的＆似乎什么也没做？两个

@Eumel这不是执行代码的顺序。在第一个之后`{`，IP跳到`2`左上角的。在`)`右上角的IP 之后，IP跳到左下角的IP `'`。然后，IP以怪异的循环环绕方式遍历线2和4。
Martin Ender

Eumel

@Eumel确实如此。这就是Hexagony中代码边缘的包装方式。
Martin Ender

8

## CJam，10个字节

``````ri_(2/*)2/
``````

### 说明

``````ri   e# Read input and convert to integer N.
_    e# Duplicate N.
(2/  e# Decrement, integer divide by two, to determine the number of rows that can be used.
*    e# Multiply by the input to determine the number of cells that can be used.
)2/  e# Increment, integer divide by two, which essentially ceil()s the result of the
e# division.
``````

8

## 迷宫，11字节

Woohoo，仅落后CJam一个字节。

``````?:(#/*)_2/!
``````

``````? reads an integer value.
: duplicates the result.
( decrements it.
# pushes the stack depth which happens to be 2.
/ is integer division.
* is multiplication.
) increments the result.
_ pushes a 0.
2 turns it into a 2.
/ is once again integer division.
! prints the result as an integer.
``````

5

## 严重的是，8个字节

``````,;D½L*½K
``````

5

# Python 2中，22 21个字节

``````lambda n:~-n/2*n+1>>1
``````

3

# JavaScript，37 35字节

``alert(((n=prompt()/2)-.5|0)*n+.5|0)``

## 说明

``````var n = parseInt(prompt());
var result = Math.ceil(Math.floor((n - 1) / 2) * n / 2);

3

# 直流电，12

``````?d1-2/*1+2/p
``````

### 测试输出：

``````\$ for t in 3 4 5 6 8; do echo \$t | dc -e?d1-2/*1+2/p; done
2
2
5
6
12
\$
``````

3

# Pyth，9个字节

``````/h*/tQ2Q2
``````

3

# Japt，16 14字节

``````U-1>>1 *U+1>>1
``````

### 怎么运行的

``````         // Implicit: U = input number
U-1>>1   // Subtract 1 from U and integer divide by 2.
*U+1>>1  // Multiply the result by U, add 1, and integer divide by 2.
// Implicit: output last expression
``````

``````U*½-½|0 *U*½+½|0
``````

3

# Java中，230 155 52

``int f(int s){return(int)Math.ceil(s*((s-1)/2)/2.0);}``

``````public class NumberOfPiecesOnACheckersBoard {

public static void main(String[] args) {
// @formatter:off
int[][] testData = new int[][] {
{3, 2},
{4, 2},
{5, 5},
{6, 6},
{8, 12}
};
// @formatter:on

for (int[] data : testData) {
System.out.println("Input: " + data[0]);
System.out.println("Expected: " + data[1]);
System.out.print("Actual:   ");
System.out.println(new NumberOfPiecesOnACheckersBoard().f(data[0]));
System.out.println();
}
}

// Begin golf
int f(int s) {
return (int) Math.ceil(s * ((s - 1) / 2) / 2.0);
}
// End golf

}``````

``````Input: 3
Expected: 2
Actual:   2

Input: 4
Expected: 2
Actual:   2

Input: 5
Expected: 5
Actual:   5

Input: 6
Expected: 6
Actual:   6

Input: 8
Expected: 12
Actual:   12``````

`throws Exception`是允许的。

1
OP允许的功能。
lirtosiast

Darrel Hoffman

2

## Zilog ez80机器码，9个字节

``````6C 2D CB3D ED6C 2C CB3D
``````

``````ld l,h
dec l
srl l
mlt hl
inc l
srl l
``````

Zilog ez80是具有8位累加器和24位寄存器的8位处理器。与z80不同，它具有`mlt`（8位乘法）指令，该指令在16位模式下将寄存器对的高字节和低字节相乘`hl`，然后存储回`hl`

2

## TI-BASIC，13个字节

``````⁻int(⁻.5Ansint(Ans/2-.5
``````

TI-BASIC的隐式乘法有帮助，但它没有整数除法。`⁻int(⁻X`是ceil（x）的较短形式。

2

# vba，46岁

``````Function f(x)
f=(((x-1)\2)*x+1)\2
End Function
``````

2

## Pyth，17 14 13字节

-3个字节，感谢Ypnypn！重新排列*运算符的数字以节省1个字节。

``````/+*Q-/Q2-1%Q2 1 2 (original)
/h*Q-/Q2!%Q2 2
/h*-/Q2!%Q2Q2
``````

``````/h*-/Q2!%Q2Q2
%Q2   Check if the number is odd
!      Logical not to make 1 if even and 0 if odd
/Q2       n/2
-          n/2-1 if even, and n/2 if odd
*        Q  n*(n/2-1) if even, n*(n/2) if odd
/           2 Divide the result by two.
``````

2

## Javascript，33个字节

``````a=prompt();alert(a*(a-1>>1)+1>>1)
``````

``````a=>a*(a-1>>1)+1>>1
``````

2

# MATLAB，37 25字节

``````@(a)ceil(fix(a/2-.5)*a/2)
``````

2

## 视网膜，18字节

``````11(..?\$)?
\$_
11?
1
``````

Retina的最新版本（比该挑战要新）可以处理四个附加字节的十进制I / O：

``````.+
\$*
11(..?\$)?
\$_
11?
``````

``````11(..?\$)?
\$_
11?
``````

### 说明

``````11(..?\$)?
\$_
``````

``````11?
1
``````

1

# Python 3，39个字节

``````n=int(input());print(((n-1)//2*n+1)//2)
``````

1

# Prolog，39岁 38字节

``````p(N):-X is ((N-1)//2*N+1)//2,write(X).
``````

``````Subtract 1 from input and integer divide by 2 to get number of rows available.
Multiply that number by input to get number of squares available.
Add one and integer divide by 2 to round up, since at at least half the rows
will have a checker at the first square.
Print.
``````

``````p(8).
12
``````

1

# 腮腺炎，17字节

``````R I W I-1\2*I+1\2
``````

``````R I W !,I-1\2*I+1\2,!
``````

1

## Bash，32个字节

``````read a;echo \$((a*(a-1>>1)+1>>1))
``````

1

## Pyke，8字节，无竞争

``````Dt2f*h2f
``````

dup，dec，一半，mult，inc，一半

1

## 批处理，30个字节

``````@cmd/cset/a(%1*((%1-1)/2)+1)/2
``````

``````@set/pa=
@cmd/cset/a(a*((a-1)/2)+1)/2
``````