-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
30 changed files
with
245 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "../exercise.h" | ||
|
||
// READ: 数组 <https://zh.cppreference.com/w/cpp/language/array> | ||
|
||
unsigned long long arr[90]{0, 1}; | ||
unsigned long long fibonacci(int i) { | ||
switch (i) { | ||
case 0: | ||
return 0; | ||
case 1: | ||
return 1; | ||
default: | ||
// TODO: 补全三目表达式缺失的部分 | ||
return <condition> ? <cache> : (arr[i] = fibonacci(i - 1) + fibonacci(i - 2)); | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
// TODO: 为此 ASSERT 填写正确的值 | ||
ASSERT(sizeof(arr) == ?, "sizeof array is size of all its elements"); | ||
// ---- 不要修改以下代码 ---- | ||
ASSERT(fibonacci(2) == 1, "fibonacci(2) should be 1"); | ||
ASSERT(fibonacci(20) == 6765, "fibonacci(20) should be 6765"); | ||
ASSERT(fibonacci(80) == 23416728348467685, "fibonacci(80) should be 23416728348467685"); | ||
return 0; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "../exercise.h" | ||
|
||
// READ: 数组向指针退化 <https://zh.cppreference.com/w/cpp/language/array#%E6%95%B0%E7%BB%84%E5%88%B0%E6%8C%87%E9%92%88%E7%9A%84%E9%80%80%E5%8C%96> | ||
bool is_fibonacci(int *ptr, int len, int stride) { | ||
ASSERT(len >= 3, "`len` should be at least 3"); | ||
// TODO: 编写代码判断从 ptr 开始,每 stride 个元素取 1 个元素,组成长度为 n 的数列是否满足 | ||
// arr[i + 2] = arr[i] + arr[i + 1] | ||
return true; | ||
} | ||
|
||
// ---- 不要修改以下代码 ---- | ||
int main(int argc, char **argv) { | ||
int arr0[]{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}, | ||
arr1[]{0, 1, 2, 3, 4, 5, 6}, | ||
arr2[]{99, 98, 4, 1, 7, 2, 11, 3, 18, 5, 29, 8, 47, 13, 76, 21, 123, 34, 199, 55, 322, 0, 0}; | ||
// clang-format off | ||
ASSERT( is_fibonacci(arr0 , sizeof(arr0) / sizeof(*arr0) , 1), "arr0 is Fibonacci" ); | ||
ASSERT( is_fibonacci(arr0 + 2, sizeof(arr0) / sizeof(*arr0) - 4, 1), "part of arr0 is Fibonacci" ); | ||
ASSERT(!is_fibonacci(arr1 , sizeof(arr1) / sizeof(*arr1) , 1), "arr1 is not Fibonacci"); | ||
ASSERT( is_fibonacci(arr1 + 1, 3 , 1), "part of arr1 is Fibonacci" ); | ||
ASSERT(!is_fibonacci(arr2 , sizeof(arr2) / sizeof(*arr2) , 1), "arr2 is not Fibonacci"); | ||
ASSERT( is_fibonacci(arr2 + 2, 10 , 2), "part of arr2 is Fibonacci" ); | ||
ASSERT( is_fibonacci(arr2 + 3, 9 , 2), "part of arr2 is Fibonacci" ); | ||
ASSERT(!is_fibonacci(arr2 + 3, 10 , 2), "guard check" ); | ||
ASSERT(!is_fibonacci(arr2 + 1, 10 , 2), "guard check" ); | ||
// clang-format on | ||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "../exercise.h" | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
// READ: `std::unique_ptr` <https://zh.cppreference.com/w/cpp/memory/unique_ptr> | ||
|
||
std::vector<std::string> RECORDS; | ||
|
||
class Resource { | ||
std::string _records; | ||
|
||
public: | ||
void record(char record) { | ||
_records.push_back(record); | ||
} | ||
|
||
~Resource() { | ||
RECORDS.push_back(_records); | ||
} | ||
}; | ||
|
||
using Unique = std::unique_ptr<Resource>; | ||
Unique reset(Unique ptr) { | ||
if (ptr) { | ||
ptr->record('r'); | ||
} | ||
return std::make_unique<Resource>(); | ||
} | ||
Unique drop(Unique ptr) { | ||
if (ptr) { | ||
ptr->record('d'); | ||
} | ||
return nullptr; | ||
} | ||
Unique forward(Unique ptr) { | ||
if (ptr) { | ||
ptr->record('f'); | ||
} | ||
return ptr; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
std::vector<std::string> problems[3]; | ||
|
||
drop(forward(reset(nullptr))); | ||
problems[0] = std::move(RECORDS); | ||
|
||
forward(drop(reset(forward(forward(reset(nullptr)))))); | ||
problems[1] = std::move(RECORDS); | ||
|
||
drop(drop(reset(drop(reset(reset(nullptr)))))); | ||
problems[2] = std::move(RECORDS); | ||
|
||
// ---- 不要修改以上代码 ---- | ||
|
||
std::vector<const char *> answers[]{ | ||
{"fd"}, | ||
// TODO: 分析 problems[1] 中资源的生命周期,将记录填入 `std::vector` | ||
{"", "", "", "", "", "", "", ""}, | ||
{"", "", "", "", "", "", "", ""}, | ||
}; | ||
|
||
// ---- 不要修改以下代码 ---- | ||
|
||
for (auto i = 0; i < 3; ++i) { | ||
ASSERT(problems[i].size() == answers[i].size(), "wrong size"); | ||
for (auto j = 0; j < problems[i].size(); ++j) { | ||
ASSERT(std::strcmp(problems[i][j].c_str(), answers[i][j]) == 0, "wrong location"); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "../exercise.h" | ||
#include <memory> | ||
|
||
// READ: `std::shared_ptr` <https://zh.cppreference.com/w/cpp/memory/shared_ptr> | ||
// READ: `std::weak_ptr` <https://zh.cppreference.com/w/cpp/memory/weak_ptr> | ||
|
||
// TODO: 将下列 `?` 替换为正确的值 | ||
int main(int argc, char **argv) { | ||
auto shared = std::make_shared<int>(10); | ||
std::shared_ptr<int> ptrs[]{shared, shared, shared}; | ||
|
||
std::weak_ptr<int> observer = shared; | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
ptrs[0].reset(); | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
ptrs[1] = nullptr; | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
ptrs[2] = std::make_shared<int>(*shared); | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
ptrs[0] = shared; | ||
ptrs[1] = shared; | ||
ptrs[2] = std::move(shared); | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
std::ignore = std::move(ptrs[0]); | ||
ptrs[1] = std::move(ptrs[1]); | ||
ptrs[1] = std::move(ptrs[2]); | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
shared = observer.lock(); | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
shared = nullptr; | ||
for (auto &ptr : ptrs) ptr = nullptr; | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
shared = observer.lock(); | ||
ASSERT(observer.use_count() == ?, ""); | ||
|
||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters