Switch branches/tags
Nothing to show
Fetching contributors…
Cannot retrieve contributors at this time
141 lines (110 sloc) 3.65 KB

# 5.2 节的练习

### 5.2.1

#### 解答

```[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 1, 2, 3, 5, 4, 6, 7, 8, 9 ],
[ 1, 2, 4, 3, 5, 6, 7, 8, 9 ],
[ 1, 3, 2, 4, 5, 6, 7, 8, 9 ],
[ 1, 3, 2, 5, 4, 6, 7, 8, 9 ],
[ 1, 3, 5, 2, 4, 6, 7, 8, 9 ],
[ 2, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 2, 1, 3, 5, 4, 6, 7, 8, 9 ],
[ 2, 1, 4, 3, 5, 6, 7, 8, 9 ],
[ 2, 4, 1, 3, 5, 6, 7, 8, 9 ]```

### 5.2.2

1. int a, b , c
2. float w, x, y, z

1. int a, b, c

### 5.2.3

1. A.s = B.i + C.s
2. A.s = B.i + C.s , D.i = A.i + B.s
3. A.s = B.s + D.s
4. ! A.s = D.i , B.i = A.s + C.s , C.i = B.s , D.i = B.i + C.i

1. 否， ？
2. 否， 是
3. 是， 是
4. 否， 否

### 5.2.4 ！

```S -> L.L|L
L -> LB|B
B -> 0|1```

#### 解答

1) S -> L_1.L_2 L_1.side = 1
L_2.side = -1
S.val = parseBin2Int(L_1.val, L_1.side) + parseBinary2Int(L_2.val, L_2.side)
2) S -> L L.side = 1
S.val = parseBin2Int(L.val, L.side)
3) L -> L_1B L_1.side = L.side
L.val = L_1.val "+" B.val
4) L -> B L.val = B.val
5) B -> 0 B.val = 0
6) B -> 1 B.val = 1

```function parseBin2Int(bin, side) {
var rt = 0
var len = bin.length
for (var i = 0; i < len; i++){
if (side === 1) {
rt = rt * 2 + bin[i]
} else {
rt = rt + bin[i] * 2^(-i-1)
}
}
}```