From cba017e9831acea7ce172550efafb9c89a2a1c14 Mon Sep 17 00:00:00 2001 From: zhangzc Date: Thu, 28 Oct 2021 18:04:14 +0800 Subject: [PATCH] update exercises --- .../solution.md" | 70 ++++++++- .../solution.md" | 128 ++++++++++++++++- .../solution.cpp" | 4 +- .../solution.md" | 108 +++++++++++++- .../solution.md" | 133 ++++++++++++++++++ 5 files changed, 432 insertions(+), 11 deletions(-) diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\25439\347\272\247\345\217\260\351\230\266/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\25439\347\272\247\345\217\260\351\230\266/solution.md" index d76b97b24..2b184ec8d 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\25439\347\272\247\345\217\260\351\230\266/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\25439\347\272\247\345\217\260\351\230\266/solution.md" @@ -11,7 +11,11 @@ ## aop ### before ```cpp - +#include +#define LEFT 0 +#define RIGHT 1 +using namespace std; +int stage[40][2]; ``` ### after ```cpp @@ -20,21 +24,77 @@ ## 答案 ```cpp - +int main() +{ + int i; + stage[1][LEFT] = 1; + stage[1][RIGHT] = 0; + stage[2][LEFT] = 1; + stage[2][RIGHT] = 1; + for (i = 3; i <= 39; i++) + { + stage[i][LEFT] = stage[i - 1][RIGHT] + stage[i - 2][RIGHT]; + stage[i][RIGHT] = stage[i - 1][LEFT] + stage[i - 2][LEFT]; + } + cout << stage[39][RIGHT] << endl; + return 0; +} ``` ## 选项 ### A ```cpp - +int main() +{ + int i; + stage[1][LEFT] = 1; + stage[1][RIGHT] = 0; + stage[2][LEFT] = 1; + stage[2][RIGHT] = 1; + for (i = 3; i <= 39; i++) + { + stage[i][LEFT] = stage[i - 1][RIGHT] + stage[i - 2][RIGHT]; + stage[i][RIGHT] = stage[i - 1][LEFT] + stage[i - 2][LEFT]; + } + cout << stage[39][LEFT] << endl; + return 0; +} ``` ### B ```cpp - +int main() +{ + int i; + stage[1][LEFT] = 1; + stage[1][RIGHT] = 0; + stage[2][LEFT] = 1; + stage[2][RIGHT] = 1; + for (i = 3; i <= 39; i++) + { + stage[i][LEFT] = stage[i + 1][RIGHT] + stage[i - 1][RIGHT]; + stage[i][RIGHT] = stage[i + 1][LEFT] + stage[i - 1][LEFT]; + } + cout << stage[39][RIGHT] << endl; + return 0; +} ``` ### C ```cpp - +int main() +{ + int i; + stage[1][LEFT] = 1; + stage[1][RIGHT] = 0; + stage[2][LEFT] = 1; + stage[2][RIGHT] = 1; + for (i = 3; i <= 39; i++) + { + stage[i][LEFT] = stage[i + 1][RIGHT] + stage[i - 1][RIGHT]; + stage[i][RIGHT] = stage[i + 1][LEFT] + stage[i - 1][LEFT]; + } + cout << stage[39][LEFT] << endl; + return 0; +} ``` diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\344\270\252\345\271\270\350\277\220\346\225\260/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\344\270\252\345\271\270\350\277\220\346\225\260/solution.md" index 9a7e5381d..dfb2dae5d 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\344\270\252\345\271\270\350\277\220\346\225\260/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\344\270\252\345\271\270\350\277\220\346\225\260/solution.md" @@ -16,7 +16,11 @@ x星的国王有个怪癖,他只喜欢数字3,5和7。 ## aop ### before ```cpp - +#include +#include +#include +#include +using namespace std; ``` ### after ```cpp @@ -25,21 +29,139 @@ x星的国王有个怪癖,他只喜欢数字3,5和7。 ## 答案 ```cpp - +int main() +{ + set st; + priority_queue, greater> pq; + const int ok[3] = {3, 5, 7}; + st.insert(1); + pq.push(1); + int times = 0; + while (true) + { + long long lucky = pq.top(); + pq.pop(); + if (lucky == 59084709587505) + { //49 + cout << times << endl; + return 0; + } + times++; + for (int i = 0; i < 3; i++) + { + long long b = lucky * ok[i]; + if (!st.count(b)) + { + st.insert(b); + pq.push(b); + } + } + } + return 0; +} ``` ## 选项 ### A ```cpp - +int main() +{ + set st; + priority_queue, greater> pq; + const int ok[3] = {3, 5, 7}; + st.insert(1); + pq.push(1); + int times = 0; + while (true) + { + long long lucky = pq.top(); + pq.pop(); + if (lucky == 59084709587505) + { //49 + cout << times << endl; + return 0; + } + times++; + for (int i = 0; i < 3; i++) + { + long long b = lucky * ok[i + 1]; + if (!st.count(b)) + { + st.insert(b); + pq.push(b); + } + } + } + return 0; +} ``` ### B ```cpp +int main() +{ + set st; + priority_queue, greater> pq; + const int ok[3] = {3, 5, 7}; + st.insert(1); + pq.push(1); + int times = 0; + while (true) + { + long long lucky = pq.top(); + pq.pop(); + if (lucky == 59084709587505) + { //49 + cout << times << endl; + return 0; + } + times++; + for (int i = 0; i < 3; i++) + { + long long b = lucky * ok[i] + 1; + if (!st.count(b)) + { + st.insert(b); + pq.push(b); + } + } + } + return 0; +} ``` ### C ```cpp +int main() +{ + set st; + priority_queue, greater> pq; + const int ok[3] = {3, 5, 7}; + st.insert(1); + pq.push(1); + int times = 0; + while (true) + { + long long lucky = pq.top(); + pq.pop(); + if (lucky == 59084709587505) + { //49 + cout << times << endl; + return 0; + } + times++; + for (int i = 0; i < 3; i++) + { + long long b = lucky + ok[i]; + if (!st.count(b)) + { + st.insert(b); + pq.push(b); + } + } + } + return 0; +} ``` diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.cpp" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.cpp" index bd9593605..8cb99129e 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.cpp" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.cpp" @@ -13,7 +13,7 @@ int main() if (is_leap(y)) //判断闰年 { - for (int i = 0; i < (m - 1); i++) //记录1-(m-1)月天数 + for (int i = 0; i < m; i++) //记录1-(m-1)月天数 { ans += L_m_d[i]; } @@ -21,7 +21,7 @@ int main() } else { - for (int i = 0; i < (m - 1); i++) + for (int i = 0; i < m; i++) { ans += nonL_m_d[i]; } diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.md" index 419a45afa..3e11bdb95 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\347\254\254\345\207\240\345\244\251/solution.md" @@ -23,7 +23,12 @@ y m d ## aop ### before ```cpp - +#include +using namespace std; +bool is_leap(int year) +{ + return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; +} ``` ### after ```cpp @@ -32,21 +37,122 @@ y m d ## 答案 ```cpp +int main() +{ + int y, m, d, ans = 0; + cin >> y >> m >> d; + int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + if (is_leap(y)) //判断闰年 + { + for (int i = 0; i < (m - 1); i++) //记录1-(m-1)月天数 + { + ans += L_m_d[i]; + } + ans += d; + } + else + { + for (int i = 0; i < (m - 1); i++) + { + ans += nonL_m_d[i]; + } + ans += d; + } + cout << ans << endl; + return 0; +} ``` ## 选项 ### A ```cpp +int main() +{ + int y, m, d, ans = 0; + cin >> y >> m >> d; + int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (is_leap(y)) //判断闰年 + { + for (int i = 0; i < m; i++) //记录1-(m-1)月天数 + { + ans += L_m_d[i]; + } + ans += d; + } + else + { + for (int i = 0; i < (m - 1); i++) + { + ans += nonL_m_d[i]; + } + ans += d; + } + cout << ans << endl; + return 0; +} ``` ### B ```cpp +int main() +{ + int y, m, d, ans = 0; + cin >> y >> m >> d; + int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (is_leap(y)) //判断闰年 + { + for (int i = 0; i < (m - 1); i++) //记录1-(m-1)月天数 + { + ans += L_m_d[i]; + } + ans += d; + } + else + { + for (int i = 0; i < m; i++) + { + ans += nonL_m_d[i]; + } + ans += d; + } + cout << ans << endl; + return 0; +} ``` ### C ```cpp +int main() +{ + int y, m, d, ans = 0; + cin >> y >> m >> d; + int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (is_leap(y)) //判断闰年 + { + for (int i = 0; i < m; i++) //记录1-(m-1)月天数 + { + ans += L_m_d[i]; + } + ans += d; + } + else + { + for (int i = 0; i < m; i++) + { + ans += nonL_m_d[i]; + } + ans += d; + } + cout << ans << endl; + return 0; +} ``` diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\351\200\222\345\242\236\344\270\211\345\205\203\347\273\204/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\351\200\222\345\242\236\344\270\211\345\205\203\347\273\204/solution.md" index 8e867c00b..15d6dccce 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\351\200\222\345\242\236\344\270\211\345\205\203\347\273\204/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\351\200\222\345\242\236\344\270\211\345\205\203\347\273\204/solution.md" @@ -35,6 +35,15 @@ C = [C1, C2, ... CN], ## aop ### before ```cpp +#include +#include + +using namespace std; + +typedef long long LL; + +const int N = 1e5 + 10; +int a[N], b[N], c[N], sa[N], sc[N], s[N]; ``` ### after @@ -44,21 +53,145 @@ C = [C1, C2, ... CN], ## 答案 ```cpp +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i], a[i]++; + for (int i = 0; i < n; i++) + cin >> b[i], b[i]++; + for (int i = 0; i < n; i++) + cin >> c[i], c[i]++; + + for (int i = 0; i < n; i++) + s[a[i]]++; + for (int i = 1; i < N; i++) + s[i] += s[i - 1]; + for (int i = 0; i < n; i++) + sa[i] = s[b[i] - 1]; + memset(s, 0, sizeof s); + + for (int i = 0; i < n; i++) + s[c[i]]++; + for (int i = 1; i < N; i++) + s[i] += s[i - 1]; + for (int i = 0; i < n; i++) + sc[i] = s[N - 1] - s[b[i]]; + + LL res = 0; + for (int i = 0; i <= n; i++) + res += (LL)sa[i] * sc[i]; + cout << res; +} ``` ## 选项 ### A ```cpp +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i], a[i]++; + for (int i = 0; i < n; i++) + cin >> b[i], b[i]++; + for (int i = 0; i < n; i++) + cin >> c[i], c[i]++; + + for (int i = 0; i < n; i++) + s[a[i]]++; + for (int i = 1; i < N; i++) + s[i] += s[i - 1]; + for (int i = 0; i < n; i++) + sa[i] = s[b[i] - 1]; + + memset(s, 0, sizeof s); + for (int i = 0; i < n; i++) + s[c[i]]++; + for (int i = 1; i < N; i++) + s[i] += s[i - 1]; + for (int i = 0; i < n; i++) + sc[i] = s[N] - s[b[i]]; + + LL res = 0; + for (int i = 0; i <= n; i++) + res += (LL)sa[i] * sc[i]; + cout << res; +} ``` ### B ```cpp +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i], a[i]++; + for (int i = 0; i < n; i++) + cin >> b[i], b[i]++; + for (int i = 0; i < n; i++) + cin >> c[i], c[i]++; + + for (int i = 0; i < n; i++) + s[a[i]]++; + for (int i = 1; i < N; i++) + s[i] += s[i - 1]; + for (int i = 0; i < n; i++) + sa[i] = s[b[i] - 1]; + + memset(s, 0, sizeof s); + + for (int i = 0; i < n; i++) + s[c[i]]++; + for (int i = 1; i < N; i++) + s[i] = s[i - 1]; + for (int i = 0; i < n; i++) + sc[i] = s[N - 1] - s[b[i]]; + LL res = 0; + for (int i = 0; i <= n; i++) + res += (LL)sa[i] * sc[i]; + cout << res; +} ``` ### C ```cpp +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i], a[i]++; + for (int i = 0; i < n; i++) + cin >> b[i], b[i]++; + for (int i = 0; i < n; i++) + cin >> c[i], c[i]++; + + for (int i = 0; i < n; i++) + s[a[i]]++; + for (int i = 1; i < N; i++) + s[i] += s[i - 1]; + for (int i = 0; i < n; i++) + sa[i] = s[b[i] - 1]; + + memset(s, 0, sizeof s); + + for (int i = 0; i < n; i++) + s[c[i]]++; + for (int i = 1; i < N; i++) + s[i] = s[i - 1]; + for (int i = 0; i < n; i++) + sc[i] = s[N - 1] - s[b[i] - 1]; + LL res = 0; + for (int i = 0; i <= n; i++) + res += (LL)sa[i] * sc[i]; + cout << res; +} ``` -- GitLab