Compare commits

...

2 Commits

Author SHA1 Message Date
e924811683 mkfile 2025-07-17 17:24:50 +08:00
e3b069be8c 3201 2025-07-17 17:19:52 +08:00
5 changed files with 150 additions and 0 deletions

11
include/solution/3201.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef INC_3201_H
#define INC_3201_H
#ifdef __cplusplus
extern "C"
{
#endif
int maximumLength(int *nums, int numsSize);
#ifdef __cplusplus
}
#endif
#endif

49
mkfile.bat Normal file
View File

@@ -0,0 +1,49 @@
@echo off
chcp 65001
setlocal enabledelayedexpansion
:: 检查参数是否存在
if "%~1"=="" (
echo 用法: %~nx0 文件名(不带扩展名)
exit /b 1
)
set "NAME=%~1"
set "NAME_UPPER=%NAME:_=_%"
set "NAME_UPPER=%NAME_UPPER:"=%"
for %%A in (%NAME_UPPER%) do (
set "NAME_UPPER=!NAME_UPPER:%%A=%%~A!"
)
set "NAME_UPPER=!NAME_UPPER:.=_!"
:: 创建目录结构
mkdir include\solution 2>nul
mkdir src 2>nul
mkdir tests 2>nul
:: 生成 include/solution/a.h
(
echo #ifndef INC_!NAME_UPPER!_H
echo #define INC_!NAME_UPPER!_H
echo #ifdef __cplusplus
echo extern "C" {
echo #endif
echo.
echo #ifdef __cplusplus
echo }
echo #endif
echo #endif
) > include\solution\%NAME%.h
:: 生成 src/a.c
(
echo #include ^<solution/%NAME%.h^>
) > src\%NAME%.c
:: 生成 tests/test_a.cpp
(
echo #include ^<gtest/gtest.h^>
echo #include ^<solution/%NAME%.h^>
) > tests\test_%NAME%.cpp
echo 文件创建完成。

39
mkfile.sh Normal file
View File

@@ -0,0 +1,39 @@
#!/bin/bash
# 检查参数
if [ $# -ne 1 ]; then
echo "用法: $0 <文件名(不带扩展名)>"
exit 1
fi
filename="$1"
upper_filename=$(echo "$filename" | tr '[:lower:]' '[:upper:]')
# 创建目录(如果不存在)
mkdir -p ./include/solution ./src ./tests
# 创建 ./include/solution/a.h
cat > ./include/solution/"$filename".h <<EOF
#ifndef INC_${upper_filename}_H
#define INC_${upper_filename}_H
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
#endif
EOF
# 创建 ./src/a.c
cat > ./src/"$filename".c <<EOF
#include <solution/$filename.h>
EOF
# 创建 ./tests/test_a.cpp
cat > ./tests/test_"$filename".cpp <<EOF
#include <gtest/gtest.h>
#include <solution/$filename.h>
EOF

13
src/3201.c Normal file
View File

@@ -0,0 +1,13 @@
#include <solution/3201.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
int maximumLength(int *nums, int numsSize)
{
int odd = 0, even = 0, odd_cnt = 0;
for (int i = 0; i < numsSize; i++)
nums[i] & 1 ? (odd = even + 1, odd_cnt++) : (even = odd + 1);
return max(max(even, odd), max(odd_cnt, numsSize - odd_cnt));
}

38
tests/test_3201.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <gtest/gtest.h>
#include <solution/3201.h>
class MaximumLengthTest : public ::testing::Test
{
protected:
// 提供统一断言接口
void AssertMaximumLength(const std::vector<int> &input, int expected)
{
int *arr = const_cast<int *>(input.data());
int result = maximumLength(arr, input.size());
EXPECT_EQ(result, expected);
}
};
// 示例 1输入 [1,2,3,4],输出 4
TEST_F(MaximumLengthTest, IncreasingSequence)
{
AssertMaximumLength({1, 2, 3, 4}, 4);
}
// 示例 2输入 [1,2,1,1,2,1,2],输出 6
TEST_F(MaximumLengthTest, AlternatingSequence)
{
AssertMaximumLength({1, 2, 1, 1, 2, 1, 2}, 6);
}
// 示例 3输入 [1,3],输出 2
TEST_F(MaximumLengthTest, ShortSequence)
{
AssertMaximumLength({1, 3}, 2);
}
// 示例 4输入 [4,51,68],输出 3
TEST_F(MaximumLengthTest, TEST4)
{
AssertMaximumLength({4, 51, 68}, 3);
}