This commit is contained in:
2025-07-12 23:57:21 +08:00
parent 321cb6c439
commit 7325a22989
4 changed files with 45 additions and 12 deletions

View File

@@ -60,7 +60,6 @@ add_executable(219 src/219.c)
add_executable(225 src/225.c) add_executable(225 src/225.c)
add_executable(228 src/228.c) add_executable(228 src/228.c)
add_executable(231 src/231.c) add_executable(231 src/231.c)
add_executable(258 src/258.c)
add_executable(263 src/263.c) add_executable(263 src/263.c)
add_executable(268 src/268.c) add_executable(268 src/268.c)
add_executable(278 src/278.c) add_executable(278 src/278.c)
@@ -96,7 +95,6 @@ set(EXECUTABLE_SOURCES
"${CMAKE_SOURCE_DIR}/src/225.c" "${CMAKE_SOURCE_DIR}/src/225.c"
"${CMAKE_SOURCE_DIR}/src/228.c" "${CMAKE_SOURCE_DIR}/src/228.c"
"${CMAKE_SOURCE_DIR}/src/231.c" "${CMAKE_SOURCE_DIR}/src/231.c"
"${CMAKE_SOURCE_DIR}/src/258.c"
"${CMAKE_SOURCE_DIR}/src/263.c" "${CMAKE_SOURCE_DIR}/src/263.c"
"${CMAKE_SOURCE_DIR}/src/268.c" "${CMAKE_SOURCE_DIR}/src/268.c"
"${CMAKE_SOURCE_DIR}/src/278.c" "${CMAKE_SOURCE_DIR}/src/278.c"

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

@@ -0,0 +1,11 @@
#ifndef INC_258_H
#define INC_258_H
#ifdef __cplusplus
extern "C"
{
#endif
int addDigits(int num);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,15 +1,16 @@
// #include <solution/258.h>
// Created by xfj12 on 2025/3/24.
//
int addDigits(int num) int addDigits(int num)
{ {
int total = 0;
do do
{ {
int n = 0;
} while (); while (num)
}
int main()
{ {
n += num % 10;
num /= 10;
}
num = n;
} while (num >= 10);
return num;
} }

23
tests/test_258.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include <gtest/gtest.h>
#include <solution/258.h>
// 测试类定义
class AddDigitsTest : public ::testing::Test
{
};
// 示例 1输入 38输出 2
TEST_F(AddDigitsTest, Example1)
{
int input = 38;
int expected = 2;
EXPECT_EQ(addDigits(input), expected);
}
// 示例 2输入 0输出 0
TEST_F(AddDigitsTest, Example2)
{
int input = 0;
int expected = 0;
EXPECT_EQ(addDigits(input), expected);
}