fix: Update C code for compatibility with the MSVC compiler (#949)

* Replace VLA with malloc
Replace VLA with malloc to make C code
compatible with cl compiler on Windows.

* Fix C code for CI compiler.

* Fix C code compability to CI.

* check the trigger
This commit is contained in:
Yudong Jin
2023-11-17 00:29:54 +08:00
committed by GitHub
parent e4aa76ed3e
commit f7c41b6bef
23 changed files with 235 additions and 174 deletions

View File

@@ -2,4 +2,7 @@ add_executable(coin_change_greedy coin_change_greedy.c)
add_executable(fractional_knapsack fractional_knapsack.c)
add_executable(max_capacity max_capacity.c)
add_executable(max_product_cutting max_product_cutting.c)
target_link_libraries(max_product_cutting m)
if (NOT CMAKE_C_COMPILER_ID STREQUAL "MSVC")
target_link_libraries(max_product_cutting m)
endif()

View File

@@ -6,8 +6,14 @@
#include "../utils/common.h"
#define MIN(a, b) (a < b ? a : b)
#define MAX(a, b) (a > b ? a : b)
/* 求最小值 */
int myMin(int a, int b) {
return a < b ? a : b;
}
/* 求最大值 */
int myMax(int a, int b) {
return a < b ? a : b;
}
/* 最大容量:贪心 */
int maxCapacity(int ht[], int htLength) {
@@ -19,8 +25,8 @@ int maxCapacity(int ht[], int htLength) {
// 循环贪心选择,直至两板相遇
while (i < j) {
// 更新最大容量
int capacity = MIN(ht[i], ht[j]) * (j - i);
res = MAX(res, capacity);
int capacity = myMin(ht[i], ht[j]) * (j - i);
res = myMax(res, capacity);
// 向内移动短板
if (ht[i] < ht[j]) {
i++;