From 7325a229893ec137fc839948e988975dcc5f947c Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Sat, 12 Jul 2025 23:57:21 +0800 Subject: [PATCH] 258 --- CMakeLists.txt | 2 -- include/solution/258.h | 11 +++++++++++ src/258.c | 21 +++++++++++---------- tests/test_258.cpp | 23 +++++++++++++++++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 include/solution/258.h create mode 100644 tests/test_258.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 62dfc11..4b30ed4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,7 +60,6 @@ add_executable(219 src/219.c) add_executable(225 src/225.c) add_executable(228 src/228.c) add_executable(231 src/231.c) -add_executable(258 src/258.c) add_executable(263 src/263.c) add_executable(268 src/268.c) add_executable(278 src/278.c) @@ -96,7 +95,6 @@ set(EXECUTABLE_SOURCES "${CMAKE_SOURCE_DIR}/src/225.c" "${CMAKE_SOURCE_DIR}/src/228.c" "${CMAKE_SOURCE_DIR}/src/231.c" - "${CMAKE_SOURCE_DIR}/src/258.c" "${CMAKE_SOURCE_DIR}/src/263.c" "${CMAKE_SOURCE_DIR}/src/268.c" "${CMAKE_SOURCE_DIR}/src/278.c" diff --git a/include/solution/258.h b/include/solution/258.h new file mode 100644 index 0000000..eb27809 --- /dev/null +++ b/include/solution/258.h @@ -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 \ No newline at end of file diff --git a/src/258.c b/src/258.c index 9bd833b..380ad78 100644 --- a/src/258.c +++ b/src/258.c @@ -1,15 +1,16 @@ -// -// Created by xfj12 on 2025/3/24. -// +#include + int addDigits(int num) { - int total = 0; do { - - } while (); -} - -int main() -{ + int n = 0; + while (num) + { + n += num % 10; + num /= 10; + } + num = n; + } while (num >= 10); + return num; } diff --git a/tests/test_258.cpp b/tests/test_258.cpp new file mode 100644 index 0000000..842e82e --- /dev/null +++ b/tests/test_258.cpp @@ -0,0 +1,23 @@ +#include +#include + +// 测试类定义 +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); +} \ No newline at end of file