generated from luckymark/c2022-challenge
-
Notifications
You must be signed in to change notification settings - Fork 15
滕鎧泽的作业 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tengkaize
wants to merge
21
commits into
luckymark:main
Choose a base branch
from
tengkaize:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
滕鎧泽的作业 #5
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9a54403
将语言由 C 切换为 C++,使用 VSCode 替换 CLion
tengkaize a3f70bb
格式化设置
tengkaize 6adf0cb
Use C++23.
tengkaize d8a018e
完成 level1 p02
tengkaize fee3dca
完成 level1 p03
tengkaize de825d5
完成 level1 p04
tengkaize fc1d7d2
fix file permissions
tengkaize 314885a
完成 level1 p06
tengkaize 6742764
完成 level1 p01
tengkaize 58e168b
完成 level2 PI,更新依赖
tengkaize bd8b3a4
重写 level1 p01
tengkaize 9a0eb27
完成 level1 p09
tengkaize d3c2ed9
不跟踪本地开发环境
tengkaize 9110be1
完成 level2 SkipList
tengkaize 5829c46
完成 level p07
tengkaize 406e6ee
update dependencies of level1 p07
tengkaize 485c51f
完成 level1 p05
tengkaize b510210
Update minimum required CMake version to 3.26
tengkaize 682d33f
Extract Gauss-Legendre algorithm as a function
tengkaize ed2f561
完成 level1 p08
tengkaize 6a25939
完成 level1 p10
tengkaize File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
# EditorConfig is awesome: https://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
|
||
[*.{c,h,cpp,hpp}] | ||
indent_size = 2 | ||
indent_style = tab |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/.idea/ | ||
/cmake-build-debug/ | ||
.vscode/ | ||
build/ | ||
|
||
*.exe |
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
cmake_minimum_required(VERSION 3.2) | ||
cmake_minimum_required(VERSION 3.26) | ||
project(c2023_challenge) | ||
|
||
set(CMAKE_C_STANDARD 11) | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD 23) | ||
|
||
add_subdirectory(level1) | ||
|
||
add_subdirectory(level2/PI) | ||
add_subdirectory(level2/SkipList) |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains hidden or 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 |
---|---|---|
@@ -1,21 +1,29 @@ | ||
project(level1) | ||
|
||
add_executable(p01_running_letter p01_running_letter/main.c) | ||
find_package(Curses REQUIRED) | ||
|
||
add_executable(p02_is_prime p02_is_prime/main.c) | ||
add_executable(p01_running_letter p01_running_letter/main.cpp) | ||
target_include_directories(p01_running_letter PRIVATE ${CURSES_INCLUDE_DIR}) | ||
target_link_libraries(p01_running_letter PRIVATE ${CURSES_LIBRARIES}) | ||
|
||
add_executable(p03_all_primes p03_all_primes/main.c) | ||
add_executable(p02_is_prime p02_is_prime/main.cpp) | ||
|
||
add_executable(p04_goldbach p04_goldbach/main.c) | ||
add_executable(p03_all_primes p03_all_primes/main.cpp) | ||
|
||
add_executable(p05_encrypt_decrypt p05_encrypt_decrypt/main.c) | ||
add_executable(p04_goldbach p04_goldbach/main.cpp) | ||
|
||
add_executable(p06_hanoi p06_hanoi/main.c) | ||
add_executable(p05_encrypt_decrypt p05_encrypt_decrypt/main.cpp) | ||
|
||
add_executable(p07_maze p07_maze/main.c) | ||
add_executable(p06_hanoi p06_hanoi/main.cpp) | ||
|
||
add_executable(p08_push_boxes p08_push_boxes/main.c) | ||
add_executable(p07_maze p07_maze/main.cpp) | ||
target_include_directories(p07_maze PRIVATE ${CURSES_INCLUDE_DIR}) | ||
target_link_libraries(p07_maze PRIVATE ${CURSES_LIBRARIES}) | ||
|
||
add_executable(p09_linked_list p09_linked_list/main.c) | ||
add_executable(p08_push_boxes p08_push_boxes/main.cpp) | ||
target_include_directories(p08_push_boxes PRIVATE ${CURSES_INCLUDE_DIR}) | ||
target_link_libraries(p08_push_boxes PRIVATE ${CURSES_LIBRARIES}) | ||
|
||
add_executable(p10_warehouse p10_warehouse/main.c) | ||
add_executable(p09_linked_list p09_linked_list/main.cpp) | ||
|
||
add_executable(p10_warehouse p10_warehouse/main.cpp) |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,25 @@ | ||
#include <cstdio> | ||
#include <thread> | ||
#include <curses.h> | ||
|
||
int main() { | ||
using namespace ::std; | ||
using namespace ::std::chrono_literals; | ||
|
||
initscr(); | ||
cbreak(); | ||
noecho(); | ||
|
||
auto f = [](int i) { | ||
mvaddch(0, i, '*'); | ||
refresh(); | ||
this_thread::sleep_for(50ms); | ||
clear(); | ||
}; | ||
while (true) { | ||
for (int i = 0; i < COLS; ++i) f(i); | ||
for (int i = COLS; i-- > 0; ) f(i); | ||
} | ||
|
||
return 0; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,18 @@ | ||
#include <iostream> | ||
|
||
bool is_prime(unsigned n) { | ||
if (n == 0 || n == 1) return false; | ||
for (unsigned i = 2; i * i <= n; ++i) | ||
if (n % i == 0) return false; | ||
return true; | ||
} | ||
|
||
int main() { | ||
using namespace std; | ||
|
||
unsigned n; | ||
cin >> n; | ||
cout << n << ' ' << (is_prime(n) ? "is" : "is not") << ' ' << "prime." << endl; | ||
|
||
return 0; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,35 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <chrono> | ||
|
||
// O(n) | ||
std::vector<unsigned> sieve(unsigned n) { | ||
std::vector<bool> v(n + 1); | ||
std::vector<unsigned> ps; | ||
|
||
for (unsigned i = 2; i <= n; ++i) { | ||
if (!v[i]) ps.push_back(i); | ||
for (unsigned p : ps) { | ||
if (i * p > n) break; | ||
v[i * p] = true; | ||
if (i % p == 0) break; | ||
} | ||
} | ||
|
||
return ps; | ||
} | ||
|
||
int main() { | ||
using namespace std; | ||
using namespace chrono; | ||
|
||
auto start = chrono::steady_clock::now(); | ||
auto primes = sieve(1000); | ||
auto end = chrono::steady_clock::now(); | ||
|
||
for (unsigned p : primes) cout << p << ' '; | ||
cout << endl; | ||
cout << "Time cost for the calculation: " << (end - start) << endl; | ||
|
||
return 0; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 <cstdio> | ||
#include <vector> | ||
|
||
int main() { | ||
using namespace std; | ||
|
||
constexpr int n = 100; | ||
vector<bool> v(n + 1); | ||
vector<int> ps; | ||
for (int i = 2; i <= n; ++i) { | ||
if (!v[i]) ps.push_back(i); | ||
for (int p : ps) { | ||
if (i * p > n) break; | ||
v[i * p] = true; | ||
if (i % p == 0) break; | ||
} | ||
} | ||
for (int i = 4; i <= n; i += 2) { | ||
for (int p : ps) { | ||
if (!v[i - p]) { | ||
printf("%d = %d + %d\n", i, p, i - p); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,116 @@ | ||
#include <bits/stdc++.h> | ||
|
||
namespace tkz::b64 { | ||
|
||
using byte = unsigned char; | ||
|
||
// clang-format off | ||
inline constexpr ::std::array<char, 64> to = { | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||
'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' | ||
}; | ||
// clang-format on | ||
|
||
inline constexpr ::std::array<byte, 256> from = [] { | ||
::std::array<byte, 256> from; | ||
::std::ranges::fill(from, 255); | ||
for (byte i = 0; i < 64; ++i) { | ||
from[to[i]] = i; | ||
} | ||
return from; | ||
}(); | ||
|
||
inline constexpr struct encode_fn { | ||
template <::std::input_iterator I, ::std::sentinel_for<I> S, ::std::output_iterator<char> O> | ||
requires ::std::convertible_to<::std::iter_value_t<I>, byte> | ||
static constexpr void operator()(I b, S e, O o) { | ||
byte s[3]; | ||
::std::size_t i = 0; | ||
while (b != e) { | ||
s[i++] = static_cast<byte>(*b++); | ||
if (i == 3) { | ||
*o++ = to[(s[0] & byte(0b11111100)) >> 2]; | ||
*o++ = to[((s[0] & byte(0b00000011)) << 4) | ((s[1] & byte(0b11110000)) >> 4)]; | ||
*o++ = to[((s[1] & byte(0b00001111)) << 2) | ((s[2] & byte(0b11000000)) >> 6)]; | ||
*o++ = to[s[2] & byte(0b00111111)]; | ||
i = 0; | ||
} | ||
} | ||
switch (i) { | ||
case 0: | ||
break; | ||
case 1: | ||
*o++ = to[(s[0] & byte(0b11111100)) >> 2]; | ||
*o++ = to[(s[0] & byte(0b00000011)) << 4]; | ||
*o++ = '='; | ||
*o++ = '='; | ||
break; | ||
case 2: | ||
*o++ = to[(s[0] & byte(0b11111100)) >> 2]; | ||
*o++ = to[((s[0] & byte(0b00000011)) << 4) | ((s[1] & byte(0b11110000)) >> 4)]; | ||
*o++ = to[(s[1] & byte(0b00001111)) << 2]; | ||
*o++ = '='; | ||
break; | ||
} | ||
} | ||
|
||
template <::std::ranges::range R, ::std::output_iterator<char> O> static constexpr void operator()(R &&r, O o) { | ||
return operator()(::std::ranges::begin(r), ::std::ranges::end(r), ::std::move(o)); | ||
} | ||
} encode{}; | ||
|
||
inline constexpr struct decode_fn { | ||
template <::std::input_iterator I, ::std::sentinel_for<I> S, ::std::output_iterator<byte> O> | ||
requires ::std::convertible_to<::std::iter_value_t<I>, char> | ||
static constexpr void operator()(I b, S e, O o) { | ||
char c[4]; | ||
::std::size_t i = 0; | ||
while (b != e) { | ||
if ((c[i++] = static_cast<char>(*b++)) == '=') { | ||
--i; | ||
break; | ||
} | ||
if (i == 4) { | ||
*o++ = from[c[0]] << 2 | from[c[1]] >> 4; | ||
*o++ = from[c[1]] << 4 | from[c[2]] >> 2; | ||
*o++ = from[c[2]] << 6 | from[c[3]]; | ||
i = 0; | ||
} | ||
} | ||
switch (i) { | ||
case 0: | ||
break; | ||
case 1: | ||
break; | ||
case 2: | ||
*o++ = from[c[0]] << 2 | from[c[1]] >> 4; | ||
break; | ||
case 3: | ||
*o++ = from[c[0]] << 2 | from[c[1]] >> 4; | ||
*o++ = from[c[1]] << 4 | from[c[2]] >> 2; | ||
break; | ||
} | ||
} | ||
|
||
template <::std::ranges::range R, ::std::output_iterator<char> O> static constexpr void operator()(R &&r, O o) { | ||
return operator()(::std::ranges::begin(r), ::std::ranges::end(r), ::std::move(o)); | ||
} | ||
} decode{}; | ||
|
||
} // namespace tkz::b64 | ||
|
||
int main() { | ||
using namespace std; | ||
string r = "hello world!"; | ||
cout << r << endl; | ||
string e; | ||
::tkz::b64::encode(r, back_inserter(e)); | ||
cout << e << endl; | ||
string d; | ||
::tkz::b64::decode(e, back_inserter(d)); | ||
cout << d << endl; | ||
return 0; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,20 @@ | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
void hanoi(int n, char a, char b, char c) { | ||
if (n == 1) { | ||
printf("%c -> %c\n", a, c); | ||
return; | ||
} | ||
hanoi(n - 1, a, c, b); | ||
printf("%c -> %c\n", a, c); | ||
hanoi(n - 1, b, a, c); | ||
} | ||
|
||
int main() { | ||
int n; | ||
scanf("%d", &n); | ||
hanoi(n, 'A', 'B', 'C'); | ||
return 0; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
与第7行重复了,能否消除掉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.