Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
GooduckZ committed Dec 7, 2023
2 parents 247628a + 11e41cb commit 01dd1f6
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 19 deletions.
5 changes: 3 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
![ckc-agc-card](graph/ckc-agc-card-black.png#only-dark)
<!-- prettier-ignore-end -->


😉 你好~

这里是竺可桢学院学业指导中心辅学计划的站点。辅学计划为竺院大一新生提供数学分析(H)、线性代数(H)、微积分(H)、普通物理学(H)、普通化学(H)、程序设计(H)等**荣誉课程**的学习指导与帮助。
这里是浙江大学竺可桢学院学业指导中心辅学计划的站点。辅学计划为大一新生提供数学分析(H)、线性代数(H)、微积分(H)、普通物理学(H)、普通化学(H)、程序设计(H)等**竺院荣誉课程**的学习指导与帮助。

我们服务的对象主要是竺院学子,但我们同样**欢迎院外同学参与我们的辅学活动**。院内院外课程内容和难度方面可能存在差异,请自行斟酌。

你可以通过网站上方或左侧的导航栏浏览我们的网站。普通辅学版块使用 📚 标注,精品课程版块使用 🏫 标注。

Expand Down
7 changes: 6 additions & 1 deletion docs/lalu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ W 代表适配吴志祥班,T 代表适配谈之奕班,H 代表合班上课

LALU 课程的讲义存放在下面的仓库中,你可以**点击图片**跳转到仓库页面。欢迎提出 issue 和 pull request。

[![](graph/pixel.jpg#only-light)![](graph/code.jpg#only-dark)](https://github.com/yhwu-is/Linear-Algebra-Left-Undone/)
<div class="github-card" data-github="yhwu-is/Linear-Algebra-Left-Undone" data-width="400" data-height="" data-theme="default"></div>
<script src="//cdn.jsdelivr.net/github-cards/latest/widget.js"></script>

<!--[![yhwu-is/Linear-Algebra-Left-Undone - GitHub](https://gh-card.dev/repos/yhwu-is/Linear-Algebra-Left-Undone.svg?fullname=)](https://github.com/yhwu-is/Linear-Algebra-Left-Undone)-->

<!--[![](graph/pixel.jpg#only-light)![](graph/code.jpg#only-dark)](https://github.com/yhwu-is/Linear-Algebra-Left-Undone/)-->

点击下面的链接可以下载讲义的最新 Release 版本:

Expand Down
294 changes: 283 additions & 11 deletions docs/programming/daily/2023.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,276 @@ h5:before {content: unset;}

## December

### 「6」 Stack and Queue (adapted from FDS mid-term exam)

Given an empty stack `S` and an empty queue `Q`. Push elements `{1, 2, 3, 4, 5, 6, 7}` one by one onto `S`. If each element that is popped from `S` is enqueued onto `Q` immediately, and if the dequeue sequence is `{3, 2, 4, 6, 7, 5, 1}`, then the minimum size of `S` should be:

<div style="display: flex">
<div style="width: 100%">A. 5</div>
<div style="width: 100%">B. 4</div>
<div style="width: 100%">C. 3</div>
<div style="width: 100%">D. 7</div>
</div>

<!-- prettier-ignore-start -->
??? note "Answer"

C.

Stack is LIFO (Last In, First Out), queue is FIFO (First In, First Out). So dequeue sequence of queue is also the enqueue sequence. If enqueue sequence of Q is {3, 2, 4, 6, 7, 5, 1}, all operation of stack S must be the following:

- Push 1 and 2 and 3 into stack sequentially. Stack S now contains 3 nodes.
- Pop 3, stack contains 2 nodes
- Pop 2, stack contains 1 node
- Push 4, stack contains 2 nodes
- Pop 4, stack contains 1 node
- Push 5, stack contains 2 nodes
- Push 6, stack contains 3 nodes
- Pop 6, stack contains 2 nodes
- Push 7, stack contains 3 nodes
- Pop 7, stack contains 2 nodes
- Pop 5, stack contains 1 node
- Pop 1, stack is empty

In the process, there are at most 3 nodes in stack S, so the minimum size of S should be 3.
<!-- prettier-ignore-end -->

> 供题人:姚雪涛
### 「5」 Monotonic stacks

Monotonic stacks are a specialized version of the standard stack data structure, designed to maintain elements in a pre-defined sorted order. Unlike regular stacks, which allow push and pop operations without any constraints, monotonic stacks enforce an order - either increasing or decreasing - on the elements. This means that elements are either **strictly** increasing or **strictly** decreasing from the top to the bottom of the stack.

The core operations of a monotonic stack, namely `push`, are modified to maintain the stack’s order. During a `push` operation, elements that break the monotonic property are removed from the stack before the new element is added.

For example, now we have a decreasing monotonic stack, where elements closer to the top have the **smaller** values:

```text
[5,4,2,1]
^
|
top
```

We push a data with the value $3$, and because $1,2$ is smaller than $3$, we remove them. And finally it will be:

```text
[5,4,3]
^
|
top
```

Now, your task is to create an decreasing monotonic stack in the C language.

```c linenums="1" hl_lines="12 17 20 24"
typedef struct Node {
int data;
struct Node* next;
} Node;

typedef struct Stack {
Node* top;
} Stack;

Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
/* (1) */;
return stack;
}

void push(Stack* stack, int data) {
while (/* (2) */) {
Node* temp = stack->top;
stack->top = temp->next;
/* (3) */;
}
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
/* (4) */;
stack->top = newNode;
}

int pop(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
Node* temp = stack->top;
int popped = temp->data;
stack->top = temp->next;
free(temp);
return popped;
}
```
<!-- prettier-ignore-start -->
??? note "Answer"
单调栈是一种特殊的栈,其中每个元素都严格小于或大于它下面的元素。大家在后续的课程中有可能会接触到他的一些应用。接下来我们逐一看看这些题目怎么填写:
**第一空**:这里需要初始化栈顶元素。由于栈是空的,所以栈顶应该设置为 `NULL`。
```c
stack->top = NULL;
```
**第二空**:这是最难的一空。根据题意,这一层循环的目的是:移除所有小于**或等于**新元素的栈顶元素,以维持栈的单调性。但是还有一种情况:如果栈本身是空的,或者所有元素都比新元素小,那必须得退出循环。
```c
while (stack->top != NULL && stack->top->data <= data)
```
**第三空**:不大容易想到。在移除栈顶元素后,应该释放该节点占用的内存。
```c
free(temp);
```
> 为什么出这个?因为 wk 明确说过,考试考这个填空,不写 free 就少 2 分,所以出了这个空来让大家注意。
**第四空**:这里需要将新节点的 `next` 指针指向之前的栈顶元素,然后将栈顶指针指向新节点,这样新节点就成为了新的栈顶。
```c
newNode->next = stack->top;
```
<!-- prettier-ignore-end -->
> 供题人:谢集
### 「4」 Command Line Arguments
If you run the following command in the terminal:
```text linenums="1"
./a.out this is a test
```

Please describe what will the program see in `argv` and `argc`.

<!-- prettier-ignore-start -->
??? note "Answer"

`argv` is an array of pointers to strings, and `argc` is the number of strings in `argv`. The `argv` array contains the following strings:

```text
argv[0] = "./a.out"
argv[1] = "this"
argv[2] = "is"
argv[3] = "a"
argv[4] = "test"
```

The `argc` is 5, because there are 5 strings in `argv`.

The `argv` array is terminated by a null pointer, so `argv[5]` is a null pointer.

!!! note inline end ""
![](graph/23.12.04.png)

For more information about `argv` and `argc`, see [cppreference-argc, argv](https://en.cppreference.com/w/c/language/main_function).
<!-- prettier-ignore-end -->

> 供题人:朱宝林
### 「3」 `sizeof` `struct`

Consider the following code fragment:

```c linenums="1"
struct A {
char a[10];
int b;

};
typedef struct A A;

struct B {
char *a;
int b;
};
typedef struct B* B;
```

What is the value of `sizeof(A)` and `sizeof(B)`?

<!-- prettier-ignore-start -->
??? note "Answer"

`sizeof(A)` is 16, and `sizeof(B)` is 8 (on a 64-bit modern system).

The `sizeof` operator returns the size of its operand in bytes. The size of a structure type is as large as the sum of the sizes of its members.

You may think the size of `A` is 14, because the size of `char[10]` is 10, and the size of `int` is 4. However, to improve performance of memory access, C standard allows the compiler to add padding bytes after each member of a structure, and the padding strategy is **implementation specific**. A common strategy is to align each member to the size of its type, that is to say, make their memory address to be a multiple of the size of the member. For example, on a 64-bit system, the address of a 4-byte integer must be a multiple of 4.

Therefore, The size of `A` is 16. The compiler will add 2 padding bytes after `char[10]`, to make the address of `int` a multiple of 4 in `struct A`.

The size of `B` is 8, because `B` is a pointer and the size of pointer is 8. The size of type to which `B` points does not affect the size of `B`. Notice that type `B` is not equivalent to type `struct B`.

For more information about `sizeof` operator, see [cppreference-sizeof](https://en.cppreference.com/w/c/language/sizeof).
<!-- prettier-ignore-end -->

> 供题人:朱宝林、孙兆江
### 「2」 Print Non-printable Characters

Write a program to convert unprintable characters (characters with ASCII codes between `0x00`~`0x1f` and `0x7f`) in the input string to hexadecimal format for output.

Requirements:

- Do not use the `printf` series of formatting output functions.
- The minimum field width for output is `2`, and if the hexadecimal number is less than 2 digits, pad it with leading zeros. For example, `0x0a`.

Sample Input:

```text
Hello
World!
```

Sample Output:

```text
Hello\0x0aWorld!
```

<!-- prettier-ignore-start -->
??? note "Answer"

```c linenums="1"
#include <stdio.h>
#include <ctype.h>

const char* hex_digits = "0123456789abcdef";

int main(void) {
char temp;
while((temp = getchar()) != EOF) {
if (isprint(temp)) {
putchar(temp);
} else {
putchar('\\');
putchar('0');
putchar('x');
putchar(hex_digits[temp >> 4]);
putchar(hex_digits[temp & 0xf]);
}
}
return 0;
}
```

If you are not familiar with bitwise operations, the 14th and 15th lines in the above code fragment can be written in a more understandable form:

```c
putchar(hex_digits[temp / 16]);
putchar(hex_digits[temp % 16]);
```
<!-- prettier-ignore-end -->

> 供题人:朱宝林
### 「1」 Hello, Cat

After executing the following code fragment, the output should be **\_\_**.
Expand Down Expand Up @@ -48,9 +318,7 @@ D. `Helloldw<, world; lloldw<, ld.`

When the `strcat` function appends the string `"ld"` after "Hello", it overwrites the null character `'\0'` at the end of the string `"Hello"` and write a new null character `'\0'` at the end of the string `"Helloldld"`. Therefore, the character `w` and `<` after the null character `'\0'` in `s1` are not printed.

![](graph/23.12.1.png)


![](graph/23.12.01.png)
<!-- prettier-ignore-end -->

> 供题人:郑俊达
Expand Down Expand Up @@ -100,20 +368,22 @@ int main(){
<!-- prettier-ignore-start -->
??? note "Answer"
![](graph/23.11.30.drawio.svg){align=right}
1. `list`
2. `list->next`
3. `node`
4. `node`
5. `list->next`
!!! note inline end ""
![](graph/23.11.30.drawio.svg)
Notice this is a doubly linked list **with a dummy node**. The dummy node is a special node that does not store any data. It is used to simplify the implementation of the linked list. In this case, the dummy node is `list`.
The `insertFront` function inserts a new node with `data` at the front of the list. The new node is inserted between the dummy node and the first node of the list. The `insertFront` function takes two steps:
1. Create a new node and set its `data` to `data`.
2. Insert the new node between the dummy node and the first node of the list.
6. Create a new node and set its `data` to `data`.
7. Insert the new node between the dummy node and the first node of the list.
If you want to iterate over the list, you should start from the first node of the list, which is `list->next`.
<!-- prettier-ignore-end -->
Expand Down Expand Up @@ -143,7 +413,6 @@ int main() {
<div style="width: 100%">D. 4</div>
</div>


<!-- prettier-ignore-start -->
??? note "Answer"
C.
Expand Down Expand Up @@ -197,6 +466,7 @@ printf("%d#%c#%c#%s", a, c1, c2, s);
ckc-agc
daily_problem
```

<!-- prettier-ignore-start -->
??? note "Answer"

Expand Down Expand Up @@ -327,9 +597,11 @@ int main()

A.

`a[]` is an array of pointers, which stores the addresses of three strings, and `pa` points to the address of the first element of `a`, as shown below:
!!! note inline end ""

![](graph/23.11.24.jpg)

![](graph/23.11.24.jpg)
`a[]` is an array of pointers, which stores the addresses of three strings, and `pa` points to the address of the first element of `a`, as shown below:

Pointer operations take precedence over addition, so `*(*pa + 1)` equals to `n`.

Expand All @@ -354,7 +626,7 @@ typedef PtrToBookcase Lib_data_base[MAX_SIZE];

After executing the following code fragment, the output should be **\_\_**.

```c
```c linenums="1"
Lib_data_base library;
*(library) = (PtrToBookcase)malloc(sizeof(struct Bookcase));
(*library)->book[0] = "C Programming Book";
Expand Down
File renamed without changes
Binary file added docs/programming/daily/graph/23.12.04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 01dd1f6

Please sign in to comment.