This commit is contained in:
Jeffrey Hsu 2025-03-25 18:08:26 +08:00
parent e0f249d1d9
commit 79b6fbe7e3
5 changed files with 63 additions and 1 deletions

View File

@ -70,5 +70,6 @@ add_executable(2786 src/2786.c)
add_executable(3110 src/3110.c)
add_library(2711 STATIC src/2711.c)
add_library(367 STATIC src/367.c)
add_subdirectory(tests)

16
include/solution/367.h Normal file
View File

@ -0,0 +1,16 @@
//
// Created by xfj12 on 2025/3/25.
//
#ifndef INC_367_H
#define INC_367_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
bool isPerfectSquare(int num);
#ifdef __cplusplus
}
#endif
#endif // INC_367_H

16
src/367.c Normal file
View File

@ -0,0 +1,16 @@
//
// Created by xfj12 on 2025/3/25.
//
#include <solution/367.h>
#include <stdint.h>
bool isPerfectSquare(int num)
{
for (int64_t i = 1;; i++)
{
if (i * i > num)
return false;
if (i * i == num)
return true;
}
}

View File

@ -1,5 +1,8 @@
add_executable(test_2711 test_2711.cpp)
target_link_libraries(test_2711 PUBLIC gtest_main 2711)
add_executable(test_367 test_367.cpp)
target_link_libraries(test_367 PUBLIC gtest_main 367)
enable_testing()
add_test(NAME test_2711 COMMAND test_2711)
add_test(NAME test_2711 COMMAND test_2711)
add_test(NAME test_367 COMMAND test_367)

26
tests/test_367.cpp Normal file
View File

@ -0,0 +1,26 @@
//
// Created by xfj12 on 2025/3/25.
//
#include <gtest/gtest.h>
#include <solution/367.h>
TEST(IsPerfectSquareTest, PerfectSquareCase)
{
// [IN] 16
// [OUT] true
EXPECT_TRUE(isPerfectSquare(16)); // 验证完全平方数
}
TEST(IsPerfectSquareTest, NonPerfectSquareCase)
{
// [IN] 14
// [OUT] false
EXPECT_FALSE(isPerfectSquare(14)); // 验证非完全平方数
}
TEST(IsPerfectSquareTest, BigIntNonPerfectSquareCase)
{
// [IN] 14
// [OUT] false
EXPECT_FALSE(isPerfectSquare(2147483647)); // 验证非完全平方数
}