Skip to content

Commit e248049

Browse files
authored
Merge pull request #218 from ikesnowy/Day95
Add Comment
2 parents 794d660 + a5db343 commit e248049

File tree

13 files changed

+71
-51
lines changed

13 files changed

+71
-51
lines changed

1 Fundamental/1.4/1.4.24/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace _1._4._24
1414
*/
1515
class Program
1616
{
17-
static int F = 100;//需要寻找的 F 值
17+
static int F = 100;// 需要寻找的 F 值
1818
struct testResult
1919
{
20-
public int F;
21-
public int BrokenEggs;
20+
public int F;// 找到的 F 值。
21+
public int BrokenEggs;// 打碎的鸡蛋数。
2222
}
2323
static void Main(string[] args)
2424
{
@@ -27,12 +27,12 @@ static void Main(string[] args)
2727
{
2828
building[i] = i;
2929
}
30-
//第一问:二分查找即可
30+
// 第一问:二分查找即可
3131
testResult A = PlanA(building);
3232
Console.WriteLine($"Plan A: F={A.F}, Broken Eggs={A.BrokenEggs}");
3333

34-
//第二问:按照第 1, 2, 4, 8,..., 2^k 层顺序查找,一直到 2^k > F,
35-
//随后在 [2^(k - 1), 2^k] 范围中二分查找。
34+
// 第二问:按照第 1, 2, 4, 8,..., 2^k 层顺序查找,一直到 2^k > F,
35+
// 随后在 [2^(k - 1), 2^k] 范围中二分查找。
3636
testResult B = PlanB(building);
3737
Console.WriteLine($"Plan B: F={B.F}, Broken Eggs={B.BrokenEggs}");
3838
}

1 Fundamental/1.4/1.4.25/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace _1._4._25
1414
*/
1515
class Program
1616
{
17-
static int F = 100;//需要寻找的 F 值
17+
static int F = 100;// 需要寻找的 F 值
1818
struct testResult
1919
{
20-
public int F;
21-
public int BrokenEggs;
22-
public int ThrowTimes;
20+
public int F;// 测试得出的 F 值
21+
public int BrokenEggs;// 碎掉的鸡蛋数。
22+
public int ThrowTimes;// 扔鸡蛋的次数。
2323
}
2424
static void Main(string[] args)
2525
{
@@ -28,13 +28,13 @@ static void Main(string[] args)
2828
{
2929
building[i] = i;
3030
}
31-
//第一问:第一个蛋按照 √(N), 2√(N), 3√(N), 4√(N),..., √(N) * √(N) 顺序查找直至碎掉。这里扔了 k 次,k <= √(N)
32-
//k-1√(N) ~ k√(N) 顺序查找直至碎掉,F 值就找到了。这里最多扔 √(N) 次。
31+
// 第一问:第一个蛋按照 √(N), 2√(N), 3√(N), 4√(N),..., √(N) * √(N) 顺序查找直至碎掉。这里扔了 k 次,k <= √(N)
32+
// k-1√(N) ~ k√(N) 顺序查找直至碎掉,F 值就找到了。这里最多扔 √(N) 次。
3333
testResult A = PlanA(building);
3434
Console.WriteLine($"Plan A: F={A.F}, Broken Eggs={A.BrokenEggs}, Throw Times={A.ThrowTimes}");
3535

36-
//第二问:按照第 1, 3, 6, 10,..., 1/2k^2 层顺序查找,一直到 1/2k^2 > F,
37-
//随后在 [1/2k^2 - k, 1/2k^2] 范围中顺序查找。
36+
// 第二问:按照第 1, 3, 6, 10,..., 1/2k^2 层顺序查找,一直到 1/2k^2 > F,
37+
// 随后在 [1/2k^2 - k, 1/2k^2] 范围中顺序查找。
3838
testResult B = PlanB(building);
3939
Console.WriteLine($"Plan B: F={B.F}, Broken Eggs={B.BrokenEggs}, Throw Times={B.ThrowTimes}");
4040
}

1 Fundamental/1.4/1.4.26/Program.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ class Program
1212
{
1313
static void Main(string[] args)
1414
{
15-
//证明点 A(a, a^3) B(b, b^3) C(c, c^3) 当且仅当 a + b + c = 0 时共线。
15+
// 证明点 A(a, a^3) B(b, b^3) C(c, c^3) 当且仅当 a + b + c = 0 时共线。
1616
//
17-
//若点 A,B,C 共线,直线 AB 斜率必定和直线 BC 斜率相等,列方程有:
18-
//(b^3 - a^3)/(b - a) = (c^3 - b^3)/(c - b)
19-
//用立方差公式化简,有:
20-
//b^2 + ab + a^2 = c^2 + bc + b^2
21-
//消去 b^2,将 c 设为未知数有
22-
//c^2 +bc - a^2 - ab = 0
23-
//用十字相乘法进行因式分解有
24-
//(a + b + c)(c - a) = 0
25-
//解方程有:
26-
//c = -a - b 或 c = a
27-
//因此当 c != a 时,当且仅当 a + b + c = 0 时 A, B, C 三点共线。
28-
//得证。
17+
// 若点 A,B,C 共线,直线 AB 斜率必定和直线 BC 斜率相等,列方程有:
18+
// (b^3 - a^3)/(b - a) = (c^3 - b^3)/(c - b)
19+
// 用立方差公式化简,有:
20+
// b^2 + ab + a^2 = c^2 + bc + b^2
21+
// 消去 b^2,将 c 设为未知数有
22+
// c^2 +bc - a^2 - ab = 0
23+
// 用十字相乘法进行因式分解有
24+
// (a + b + c)(c - a) = 0
25+
// 解方程有:
26+
// c = -a - b 或 c = a
27+
// 因此当 c != a 时,当且仅当 a + b + c = 0 时 A, B, C 三点共线。
28+
// 得证。
2929
}
3030
}
3131
}

1 Fundamental/1.4/1.4.27/StackQueue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/// <typeparam name="Item">队列中的元素。</typeparam>
77
class StackQueue<Item>
88
{
9-
Stack<Item> H;//用于保存出队元素
10-
Stack<Item> T;//用于保存入队元素
9+
Stack<Item> H;// 用于保存出队元素
10+
Stack<Item> T;// 用于保存入队元素
1111

1212
/// <summary>
1313
/// 构造一个队列。
@@ -35,7 +35,7 @@ private void Reverse()
3535
/// <returns></returns>
3636
public Item Dequeue()
3737
{
38-
//如果没有足够的出队元素,则将 T 中的元素移动过来
38+
// 如果没有足够的出队元素,则将 T 中的元素移动过来
3939
if (this.H.IsEmpty())
4040
{
4141
Reverse();

1 Fundamental/1.4/1.4.28/QueueStack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void Push(Item item)
2424
{
2525
this.queue.Enqueue(item);
2626
int size = this.queue.Size();
27-
//倒转队列
27+
// 倒转队列
2828
for (int i = 0; i < size - 1; ++i)
2929
{
3030
this.queue.Enqueue(this.queue.Dequeue());

1 Fundamental/1.4/1.4.29/StackSteque.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ public StackSteque()
2424
/// <param name="item"></param>
2525
public void Push(Item item)
2626
{
27+
ReverseT();
2728
this.H.Push(item);
2829
}
2930

3031
/// <summary>
3132
/// 将 T 中的元素弹出并压入到 H 中。
3233
/// </summary>
33-
private void Reverse()
34+
private void ReverseT()
3435
{
3536
while (!this.T.IsEmpty())
3637
{
@@ -39,16 +40,23 @@ private void Reverse()
3940
}
4041

4142
/// <summary>
42-
/// 从 Steque 中弹出一个元素
43+
/// 将 H 中的元素弹出并压入到 T 中
4344
/// </summary>
44-
/// <returns></returns>
45-
public Item Pop()
45+
private void ReverseH()
4646
{
47-
if (this.H.IsEmpty())
47+
while (!this.H.IsEmpty())
4848
{
49-
Reverse();
49+
this.T.Push(this.H.Pop());
5050
}
51+
}
5152

53+
/// <summary>
54+
/// 从 Steque 中弹出一个元素。
55+
/// </summary>
56+
/// <returns></returns>
57+
public Item Pop()
58+
{
59+
ReverseT();
5260
return this.H.Pop();
5361
}
5462

@@ -58,6 +66,7 @@ public Item Pop()
5866
/// <param name="item"></param>
5967
public void Enqueue(Item item)
6068
{
69+
ReverseH();
6170
this.T.Push(item);
6271
}
6372

1 Fundamental/1.4/1.4.30/Deque.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/// <typeparam name="Item">双向队列中保存的元素类型。</typeparam>
77
class Deque<Item>
88
{
9-
Stack<Item> stack;//代表队列尾部
10-
Steque<Item> steque;//代表队列头部
9+
Stack<Item> stack;// 代表队列尾部
10+
Steque<Item> steque;// 代表队列头部
1111

1212
/// <summary>
1313
/// 创建一条空的双向队列。
@@ -68,7 +68,14 @@ public Item PopLeft()
6868
/// <param name="item">要插入的元素。</param>
6969
public void PushRight(Item item)
7070
{
71-
this.stack.Push(item);
71+
if (this.stack.IsEmpty())
72+
{
73+
this.steque.Enqueue(item);
74+
}
75+
else
76+
{
77+
this.stack.Push(item);
78+
}
7279
}
7380

7481
/// <summary>

1 Fundamental/1.4/1.4.31/Deque.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ public void PushRight(Item item)
4646
private void Move(Stack<Item> source, Stack<Item> destination)
4747
{
4848
int n = source.Size();
49-
//将上半部分元素移动到临时栈 middle
49+
// 将上半部分元素移动到临时栈 middle
5050
for (int i = 0; i < n / 2; ++i)
5151
{
5252
this.middle.Push(source.Pop());
5353
}
54-
//将下半部分移动到另一侧栈中
54+
// 将下半部分移动到另一侧栈中
5555
while (!source.IsEmpty())
5656
{
5757
destination.Push(source.Pop());
5858
}
59-
//从 middle 取回上半部分元素
59+
// 从 middle 取回上半部分元素
6060
while (!this.middle.IsEmpty())
6161
{
6262
source.Push(this.middle.Pop());

1 Fundamental/1.4/1.4.32/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static void Main(string[] args)
1616
// 这里简单证明,设 M 次操作之后栈的大小为 n,那么额外访问数组的次数为:
1717
// S = n/2 + n/4 + n/8 +...+ 2 < n
1818
// 为了能使栈大小达到 n,M 必须大于等于 n/2
19-
// 因此 2M >= n > S,得证。
19+
// 因此 2M >= n > S,得证。
2020
// 因此我们可以得到 M 次操作后访问数组次数的总和 S' = S + M < 3M
2121
// 得证。
2222
}

1 Fundamental/1.4/1.4.33/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static void Main(string[] args)
1818
// int[] = 8(对象开销) + 4(数组长度) + 4N = 12 + 4N
1919
// double[] = 8(对象开销) + 4(数组长度) + 8N = 12 + 8N
2020
// double[][] = 8(对象开销) + 4(数组长度) + 4M(引用) + M(12 + 8N)(M 个一维数组) = 12 + 16M + 8MN
21-
// String = 8(对象开销) + 3*4(int * 3) + 4(字符数组的引用) + 8(字符数组对象开销) + 4(字符数组长度) + 2N(字符串) = 36 + 2N
21+
// String = 8(对象开销) + 3*4(int * 3) + 4(字符数组的引用) = 24
2222
// Node = 8(对象开销) + 4*2(引用*2) = 16
2323
// Stack = 8(对象开销) + 4(引用) + 4(int) + N(Node + Integer)(元素) = 16 + 28N
2424
}

0 commit comments

Comments
 (0)