-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2928.cpp
35 lines (28 loc) · 810 Bytes
/
2928.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cassert>
#include <algorithm> // 用于 std::min 和 std::max 函数
using namespace std;
class Solution {
public:
int distributeCandies(int n, int limit) {
int res = 0;
for (int i = 0; i <= limit; i++) {
if (n - i > limit * 2) {
continue;
}
res += min(limit, n - i) - max(0, n - i - limit) + 1;
}
return res;
}
};
int main() {
Solution solution;
int n1 = 10, limit1 = 2;
assert(solution.distributeCandies(n1, limit1) == 0);
int n2 = 5, limit2 = 3;
assert(solution.distributeCandies(n2, limit2) == 12);
int n3 = 7, limit3 = 3;
assert(solution.distributeCandies(n3, limit3) == 6);
cout << "All test cases passed!" << endl;
return 0;
}