diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index c281a00..68352ba 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -23,22 +23,24 @@
+
+
-
-
+
-
+
+
@@ -50,6 +52,7 @@
+
@@ -61,18 +64,20 @@
+
+
+
+
-
-
@@ -81,66 +86,75 @@
+
+
+
+
-
+
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -159,8 +173,8 @@
-
+
@@ -173,6 +187,10 @@
+
+
+
+
@@ -246,7 +264,15 @@
"CMake 应用程序.cs101__dh_20260923_01_bike_or_walk.executor": "Run",
"CMake 应用程序.cs101__dh_20260923_02_draw_rectangles.executor": "Run",
"CMake 应用程序.cs101__dh_20260923_03.executor": "Run",
+ "CMake 应用程序.cs101__dh_20260923_04_lamps.executor": "Run",
+ "CMake 应用程序.cs101__dh_20260923_05_perfect_cubes.executor": "Run",
+ "CMake 应用程序.cs101__dh_20260923_11_fibonacci.executor": "Run",
+ "CMake 应用程序.cs101__sh_20260923_14_balloons.executor": "Run",
"CMake 应用程序.dp__TJOI2007_walk_segments_dp.executor": "Run",
+ "CMake 应用程序.dsa__ch3_stack_queue__dsa_0301_sliding_window.executor": "Run",
+ "CMake 应用程序.dsa__ch3_stack_queue__dsa_0301_sliding_window_SLOW.executor": "Run",
+ "CMake 应用程序.dsa__ch3_stack_queue__dsa_0302_calculator.executor": "Run",
+ "CMake 应用程序.dsa__ch3_stack_queue__dsa_0306_stack_operation.executor": "Run",
"CMake 应用程序.dsa__linear_list__dsa_0201_dictionary.executor": "Run",
"CMake 应用程序.dsa__linear_list__dsa_0202_card_stacking.executor": "Run",
"CMake 应用程序.dsa__linear_list__dsa_0204_prefix_cycle.executor": "Run",
@@ -277,6 +303,7 @@
"CidrDebugProfileConversion": "true",
"ModuleVcsDetector.initialDetectionPerformed": "true",
"Python.dh_20260909_11_count_digits_init.executor": "Run",
+ "Python.dh_20260923_14_balloons_init1.executor": "Run",
"Python.lc_127.executor": "Run",
"Python.noi2001_food_chain_dsu.executor": "Run",
"Python.pojcs101_M03247_palindromic_prime_init1.executor": "Debug",
@@ -293,21 +320,24 @@
"codeWithMe.voiceChat.enabledByDefault": "false",
"deletionFromPopupRequiresConfirmation": "false",
"git-widget-placeholder": "main",
- "last_opened_file_path": "/Users/roger/coding/exercises/_others/jzh",
+ "last_opened_file_path": "/Users/roger/coding/exercises/cs101",
"node.js.detected.package.jshint": "true",
"node.js.selected.package.jshint": "",
"nodejs_package_manager_path": "npm",
"org.rust.first.attach.projects": "true",
- "settings.editor.selected.configurable": "fileTemplates"
+ "settings.editor.selected.configurable": "preferences.keymap"
}
}]]>
+
+
+
-
+
+
-
@@ -317,7 +347,7 @@
-
+
@@ -383,12 +413,7 @@
-
-
-
-
-
-
+
@@ -403,12 +428,92 @@
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -418,72 +523,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -533,37 +573,62 @@
-
+
-
+
-
+
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -783,7 +848,7 @@
-
+
@@ -798,7 +863,31 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -833,57 +922,66 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -911,21 +1009,21 @@
-
-
-
+
-
+
+
+
-
+
@@ -939,7 +1037,7 @@
},
{
"first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__linear_list__dsa_0202_card_stacking",
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch3_stack_queue__dsa_0301_sliding_window",
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
@@ -953,14 +1051,14 @@
},
{
"first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260917_02_subway",
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch3_stack_queue__dsa_0301_sliding_window_SLOW",
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
{
"first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__linear_list__dsa_0206_binary_query",
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260917_02_subway",
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
@@ -974,7 +1072,7 @@
},
{
"first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260917_04",
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260923_05_perfect_cubes",
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
@@ -986,6 +1084,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260917_04",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.NOIP2016J_bay",
@@ -1002,7 +1107,7 @@
},
{
"first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__linear_list__dsa_0205_polynomial_addition",
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch3_stack_queue__dsa_0302_calculator",
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
@@ -1042,27 +1147,6 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
- {
- "first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__linear_list__dsa_0217_magic_square",
- "cmake": "CMakeBuildProfile:Debug-GCC"
- },
- "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
- },
- {
- "first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序._others__qyz__gdbh",
- "cmake": "CMakeBuildProfile:Debug-GCC"
- },
- "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
- },
- {
- "first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__linear_list__dsa_0204_prefix_cycle",
- "cmake": "CMakeBuildProfile:Debug-GCC"
- },
- "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
- },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260923_02_draw_rectangles",
@@ -1070,6 +1154,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__sh_20260923_14_balloons",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260909_12_bottle_exchange",
@@ -1079,14 +1170,14 @@
},
{
"first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__linear_list__dsa_0201_dictionary",
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260917_03",
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
{
"first": {
- "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260917_03",
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260923_04_lamps",
"cmake": "CMakeBuildProfile:Debug-GCC"
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
@@ -1112,6 +1203,13 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.dsa__ch3_stack_queue__dsa_0306_stack_operation",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260909_15_jusephus",
@@ -1140,6 +1238,19 @@
},
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
},
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260923_11_fibonacci",
+ "cmake": "CMakeBuildProfile:Debug-GCC"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
+ {
+ "first": {
+ "##RUN_CONFIGURATION##": "Python.dh_20260923_14_balloons_init1"
+ },
+ "second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
+ },
{
"first": {
"##RUN_CONFIGURATION##": "CMake 应用程序.cs101__dh_20260923_01_bike_or_walk",
@@ -1148,7 +1259,7 @@
"second": "2c1c9069-eeff-46e4-9092-db62e2b1ded5"
}
],
- "modificationCount": 133
+ "modificationCount": 154
}]]>
@@ -1186,7 +1297,8 @@
-
+
+
@@ -1196,6 +1308,11 @@
75
+
+ file://$PROJECT_DIR$/cs101/dh_04_04_balloons_init1.py
+ 9
+
+
file://$PROJECT_DIR$/pojcs101_M03441_sum_zero.cpp
32
@@ -1217,22 +1334,47 @@
- file://$PROJECT_DIR$/dsa/linear_list/dsa_0202_card_stacking.cpp
+ file://$PROJECT_DIR$/dsa/ch2_linear_list/dsa_0202_card_stacking.cpp
40
- file://$PROJECT_DIR$/cs101/dh_20260917_01.cpp
+ file://$PROJECT_DIR$/cs101/dh_01_01.cpp
5
+
+ file://$PROJECT_DIR$/cs101/dh_03_05_perfect_cubes.cpp
+ 43
+
+
+
+ file://$PROJECT_DIR$/dsa/ch3_stack_queue/dsa_0301_sliding_window.cpp
+ 69
+
+
+
+ file://$PROJECT_DIR$/dsa/ch3_stack_queue/dsa_0306_stack_operation.cpp
+ 24
+
+
+
+ file://$PROJECT_DIR$/dsa/ch3_stack_queue/dsa_0302_calculator_WRONG.cpp
+ 99
+
+
+
+ file://$PROJECT_DIR$/dsa/ch3_stack_queue/dsa_0302_calculator.cpp
+ 102
+
+
+
+ file://$PROJECT_DIR$/cs101/sh_04_04_balloons.cpp
+ 10
+
+
-
-
-
-
-
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 588d036..af81406 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(exercises C CXX)
-set(CMAKE_C_STANDARD 11)
+set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
diff --git a/_others/others_test.c.cpp b/_others/others_test.c.cpp
new file mode 100644
index 0000000..972cab0
--- /dev/null
+++ b/_others/others_test.c.cpp
@@ -0,0 +1,50 @@
+#include
+
+int main() {
+ long long T; //注意变量都要初始化 ,T为数组个数
+ scanf("%lld", &T); //数组个数,决定过程要执行多少次
+ for (int i = 0; i < T; i++) {
+ long long n, m, bianhao, yushu, max = -1, index; //注意变量都要初始化,n为包裹个数,m为箱子个数,result是余数,max是装的最多
+ //以上变量必须定义在循环里,因为一组过后所有数据要重新开始!要不然max,index值未清除!
+ scanf("%lld%lld", &n, &m); //键入包裹的个数和箱子的个数
+ long long Amount[m], number[n]; //Amount是定义对应的箱子里装几个 ,number是用数组记录包裹的“编号”
+ //禁止写错数组个数!
+ for (int j = 0; j < m; j++) //初始化箱子数组 ,注意数组是m个
+ {
+ Amount[j] = 0;
+ }
+ for (int k = 0; k < n; k++) //把包裹赋值,记录到number里 注意数组是n个
+ {
+ scanf("%lld", &bianhao);
+ number[k] = bianhao;
+ }
+ for (int s = 0; s < n; s++) //进行mol m运算,余数计入number里
+ {
+ yushu = number[s] % m;
+ Amount[yushu] = Amount[yushu] + 1;
+ }
+ for (int t = 0; t < m; t++) //此循环计算哪个箱子装的最多
+ {
+ //max=-1;错误 在循环里定义会导致max每走一个t都会被重置!
+ if (max < Amount[t]) {
+ max = Amount[t];
+ }
+ }
+ for (int u = 0; u < m; u++) //此循环寻找下标
+ {
+ if (Amount[u] == max) {
+ index = u;
+ break;
+ }
+ }
+ for (int v = 0; v < m; v++) //此循环输出amount数组里各个元素的大小
+ {
+ printf("%lld ", Amount[v]);
+ }
+ printf("\n");
+ printf("%lld %lld", index, max);
+ printf("\n");
+ }
+ return 0;
+}
+
diff --git a/_others/qyz/gdbh.cpp b/_others/qyz/gdbh.cpp
deleted file mode 100644
index b28f11b..0000000
--- a/_others/qyz/gdbh.cpp
+++ /dev/null
@@ -1,43 +0,0 @@
-#include
-
-using namespace std;
-
-int primes[2001];
-
-bool is_prime(int a) {
- for (int i = 2; i * i <= a; i++) {
- if (a % i != 0) {
- continue;
- } else {
- return false;
- }
- }
- return true;
-}
-
-void init_primes() {
- int current_num = 2, i = 0;
- for (;; current_num++) {
- if (is_prime(current_num)) {
- primes[i] = current_num;
- i++;
- }
- }
-}
-
-int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int a;
- cin >> a;
- if (a % 2 != 0 || a < 6) {
- cout << "Error!" << endl;
- } else
- for (int i = 0; primes[i] <= a / 2; i++) {
- if (is_prime(a - primes[i])) {
- cout << a << "=" << i << "+" << a - i << endl;
- }
- }
-
- return 0;
-}
diff --git a/_others/qyz/test.cpp b/_others/qyz/test.cpp
deleted file mode 100644
index 8d6ba74..0000000
--- a/_others/qyz/test.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-#include
-
-using namespace std;
-
-
-// x[0]..x[99]
-
-bool is_prime(int n) {
- for (int i=2; i*i<=n; i++) {
- if (n%i == 0) {return false;}
- }
- return true;
-}
-
-int main() {
- cout << is_prime(4);
- return 0;
-}
\ No newline at end of file
diff --git a/cs101/dh_20260909_05_sum_fractions.cpp b/cs101/dh_00_05_sum_fractions.cpp
similarity index 100%
rename from cs101/dh_20260909_05_sum_fractions.cpp
rename to cs101/dh_00_05_sum_fractions.cpp
diff --git a/cs101/dh_20260909_10_smaller_numbers.cpp b/cs101/dh_00_10_smaller_numbers.cpp
similarity index 100%
rename from cs101/dh_20260909_10_smaller_numbers.cpp
rename to cs101/dh_00_10_smaller_numbers.cpp
diff --git a/cs101/dh_20260909_11_count_digits_init.py b/cs101/dh_00_11_count_digits_init.py
similarity index 100%
rename from cs101/dh_20260909_11_count_digits_init.py
rename to cs101/dh_00_11_count_digits_init.py
diff --git a/cs101/dh_20260909_12_bottle_exchange.cpp b/cs101/dh_00_12_bottle_exchange.cpp
similarity index 100%
rename from cs101/dh_20260909_12_bottle_exchange.cpp
rename to cs101/dh_00_12_bottle_exchange.cpp
diff --git a/cs101/dh_20260909_13_flip.cpp b/cs101/dh_00_13_flip.cpp
similarity index 100%
rename from cs101/dh_20260909_13_flip.cpp
rename to cs101/dh_00_13_flip.cpp
diff --git a/cs101/dh_20260909_14_encryption.cpp b/cs101/dh_00_14_encryption.cpp
similarity index 100%
rename from cs101/dh_20260909_14_encryption.cpp
rename to cs101/dh_00_14_encryption.cpp
diff --git a/cs101/dh_20260909_15_jusephus.cpp b/cs101/dh_00_15_jusephus.cpp
similarity index 100%
rename from cs101/dh_20260909_15_jusephus.cpp
rename to cs101/dh_00_15_jusephus.cpp
diff --git a/cs101/dh_20260909_16_swap_bits.cpp b/cs101/dh_00_16_swap_bits.cpp
similarity index 100%
rename from cs101/dh_20260909_16_swap_bits.cpp
rename to cs101/dh_00_16_swap_bits.cpp
diff --git a/cs101/dh_20260917_01.cpp b/cs101/dh_01_01.cpp
similarity index 100%
rename from cs101/dh_20260917_01.cpp
rename to cs101/dh_01_01.cpp
diff --git a/cs101/dh_20260917_02_subway.cpp b/cs101/dh_01_02_subway.cpp
similarity index 100%
rename from cs101/dh_20260917_02_subway.cpp
rename to cs101/dh_01_02_subway.cpp
diff --git a/cs101/dh_20260917_03.cpp b/cs101/dh_01_03.cpp
similarity index 100%
rename from cs101/dh_20260917_03.cpp
rename to cs101/dh_01_03.cpp
diff --git a/cs101/dh_20260917_04.cpp b/cs101/dh_01_04.cpp
similarity index 100%
rename from cs101/dh_20260917_04.cpp
rename to cs101/dh_01_04.cpp
diff --git a/cs101/dh_20260917_05_vote.cpp b/cs101/dh_01_05_vote.cpp
similarity index 100%
rename from cs101/dh_20260917_05_vote.cpp
rename to cs101/dh_01_05_vote.cpp
diff --git a/cs101/dh_20260923_01_bike_or_walk.cpp b/cs101/dh_03_01_bike_or_walk.cpp
similarity index 100%
rename from cs101/dh_20260923_01_bike_or_walk.cpp
rename to cs101/dh_03_01_bike_or_walk.cpp
diff --git a/cs101/dh_20260923_02_draw_rectangles.cpp b/cs101/dh_03_02_draw_rectangles.cpp
similarity index 100%
rename from cs101/dh_20260923_02_draw_rectangles.cpp
rename to cs101/dh_03_02_draw_rectangles.cpp
diff --git a/cs101/dh_20260923_03.cpp b/cs101/dh_03_03.cpp
similarity index 100%
rename from cs101/dh_20260923_03.cpp
rename to cs101/dh_03_03.cpp
diff --git a/cs101/dh_20260923_04_lamps.cpp b/cs101/dh_03_04_lamps.cpp
similarity index 100%
rename from cs101/dh_20260923_04_lamps.cpp
rename to cs101/dh_03_04_lamps.cpp
diff --git a/cs101/dh_03_05_perfect_cubes.cpp b/cs101/dh_03_05_perfect_cubes.cpp
new file mode 100644
index 0000000..4168a6d
--- /dev/null
+++ b/cs101/dh_03_05_perfect_cubes.cpp
@@ -0,0 +1,54 @@
+#include
+#include
+#include
+#include
+
+struct PerfectCubeNumber {
+ int num = 0;
+ std::tuple children;
+
+ constexpr bool operator<(const PerfectCubeNumber& other) const {
+ return std::tie(num, children) < std::tie(other.num, other.children);
+ }
+};
+
+constexpr int MAX_N = 101;
+std::vector perfect_cube_numbers;
+int n;
+
+constexpr int cubes_sum(int a, int b, int c) {
+ return a * a * a + b * b * b + c * c * c;
+}
+
+void init_numbers() {
+ for (int a = 2; a < MAX_N; a++) {
+ for (int b = a; b < MAX_N; b++) {
+ for (int c = b; ; c++) {
+ const int x_cube = cubes_sum(a, b, c);
+ if (x_cube > MAX_N * MAX_N * MAX_N) { break; }
+ for (int x = std::floor(std::cbrt(x_cube)), i = 0; i < 3; x++, i++) {
+ if (x * x * x == x_cube) {
+ perfect_cube_numbers.push_back({x, std::tuple(a, b, c)});
+ }
+ }
+ }
+ }
+ }
+ std::sort(perfect_cube_numbers.begin(), perfect_cube_numbers.end());
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ init_numbers();
+
+ std::cin >> n;
+ for (const auto [num, children] : perfect_cube_numbers) {
+ if (num > n) { break; }
+ auto [a,b,c] = children;
+ std::cout << "Cube = " << num << ", Triple = (" << a << "," << b << "," << c << ")" << std::endl;
+ }
+
+ return 0;
+}
diff --git a/cs101/dh_04_01_fibonacci.cpp b/cs101/dh_04_01_fibonacci.cpp
new file mode 100644
index 0000000..6577c27
--- /dev/null
+++ b/cs101/dh_04_01_fibonacci.cpp
@@ -0,0 +1,31 @@
+#include
+#include
+
+constexpr int MAX_N = 21;
+
+// 编译期生成斐波那契数列
+constexpr std::array make_fib() {
+ std::array fib{}; // 零初始化
+ fib[0] = 1;
+ fib[1] = 1;
+ for (int i = 2; i < MAX_N; ++i) {
+ fib[i] = fib[i - 1] + fib[i - 2];
+ }
+ return fib;
+}
+
+static constexpr auto fib = make_fib();
+
+int x;
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> x;
+ while (std::cin >> x) {
+ std::cout << fib[x - 1] << std::endl;
+ }
+
+ return 0;
+}
diff --git a/cs101/dh_04_04_balloons_init1.py b/cs101/dh_04_04_balloons_init1.py
new file mode 100644
index 0000000..b4000bf
--- /dev/null
+++ b/cs101/dh_04_04_balloons_init1.py
@@ -0,0 +1,17 @@
+def solve(n: int) -> int:
+ balloons = [i for i in range(1, n + 1)]
+ while True:
+ deleted_amount = 0
+ for x in range(1, n):
+ if len(balloons) <= x * x - deleted_amount - 1 or len(balloons) == 1:
+ break
+ del balloons[x * x - deleted_amount - 1]
+ deleted_amount += 1
+ if len(balloons) == 1:
+ break
+ return balloons[0]
+
+
+if __name__ == '__main__':
+ for n in range(1, 1150):
+ print(n, "->", solve(n))
diff --git a/cs101/sh_04_04_balloons.cpp b/cs101/sh_04_04_balloons.cpp
new file mode 100644
index 0000000..59d196e
--- /dev/null
+++ b/cs101/sh_04_04_balloons.cpp
@@ -0,0 +1,26 @@
+/*
+ * 本来想打表的,打着打着发现了一些数学规律。具体怎么证明嘛……🤷
+ */
+
+#include
+#include
+
+constexpr size_t MAX_N = 10001;
+
+int get_ans(int n) {
+ const int k = ceil(-0.5 + 0.5 * sqrt(4 * n + 1));
+ const bool extra = k * k < n;
+ const int ans = k * (k - 1) + 1 + k * extra;
+ return ans;
+}
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n;
+ std::cin >> n;
+ std::cout << get_ans(n) << std::endl;
+
+ return 0;
+}
diff --git a/dsa/linear_list/dsa_0201_dictionary.cpp b/dsa/ch2_linear_list/dsa_0201_dictionary.cpp
similarity index 100%
rename from dsa/linear_list/dsa_0201_dictionary.cpp
rename to dsa/ch2_linear_list/dsa_0201_dictionary.cpp
diff --git a/dsa/linear_list/dsa_0202_card_stacking.cpp b/dsa/ch2_linear_list/dsa_0202_card_stacking.cpp
similarity index 100%
rename from dsa/linear_list/dsa_0202_card_stacking.cpp
rename to dsa/ch2_linear_list/dsa_0202_card_stacking.cpp
diff --git a/dsa/linear_list/dsa_0203_string_insertion.cpp b/dsa/ch2_linear_list/dsa_0203_string_insertion.cpp
similarity index 100%
rename from dsa/linear_list/dsa_0203_string_insertion.cpp
rename to dsa/ch2_linear_list/dsa_0203_string_insertion.cpp
diff --git a/dsa/linear_list/dsa_0204_prefix_cycle.cpp b/dsa/ch2_linear_list/dsa_0204_prefix_cycle.cpp
similarity index 100%
rename from dsa/linear_list/dsa_0204_prefix_cycle.cpp
rename to dsa/ch2_linear_list/dsa_0204_prefix_cycle.cpp
diff --git a/dsa/linear_list/dsa_0205_polynomial_addition.cpp b/dsa/ch2_linear_list/dsa_0205_polynomial_addition.cpp
similarity index 100%
rename from dsa/linear_list/dsa_0205_polynomial_addition.cpp
rename to dsa/ch2_linear_list/dsa_0205_polynomial_addition.cpp
diff --git a/dsa/linear_list/dsa_0206_binary_query.cpp b/dsa/ch2_linear_list/dsa_0206_binary_query.cpp
similarity index 100%
rename from dsa/linear_list/dsa_0206_binary_query.cpp
rename to dsa/ch2_linear_list/dsa_0206_binary_query.cpp
diff --git a/dsa/linear_list/dsa_0217_magic_square.cpp b/dsa/ch2_linear_list/dsa_0217_magic_square.cpp
similarity index 100%
rename from dsa/linear_list/dsa_0217_magic_square.cpp
rename to dsa/ch2_linear_list/dsa_0217_magic_square.cpp
diff --git a/dsa/ch3_stack_queue/dsa_0301_sliding_window.cpp b/dsa/ch3_stack_queue/dsa_0301_sliding_window.cpp
new file mode 100644
index 0000000..aa3b934
--- /dev/null
+++ b/dsa/ch3_stack_queue/dsa_0301_sliding_window.cpp
@@ -0,0 +1,111 @@
+#include
+#include
+#include
+
+constexpr size_t MAX_N = 1e6 + 1;
+constexpr unsigned long long SOLVE_1_THRESHOLD = 0;
+
+template
+class QueueWithMinMax {
+protected:
+ std::deque max_queue, min_queue;
+ // std::priority_queue, std::greater<>> min_queue;
+
+public:
+ std::queue q;
+
+ void push(const T& x) {
+ q.push(x);
+ while (!max_queue.empty() && max_queue.back() < x) {
+ max_queue.pop_back();
+ }
+ max_queue.push_back(x);
+ while (!min_queue.empty() && min_queue.back() > x) {
+ min_queue.pop_back();
+ }
+ min_queue.push_back(x);
+ }
+
+ void pop() {
+ if (!max_queue.empty() && q.front() >= max_queue.front()) {
+ max_queue.pop_front();
+ }
+ if (!min_queue.empty() && q.front() <= min_queue.front()) {
+ min_queue.pop_front();
+ }
+ q.pop();
+ }
+
+ [[nodiscard]] T front() const { return q.front(); }
+
+ [[nodiscard]] T get_max() const {
+ return max_queue.front();
+ }
+
+ [[nodiscard]] T get_min() const {
+ return min_queue.front();
+ }
+};
+
+int n, window_length;
+int nums[MAX_N], max_nums[MAX_N], min_nums[MAX_N];
+QueueWithMinMax queue;
+
+
+void solve_1() {
+ for (int i = 0; i + window_length <= n; i++) {
+ max_nums[i] = *std::max_element(nums + i, nums + i + window_length);
+ min_nums[i] = *std::min_element(nums + i, nums + i + window_length);
+ }
+}
+
+void solve_2() {
+ for (int i = 0; i < window_length; i++) {
+ queue.push(nums[i]);
+ }
+
+ for (int i = 0; i + window_length < n; i++) {
+ max_nums[i] = queue.get_max();
+ min_nums[i] = queue.get_min();
+ queue.pop();
+ queue.push(nums[i + window_length]);
+ }
+ max_nums[n - window_length] = queue.get_max();
+ min_nums[n - window_length] = queue.get_min();
+}
+
+void print_ans() {
+ for (int i = 0; i <= n - window_length; i++) {
+ std::cout << min_nums[i] << " ";
+ }
+ std::cout << std::endl;
+ for (int i = 0; i <= n - window_length; i++) {
+ std::cout << max_nums[i] << " ";
+ }
+ std::cout << std::endl;
+}
+
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> n >> window_length;
+ if (window_length > n) { window_length = n; }
+ for (int i = 0; i < n; i++) {
+ std::cin >> nums[i];
+ }
+
+ if (1ll * n * window_length < SOLVE_1_THRESHOLD) {
+ solve_1();
+ } else {
+ solve_2();
+ }
+
+ // solve_1();
+ // print_ans();
+ // solve_2();
+ print_ans();
+
+ return 0;
+}
diff --git a/dsa/ch3_stack_queue/dsa_0301_sliding_window_SLOW.cpp b/dsa/ch3_stack_queue/dsa_0301_sliding_window_SLOW.cpp
new file mode 100644
index 0000000..65aff0b
--- /dev/null
+++ b/dsa/ch3_stack_queue/dsa_0301_sliding_window_SLOW.cpp
@@ -0,0 +1,38 @@
+#include
+#include
+#include
+
+constexpr size_t MAX_N = 1e6 + 1;
+
+
+int n, window_length;
+int nums[MAX_N], max_nums[MAX_N], min_nums[MAX_N];
+
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> n >> window_length;
+ if (window_length > n) { window_length = n; }
+ for (int i = 0; i < n; i++) {
+ std::cin >> nums[i];
+ }
+
+ for (int i = 0; i + window_length <= n; i++) {
+ max_nums[i] = *std::max_element(nums + i, nums + i + window_length);
+ min_nums[i] = *std::min_element(nums + i, nums + i + window_length);
+ }
+
+ for (int i = 0; i <= n - window_length; i++) {
+ std::cout << min_nums[i] << " ";
+ }
+ std::cout << std::endl;
+ for (int i = 0; i <= n - window_length; i++) {
+ std::cout << max_nums[i] << " ";
+ }
+ std::cout << std::endl;
+
+
+ return 0;
+}
diff --git a/dsa/ch3_stack_queue/dsa_0302_calculator.cpp b/dsa/ch3_stack_queue/dsa_0302_calculator.cpp
new file mode 100644
index 0000000..f0332b2
--- /dev/null
+++ b/dsa/ch3_stack_queue/dsa_0302_calculator.cpp
@@ -0,0 +1,165 @@
+#include
+#include
+#include
+#include
+#include
+
+class Token {
+protected:
+ std::variant token_variant_;
+
+public:
+ explicit Token(char c) : token_variant_(c) {}
+ explicit Token(int x) : token_variant_(x) {}
+
+ [[nodiscard]] bool is_operator() const {
+ return std::holds_alternative(token_variant_);
+ }
+
+ [[nodiscard]] char get_operator() const {
+ if (!is_operator()) { throw std::invalid_argument("Not an operator"); }
+ return std::get(token_variant_);
+ }
+
+ [[nodiscard]] int get_number() const {
+ if (is_operator()) { throw std::invalid_argument("Is an operator"); }
+ return std::get(token_variant_);
+ }
+
+ [[nodiscard]] int get_priority() const {
+ if (get_operator() == '+' || get_operator() == '-') { return 1; }
+ if (get_operator() == '*' || get_operator() == '/') { return 2; }
+ throw std::invalid_argument("Unknown priority");
+ }
+
+ template
+ [[nodiscard]] auto calc_operator(T1 a, T2 b) const {
+ switch (get_operator()) {
+ case '+': return a + b;
+ case '-': return a - b;
+ case '*': return a * b;
+ case '/': return a / b;
+ default: throw std::invalid_argument("Uncalculatable operator");
+ }
+ }
+
+ bool operator==(char c) const {
+ if (!is_operator()) { return false; }
+ return get_operator() == c;
+ }
+
+ bool operator==(int x) const {
+ if (is_operator()) { return false; }
+ return get_number() == x;
+ }
+
+ template
+ bool operator!=(T x) {
+ return !(*this == x);
+ }
+
+ friend std::ostream& operator<<(std::ostream& os, Token token) {
+ if (token.is_operator()) {
+ os << token.get_operator();
+ } else {
+ os << '[' << token.get_number() << ']';
+ }
+ return os;
+ }
+};
+
+using Expression = std::vector;
+
+std::ostream& operator<<(std::ostream& os, const Expression& expression) {
+ for (const auto& token : expression) {
+ os << token;
+ }
+ return os;
+}
+
+Expression parse_string(const std::string& str) {
+ Expression expression;
+ std::string temp_token;
+ for (char c : str) {
+ if (!(c >= '0' && c <= '9')) {
+ if (!temp_token.empty()) {
+ expression.emplace_back(std::stoi(temp_token));
+ temp_token.clear();
+ }
+ expression.emplace_back(c);
+ } else {
+ temp_token += c;
+ }
+ }
+ if (!temp_token.empty()) { expression.emplace_back(std::stoi(temp_token)); }
+ return expression;
+}
+
+Expression convert_to_postfix(const Expression& infix_expression) {
+ Expression postfix_expression;
+ std::stack operators;
+
+ for (const auto& token : infix_expression) {
+ if (!token.is_operator()) {
+ postfix_expression.push_back(token);
+ continue;
+ }
+ if (token.get_operator() == '(') {
+ operators.push(token);
+ continue;
+ }
+ if (token.get_operator() == ')') {
+ while (operators.top().get_operator() != '(') {
+ postfix_expression.push_back(operators.top());
+ operators.pop();
+ }
+ operators.pop();
+ continue;
+ }
+ while (!operators.empty() && operators.top() != '('
+ && token.get_priority() <= operators.top().get_priority()) {
+ postfix_expression.push_back(operators.top());
+ operators.pop();
+ }
+ operators.push(token);
+ }
+ while (!operators.empty()) {
+ postfix_expression.push_back(operators.top());
+ operators.pop();
+ }
+ return postfix_expression;
+}
+
+int calculate_postfix(const Expression& postfix_expression) {
+ std::stack numbers;
+ for (const auto& token : postfix_expression) {
+ if (!token.is_operator()) {
+ numbers.push(token.get_number());
+ continue;
+ }
+ int b = numbers.top();
+ numbers.pop();
+ int a = numbers.top();
+ numbers.pop();
+ numbers.push(token.calc_operator(a, b));
+ }
+ assert(numbers.size() == 1);
+ return numbers.top();
+}
+
+std::string s;
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> s;
+ while (std::cin >> s) {
+ Expression infix_expression = parse_string(s);
+ std::clog << infix_expression << std::endl;
+ Expression postfix_expression = convert_to_postfix(infix_expression);
+ std::cout << calculate_postfix(postfix_expression) << std::endl;
+ }
+
+ return 0;
+}
diff --git a/dsa/ch3_stack_queue/dsa_0302_calculator_WRONG.cpp b/dsa/ch3_stack_queue/dsa_0302_calculator_WRONG.cpp
new file mode 100644
index 0000000..e0b8956
--- /dev/null
+++ b/dsa/ch3_stack_queue/dsa_0302_calculator_WRONG.cpp
@@ -0,0 +1,137 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+
+struct Token {
+ std::variant token;
+
+ explicit Token(char c) : token(c) {}
+ explicit Token(int x) : token(x) {}
+
+ [[nodiscard]] bool is_operator() const {
+ return std::holds_alternative(token);
+ }
+
+ [[nodiscard]] char get_operator() const {
+ if (!is_operator()) { throw std::invalid_argument("Not an operator"); }
+ return std::get(token);
+ }
+
+ [[nodiscard]] int get_number() const {
+ if (is_operator()) { throw std::invalid_argument("Is an operator"); }
+ return std::get(token);
+ }
+
+ bool operator==(char c) const {
+ if (!is_operator()) { return false; }
+ return get_operator() == c;
+ }
+
+ bool operator==(int x) const {
+ if (is_operator()) { return false; }
+ return get_number() == x;
+ }
+
+ template
+ bool operator!=(T x) {
+ return !(*this == x);
+ }
+};
+
+using Expression = std::vector;
+
+Expression parse_string(const std::string& str) {
+ Expression expression;
+ std::string temp_token;
+ for (char c : str) {
+ if (!(c > '0' && c < '9')) {
+ if (!temp_token.empty()) {
+ expression.emplace_back(std::stoi(temp_token));
+ temp_token.clear();
+ }
+ expression.emplace_back(c);
+ } else {
+ temp_token += c;
+ }
+ }
+ if (!temp_token.empty()) { expression.emplace_back(std::stoi(temp_token)); }
+ return expression;
+}
+
+void calculate_chunk(std::stack& undealt_tokens, std::stack& ready_tokens) {
+ while (!undealt_tokens.empty() && undealt_tokens.top() != '(') {
+ ready_tokens.push(undealt_tokens.top());
+ undealt_tokens.pop();
+ }
+ int temp_result = ready_tokens.top().get_number();
+ ready_tokens.pop();
+ while (!ready_tokens.empty()) {
+ char op = ready_tokens.top().get_operator();
+ ready_tokens.pop();
+ if (op == '+') {
+ temp_result += ready_tokens.top().get_number();
+ } else if (op == '-') {
+ temp_result -= ready_tokens.top().get_number();
+ } else {
+ throw std::logic_error("Unexpected operator token in chunk.");
+ }
+ ready_tokens.pop();
+ }
+ if (!undealt_tokens.empty()) {
+ undealt_tokens.pop();
+ }
+ undealt_tokens.emplace(temp_result);
+}
+
+int calculate(const Expression& expression) {
+ // 策略:
+ // 遇到 * / 直接算(本质上是把上一个子 expression 倒栈)
+ // 遇到右括号 / 结尾开始倒栈
+ // 对于倒栈,只剩下 +- 需要处理。
+ std::stack undealt_tokens, ready_tokens;
+ std::optional temp_token;
+
+ for (const auto& token : expression) {
+ if (token == '*' || token == '/') {
+ temp_token = token;
+ continue;
+ }
+ if (temp_token.has_value()) {
+ int a = undealt_tokens.top().get_number();
+ undealt_tokens.pop();
+ undealt_tokens.emplace(
+ temp_token == '*' ? a * token.get_number() : a / token.get_number()
+ );
+ temp_token.reset();
+ continue;
+ }
+ if (token == ')') {
+ calculate_chunk(undealt_tokens, ready_tokens);
+ continue;
+ }
+ undealt_tokens.push(token);
+ }
+ calculate_chunk(undealt_tokens, ready_tokens);
+ assert(undealt_tokens.size() == 1);
+ return undealt_tokens.top().get_number();
+}
+
+int n;
+std::string s;
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+ std::cin >> n;
+ for (int _ = 0; _ < n; _++) {
+ std::cin >> s;
+ std::cout << calculate(parse_string(s)) << std::endl;
+ }
+
+ return 0;
+}
diff --git a/dsa/ch3_stack_queue/dsa_0306_stack_operation.cpp b/dsa/ch3_stack_queue/dsa_0306_stack_operation.cpp
new file mode 100644
index 0000000..42dea05
--- /dev/null
+++ b/dsa/ch3_stack_queue/dsa_0306_stack_operation.cpp
@@ -0,0 +1,46 @@
+#include
+#include
+#include
+
+using Operation = std::pair;
+
+int n, idx = 0;
+std::stack stack;
+std::vector sequence;
+std::vector operation_log;
+
+
+int main() {
+ std::ios::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ std::cin >> n;
+ operation_log.reserve(2 * n);
+ sequence.resize(n);
+ for (int i = 0; i < n; i++) {
+ std::cin >> sequence[i];
+ }
+
+ for (int current_num = 1; current_num <= n;) {
+ while ((stack.empty() || stack.top() != sequence[idx]) && current_num <= n) {
+ stack.push(current_num);
+ operation_log.emplace_back(false, current_num);
+ current_num++;
+ }
+ while (!stack.empty() && stack.top() == sequence[idx]) {
+ stack.pop();
+ operation_log.emplace_back(true, sequence[idx]);
+ idx++;
+ }
+ }
+ if (!stack.empty()) {
+ std::cout << "NO" << std::endl;
+ } else {
+ for (const auto [op, x] : operation_log) {
+ std::cout << (op ? "POP " : "PUSH ") << x << std::endl;
+ }
+ }
+
+
+ return 0;
+}