Skip to content

Commit 56c11cf

Browse files
fix: submitted answers get possible sigfault and with implicit declarations
1 parent a32f7c4 commit 56c11cf

2 files changed

Lines changed: 22 additions & 31 deletions

File tree

documents/vol1-fundamentals/c_tutorials/04-control-flow.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -472,23 +472,20 @@ C++17 引入了 `if constexpr`,它在编译期评估条件,直接把不满
472472
### 练习 1 参考答案
473473

474474
```c
475+
bool is_leap_year(int year) {
476+
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
477+
}
478+
475479
int month_day(int year, int month) {
476480
switch (month) {
477-
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
481+
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
478482
return 31;
479-
case 4:case 6:case 9:case 11:
483+
case 4: case 6: case 9: case 11:
480484
return 30;
481485
case 2:
482486
return is_leap_year(year) ? 29 : 28;
483-
}
484-
}
485-
486-
bool is_leap_year(int year) {
487-
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
488-
return true;
489-
}
490-
else {
491-
return false;
487+
default:
488+
return -1;
492489
}
493490
}
494491
```

documents/vol1-fundamentals/c_tutorials/05-function-basics.md

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,13 @@ void log_message(LogLevel level, const char* format, ...) {
349349
}
350350
printf("%s\n", level_str);
351351
352-
va_list log;
353-
va_start(log, format);
352+
va_list args;
353+
va_start(args, format);
354354
355-
vprintf(format, log);
355+
vprintf(format, args);
356356
printf("\n");
357357
358-
va_end(log);
358+
va_end(args);
359359
}
360360
```
361361

@@ -381,30 +381,24 @@ int binary_search_recursive(const int* arr, size_t len, int target) {
381381
if (arr[mid] == target) {return mid;}
382382
if (arr[mid] < target) {
383383
int res = binary_search_recursive(arr + mid + 1, len - mid - 1, target);
384-
return (res == -1) ? -1 : (res + mid + 1);
384+
return (res == -1) ? -1 : (int)(res + mid + 1);
385385
}
386386
if (arr[mid] > target) {return binary_search_recursive(arr, mid , target);}
387387
return -1;
388388
}
389389
390390
int binary_search_iterative(const int* arr, size_t len, int target) {
391-
if (len < 1) {
392-
printf("%d is not found in index\n", target);
393-
return -1;
394-
}
395-
size_t low = 0;
396-
size_t high = len - 1;
397-
size_t mid = low + (high - low) / 2;
398-
while (low <= high) {
399-
if (arr[mid] == target) {return mid;}
400-
401-
if (arr[mid] < target) {
402-
low = mid + 1;
391+
size_t lo = 0, hi = len; // 搜索区间 [lo, hi),左闭右开
392+
while (lo < hi) {
393+
size_t mid = lo + (hi - lo) / 2;
394+
if (arr[mid] == target) {
395+
return mid;
403396
}
404-
else if (arr[mid] > target) {
405-
high = mid - 1;
397+
if (arr[mid] < target) {
398+
lo = mid + 1; // 搜右半边,lo 只增不下溢
399+
} else {
400+
hi = mid; // 搜左半边,hi 收敛到 mid,不下溢
406401
}
407-
mid = low + (high - low) / 2;
408402
}
409403
printf("%d is not found in index\n", target);
410404
return -1;

0 commit comments

Comments
 (0)