2829
This commit is contained in:
parent
93f367dee9
commit
572479248d
15
include/solution/2829.h
Normal file
15
include/solution/2829.h
Normal file
@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/26.
|
||||
//
|
||||
|
||||
#ifndef INC_2829_H
|
||||
#define INC_2829_H
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
int minimumSum(int n, int k);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // INC_2829_H
|
31
src/2829.c
Normal file
31
src/2829.c
Normal file
@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/26.
|
||||
//
|
||||
#include <solution/2829.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int minimumSum(int n, int k)
|
||||
{
|
||||
int *KAvoid = malloc(sizeof(int) * n);
|
||||
int idx = 1;
|
||||
KAvoid[0] = 1;
|
||||
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 2; idx < n; i++)
|
||||
{
|
||||
bool flag = true;
|
||||
for (int j = 0; j < idx && flag; j++)
|
||||
if (KAvoid[j] + i == k)
|
||||
flag = false;
|
||||
if (flag)
|
||||
KAvoid[idx++] = i;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
sum += KAvoid[i];
|
||||
|
||||
free(KAvoid);
|
||||
return sum;
|
||||
}
|
16
tests/test_2829.cpp
Normal file
16
tests/test_2829.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/2829.h>
|
||||
|
||||
TEST(MinimumSumTest, Test1)
|
||||
{
|
||||
int n = 5, k = 4;
|
||||
int expected = 18;
|
||||
EXPECT_EQ(minimumSum(n, k), expected);
|
||||
}
|
||||
|
||||
TEST(MinimumSumTest, Test2)
|
||||
{
|
||||
int n = 2, k = 6;
|
||||
int expected = 3;
|
||||
EXPECT_EQ(minimumSum(n, k), expected);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user