Skip to content

滕鎧泽的作业 #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
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.idea/
/cmake-build-debug/
.vscode/
build/

*.exe
7 changes: 5 additions & 2 deletions CMakeLists.txt
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 modified C语言学习笔记.md
100755 → 100644
Empty file.
Empty file modified README.md
100755 → 100644
Empty file.
Empty file modified level0/README.md
100755 → 100644
Empty file.
Empty file modified level0/bubbleSort/README.md
100755 → 100644
Empty file.
28 changes: 18 additions & 10 deletions level1/CMakeLists.txt
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 modified level1/p01_running_letter/README.md
100755 → 100644
Empty file.
6 changes: 0 additions & 6 deletions level1/p01_running_letter/main.c

This file was deleted.

25 changes: 25 additions & 0 deletions level1/p01_running_letter/main.cpp
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 modified level1/p02_is_prime/README.md
100755 → 100644
Empty file.
6 changes: 0 additions & 6 deletions level1/p02_is_prime/main.c

This file was deleted.

18 changes: 18 additions & 0 deletions level1/p02_is_prime/main.cpp
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 modified level1/p03_all_primes/README.md
100755 → 100644
Empty file.
6 changes: 0 additions & 6 deletions level1/p03_all_primes/main.c

This file was deleted.

35 changes: 35 additions & 0 deletions level1/p03_all_primes/main.cpp
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 modified level1/p04_goldbach/README.md
100755 → 100644
Empty file.
6 changes: 0 additions & 6 deletions level1/p04_goldbach/main.c

This file was deleted.

28 changes: 28 additions & 0 deletions level1/p04_goldbach/main.cpp
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 modified level1/p05_encrypt_decrypt/README.md
100755 → 100644
Empty file.
6 changes: 0 additions & 6 deletions level1/p05_encrypt_decrypt/main.c

This file was deleted.

116 changes: 116 additions & 0 deletions level1/p05_encrypt_decrypt/main.cpp
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 modified level1/p06_hanoi/README.md
100755 → 100644
Empty file.
6 changes: 0 additions & 6 deletions level1/p06_hanoi/main.c

This file was deleted.

20 changes: 20 additions & 0 deletions level1/p06_hanoi/main.cpp
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

与第7行重复了,能否消除掉

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void hanoi(int n, char a, char b, char c) {
	if (n == 0) return;
	hanoi(n - 1, a, c, b);
	printf("%c -> %c\n", a, c);
	hanoi(n - 1, b, a, c);
}

hanoi(n - 1, b, a, c);
}

int main() {
int n;
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
Empty file modified level1/p07_maze/README.md
100755 → 100644
Empty file.
6 changes: 0 additions & 6 deletions level1/p07_maze/main.c

This file was deleted.

Loading