# 課堂測驗 (Week # 1) ## Q1: 以下程式的作用為何? ```C int p(int i, int N) { return (i < N && printf("%d\n", i) && !p(i + 1, N)) \ || printf("%d\n", i); } ``` * `&&` 前後的敘述對調,是否影響輸出? * 從 `i < N && printf("%d\n", i)` 變更為 `printf("%d\n", i) && (i < N)` 的話,程式輸出為何? ## Q2: 考慮以下 swap 程式: ```C void swap(int *a, int *b) { int t; t = *a; *a = *b; *b = t; } ``` 能否避免使用區域變數 t?又,可否改用 bit-wise operator 來改寫? --> 算術: ``` *a = *a - *b; *b = *a + *b; *a = *b - *a; ``` 邏輯運算: ``` *a = *a XOR *b *b = *a XOR *b *a = *a XOR *b ``` 邏輯運算的好處是,可避免前述算術會遇到的 integer overflow,而且不限定位元數。 ## Q3: 用 C 語言實做 `char *reverse(char *s)` 來反轉 NULL 結尾的字串,限定 in-place 與遞迴 參考實作 (都還可以大幅改進,只是列出來給大家看) * [Day2](https://embedded2015.hackpad.com/GGI4IhFdLUW) **C-style string reverse (遞迴 + in-place)** * 先想想這個版本可用嗎? (以及對應的問題) ```C void reverse (char *s) { if (*s == ’\0’) return; reverse (s+1); printf ("%c", *s); } ```
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.