From f58d11427ba6cf38e221a92914031c2ecc6083d5 Mon Sep 17 00:00:00 2001 From: astrbot-bot Date: Sat, 25 Apr 2026 23:01:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E6=89=B9=E9=87=8F=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83=E9=97=AE=E9=A2=98=20-=20?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=A4=B4=E6=96=87=E4=BB=B6/=E6=B8=85?= =?UTF-8?q?=E7=90=86=E8=A1=8C=E5=B0=BE=E7=A9=BA=E6=A0=BC/=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=9C=AB=E5=B0=BE=E6=8D=A2=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../examples/algorithm_template_example.cpp | 330 ++-- CodeForce/CR1006(div3)/A.cpp | 52 +- CodeForce/CR1006(div3)/B.cpp | 94 +- CodeForce/CR1006(div3)/C.cpp | 163 +- CodeForce/CR176(div2)/A.cpp | 72 +- CodeForce/CR176(div2)/B.cpp | 270 +-- .../Codeforces Round 1063 (Div. 2)/A/A.cpp | 2 +- .../Codeforces Round 1063 (Div. 2)/A/A2.cpp | 5 +- .../Codeforces Round 1063 (Div. 2)/B/B.cpp | 3 +- CodeForce/Codeforces Round 1078 (Div. 2)/B.py | 4 +- CodeForce/Codeforces Round 1086 (Div. 2)/A.py | 2 +- .../A.cpp | 6 +- .../A.cpp | 14 +- .../A2.py | 10 +- .../A. Trilobyte/1.cpp | 72 +- ICPC/1/1.cpp | 30 +- ICPC/1/tempCodeRunnerFile.cpp | 58 +- acwing/problems/.cpp | 4 +- ...5\347\232\204\350\212\202\347\202\271.cpp" | 66 +- ...6\345\274\217\345\214\271\351\205\215.cpp" | 3 +- ...4\345\255\227\347\254\246\344\270\262.cpp" | 4 +- ...6\346\225\260\345\211\215\351\235\242.cpp" | 1 + ...344\270\252\350\212\202\347\202\271-2.cpp" | 80 +- ...k\344\270\252\350\212\202\347\202\271.cpp" | 70 +- ...5\345\217\243\347\273\223\347\202\271.cpp" | 96 +- ...350\275\254\351\223\276\350\241\250-2.cpp" | 72 +- ...5\350\275\254\351\223\276\350\241\250.cpp" | 80 +- ...7\347\232\204\351\223\276\350\241\250.cpp" | 98 +- ...4\345\255\220\347\273\223\346\236\204.cpp" | 64 +- ...1\347\232\204\351\225\234\345\203\217.cpp" | 54 +- ...4\344\272\214\345\217\211\346\240\221.cpp" | 72 +- ...3\345\215\260\347\237\251\351\230\265.cpp" | 3 +- ...5\346\225\260\347\232\204\346\240\210.cpp" | 98 +- ...1\345\207\272\345\272\217\345\210\227.cpp" | 1 + ...0\344\272\214\345\217\211\346\240\221.cpp" | 77 +- ...0\344\272\214\345\217\211\346\240\221.cpp" | 99 +- ...0\344\272\214\345\217\211\346\240\221.cpp" | 117 +- ...5\345\216\206\345\272\217\345\210\227.cpp" | 1 + .../1.cpp" | 1 + .../2.cpp" | 1 + .../1.cpp" | 1 + .../2.cpp" | 1 + .../1.cpp" | 1 + .../1.cpp" | 12 +- .../1.cpp" | 1 + .../1.cpp" | 1 + .../1.cpp" | 3 +- .../1.cpp" | 2 +- .../1.cpp" | 4 +- .../1.py" | 2 +- atcoder/AtCoder Beginner Contest 413/A/1.cpp | 34 +- .../B - cat 2/1.cpp | 80 +- .../B - cat 2/algorithm_parsing_examples.cpp | 266 +-- .../B - cat 2/line_parsing_examples.cpp | 180 +- .../C - Large Queue/1.cpp | 140 +- .../D - Make Geometric Sequence/1.cpp | 244 +-- .../A.Too Many Requests/1.cpp | 22 +- .../A.Too Many Requests/2.cpp | 46 +- .../B - N - 1/1.cpp | 52 +- .../C - Odd One Subsequence/2.cpp | 48 +- atcoder/AtCoder Beginner Contest 433/A.py | 2 +- atcoder/AtCoder Beginner Contest 433/B.py | 2 +- atcoder/AtCoder Beginner Contest 433/C.py | 2 +- atcoder/AtCoder Beginner Contest 444/B.py | 2 +- atcoder/AtCoder Beginner Contest 444/C.py | 6 +- atcoder/abc394-2/abc394_b/abc394_b.cpp | 1 + atcoder/abc394-2/abc394_d/abc394_d.cpp | 2 +- atcoder/abc394/A.cpp | 30 +- atcoder/abc394/B.cpp | 71 +- atcoder/abc394/C.cpp | 75 +- atcoder/abc394/D.cpp | 136 +- atcoder/abc394/E.cpp | 147 +- atcoder/abc394/F.cpp | 175 +- atcoder/abc394/G.cpp | 197 +- atcoder/abc413/abc413_a/abc413_a.cpp | 2 +- atcoder/abc413/abc413_b/abc413_b.cpp | 2 +- .../A/1-2.cpp" | 60 +- .../A/1.cpp" | 66 +- .../B/2-2.cpp" | 118 +- .../B/2-3.cpp" | 66 +- .../B/2.cpp" | 66 +- .../B/tempCodeRunnerFile.cpp" | 6 +- .../A.cpp" | 138 +- ...3\346\211\276\350\264\250\346\225\260.cpp" | 74 +- ...346\211\276\350\264\250\346\225\260_1.cpp" | 58 +- ...B\351\273\221\347\231\275\346\243\213.cpp" | 18 +- ...D\347\272\242\351\273\221\346\240\221.cpp" | 54 +- ...G\345\234\260\351\233\267\351\230\265.cpp" | 151 +- blue_braige/base/1.cpp | 82 +- blue_braige/base/2.cpp | 90 +- blue_braige/base/3.cpp | 38 +- .../1.cpp" | 76 +- .../DFS/1.cpp" | 94 +- .../1.cpp" | 88 +- .../1.cpp" | 52 +- .../1.cpp" | 82 +- .../1.cpp" | 44 +- .../tempCodeRunnerFile.cpp" | 4 +- .../1.cpp" | 52 +- .../2.cpp" | 74 +- .../1.cpp" | 66 +- .../\351\200\222\345\275\222/1.cpp" | 62 +- .../\351\200\222\345\275\222/2.cpp" | 38 +- .../\351\200\222\345\275\222/3.cpp" | 46 +- .../1.cpp" | 42 +- .../2.cpp" | 46 +- .../3.cpp" | 68 +- .../4_1.cpp" | 53 +- .../4_2.cpp" | 49 +- .../2.cpp" | 2 +- .../2_1.cpp" | 7 +- .../3.cpp" | 2 +- .../4.cpp" | 3 +- .../5.cpp" | 2 +- .../6.cpp" | 2 +- .../6_1.cpp" | 2 +- .../N_Queen/1.cpp" | 96 +- .../N_Queen/2.cpp" | 110 +- .../1.cpp" | 83 +- book/hello algorithm/ch2/iteration.py | 2 +- book/hello algorithm/ch2/recursion.py | 4 +- .../KMP \347\256\227\346\263\225/1.cpp" | 1 + .../1.cpp" | 1 + .../1.cpp" | 10 +- .../2.cpp" | 12 +- .../1.cpp" | 10 +- .../1.cpp" | 2 +- .../Search In Infinite Sorted 0-1 Array/1.cpp | 56 +- .../1.cpp" | 46 +- .../A. A + B \351\227\256\351\242\230.cpp" | 24 +- .../1.cpp" | 18 +- .../1.cpp" | 76 +- .../1.py" | 30 +- .../1.cpp" | 44 +- .../1.cpp" | 32 +- .../1.py" | 6 +- .../2.cpp" | 1 + .../1.cpp" | 32 +- .../1.cpp" | 62 +- .../1.cpp" | 111 +- .../2.cpp" | 104 +- .../J. 10^N/1.cpp | 46 +- .../1.cpp" | 46 +- .../1.cpp" | 56 +- .../1.cpp" | 48 +- .../1.cpp" | 42 +- .../1.cpp" | 60 +- .../1.cpp" | 72 +- .../1.cpp" | 58 +- .../1.cpp" | 60 +- .../S. 0-1 \346\216\222\345\272\217/1.cpp" | 82 +- .../S. 0-1 \346\216\222\345\272\217/2.cpp" | 84 +- intro/cpp/sort/sort.cpp | 84 +- "intro/python/1_\346\236\232\344\270\276.py" | 32 +- "intro/python/1_\346\236\232\344\270\2762.py" | 32 +- ...02\345\272\246\346\265\213\350\257\225.py" | 100 +- ...64\345\244\215\346\235\202\345\272\246.py" | 42 +- .../1.cpp" | 1 + .../1.cpp" | 42 +- .../1.py" | 28 +- .../1.cpp" | 9 +- .../1.py" | 4 +- .../1.cpp" | 46 +- .../1.cpp" | 3 +- leetcode/Study Plan/13/13.cpp | 84 +- leetcode/Study Plan/13/13_2.cpp | 62 +- ...27\350\275\254\346\225\264\346\225\260.py" | 68 +- leetcode/Study Plan/1342/1.cpp | 64 +- .../1.cpp" | 42 +- ...54\345\205\261\345\211\215\347\274\200.py" | 238 +-- leetcode/Study Plan/14/14.cpp | 60 +- leetcode/Study Plan/144/144.cpp | 136 +- leetcode/Study Plan/1480/1.cpp | 58 +- .../1.py" | 2 +- .../1.cpp" | 1 + .../1.py" | 72 +- .../1.cpp" | 1 + .../2.cpp" | 1 + .../1.py" | 2 +- .../1.cpp" | 4 +- .../1_\346\236\232\344\270\276.cpp" | 74 +- ...0\345\214\226\346\236\232\344\270\276.cpp" | 108 +- .../1.py" | 4 +- .../1.py" | 8 +- .../2.py" | 2 +- leetcode/Study Plan/1588/1.cpp | 45 +- .../1.py" | 14 +- .../1.py" | 2 +- .../1.py" | 2 +- leetcode/Study Plan/1672/1.cpp | 66 +- .../1.py" | 2 +- .../1.cpp" | 10 +- .../1.py" | 18 +- leetcode/Study Plan/1768/1.py | 24 +- .../1.cpp" | 1 + .../1.py" | 12 +- leetcode/Study Plan/1922/1.cpp | 36 +- .../1.py" | 2 +- .../1.py" | 2 +- .../2.py" | 2 +- .../3.py" | 2 +- ...10\347\232\204\346\213\254\345\217\267.py" | 74 +- .../1.py" | 22 +- .../1.py" | 62 +- .../1.py" | 44 +- ...11\345\272\217\351\223\276\350\241\250.py" | 82 +- ...3\346\263\225_\350\277\255\344\273\243.py" | 78 +- .../21/21_\351\200\222\345\275\222.py" | 2 +- leetcode/Study Plan/21/test_1.py | 16 +- .../1.py" | 2 +- .../1.cpp" | 11 +- .../2.cpp" | 1 + .../1.py" | 4 +- .../2.py" | 2 +- .../1.cpp" | 157 +- .../1.cpp" | 10 +- .../2.cpp" | 12 +- .../1.py" | 78 +- leetcode/Study Plan/2270/1.cpp | 64 +- leetcode/Study Plan/2270/2.cpp | 72 +- leetcode/Study Plan/2275/1.cpp | 65 +- .../231. 2 \347\232\204\345\271\202/1.cpp" | 2 +- .../1.cpp" | 10 +- .../1.cpp" | 40 +- .../1.cpp" | 3 +- .../1.cpp" | 43 +- .../1.py" | 24 +- ...04\351\207\215\345\244\215\351\241\271.py" | 38 +- .../1.py" | 2 +- leetcode/Study Plan/27/1.cpp | 37 +- leetcode/Study Plan/2843/1.cpp | 96 +- .../1.cpp" | 1 + .../1.py" | 8 +- leetcode/Study Plan/3065/1.cpp | 56 +- leetcode/Study Plan/3066/1.cpp | 70 +- ...4\345\211\215\347\274\200\345\222\214.cpp" | 2 +- leetcode/Study Plan/3095/1.cpp | 78 +- leetcode/Study Plan/3095/2.cpp | 87 +- leetcode/Study Plan/3095/3.cpp | 137 +- .../1.py" | 8 +- leetcode/Study Plan/3310/1.cpp | 72 +- .../1.py" | 4 +- .../2.py" | 4 +- .../1.py" | 2 +- leetcode/Study Plan/3340/1.cpp | 44 +- .../2.cpp" | 114 +- .../Dijkstra's shortest path.cpp" | 131 +- .../1.py" | 150 +- .../2.py" | 38 +- .../1.py" | 4 +- .../2.cpp" | 1 + .../binary.cpp" | 200 +- .../simple_binary.cpp" | 170 +- .../stl_binary.cpp" | 116 +- .../2.cpp" | 1 + .../1.py" | 4 +- .../1.cpp" | 1 + .../2.cpp" | 1 + .../1.cpp" | 1 + .../2.py" | 22 +- .../1.cpp" | 1 + .../2.cpp" | 1 + .../1.py" | 8 +- .../1.cpp" | 1 + .../1.cpp" | 1 + .../2.cpp" | 1 + .../1.cpp" | 1 + .../1.py" | 4 +- .../1.cpp" | 11 +- leetcode/Study Plan/382/382.cpp | 92 +- leetcode/Study Plan/383/1.cpp | 56 +- leetcode/Study Plan/383/2.cpp | 82 +- leetcode/Study Plan/383/3.cpp | 62 +- leetcode/Study Plan/389/1.cpp | 64 +- leetcode/Study Plan/389/2.cpp | 70 +- leetcode/Study Plan/389/3.cpp | 74 +- leetcode/Study Plan/389/4.cpp | 90 +- leetcode/Study Plan/412/1.cpp | 72 +- .../1.cpp" | 53 +- .../1.py" | 48 +- .../1.cpp" | 1 + .../1.cpp" | 1 + .../2.cpp" | 1 + .../1.cpp" | 1 + .../\345\277\253\351\200\237\345\271\202.cpp" | 66 +- ...\345\277\253\351\200\237\345\271\2022.cpp" | 78 +- .../1.cpp" | 1 + leetcode/Study Plan/62/62_1.cpp | 26 +- leetcode/Study Plan/62/62_2.cpp | 33 +- leetcode/Study Plan/64/1.cpp | 14 +- leetcode/Study Plan/66/1.cpp | 49 +- leetcode/Study Plan/66/2.cpp | 88 +- leetcode/Study Plan/66/simulate.cpp | 74 +- leetcode/Study Plan/67/1.cpp | 50 +- leetcode/Study Plan/70/1.cpp | 54 +- leetcode/Study Plan/70/2.cpp | 112 +- leetcode/Study Plan/70/3.cpp | 52 +- leetcode/Study Plan/70/4.py | 22 +- .../1.cpp" | 27 +- leetcode/Study Plan/707/707.cpp | 152 +- .../1.py" | 40 +- .../1.py" | 2 +- .../enumerate.cpp" | 120 +- .../BFS.cpp" | 124 +- .../2.cpp" | 1 + .../1.cpp" | 1 + leetcode/Study Plan/876/1.cpp | 108 +- leetcode/Study Plan/876/fast_slow_pointer.cpp | 120 +- leetcode/Study Plan/9/9.py | 16 +- .../\345\221\250\350\265\233439/1.cpp" | 152 +- .../\345\221\250\350\265\233439/2.cpp" | 154 +- .../1.py" | 16 +- ...\346\225\260\345\255\227\345\222\214 I.py" | 2 +- ...04\347\232\204\351\225\277\345\272\246.py" | 2 +- ...346\225\260\345\255\227\345\222\214 II.py" | 16 +- ...27\347\232\204\346\225\260\351\207\217.py" | 22 +- .../1.cpp" | 1 + .../1.cpp" | 1 + .../10.regular-expression-matching.cpp | 2 +- leetcode/problems/198.house-robber.py | 2 +- ...2.words-within-two-edits-of-dictionary.cpp | 2 +- ...1\346\225\210\346\225\260\345\255\227.cpp" | 2 +- .../1.py" | 12 +- .../P4956 [COCI 2017/2018 #6] Davor/1.cpp | 2 +- luogu/algorithm/1-2_Sort/P1271/1.cpp | 6 +- .../1-2_Sort/P1271/counting_sort.cpp | 2 +- .../algorithm/1-2_Sort/P1271/insert_sort.cpp | 2 +- luogu/algorithm/1-3_Brute Force/P2089/1.cpp | 4 +- luogu/algorithm/1-3_Brute Force/P2241/1.py | 2 +- .../q1.cpp" | 2 +- .../q2.cpp" | 6 +- .../A.cpp" | 16 +- .../B.cpp" | 7 +- luogu/learning/P1706/1.cpp | 1 + luogu/month_contest/B3648/1.py | 2 +- luogu/month_contest/B3652/1.py | 2 +- luogu/month_contest/T528356/1.py | 4 +- luogu/month_contest/T528356/4.py | 2 +- "luogu/\345\205\245\351\227\250/P1009/1.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P1046/1.cpp" | 1 + "luogu/\345\205\245\351\227\250/P1055/1.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P1055/1.py" | 4 +- .../P1055/tempCodeRunnerFile.cpp" | 2 +- .../\345\205\245\351\227\250/P1085/P1085.py" | 2 +- "luogu/\345\205\245\351\227\250/P1217/1.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P1217/3.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P1255/1.cpp" | 14 +- "luogu/\345\205\245\351\227\250/P1307/1.cpp" | 10 +- "luogu/\345\205\245\351\227\250/P1420/1.cpp" | 2 +- .../\345\205\245\351\227\250/P1421/P1421.cpp" | 2 +- .../\345\205\245\351\227\250/P1421/P1421.py" | 2 +- "luogu/\345\205\245\351\227\250/P1423/2.cpp" | 2 +- .../P1424/1_branch.cpp" | 2 +- .../\345\205\245\351\227\250/P1424/2_loop.py" | 2 +- .../P1424/tempCodeRunnerFile.py" | 2 +- .../\345\205\245\351\227\250/P1425/P1425.cpp" | 2 +- .../\345\205\245\351\227\250/P1425/P1425.py" | 2 +- .../1.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P1720/2.cpp" | 1 + "luogu/\345\205\245\351\227\250/P1720/3.cpp" | 4 +- .../\345\205\245\351\227\250/P1909/P1909.py" | 2 +- .../\345\205\245\351\227\250/P2029/P2029.cpp" | 2 +- .../\345\205\245\351\227\250/P2029/P2029.py" | 2 +- .../\345\205\245\351\227\250/P2433/P2433.cpp" | 4 +- .../\345\205\245\351\227\250/P3954/P3954.cpp" | 4 +- .../\345\205\245\351\227\250/P3954/P3954.py" | 2 +- "luogu/\345\205\245\351\227\250/P4414/1.cpp" | 1 + .../\345\205\245\351\227\250/P5707/P5707.cpp" | 2 +- .../\345\205\245\351\227\250/P5707/P5707.py" | 2 +- .../\345\205\245\351\227\250/P5708/P5708.cpp" | 2 +- .../\345\205\245\351\227\250/P5708/P5708.py" | 2 +- .../\345\205\245\351\227\250/P5709/P5709.py" | 2 +- .../\345\205\245\351\227\250/P5710/P5710.cpp" | 2 +- .../\345\205\245\351\227\250/P5710/P5710.py" | 2 +- .../\345\205\245\351\227\250/P5711/P5711.cpp" | 2 +- .../\345\205\245\351\227\250/P5712/P5712.py" | 2 +- .../\345\205\245\351\227\250/P5713/P5713.cpp" | 2 +- .../\345\205\245\351\227\250/P5713/P5713.py" | 4 +- .../\345\205\245\351\227\250/P5714/P5714.cpp" | 2 +- .../\345\205\245\351\227\250/P5714/P5714.py" | 2 +- .../\345\205\245\351\227\250/P5715/P5715.cpp" | 2 +- ...15\346\225\260\346\216\222\345\272\217.py" | 2 +- .../\345\205\245\351\227\250/P5716/P5716.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P5717/1.py" | 2 +- "luogu/\345\205\245\351\227\250/P5723/2.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P5724/1.cpp" | 2 +- "luogu/\345\205\245\351\227\250/P5733/1.cpp" | 2 +- .../P1102 A-B \346\225\260\345\257\271/1.cpp" | 2 +- .../P1102 A-B \346\225\260\345\257\271/2.cpp" | 2 +- .../sort_binary.cpp" | 2 +- .../tempCodeRunnerFile.cpp" | 4 +- naukri/Weekly 186/A.cpp | 76 +- .../beginner-contest-90/tk/A Parity Parts.cpp | 98 +- .../tk/B Maximum Value of F(x) - VI.cpp | 205 +- .../C Longest Sub-array with bitwise AND!.cpp | 71 +- naukri/beginner-contest-90/tk/D Add Edges.cpp | 101 +- .../contest1/A.cpp" | 3 +- .../contest1/A1.cpp" | 1 + .../contest1/B.cpp" | 1 + .../contest1/C.cpp" | 5 +- .../contest1/E.cpp" | 5 +- .../contest1/H.cpp" | 1 + .../contest1/K.cpp" | 2 +- .../contest2/B.cpp" | 5 +- .../contest2/E.cpp" | 1 + .../contest2/H.cpp" | 1 + .../contest2/J.cpp" | 7 +- .../contest2/J1.cpp" | 1 + .../contest3/B.cpp" | 1 + .../contest3/G.cpp" | 1 + .../contest4/A.cpp" | 1 + .../contest4/B.cpp" | 1 + .../contest4/F.cpp" | 1 + .../contest4/H.cpp" | 1 + .../12.py" | 2 +- .../2.py" | 2 +- .../3.py" | 4 +- .../4.py" | 4 +- .../5.py" | 6 +- .../6.py" | 6 +- .../1.cpp" | 50 +- .../2.cpp" | 98 +- .../2.py" | 46 +- .../3.cpp" | 122 +- .../4.cpp" | 164 +- .../class/1.cpp" | 40 +- .../class/2.cpp" | 42 +- .../class/4.cpp" | 38 +- .../class/6.cpp" | 4 +- .../xiaobai2/1.cpp" | 42 +- .../xiaobai2/2.cpp" | 79 +- .../xiaobai2/2.py" | 50 +- .../xiaobai2/3.py" | 152 +- .../xiaobai2/tempCodeRunnerFile.cpp" | 4 +- .../xiaobai2/tempCodeRunnerFile.py" | 2 +- .../A.cpp" | 7 +- .../B.cpp" | 3 +- .../C.cpp" | 5 +- .../b1.cpp" | 7 +- .../c1.cpp" | 3 +- .../d1.cpp" | 1 + .../e1.cpp" | 1 + .../f1.cpp" | 1 + .../1.cpp" | 58 +- .../A.cpp" | 2 +- .../B.cpp" | 1 + .../A.cpp" | 2 +- .../B.cpp" | 2 +- .../B2.cpp" | 3 +- .../C.cpp" | 9 +- .../D.cpp" | 8 +- .../1.cpp" | 28 +- .../2.cpp" | 55 +- .../3.cpp" | 97 +- .../test.cpp" | 63 +- .../1.cpp" | 72 +- .../2.cpp" | 86 +- .../3.cpp" | 526 ++--- .../4.cpp" | 98 +- .../4_2.cpp" | 100 +- .../6.cpp" | 148 +- .../A.cpp" | 34 +- .../B.cpp" | 64 +- .../C.cpp" | 62 +- .../C_2.cpp" | 98 +- .../D.cpp" | 174 +- .../E.cpp" | 344 ++-- .../E_2.cpp" | 314 +-- .../F.cpp" | 156 +- .../B.py" | 2 +- .../C.py" | 2 +- .../1.cpp" | 62 +- .../1.cpp" | 92 +- .../2.cpp" | 268 +-- .../C_\346\233\277\350\272\253/1.cpp" | 296 +-- .../A.py" | 4 +- .../B.py" | 2 +- .../C.py" | 6 +- .../1.cpp" | 41 +- .../1.cpp" | 36 +- .../25_10-18\347\277\273\344\271\213/2.cpp" | 79 +- .../1.cpp" | 69 +- .../2.cpp" | 55 +- .../brute.cpp" | 97 +- .../others/bfs.cpp" | 100 +- .../others/ida.cpp" | 2 +- .../1.cpp" | 141 +- .../1.cpp" | 60 +- .../1.cpp" | 169 +- .../2.cpp" | 117 +- .../3.cpp" | 68 +- .../1.cpp" | 90 +- .../1.cpp" | 2 +- .../1 copy.cpp" | 1 + .../1.cpp" | 2 +- .../1.cpp" | 2 +- .../1.cpp" | 8 +- .../1.cpp" | 3 +- .../1.py" | 2 +- .../1.cpp" | 3 +- .../1_1.cpp" | 1 + .../1.cpp" | 1 + .../2.cpp" | 2 +- .../1.cpp" | 1 + .../2.cpp" | 3 +- .../2_2.cpp" | 2 +- .../1.cpp" | 1 + .../1.py" | 8 +- .../1_1.py" | 14 +- .../25_11-19\346\227\205\346\270\270/1.cpp" | 3 +- .../25_11-19\346\227\205\346\270\270/1_1.cpp" | 1 + .../1.cpp" | 7 +- .../1_1.cpp" | 1 + .../1.cpp" | 2 +- .../1.py" | 2 +- .../1.cpp" | 6 +- .../1.cpp" | 5 +- .../1.cpp" | 16 +- .../2.cpp" | 4 +- .../1.cpp" | 3 +- .../1.cpp" | 4 +- .../1.cpp" | 2 +- .../1.cpp" | 104 +- .../1.cpp" | 4 +- .../2.cpp" | 6 +- .../1.cpp" | 37 +- .../2.cpp" | 4 +- .../25_11-4\347\273\204\351\230\237/1.cpp" | 22 +- .../1.cpp" | 2 +- .../2.cpp" | 2 +- .../1.cpp" | 2 +- .../1.cpp" | 4 +- .../1.cpp" | 7 +- .../1.cpp" | 2 +- .../2.cpp" | 1 + .../1.cpp" | 3 +- .../1.cpp" | 4 +- .../1.cpp" | 8 +- .../1.cpp" | 4 +- .../1.cpp" | 12 +- .../2.cpp" | 11 +- .../prime_sieve_examples.cpp" | 33 +- .../1.cpp" | 24 +- .../2.cpp" | 36 +- .../1.cpp" | 5 +- .../25_12-7\345\257\222\346\275\256/1.cpp" | 5 +- .../25_12-7\345\257\222\346\275\256/2.cpp" | 1 + .../1.cpp" | 5 +- .../1.cpp" | 7 +- .../1.cpp | 2 +- .../2.cpp | 2 +- .../1.cpp" | 4 +- .../2.cpp" | 2 +- niuke/daily_problem/26_01-14many sum/1.cpp | 1 + .../1.cpp" | 1 + .../2.cpp" | 1 + .../26_01-29\350\256\241\346\225\260/1.cpp" | 1 + .../1.cpp" | 4 +- .../1.cpp" | 5 +- .../26_2-1\350\272\262\350\227\217/1.cpp" | 4 +- .../1.cpp" | 5 +- .../1.cpp" | 1 + niuke/practice/noob/noob1/noob1.cpp | 2 +- niuke/practice/noob/noob10/noob10.cpp | 2 +- niuke/practice/noob/noob11/noob11.cpp | 2 +- niuke/practice/noob/noob12/noob12.cpp | 2 +- niuke/practice/noob/noob13/noob13.cpp | 2 +- niuke/practice/noob/noob14/noob14.cpp | 2 +- niuke/practice/noob/noob15/noob15.cpp | 4 +- niuke/practice/noob/noob16/noob16.cpp | 4 +- niuke/practice/noob/noob18/noob18.cpp | 2 +- niuke/practice/noob/noob19/noob19.cpp | 2 +- niuke/practice/noob/noob2/noob2.cpp | 2 +- niuke/practice/noob/noob20/noob20.cpp | 2 +- niuke/practice/noob/noob21/noob21.cpp | 2 +- niuke/practice/noob/noob22/noob22.cpp | 2 +- niuke/practice/noob/noob23/noob23.cpp | 2 +- niuke/practice/noob/noob24/noob24.cpp | 2 +- niuke/practice/noob/noob25/noob25.cpp | 2 +- niuke/practice/noob/noob27/noob27.cpp | 2 +- niuke/practice/noob/noob28/noob28.cpp | 2 +- niuke/practice/noob/noob29/noob29.cpp | 2 +- niuke/practice/noob/noob3/noob3.cpp | 4 +- niuke/practice/noob/noob30/noob30.cpp | 4 +- niuke/practice/noob/noob31/noob31.cpp | 2 +- niuke/practice/noob/noob32/noob32.cpp | 2 +- niuke/practice/noob/noob33/noob33.cpp | 4 +- niuke/practice/noob/noob34/noob34.cpp | 4 +- niuke/practice/noob/noob35/noob35.cpp | 2 +- niuke/practice/noob/noob36/noob36.cpp | 6 +- niuke/practice/noob/noob37/noob37.cpp | 6 +- niuke/practice/noob/noob38/noob38.cpp | 6 +- niuke/practice/noob/noob38/noob38.py | 2 +- niuke/practice/noob/noob39/noob39.py | 2 +- niuke/practice/noob/noob4/noob4.cpp | 2 +- niuke/practice/noob/noob40/noob40.py | 2 +- ...34\345\203\217\345\272\217\345\210\227.py" | 2 +- ...0\345\255\246\344\275\234\344\270\232.cpp" | 7 +- ...41\346\225\260\347\273\264\346\212\244.py" | 2 +- ...60\346\225\260\351\227\256\351\242\230.py" | 6 +- ...46\347\221\237\345\244\253\347\216\257.py" | 2 +- ...50\345\244\226\347\232\204\346\240\221.py" | 24 +- ...14\347\273\264\346\225\260\347\273\204.py" | 2 +- ...51\351\230\265\345\210\244\345\256\232.py" | 2 +- niuke/practice/noob/noob5/noob5.cpp | 8 +- niuke/practice/noob/noob53/1.cpp | 2 +- niuke/practice/noob/noob54/1.cpp | 2 +- niuke/practice/noob/noob55/1.cpp | 4 +- niuke/practice/noob/noob56/1.cpp | 4 +- niuke/practice/noob/noob57/1.cpp | 4 +- niuke/practice/noob/noob58/1.cpp | 4 +- niuke/practice/noob/noob59/1.cpp | 4 +- niuke/practice/noob/noob6/noob6.cpp | 2 +- niuke/practice/noob/noob60/1.cpp | 7 +- niuke/practice/noob/noob61/1.cpp | 4 +- ...0\345\207\275\346\225\260\357\274\211.cpp" | 4 +- .../1.cpp" | 4 +- .../1.cpp" | 2 +- .../1.cpp" | 6 +- niuke/practice/noob/noob7/noob7.cpp | 2 +- .../1.cpp" | 6 +- .../3.py" | 6 +- .../1.cpp" | 1 + .../1.cpp" | 1 + .../1.cpp" | 1 + niuke/practice/noob/noob8/noob8.cpp | 2 +- .../1.cpp" | 1 + .../1.cpp" | 1 + .../1.cpp" | 6 +- .../1.cpp" | 2 +- niuke/practice/noob/noob9/noob9.cpp | 2 +- .../1.cpp" | 2 +- .../1.cpp" | 2 +- .../3.cpp" | 3 +- .../1.cpp" | 4 +- .../NP16.\345\217\221\351\200\201offer.py" | 2 +- ...40\351\231\244\345\245\275\345\217\213.py" | 2 +- note/vector/c1.cpp | 72 +- online_judge/10487-Closest Sums.cpp | 49 +- open_judge/2811/1.cpp | 134 +- open_judge/2811/2.cpp | 178 +- open_judge/2811/3.cpp | 32 +- open_judge/2811/4.cpp | 38 +- open_judge/3750/1.cpp | 1684 ++++++++--------- open_judge/NOIP2014/1.cpp | 112 +- tools/blog_generator.py | 140 +- tools/organize_noob.py | 10 +- 647 files changed, 12375 insertions(+), 12207 deletions(-) diff --git a/.cursor/rules/examples/algorithm_template_example.cpp b/.cursor/rules/examples/algorithm_template_example.cpp index f537538c..7e8a5500 100644 --- a/.cursor/rules/examples/algorithm_template_example.cpp +++ b/.cursor/rules/examples/algorithm_template_example.cpp @@ -1,165 +1,165 @@ -/** - * Algorithm: Binary Search - * Category: Searching - * - * Description: - * An efficient search algorithm that finds the position of a target value - * within a sorted array. Binary search compares the target value to the middle - * element of the array. If they are not equal, the half in which the target cannot - * lie is eliminated and the search continues on the remaining half, again taking - * the middle element to compare to the target value, and repeating until the target - * value is found or the array is exhausted. - * - * Time Complexity: O(log n) - * Space Complexity: O(1) - * - * Parameters: - * - arr: A sorted array of elements - * - target: The value to search for in the array - * - left: The starting index of the search range (optional) - * - right: The ending index of the search range (optional) - * - * Returns: - * - The index of the target if found, otherwise -1 - * - * Example Usage: - * vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - * int target = 7; - * int result = binarySearch(nums, target); // Returns 6 - * - * Variants: - * - Lower bound: Find the first element >= target - * - Upper bound: Find the first element > target - * - Closest element: Find the element closest to target - * - * References: - * - Introduction to Algorithms (CLRS), Chapter 2.3 - * - https://en.wikipedia.org/wiki/Binary_search_algorithm - * - * Related Problems: - * - LeetCode 704: Binary Search - * - LeetCode 35: Search Insert Position - * - LeetCode 33: Search in Rotated Sorted Array - */ - -#include - -template -int binarySearch(const std::vector &arr, const T &target) -{ - int left = 0; - int right = arr.size() - 1; - - while (left <= right) - { - // Calculate middle index (avoiding potential overflow) - int mid = left + (right - left) / 2; - - // Check if target is present at mid - if (arr[mid] == target) - { - return mid; - } - - // If target is greater, ignore left half - if (arr[mid] < target) - { - left = mid + 1; - } - // If target is smaller, ignore right half - else - { - right = mid - 1; - } - } - - // Target not found - return -1; -} - -// Lower bound variant: finds the first element >= target -template -int lowerBound(const std::vector &arr, const T &target) -{ - int left = 0; - int right = arr.size(); - - while (left < right) - { - int mid = left + (right - left) / 2; - if (arr[mid] < target) - { - left = mid + 1; - } - else - { - right = mid; - } - } - - // If left == arr.size(), target is greater than all elements - return (left < arr.size()) ? left : -1; -} - -// Upper bound variant: finds the first element > target -template -int upperBound(const std::vector &arr, const T &target) -{ - int left = 0; - int right = arr.size(); - - while (left < right) - { - int mid = left + (right - left) / 2; - if (arr[mid] <= target) - { - left = mid + 1; - } - else - { - right = mid; - } - } - - // If left == arr.size(), all elements are <= target - return (left < arr.size()) ? left : -1; -} - -// Usage example -void usage_example() -{ - std::vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - - // Basic binary search - int target = 7; - int result = binarySearch(nums, target); // Returns 6 - - // Lower bound example - int lb = lowerBound(nums, 7); // Returns 6 - - // Upper bound example - int ub = upperBound(nums, 7); // Returns 7 -} - -// Test cases -void test_cases() -{ - std::vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - - // Test binary search - assert(binarySearch(nums, 1) == 0); // First element - assert(binarySearch(nums, 10) == 9); // Last element - assert(binarySearch(nums, 5) == 4); // Middle element - assert(binarySearch(nums, 11) == -1); // Not found, greater than all - assert(binarySearch(nums, 0) == -1); // Not found, smaller than all - - // Test lower bound - assert(lowerBound(nums, 0) == 0); // Smaller than all - assert(lowerBound(nums, 5) == 4); // Existing element - assert(lowerBound(nums, 6.5) == 6); // Between elements - - // Test upper bound - assert(upperBound(nums, 5) == 5); // Existing element - assert(upperBound(nums, 10) == -1); // Larger than all - assert(upperBound(nums, 6.5) == 6); // Between elements -} \ No newline at end of file +/** + * Algorithm: Binary Search + * Category: Searching + * + * Description: + * An efficient search algorithm that finds the position of a target value + * within a sorted array. Binary search compares the target value to the middle + * element of the array. If they are not equal, the half in which the target cannot + * lie is eliminated and the search continues on the remaining half, again taking + * the middle element to compare to the target value, and repeating until the target + * value is found or the array is exhausted. + * + * Time Complexity: O(log n) + * Space Complexity: O(1) + * + * Parameters: + * - arr: A sorted array of elements + * - target: The value to search for in the array + * - left: The starting index of the search range (optional) + * - right: The ending index of the search range (optional) + * + * Returns: + * - The index of the target if found, otherwise -1 + * + * Example Usage: + * vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + * int target = 7; + * int result = binarySearch(nums, target); // Returns 6 + * + * Variants: + * - Lower bound: Find the first element >= target + * - Upper bound: Find the first element > target + * - Closest element: Find the element closest to target + * + * References: + * - Introduction to Algorithms (CLRS), Chapter 2.3 + * - https://en.wikipedia.org/wiki/Binary_search_algorithm + * + * Related Problems: + * - LeetCode 704: Binary Search + * - LeetCode 35: Search Insert Position + * - LeetCode 33: Search in Rotated Sorted Array + */ + +#include + +template +int binarySearch(const std::vector &arr, const T &target) +{ + int left = 0; + int right = arr.size() - 1; + + while (left <= right) + { + // Calculate middle index (avoiding potential overflow) + int mid = left + (right - left) / 2; + + // Check if target is present at mid + if (arr[mid] == target) + { + return mid; + } + + // If target is greater, ignore left half + if (arr[mid] < target) + { + left = mid + 1; + } + // If target is smaller, ignore right half + else + { + right = mid - 1; + } + } + + // Target not found + return -1; +} + +// Lower bound variant: finds the first element >= target +template +int lowerBound(const std::vector &arr, const T &target) +{ + int left = 0; + int right = arr.size(); + + while (left < right) + { + int mid = left + (right - left) / 2; + if (arr[mid] < target) + { + left = mid + 1; + } + else + { + right = mid; + } + } + + // If left == arr.size(), target is greater than all elements + return (left < arr.size()) ? left : -1; +} + +// Upper bound variant: finds the first element > target +template +int upperBound(const std::vector &arr, const T &target) +{ + int left = 0; + int right = arr.size(); + + while (left < right) + { + int mid = left + (right - left) / 2; + if (arr[mid] <= target) + { + left = mid + 1; + } + else + { + right = mid; + } + } + + // If left == arr.size(), all elements are <= target + return (left < arr.size()) ? left : -1; +} + +// Usage example +void usage_example() +{ + std::vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + + // Basic binary search + int target = 7; + int result = binarySearch(nums, target); // Returns 6 + + // Lower bound example + int lb = lowerBound(nums, 7); // Returns 6 + + // Upper bound example + int ub = upperBound(nums, 7); // Returns 7 +} + +// Test cases +void test_cases() +{ + std::vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + + // Test binary search + assert(binarySearch(nums, 1) == 0); // First element + assert(binarySearch(nums, 10) == 9); // Last element + assert(binarySearch(nums, 5) == 4); // Middle element + assert(binarySearch(nums, 11) == -1); // Not found, greater than all + assert(binarySearch(nums, 0) == -1); // Not found, smaller than all + + // Test lower bound + assert(lowerBound(nums, 0) == 0); // Smaller than all + assert(lowerBound(nums, 5) == 4); // Existing element + assert(lowerBound(nums, 6.5) == 6); // Between elements + + // Test upper bound + assert(upperBound(nums, 5) == 5); // Existing element + assert(upperBound(nums, 10) == -1); // Larger than all + assert(upperBound(nums, 6.5) == 6); // Between elements +} diff --git a/CodeForce/CR1006(div3)/A.cpp b/CodeForce/CR1006(div3)/A.cpp index de838169..4ef6b8a6 100644 --- a/CodeForce/CR1006(div3)/A.cpp +++ b/CodeForce/CR1006(div3)/A.cpp @@ -1,26 +1,26 @@ -#include -#include -#include - -using namespace std; - -int main(){ - int t; - cin >> t; - vector a(3); - while(t--){ - for(int i = 0; i < 3; i++){ - cin >> a[i]; - } - double step = abs(double(a[1])/a[2]); - // printf("%.3f\n",step); - if(a[1] == 0){ - cout << 0 << endl; - }else if(step > a[0]){ - cout << -1 << endl; - }else{ - cout << ceil(step) << endl; - } - - } -} +#include +#include +#include + +using namespace std; + +int main(){ + int t; + cin >> t; + vector a(3); + while(t--){ + for(int i = 0; i < 3; i++){ + cin >> a[i]; + } + double step = abs(double(a[1])/a[2]); + // printf("%.3f\n",step); + if(a[1] == 0){ + cout << 0 << endl; + }else if(step > a[0]){ + cout << -1 << endl; + }else{ + cout << ceil(step) << endl; + } + + } +} diff --git a/CodeForce/CR1006(div3)/B.cpp b/CodeForce/CR1006(div3)/B.cpp index 0ddf7eef..a5f6b630 100644 --- a/CodeForce/CR1006(div3)/B.cpp +++ b/CodeForce/CR1006(div3)/B.cpp @@ -1,47 +1,47 @@ -// 引入输入输出流头文件 -#include - -// 使用标准命名空间 -using namespace std; - -int main() -{ - // 关闭同步流,提高IO速度 - ios::sync_with_stdio(false); - cin.tie(nullptr); - - // 读入测试用例数量 - int t; - cin >> t; - while (t--) // 处理每个测试用例 - { - // 读入字符串长度n和字符串s - int n; - string s; - cin >> n >> s; - - // 统计字符串中'-'和'_'的数量 - int c = 0, m = 0; // c统计'-',m统计'_' - for (char ch : s) - { - if (ch == '-') - c++; - else - m++; - } - - // 如果字符串长度小于3,或'-'数量小于2,或'_'数量小于1,输出0 - if (n < 3 || c < 2 || m < 1) - { - cout << "0\n"; - continue; - } - - // 将'-'分成左右两部分 - int left = c / 2; // 左边'-'的数量 - int right = c - left; // 右边'-'的数量 - // 计算并输出结果: '_'的数量 * 左边'-'的数量 * 右边'-'的数量 - cout << (long long)m * left * right << '\n'; - } - return 0; -} +// 引入输入输出流头文件 +#include + +// 使用标准命名空间 +using namespace std; + +int main() +{ + // 关闭同步流,提高IO速度 + ios::sync_with_stdio(false); + cin.tie(nullptr); + + // 读入测试用例数量 + int t; + cin >> t; + while (t--) // 处理每个测试用例 + { + // 读入字符串长度n和字符串s + int n; + string s; + cin >> n >> s; + + // 统计字符串中'-'和'_'的数量 + int c = 0, m = 0; // c统计'-',m统计'_' + for (char ch : s) + { + if (ch == '-') + c++; + else + m++; + } + + // 如果字符串长度小于3,或'-'数量小于2,或'_'数量小于1,输出0 + if (n < 3 || c < 2 || m < 1) + { + cout << "0\n"; + continue; + } + + // 将'-'分成左右两部分 + int left = c / 2; // 左边'-'的数量 + int right = c - left; // 右边'-'的数量 + // 计算并输出结果: '_'的数量 * 左边'-'的数量 * 右边'-'的数量 + cout << (long long)m * left * right << '\n'; + } + return 0; +} diff --git a/CodeForce/CR1006(div3)/C.cpp b/CodeForce/CR1006(div3)/C.cpp index 1bedee3f..4f2e4641 100644 --- a/CodeForce/CR1006(div3)/C.cpp +++ b/CodeForce/CR1006(div3)/C.cpp @@ -1,81 +1,82 @@ -#include - -using namespace std; - -// 这个函数用来解决每一个测试用例 -void solve() -{ - // 输入两个整数 n 和 x - // n 表示要生成的数组长度 - // x 是一个给定的数字,用于后续计算 - int n, x; - cin >> n >> x; - - // 如果数组长度为1,直接输出x并返回 - if (n == 1) - { - cout << x << "\n"; - return; - } - - std::vector ans(n); - - // 创建一个长度为n的数组来存储结果 - // sum用来存储按位或的结果 - // max记录最大可用的数字 - // val存储最终的按位或结果 - int sum = 0, max = 0, val = 0; - - // 这个循环用位运算来找到最大的可用数字 - // |= 是按位或运算,比如 5|3 = 7 (二进制:101|011 = 111) - // & 是按位与运算,用来检查结果是否合法 - for (int i = 0; i < n; ++i) - { - sum |= i; // 累积按位或的结果 - if ((sum & x) == sum) // 检查结果是否满足条件 - { - max = i; // 更新最大可用数字 - val = sum; // 保存当前的按位或结果 - } - } - - // 特殊情况处理:如果最大数是n-1但结果不满足条件 - // 就把最大数减少1 - if (max == n - 1 && (val & x) != x) - { - --max; - } - - // 从大到小填充数组的前半部分 - int i = 0; - while (i < n && max >= 0) - { - ans[i] = max--; // 依次放入递减的数字 - ++i; - } - - // 如果数组还没填满,剩余位置都填入x - if (i < n) - { - for (int j = i; j < n; ++j) - { - ans[j] = x; - } - } - - // 输出结果数组 - // [i == n - 1]是一个技巧,用来在最后一个数字后输出换行,其他数字后输出空格 - for (int i = 0; i < n; ++i) - { - cout << ans[i] << " \n"[i == n - 1]; - } -} - -// 主函数:处理多个测试用例 -int main(){ - int t; // 测试用例的数量 - cin >> t; - while(t--){ // 循环处理每个测试用例 - solve(); - } -} +#include +#include + +using namespace std; + +// 这个函数用来解决每一个测试用例 +void solve() +{ + // 输入两个整数 n 和 x + // n 表示要生成的数组长度 + // x 是一个给定的数字,用于后续计算 + int n, x; + cin >> n >> x; + + // 如果数组长度为1,直接输出x并返回 + if (n == 1) + { + cout << x << "\n"; + return; + } + + std::vector ans(n); + + // 创建一个长度为n的数组来存储结果 + // sum用来存储按位或的结果 + // max记录最大可用的数字 + // val存储最终的按位或结果 + int sum = 0, max = 0, val = 0; + + // 这个循环用位运算来找到最大的可用数字 + // |= 是按位或运算,比如 5|3 = 7 (二进制:101|011 = 111) + // & 是按位与运算,用来检查结果是否合法 + for (int i = 0; i < n; ++i) + { + sum |= i; // 累积按位或的结果 + if ((sum & x) == sum) // 检查结果是否满足条件 + { + max = i; // 更新最大可用数字 + val = sum; // 保存当前的按位或结果 + } + } + + // 特殊情况处理:如果最大数是n-1但结果不满足条件 + // 就把最大数减少1 + if (max == n - 1 && (val & x) != x) + { + --max; + } + + // 从大到小填充数组的前半部分 + int i = 0; + while (i < n && max >= 0) + { + ans[i] = max--; // 依次放入递减的数字 + ++i; + } + + // 如果数组还没填满,剩余位置都填入x + if (i < n) + { + for (int j = i; j < n; ++j) + { + ans[j] = x; + } + } + + // 输出结果数组 + // [i == n - 1]是一个技巧,用来在最后一个数字后输出换行,其他数字后输出空格 + for (int i = 0; i < n; ++i) + { + cout << ans[i] << " \n"[i == n - 1]; + } +} + +// 主函数:处理多个测试用例 +int main(){ + int t; // 测试用例的数量 + cin >> t; + while(t--){ // 循环处理每个测试用例 + solve(); + } +} diff --git a/CodeForce/CR176(div2)/A.cpp b/CodeForce/CR176(div2)/A.cpp index d6075f5c..36c151ec 100644 --- a/CodeForce/CR176(div2)/A.cpp +++ b/CodeForce/CR176(div2)/A.cpp @@ -1,36 +1,36 @@ -#include - -using namespace std; - -int main() -{ - int t; - cin >> t; - - while (t--) - { - long long n, k; - cin >> n >> k; - - long long ans = 0; - - // 处理n为奇数的情况 - if (n % 2 == 1) - { - ans = 1; // 第一步必须减去一个奇数 - n -= k; // 减去最大的奇数k - } - - // 此时n一定是偶数(或负数) - if (n > 0) - { - // 计算需要多少次操作将n减到0 - // 每次减去k-1(最大的偶数) - ans += (n + k - 2) / (k - 1); - } - - cout << ans << endl; - } - - return 0; -} +#include + +using namespace std; + +int main() +{ + int t; + cin >> t; + + while (t--) + { + long long n, k; + cin >> n >> k; + + long long ans = 0; + + // 处理n为奇数的情况 + if (n % 2 == 1) + { + ans = 1; // 第一步必须减去一个奇数 + n -= k; // 减去最大的奇数k + } + + // 此时n一定是偶数(或负数) + if (n > 0) + { + // 计算需要多少次操作将n减到0 + // 每次减去k-1(最大的偶数) + ans += (n + k - 2) / (k - 1); + } + + cout << ans << endl; + } + + return 0; +} diff --git a/CodeForce/CR176(div2)/B.cpp b/CodeForce/CR176(div2)/B.cpp index adfa7ec5..c4e2fb68 100644 --- a/CodeForce/CR176(div2)/B.cpp +++ b/CodeForce/CR176(div2)/B.cpp @@ -1,135 +1,135 @@ -#include -#include -#include -#include - -using namespace std; - -int main() -{ - int t; - cin >> t; - - while (t--) - { - int n, k; - cin >> n >> k; - vector a(n); - - for (int i = 0; i < n; i++) - { - cin >> a[i]; - } - - // 如果k=n-1,所有元素都会被染色,最后一个染色的元素是剩下的那个 - if (k == n - 1) - { - // 找出最小的元素作为最后染色的元素 - long long min_val = *min_element(a.begin(), a.end()); - long long sum = 0; - for (int i = 0; i < n; i++) - { - if (a[i] != min_val || sum == 0) - { - sum += a[i]; - } - } - cout << sum << endl; - continue; - } - - // 一般情况:尝试不同的初始染色方案 - long long max_cost = 0; - - // 尝试每个元素作为最后染色的元素 - for (int last = 0; last < n; last++) - { - // 创建元素值和索引的对 - vector> val_idx; - for (int i = 0; i < n; i++) - { - if (i != last) - { - val_idx.push_back({a[i], i}); - } - } - - // 按值从大到小排序 - sort(val_idx.rbegin(), val_idx.rend()); - - // 选择前k个最大值 - vector selected(n, 0); - long long initial_sum = 0; - for (int i = 0; i < k; i++) - { - selected[val_idx[i].second] = 1; - initial_sum += val_idx[i].first; - } - - // 检查是否能染色所有元素 - bool can_color_all = false; - vector colored = selected; - queue q; - - // 将初始蓝色元素加入队列 - for (int i = 0; i < n; i++) - { - if (colored[i]) - { - q.push(i); - } - } - - // BFS扩展染色 - while (!q.empty()) - { - int curr = q.front(); - q.pop(); - - // 检查左邻居 - if (curr > 0 && !colored[curr - 1]) - { - colored[curr - 1] = 1; - q.push(curr - 1); - } - - // 检查右邻居 - if (curr < n - 1 && !colored[curr + 1]) - { - colored[curr + 1] = 1; - q.push(curr + 1); - } - } - - // 检查last是否是最后一个被染色的元素 - int colored_count = 0; - for (int i = 0; i < n; i++) - { - if (colored[i]) - colored_count++; - } - - if (colored_count == n && !selected[last]) - { - // 计算成本 - long long cost = initial_sum + a[last]; - max_cost = max(max_cost, cost); - } - } - - // 特殊情况:如果我们可以直接选择所有元素 - if (k == n) - { - long long sum = 0; - for (int i = 0; i < n; i++) - { - sum += a[i]; - } - max_cost = max(max_cost, sum); - } - - cout << max_cost << endl; - } - - return 0; -} +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int t; + cin >> t; + + while (t--) + { + int n, k; + cin >> n >> k; + vector a(n); + + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + + // 如果k=n-1,所有元素都会被染色,最后一个染色的元素是剩下的那个 + if (k == n - 1) + { + // 找出最小的元素作为最后染色的元素 + long long min_val = *min_element(a.begin(), a.end()); + long long sum = 0; + for (int i = 0; i < n; i++) + { + if (a[i] != min_val || sum == 0) + { + sum += a[i]; + } + } + cout << sum << endl; + continue; + } + + // 一般情况:尝试不同的初始染色方案 + long long max_cost = 0; + + // 尝试每个元素作为最后染色的元素 + for (int last = 0; last < n; last++) + { + // 创建元素值和索引的对 + vector> val_idx; + for (int i = 0; i < n; i++) + { + if (i != last) + { + val_idx.push_back({a[i], i}); + } + } + + // 按值从大到小排序 + sort(val_idx.rbegin(), val_idx.rend()); + + // 选择前k个最大值 + vector selected(n, 0); + long long initial_sum = 0; + for (int i = 0; i < k; i++) + { + selected[val_idx[i].second] = 1; + initial_sum += val_idx[i].first; + } + + // 检查是否能染色所有元素 + bool can_color_all = false; + vector colored = selected; + queue q; + + // 将初始蓝色元素加入队列 + for (int i = 0; i < n; i++) + { + if (colored[i]) + { + q.push(i); + } + } + + // BFS扩展染色 + while (!q.empty()) + { + int curr = q.front(); + q.pop(); + + // 检查左邻居 + if (curr > 0 && !colored[curr - 1]) + { + colored[curr - 1] = 1; + q.push(curr - 1); + } + + // 检查右邻居 + if (curr < n - 1 && !colored[curr + 1]) + { + colored[curr + 1] = 1; + q.push(curr + 1); + } + } + + // 检查last是否是最后一个被染色的元素 + int colored_count = 0; + for (int i = 0; i < n; i++) + { + if (colored[i]) + colored_count++; + } + + if (colored_count == n && !selected[last]) + { + // 计算成本 + long long cost = initial_sum + a[last]; + max_cost = max(max_cost, cost); + } + } + + // 特殊情况:如果我们可以直接选择所有元素 + if (k == n) + { + long long sum = 0; + for (int i = 0; i < n; i++) + { + sum += a[i]; + } + max_cost = max(max_cost, sum); + } + + cout << max_cost << endl; + } + + return 0; +} diff --git a/CodeForce/Codeforces Round 1063 (Div. 2)/A/A.cpp b/CodeForce/Codeforces Round 1063 (Div. 2)/A/A.cpp index e6cc2de5..b43e9210 100644 --- a/CodeForce/Codeforces Round 1063 (Div. 2)/A/A.cpp +++ b/CodeForce/Codeforces Round 1063 (Div. 2)/A/A.cpp @@ -30,6 +30,6 @@ int main() { } } - + return 0; } diff --git a/CodeForce/Codeforces Round 1063 (Div. 2)/A/A2.cpp b/CodeForce/Codeforces Round 1063 (Div. 2)/A/A2.cpp index f5da0d84..bddabb6d 100644 --- a/CodeForce/Codeforces Round 1063 (Div. 2)/A/A2.cpp +++ b/CodeForce/Codeforces Round 1063 (Div. 2)/A/A2.cpp @@ -1,10 +1,11 @@ +#include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); - + int t; cin >> t; while (t--) { @@ -12,7 +13,7 @@ int main() { cin >> n; vector a(n); for (int i = 0; i < n; ++i) cin >> a[i]; - + // 判断是否有重复元素 sort(a.begin(), a.end()); bool hasDuplicate = false; diff --git a/CodeForce/Codeforces Round 1063 (Div. 2)/B/B.cpp b/CodeForce/Codeforces Round 1063 (Div. 2)/B/B.cpp index 2cba8914..6aa9225d 100644 --- a/CodeForce/Codeforces Round 1063 (Div. 2)/B/B.cpp +++ b/CodeForce/Codeforces Round 1063 (Div. 2)/B/B.cpp @@ -1,10 +1,11 @@ +#include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); - + int t; cin >> t; while (t--) { diff --git a/CodeForce/Codeforces Round 1078 (Div. 2)/B.py b/CodeForce/Codeforces Round 1078 (Div. 2)/B.py index 24b11ebe..617ef33e 100644 --- a/CodeForce/Codeforces Round 1078 (Div. 2)/B.py +++ b/CodeForce/Codeforces Round 1078 (Div. 2)/B.py @@ -21,7 +21,7 @@ def solve(): best = 0 for v,f in zip(a,floors): - cand = v + (S-f)*y + cand = v + (S-f)*y if cand > best: best = cand @@ -33,5 +33,5 @@ def solve(): out = [] for _ in range(t): solve() - + sys.stdout.write("\n".join(out)) diff --git a/CodeForce/Codeforces Round 1086 (Div. 2)/A.py b/CodeForce/Codeforces Round 1086 (Div. 2)/A.py index 9c9103c6..1825167a 100644 --- a/CodeForce/Codeforces Round 1086 (Div. 2)/A.py +++ b/CodeForce/Codeforces Round 1086 (Div. 2)/A.py @@ -18,7 +18,7 @@ def solve(): if counts[color] > limit: ok = False break - + print("YES" if ok else "NO") if __name__ == "__main__": # sys.setrecursionlimit(10**7) diff --git a/CodeForce/Codeforces Round 1091 (Div. 2) and CodeCraft 26/A.cpp b/CodeForce/Codeforces Round 1091 (Div. 2) and CodeCraft 26/A.cpp index 154a3963..316c90e5 100644 --- a/CodeForce/Codeforces Round 1091 (Div. 2) and CodeCraft 26/A.cpp +++ b/CodeForce/Codeforces Round 1091 (Div. 2) and CodeCraft 26/A.cpp @@ -31,19 +31,19 @@ il void solve(){ }else{ cout << "NO" << endl; } - + } int main() { fastio; - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A.cpp b/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A.cpp index 51c13d15..f5f6642c 100644 --- a/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A.cpp +++ b/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A.cpp @@ -6,28 +6,28 @@ using namespace std; int main() { int t; cin >> t; - + while (t--) { int n, a; cin >> n >> a; - + vector v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } - + // 找到 a 的插入位置(第一个 >= a 的位置) int idx = lower_bound(v.begin(), v.end(), a) - v.begin(); - + int left_count = idx; // 左边元素数量 int right_count = n - idx; // 右边元素数量 - + if (left_count > right_count) { cout << a - 1 << endl; } else { cout << a + 1 << endl; } } - + return 0; -} \ No newline at end of file +} diff --git a/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A2.py b/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A2.py index adf055eb..8a24420a 100644 --- a/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A2.py +++ b/CodeForce/Educational Codeforces Round 184 (Rated for Div. 2)/A2.py @@ -4,19 +4,19 @@ for _ in range(t): n, a = map(int, input().split()) v = list(map(int, input().split())) - + idx = bisect.bisect_left(v, a) - + # 检查 a 是否在数组中 has_a = (idx < n and v[idx] == a) - + left_count = idx if has_a: right_count = n - idx - 1 # 不包括 a 本身 else: right_count = n - idx - + if left_count > right_count: print(a - 1) else: - print(a + 1) \ No newline at end of file + print(a + 1) diff --git a/CodeForce/UTPC Contest 10-15-25 Div. 2 (Beginner)/A. Trilobyte/1.cpp b/CodeForce/UTPC Contest 10-15-25 Div. 2 (Beginner)/A. Trilobyte/1.cpp index 2bd734f2..d0bf1a6e 100644 --- a/CodeForce/UTPC Contest 10-15-25 Div. 2 (Beginner)/A. Trilobyte/1.cpp +++ b/CodeForce/UTPC Contest 10-15-25 Div. 2 (Beginner)/A. Trilobyte/1.cpp @@ -1,36 +1,36 @@ - -#include -using namespace std; - -typedef long long ll; - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - //8,16.32,64,128,216 - int n, k; - cin >> n; - - if (n % 8 == 0) { - cout << 0 << '\n'; - return 0; - } - - for (int i = 0; i < 100; ++i) { - if (i < 10) { - if ((n * 10 + i) % 8 == 0) { - cout << i << '\n'; - return 0; - } - else continue; - } - else if (i >= 10) { - if ((n * 100 + i) % 8 == 0) { - cout << i << '\n'; - return 0; - } - else continue; - } - } - - return 0; -} \ No newline at end of file + +#include +using namespace std; + +typedef long long ll; + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + //8,16.32,64,128,216 + int n, k; + cin >> n; + + if (n % 8 == 0) { + cout << 0 << '\n'; + return 0; + } + + for (int i = 0; i < 100; ++i) { + if (i < 10) { + if ((n * 10 + i) % 8 == 0) { + cout << i << '\n'; + return 0; + } + else continue; + } + else if (i >= 10) { + if ((n * 100 + i) % 8 == 0) { + cout << i << '\n'; + return 0; + } + else continue; + } + } + + return 0; +} diff --git a/ICPC/1/1.cpp b/ICPC/1/1.cpp index 98a9a930..14e9884d 100644 --- a/ICPC/1/1.cpp +++ b/ICPC/1/1.cpp @@ -1,15 +1,15 @@ -#include - -using namespace std; - -int main(){ - unsigned long long T; - cin >> T; - while(T--){ - unsigned long long x, y; - cin >> x >> y; - // if(x>y) swap(x,y); - cout << "1 " << ((x * y) /(__gcd(x, y))/(__gcd(x, y))) << endl; - } - return 0; -} +#include + +using namespace std; + +int main(){ + unsigned long long T; + cin >> T; + while(T--){ + unsigned long long x, y; + cin >> x >> y; + // if(x>y) swap(x,y); + cout << "1 " << ((x * y) /(__gcd(x, y))/(__gcd(x, y))) << endl; + } + return 0; +} diff --git a/ICPC/1/tempCodeRunnerFile.cpp b/ICPC/1/tempCodeRunnerFile.cpp index c82b60a5..cf0afbe5 100644 --- a/ICPC/1/tempCodeRunnerFile.cpp +++ b/ICPC/1/tempCodeRunnerFile.cpp @@ -1,29 +1,29 @@ -#include - -using namespace std; - -int gcd(int a, int b) { - while (b != 0) { - int t = b; - b = a % b; - a = t; - } - return a; -} - -int main(){ - int T; - cin >> T; - int a[T]; - - for(int i = 0;i < T;i++){ - int x, y, N; - cin >> x >> y; - N = (int)(x * y) / pow(gcd(x, y), 2); - a[i] = N; - } - for(int i = 0;i < T;i++){ - printf("1 %d\n",a[i]); - } - return 0; -} +#include + +using namespace std; + +int gcd(int a, int b) { + while (b != 0) { + int t = b; + b = a % b; + a = t; + } + return a; +} + +int main(){ + int T; + cin >> T; + int a[T]; + + for(int i = 0;i < T;i++){ + int x, y, N; + cin >> x >> y; + N = (int)(x * y) / pow(gcd(x, y), 2); + a[i] = N; + } + for(int i = 0;i < T;i++){ + printf("1 %d\n",a[i]); + } + return 0; +} diff --git a/acwing/problems/.cpp b/acwing/problems/.cpp index c23496b9..523c881b 100644 --- a/acwing/problems/.cpp +++ b/acwing/problems/.cpp @@ -1,10 +1,10 @@ /* * @acwing app=acwing.cn id=38 lang=C++ * -* +* */ // @acwing code start -// @acwing code end \ No newline at end of file +// @acwing code end diff --git "a/acwing/problems/29.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\350\212\202\347\202\271.cpp" "b/acwing/problems/29.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\350\212\202\347\202\271.cpp" index 66f5e2f0..1b3d14ae 100644 --- "a/acwing/problems/29.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\350\212\202\347\202\271.cpp" +++ "b/acwing/problems/29.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\351\207\215\345\244\215\347\232\204\350\212\202\347\202\271.cpp" @@ -1,33 +1,33 @@ -/* -* @acwing app=acwing.cn id=27 lang=C++ -* -* 29. 删除链表中重复的节点 -*/ - -// @acwing code start - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - ListNode* deleteDuplication(ListNode* head) { - auto dummy = new ListNode(-1); - dummy->next = head; - - auto p = dummy; - while(p->next){ - auto q = p->next; - while(q && p->next->val == q->val)q = q->next; - if(p->next->next == q)p = p->next; - else p->next = q; - } - return dummy->next; - } -}; -// @acwing code end +/* +* @acwing app=acwing.cn id=27 lang=C++ +* +* 29. 删除链表中重复的节点 +*/ + +// @acwing code start + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* deleteDuplication(ListNode* head) { + auto dummy = new ListNode(-1); + dummy->next = head; + + auto p = dummy; + while(p->next){ + auto q = p->next; + while(q && p->next->val == q->val)q = q->next; + if(p->next->next == q)p = p->next; + else p->next = q; + } + return dummy->next; + } +}; +// @acwing code end diff --git "a/acwing/problems/30.\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.cpp" "b/acwing/problems/30.\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.cpp" index bbb57f63..19ccd34b 100644 --- "a/acwing/problems/30.\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.cpp" +++ "b/acwing/problems/30.\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.cpp" @@ -1,3 +1,4 @@ +#include /* * @acwing app=acwing.cn id=28 lang=C++ * @@ -16,7 +17,7 @@ class Solution { s = _s,p = _p; n = s.size(); m = p.size(); - + f = vector>(n+1,vector(m+1,-1)); return dp(0,0); } diff --git "a/acwing/problems/31.\350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.cpp" "b/acwing/problems/31.\350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.cpp" index e06d2fe3..186a1aa2 100644 --- "a/acwing/problems/31.\350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.cpp" +++ "b/acwing/problems/31.\350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262.cpp" @@ -12,13 +12,13 @@ class Solution { // 去除前后空格 int i = 0; // 删除 s 前面的空格 - while(i < s.size() && s[i] == ' ')++i; + while(i < s.size() && s[i] == ' ')++i; s = s.substr(i); // 删除 s 后面的空格 while(s.size() && s.back() == ' ')s.pop_back(); if(s.empty())return false; - + bool numFlag = false; bool dotFlag = false; bool eFlag = false; diff --git "a/acwing/problems/32.\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.cpp" "b/acwing/problems/32.\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.cpp" index c37885e3..ba82e952 100644 --- "a/acwing/problems/32.\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.cpp" +++ "b/acwing/problems/32.\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.cpp" @@ -1,3 +1,4 @@ +#include /* * @acwing app=acwing.cn id=30 lang=C++ * diff --git "a/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271-2.cpp" "b/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271-2.cpp" index f17ff1d0..ba126eda 100644 --- "a/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271-2.cpp" +++ "b/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271-2.cpp" @@ -1,40 +1,40 @@ -/* - * @Author: tkzzzzzz6 - * @Date: 2026-04-21 18:45:16 - * @LastEditors: tkzzzzzz6 - * @LastEditTime: 2026-04-21 18:47:00 - */ -/* -* @acwing app=acwing.cn id=32 lang=C++ -* -* 33. 链表中倒数第k个节点 -*/ - -// @acwing code start - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - ListNode* findKthToTail(ListNode* head, int k) { - auto fast = head,slow = head; - - while(k--){ - if(fast == nullptr)return nullptr; - fast = fast->next; - } - - while(fast){ - slow = slow->next; - fast = fast->next; - } - return slow; - } -}; -// @acwing code end +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-21 18:45:16 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-21 18:47:00 + */ +/* +* @acwing app=acwing.cn id=32 lang=C++ +* +* 33. 链表中倒数第k个节点 +*/ + +// @acwing code start + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* findKthToTail(ListNode* head, int k) { + auto fast = head,slow = head; + + while(k--){ + if(fast == nullptr)return nullptr; + fast = fast->next; + } + + while(fast){ + slow = slow->next; + fast = fast->next; + } + return slow; + } +}; +// @acwing code end diff --git "a/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.cpp" "b/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.cpp" index 53275dac..1dab51bf 100644 --- "a/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.cpp" +++ "b/acwing/problems/33.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.cpp" @@ -1,35 +1,35 @@ -/* -* @acwing app=acwing.cn id=32 lang=C++ -* -* 33. 链表中倒数第k个节点 -*/ - -// @acwing code start - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - ListNode* findKthToTail(ListNode* head, int k) { - int n = 0; - for(auto cur = head;cur != nullptr;cur = cur->next){ - ++n; - } - - if(k > n)return nullptr; - - auto p = head; - for(int i = 0;inext; - } - - return p; - } -}; -// @acwing code end +/* +* @acwing app=acwing.cn id=32 lang=C++ +* +* 33. 链表中倒数第k个节点 +*/ + +// @acwing code start + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* findKthToTail(ListNode* head, int k) { + int n = 0; + for(auto cur = head;cur != nullptr;cur = cur->next){ + ++n; + } + + if(k > n)return nullptr; + + auto p = head; + for(int i = 0;inext; + } + + return p; + } +}; +// @acwing code end diff --git "a/acwing/problems/34.\351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\347\273\223\347\202\271.cpp" "b/acwing/problems/34.\351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\347\273\223\347\202\271.cpp" index c411b36e..e82bac36 100644 --- "a/acwing/problems/34.\351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\347\273\223\347\202\271.cpp" +++ "b/acwing/problems/34.\351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\347\273\223\347\202\271.cpp" @@ -1,48 +1,48 @@ -/* - * @Author: tkzzzzzz6 - * @Date: 2026-04-21 21:19:52 - * @LastEditors: tkzzzzzz6 - * @LastEditTime: 2026-04-21 21:29:08 - */ -/* -* @acwing app=acwing.cn id=86 lang=C++ -* -* 34. 链表中环的入口结点 -*/ - -// @acwing code start - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - ListNode *entryNodeOfLoop(ListNode *head) { - if(!head || !head->next)return nullptr; - - auto slow = head,fast = head; - - while(fast && fast->next){ - slow = slow->next; - fast = fast->next->next; - if(slow == fast)break; - } - - if(!fast || !fast->next)return nullptr; - - slow = head; - while(slow != fast){ - slow = slow->next; - fast = fast->next; - } - - return slow; - - } -}; -// @acwing code end +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-21 21:19:52 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-21 21:29:08 + */ +/* +* @acwing app=acwing.cn id=86 lang=C++ +* +* 34. 链表中环的入口结点 +*/ + +// @acwing code start + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *entryNodeOfLoop(ListNode *head) { + if(!head || !head->next)return nullptr; + + auto slow = head,fast = head; + + while(fast && fast->next){ + slow = slow->next; + fast = fast->next->next; + if(slow == fast)break; + } + + if(!fast || !fast->next)return nullptr; + + slow = head; + while(slow != fast){ + slow = slow->next; + fast = fast->next; + } + + return slow; + + } +}; +// @acwing code end diff --git "a/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250-2.cpp" "b/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250-2.cpp" index 2f5fb0a4..c76844e6 100644 --- "a/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250-2.cpp" +++ "b/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250-2.cpp" @@ -1,36 +1,36 @@ -/* - * @Author: tkzzzzzz6 - * @Date: 2026-04-21 22:02:20 - * @LastEditors: tkzzzzzz6 - * @LastEditTime: 2026-04-21 22:04:31 - */ -/* -* @acwing app=acwing.cn id=33 lang=C++ -* -* 35. 反转链表 -*/ - -// @acwing code start - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - ListNode* reverseList(ListNode* head) { - if(!head || !head->next)return head; - - auto tail = reverseList(head->next); - - head->next->next = head; - head->next = nullptr; - - return tail; - } -}; -// @acwing code end +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-21 22:02:20 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-21 22:04:31 + */ +/* +* @acwing app=acwing.cn id=33 lang=C++ +* +* 35. 反转链表 +*/ + +// @acwing code start + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + if(!head || !head->next)return head; + + auto tail = reverseList(head->next); + + head->next->next = head; + head->next = nullptr; + + return tail; + } +}; +// @acwing code end diff --git "a/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250.cpp" "b/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250.cpp" index f6f2c3da..21cdb771 100644 --- "a/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250.cpp" +++ "b/acwing/problems/35.\345\217\215\350\275\254\351\223\276\350\241\250.cpp" @@ -1,40 +1,40 @@ -/* - * @Author: tkzzzzzz6 - * @Date: 2026-04-21 22:02:20 - * @LastEditors: tkzzzzzz6 - * @LastEditTime: 2026-04-21 22:04:31 - */ -/* -* @acwing app=acwing.cn id=33 lang=C++ -* -* 35. 反转链表 -*/ - -// @acwing code start - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - ListNode* reverseList(ListNode* head) { - if(!head || !head->next)return head; - auto a = head,b = head->next; - - while(b){ - auto t = b->next; - b->next = a; - a = b,b = t; - } - - head->next = nullptr; - - return a; - } -}; -// @acwing code end +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-21 22:02:20 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-21 22:04:31 + */ +/* +* @acwing app=acwing.cn id=33 lang=C++ +* +* 35. 反转链表 +*/ + +// @acwing code start + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + if(!head || !head->next)return head; + auto a = head,b = head->next; + + while(b){ + auto t = b->next; + b->next = a; + a = b,b = t; + } + + head->next = nullptr; + + return a; + } +}; +// @acwing code end diff --git "a/acwing/problems/36.\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250.cpp" "b/acwing/problems/36.\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250.cpp" index ea219ca9..f4f65b0c 100644 --- "a/acwing/problems/36.\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250.cpp" +++ "b/acwing/problems/36.\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250.cpp" @@ -1,49 +1,49 @@ -/* - * @Author: tkzzzzzz6 - * @Date: 2026-04-22 08:01:54 - * @LastEditors: tkzzzzzz6 - * @LastEditTime: 2026-04-22 08:09:06 - */ -/* -* @acwing app=acwing.cn id=34 lang=C++ -* -* 36. 合并两个排序的链表 -*/ - -// @acwing code start - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - ListNode* merge(ListNode* l1, ListNode* l2) { - auto dummy = new ListNode(0); - auto cur = dummy; - - while(l1 && l2){ - if(l1->val > l2->val){ - cur->next = l2; - l2 = l2->next; - }else{ - cur->next = l1; - l1 = l1->next; - } - cur = cur->next; - } - - if(l1){ - cur->next = l1; - }else{ - cur->next = l2; - } - - return dummy->next; - } -}; -// @acwing code end +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-22 08:01:54 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-22 08:09:06 + */ +/* +* @acwing app=acwing.cn id=34 lang=C++ +* +* 36. 合并两个排序的链表 +*/ + +// @acwing code start + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* merge(ListNode* l1, ListNode* l2) { + auto dummy = new ListNode(0); + auto cur = dummy; + + while(l1 && l2){ + if(l1->val > l2->val){ + cur->next = l2; + l2 = l2->next; + }else{ + cur->next = l1; + l1 = l1->next; + } + cur = cur->next; + } + + if(l1){ + cur->next = l1; + }else{ + cur->next = l2; + } + + return dummy->next; + } +}; +// @acwing code end diff --git "a/acwing/problems/37.\346\240\221\347\232\204\345\255\220\347\273\223\346\236\204.cpp" "b/acwing/problems/37.\346\240\221\347\232\204\345\255\220\347\273\223\346\236\204.cpp" index c13ac65f..11d16f72 100644 --- "a/acwing/problems/37.\346\240\221\347\232\204\345\255\220\347\273\223\346\236\204.cpp" +++ "b/acwing/problems/37.\346\240\221\347\232\204\345\255\220\347\273\223\346\236\204.cpp" @@ -1,32 +1,32 @@ -/* -* @acwing app=acwing.cn id=35 lang=C++ -* -* 37. 树的子结构 -*/ - -// @acwing code start - -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) { - if(!pRoot1 || !pRoot2)return false; - if(isSame(pRoot1,pRoot2))return true; - return hasSubtree(pRoot1->left,pRoot2) || hasSubtree(pRoot1->right,pRoot2); - } - - bool isSame(TreeNode* root1,TreeNode* root2){ - if(!root2)return true; - if(!root1 || root1->val != root2->val)return false; - return isSame(root1->left,root2->left) && isSame(root1->right,root2->right); - } -}; -// @acwing code end +/* +* @acwing app=acwing.cn id=35 lang=C++ +* +* 37. 树的子结构 +*/ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) { + if(!pRoot1 || !pRoot2)return false; + if(isSame(pRoot1,pRoot2))return true; + return hasSubtree(pRoot1->left,pRoot2) || hasSubtree(pRoot1->right,pRoot2); + } + + bool isSame(TreeNode* root1,TreeNode* root2){ + if(!root2)return true; + if(!root1 || root1->val != root2->val)return false; + return isSame(root1->left,root2->left) && isSame(root1->right,root2->right); + } +}; +// @acwing code end diff --git "a/acwing/problems/38.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.cpp" "b/acwing/problems/38.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.cpp" index 6e5ded35..224b9948 100644 --- "a/acwing/problems/38.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.cpp" +++ "b/acwing/problems/38.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.cpp" @@ -1,27 +1,27 @@ -/* -* @acwing app=acwing.cn id=37 lang=C++ -* -* 38. 二叉树的镜像 -*/ - -// @acwing code start - -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - void mirror(TreeNode* root) { - if(!root)return; - swap(root->left,root->right); - mirror(root->left); - mirror(root->right); - } -}; -// @acwing code end +/* +* @acwing app=acwing.cn id=37 lang=C++ +* +* 38. 二叉树的镜像 +*/ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + void mirror(TreeNode* root) { + if(!root)return; + swap(root->left,root->right); + mirror(root->left); + mirror(root->right); + } +}; +// @acwing code end diff --git "a/acwing/problems/39.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.cpp" "b/acwing/problems/39.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.cpp" index 93c0d0be..690587be 100644 --- "a/acwing/problems/39.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.cpp" +++ "b/acwing/problems/39.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.cpp" @@ -1,36 +1,36 @@ -/* - * @Author: tkzzzzzz6 - * @Date: 2026-04-22 10:58:49 - * @LastEditors: tkzzzzzz6 - * @LastEditTime: 2026-04-22 11:01:05 - */ -/* -* @acwing app=acwing.cn id=38 lang=C++ -* -* 39. 对称的二叉树 -*/ - -// @acwing code start - -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - bool isSymmetric(TreeNode* root) { - return !root || dfs(root->left,root->right); - } - - bool dfs(TreeNode* p,TreeNode* q){ - if(!p || !q)return !p && !q; - - return (p->val == q->val) && dfs(p->left,q->right) && dfs(p->right,q->left); - } -}; -// @acwing code end +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-22 10:58:49 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-22 11:01:05 + */ +/* +* @acwing app=acwing.cn id=38 lang=C++ +* +* 39. 对称的二叉树 +*/ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + bool isSymmetric(TreeNode* root) { + return !root || dfs(root->left,root->right); + } + + bool dfs(TreeNode* p,TreeNode* q){ + if(!p || !q)return !p && !q; + + return (p->val == q->val) && dfs(p->left,q->right) && dfs(p->right,q->left); + } +}; +// @acwing code end diff --git "a/acwing/problems/40.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.cpp" "b/acwing/problems/40.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.cpp" index 4dbba5fc..1dcbcc64 100644 --- "a/acwing/problems/40.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.cpp" +++ "b/acwing/problems/40.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.cpp" @@ -1,3 +1,4 @@ +#include /* * @acwing app=acwing.cn id=39 lang=C++ * @@ -10,7 +11,7 @@ class Solution { public: vector printMatrix(vector > matrix) { if(matrix.empty() || matrix[0].empty())return vector(); - + vector res; int n = matrix.size(),m = matrix[0].size(); int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0}; diff --git "a/acwing/problems/41.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.cpp" "b/acwing/problems/41.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.cpp" index c3e34bdf..949efa87 100644 --- "a/acwing/problems/41.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.cpp" +++ "b/acwing/problems/41.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.cpp" @@ -1,49 +1,49 @@ -/* -* @acwing app=acwing.cn id=90 lang=C++ -* -* 41. 包含min函数的栈 -*/ - -// @acwing code start - -class MinStack { -public: - /** initialize your data structure here. */ - stack st,stmin; - MinStack() { - - } - - void push(int x) { - st.push(x); - if(stmin.empty()){ - stmin.push(x); - }else{ - stmin.push(min(x,stmin.top())); - } - } - - void pop() { - if(st.empty())return; - st.pop(); - stmin.pop(); - } - - int top() { - return st.empty() ? -1 : st.top(); - } - - int getMin() { - return stmin.empty() ? -1 : stmin.top(); - } -}; - -/** - * Your MinStack object will be instantiated and called as such: - * MinStack obj = new MinStack(); - * obj.push(x); - * obj.pop(); - * int param_3 = obj.top(); - * int param_4 = obj.getMin(); - */ -// @acwing code end +/* +* @acwing app=acwing.cn id=90 lang=C++ +* +* 41. 包含min函数的栈 +*/ + +// @acwing code start + +class MinStack { +public: + /** initialize your data structure here. */ + stack st,stmin; + MinStack() { + + } + + void push(int x) { + st.push(x); + if(stmin.empty()){ + stmin.push(x); + }else{ + stmin.push(min(x,stmin.top())); + } + } + + void pop() { + if(st.empty())return; + st.pop(); + stmin.pop(); + } + + int top() { + return st.empty() ? -1 : st.top(); + } + + int getMin() { + return stmin.empty() ? -1 : stmin.top(); + } +}; + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(x); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ +// @acwing code end diff --git "a/acwing/problems/42.\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227.cpp" "b/acwing/problems/42.\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227.cpp" index 9d19d3b8..0b762be2 100644 --- "a/acwing/problems/42.\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227.cpp" +++ "b/acwing/problems/42.\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227.cpp" @@ -1,3 +1,4 @@ +#include /* * @acwing app=acwing.cn id=40 lang=C++ * diff --git "a/acwing/problems/43.\344\270\215\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" "b/acwing/problems/43.\344\270\215\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" index 429914bb..5881bcd4 100644 --- "a/acwing/problems/43.\344\270\215\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" +++ "b/acwing/problems/43.\344\270\215\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" @@ -1,38 +1,39 @@ -/* -* @acwing app=acwing.cn id=41 lang=C++ -* -* 43. 不分行从上往下打印二叉树 -*/ - -// @acwing code start - -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - vector printFromTopToBottom(TreeNode* root) { - vector res; - if(!root)return res; - - queue q; - q.push(root); - - while(q.size()){ - auto t = q.front(); - q.pop(); - res.push_back(t->val); - if(t->left)q.push(t->left); - if(t->right)q.push(t->right); - } - - return res; - } -}; -// @acwing code end +#include +/* +* @acwing app=acwing.cn id=41 lang=C++ +* +* 43. 不分行从上往下打印二叉树 +*/ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + vector printFromTopToBottom(TreeNode* root) { + vector res; + if(!root)return res; + + queue q; + q.push(root); + + while(q.size()){ + auto t = q.front(); + q.pop(); + res.push_back(t->val); + if(t->left)q.push(t->left); + if(t->right)q.push(t->right); + } + + return res; + } +}; +// @acwing code end diff --git "a/acwing/problems/44.\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" "b/acwing/problems/44.\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" index 4197ce72..43316f0c 100644 --- "a/acwing/problems/44.\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" +++ "b/acwing/problems/44.\345\210\206\350\241\214\344\273\216\344\270\212\345\276\200\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" @@ -1,49 +1,50 @@ -/* -* @acwing app=acwing.cn id=42 lang=C++ -* -* 44. 分行从上往下打印二叉树 -*/ - -// @acwing code start - -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - vector> printFromTopToBottom(TreeNode* root) { - vector> res; - if(!root)return res; - - queue q; - q.push(root); - q.push(nullptr); - - vector level; - while(q.size()){ - auto t = q.front(); - q.pop(); - - if(t == nullptr){ - res.push_back(level); - level.clear(); - if(!q.empty()){ - q.push(nullptr); - } - }else{ - level.push_back(t->val); - if(t->left)q.push(t->left); - if(t->right)q.push(t->right); - } - } - - return res; - } -}; -// @acwing code end +#include +/* +* @acwing app=acwing.cn id=42 lang=C++ +* +* 44. 分行从上往下打印二叉树 +*/ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + vector> printFromTopToBottom(TreeNode* root) { + vector> res; + if(!root)return res; + + queue q; + q.push(root); + q.push(nullptr); + + vector level; + while(q.size()){ + auto t = q.front(); + q.pop(); + + if(t == nullptr){ + res.push_back(level); + level.clear(); + if(!q.empty()){ + q.push(nullptr); + } + }else{ + level.push_back(t->val); + if(t->left)q.push(t->left); + if(t->right)q.push(t->right); + } + } + + return res; + } +}; +// @acwing code end diff --git "a/acwing/problems/45.\344\271\213\345\255\227\345\275\242\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" "b/acwing/problems/45.\344\271\213\345\255\227\345\275\242\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" index 5afc92cd..0b1539b9 100644 --- "a/acwing/problems/45.\344\271\213\345\255\227\345\275\242\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" +++ "b/acwing/problems/45.\344\271\213\345\255\227\345\275\242\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.cpp" @@ -1,58 +1,59 @@ -/* - * @Author: tkzzzzzz6 - * @Date: 2026-04-22 13:47:29 - * @LastEditors: tkzzzzzz6 - * @LastEditTime: 2026-04-22 13:47:47 - */ -/* -* @acwing app=acwing.cn id=43 lang=C++ -* -* 45. 之字形打印二叉树 -*/ - -// @acwing code start - -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - vector> printFromTopToBottom(TreeNode* root) { - vector> res; - if(!root)return res; - - queue q; - q.push(root); - q.push(nullptr); - - vector level; - bool reverseFlag = false; - while(q.size()){ - auto t = q.front(); - q.pop(); - - if(t == nullptr){ - if(reverseFlag)reverse(level.begin(),level.end()); - res.push_back(level); - reverseFlag = !reverseFlag; - level.clear(); - if(!q.empty()){ - q.push(nullptr); - } - }else{ - level.push_back(t->val); - if(t->left)q.push(t->left); - if(t->right)q.push(t->right); - } - } - - return res; - } -}; -// @acwing code end +#include +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-22 13:47:29 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-22 13:47:47 + */ +/* +* @acwing app=acwing.cn id=43 lang=C++ +* +* 45. 之字形打印二叉树 +*/ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + vector> printFromTopToBottom(TreeNode* root) { + vector> res; + if(!root)return res; + + queue q; + q.push(root); + q.push(nullptr); + + vector level; + bool reverseFlag = false; + while(q.size()){ + auto t = q.front(); + q.pop(); + + if(t == nullptr){ + if(reverseFlag)reverse(level.begin(),level.end()); + res.push_back(level); + reverseFlag = !reverseFlag; + level.clear(); + if(!q.empty()){ + q.push(nullptr); + } + }else{ + level.push_back(t->val); + if(t->left)q.push(t->left); + if(t->right)q.push(t->right); + } + } + + return res; + } +}; +// @acwing code end diff --git "a/acwing/problems/46.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227.cpp" "b/acwing/problems/46.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227.cpp" index 55870a98..5d609ad2 100644 --- "a/acwing/problems/46.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227.cpp" +++ "b/acwing/problems/46.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227.cpp" @@ -1,3 +1,4 @@ +#include /* * @acwing app=acwing.cn id=44 lang=C++ * diff --git "a/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" index 7d8d551f..d8c1035c 100644 --- "a/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-13 10:26:46 diff --git "a/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" "b/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" index bf3a46c3..9ee93259 100644 --- "a/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-13 10:45:59 diff --git "a/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" index 424f6b56..35f03c24 100644 --- "a/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-14 08:24:21 diff --git "a/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" "b/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" index b2ddec1a..922a518a 100644 --- "a/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/14. \344\270\215\344\277\256\346\224\271\346\225\260\347\273\204\346\211\276\345\207\272\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/2.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-14 08:24:21 diff --git "a/acwing/\345\211\221\346\214\207 offer/15.\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276.cpp/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/15.\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276.cpp/1.cpp" index 839e70c1..ebae0e41 100644 --- "a/acwing/\345\211\221\346\214\207 offer/15.\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276.cpp/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/15.\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276.cpp/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @acwing app=acwing.cn id=16 lang=C++ * diff --git "a/acwing/\345\211\221\346\214\207 offer/19. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/19. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" index 10bf50df..9e9acc30 100644 --- "a/acwing/\345\211\221\346\214\207 offer/19. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/19. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" @@ -17,14 +17,14 @@ class MyQueue { /** Initialize your data structure here. */ stack st1,st2; MyQueue() { - + } - + /** Push element x to the back of queue. */ void push(int x) { st1.push(x); } - + /** Removes the element from in front of queue and returns that element. */ int pop() { if(!st2.empty()){ @@ -45,7 +45,7 @@ class MyQueue { } } } - + /** Get the front element. */ int peek() { if(!st2.empty()){ @@ -53,7 +53,7 @@ class MyQueue { }else{ while(!st1.empty()){ st2.push(st1.top()); - st1.pop(); + st1.pop(); } if(!st2.empty()){ return st2.top(); @@ -62,7 +62,7 @@ class MyQueue { } } } - + /** Returns whether the queue is empty. */ bool empty() { return st1.empty() && st2.empty(); diff --git "a/acwing/\345\211\221\346\214\207 offer/22. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/22. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/1.cpp" index 484436e2..799c4318 100644 --- "a/acwing/\345\211\221\346\214\207 offer/22. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/22. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @acwing app=acwing.cn id=20 lang=C++ * diff --git "a/acwing/\345\211\221\346\214\207 offer/23. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/23. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/1.cpp" index ccd5cfda..b0f5f999 100644 --- "a/acwing/\345\211\221\346\214\207 offer/23. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/23. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-17 14:53:44 diff --git "a/acwing/\345\211\221\346\214\207 offer/24. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/24. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/1.cpp" index 58b88a69..5479c1a2 100644 --- "a/acwing/\345\211\221\346\214\207 offer/24. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/24. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-17 18:08:33 @@ -24,7 +25,7 @@ class Solution { return getSingleSum(p.first) + getSingleSum(p.second); } - + int movingCount(int threshold, int rows, int cols) { if(!rows || !cols)return 0; diff --git "a/acwing/\345\211\221\346\214\207 offer/25. \345\211\252\347\273\263\345\255\220/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/25. \345\211\252\347\273\263\345\255\220/1.cpp" index 44606b7c..d6872585 100644 --- "a/acwing/\345\211\221\346\214\207 offer/25. \345\211\252\347\273\263\345\255\220/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/25. \345\211\252\347\273\263\345\255\220/1.cpp" @@ -21,7 +21,7 @@ class Solution { if(length % 3 == 1)length-=4,res = 4; else if(length % 3 == 2)length-=2,res =2; - + while(length)res*=3,length-=3; return res; } diff --git "a/acwing/\345\211\221\346\214\207 offer/26. \344\272\214\350\277\233\345\210\266\344\270\255 1 \347\232\204\344\270\252\346\225\260/1.cpp" "b/acwing/\345\211\221\346\214\207 offer/26. \344\272\214\350\277\233\345\210\266\344\270\255 1 \347\232\204\344\270\252\346\225\260/1.cpp" index 323b00be..7940bac3 100644 --- "a/acwing/\345\211\221\346\214\207 offer/26. \344\272\214\350\277\233\345\210\266\344\270\255 1 \347\232\204\344\270\252\346\225\260/1.cpp" +++ "b/acwing/\345\211\221\346\214\207 offer/26. \344\272\214\350\277\233\345\210\266\344\270\255 1 \347\232\204\344\270\252\346\225\260/1.cpp" @@ -17,9 +17,9 @@ class Solution { int NumberOf1(int n) { unsigned int us_n = n; int s = 0; - + while(us_n)s += us_n & 1,us_n >>= 1; - + return s; } }; diff --git "a/acwing/\345\211\221\346\214\207 offer/30. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/1.py" "b/acwing/\345\211\221\346\214\207 offer/30. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/1.py" index 77486e74..8582e4f5 100644 --- "a/acwing/\345\211\221\346\214\207 offer/30. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/1.py" +++ "b/acwing/\345\211\221\346\214\207 offer/30. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/1.py" @@ -20,4 +20,4 @@ def dfs(i:int,j:int)->bool: # s[i] 和 p[j]相匹配 return is_match and dfs(i+1,j+1) - return dfs(0,0) + return dfs(0,0) diff --git a/atcoder/AtCoder Beginner Contest 413/A/1.cpp b/atcoder/AtCoder Beginner Contest 413/A/1.cpp index 95446947..e161ffe4 100644 --- a/atcoder/AtCoder Beginner Contest 413/A/1.cpp +++ b/atcoder/AtCoder Beginner Contest 413/A/1.cpp @@ -1,17 +1,17 @@ -#include - -using namespace std; - - -int main(){ - int n, m; - cin >> n >> m; - int sum = 0,a = 0; - for (int i = 0; i < n;++i){ - cin >> a; - sum += a; - } - if(sum <= m)cout << "Yes" << endl; - else cout << "No" << endl; - return 0; -} \ No newline at end of file +#include + +using namespace std; + + +int main(){ + int n, m; + cin >> n >> m; + int sum = 0,a = 0; + for (int i = 0; i < n;++i){ + cin >> a; + sum += a; + } + if(sum <= m)cout << "Yes" << endl; + else cout << "No" << endl; + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 413/B - cat 2/1.cpp b/atcoder/AtCoder Beginner Contest 413/B - cat 2/1.cpp index 259abe96..0ef682a3 100644 --- a/atcoder/AtCoder Beginner Contest 413/B - cat 2/1.cpp +++ b/atcoder/AtCoder Beginner Contest 413/B - cat 2/1.cpp @@ -1,40 +1,40 @@ -#include -#include -#include - -using namespace std; - -int main() -{ - int n; - cin >> n; - - // 读取N个字符串 - string s[101]; - for (int i = 1; i <= n; i++) - { - cin >> s[i]; - } - - // 使用set来存储所有不同的拼接结果 - set result; - - // 遍历所有可能的(i,j)组合,其中i≠j - for (int i = 1; i <= n; i++) - { - for (int j = 1; j <= n; j++) - { - if (i != j) - { // 确保选择不同的字符串 - // 拼接S[i]和S[j] - string concatenated = s[i] + s[j]; - result.insert(concatenated); - } - } - } - - // 输出不同字符串的数量 - cout << result.size() << endl; - - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + // 读取N个字符串 + string s[101]; + for (int i = 1; i <= n; i++) + { + cin >> s[i]; + } + + // 使用set来存储所有不同的拼接结果 + set result; + + // 遍历所有可能的(i,j)组合,其中i≠j + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (i != j) + { // 确保选择不同的字符串 + // 拼接S[i]和S[j] + string concatenated = s[i] + s[j]; + result.insert(concatenated); + } + } + } + + // 输出不同字符串的数量 + cout << result.size() << endl; + + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 413/B - cat 2/algorithm_parsing_examples.cpp b/atcoder/AtCoder Beginner Contest 413/B - cat 2/algorithm_parsing_examples.cpp index 6c9ed309..fe1accf6 100644 --- a/atcoder/AtCoder Beginner Contest 413/B - cat 2/algorithm_parsing_examples.cpp +++ b/atcoder/AtCoder Beginner Contest 413/B - cat 2/algorithm_parsing_examples.cpp @@ -1,133 +1,133 @@ -#include -#include -#include -#include -#include - -using namespace std; - -int main() -{ - cout << "=== 算法题中常见的行解析模式 ===" << endl; - - // 模式1: 读取一行包含多个整数的数据 - cout << "\n模式1: 读取一行多个整数" << endl; - cout << "示例输入: 1 2 3 4 5" << endl; - cout << "请输入一行整数: "; - - string line; - getline(cin, line); - stringstream ss(line); - vector nums; - int x; - while (ss >> x) - { - nums.push_back(x); - } - - cout << "解析结果: "; - for (int num : nums) - { - cout << num << " "; - } - cout << endl; - - // 模式2: 读取一行字符串数组 - cout << "\n模式2: 读取一行字符串数组" << endl; - cout << "示例输入: apple banana orange" << endl; - cout << "请输入一行字符串: "; - - string str_line; - getline(cin, str_line); - stringstream str_ss(str_line); - vector strings; - string word; - while (str_ss >> word) - { - strings.push_back(word); - } - - cout << "解析结果: "; - for (const string &s : strings) - { - cout << "[" << s << "] "; - } - cout << endl; - - // 模式3: 读取特定格式的数据 - cout << "\n模式3: 读取特定格式 (例如: N=5, M=10)" << endl; - cout << "请输入格式: N=数字, M=数字: "; - - string format_line; - getline(cin, format_line); - - int N = 0, M = 0; - size_t n_pos = format_line.find("N="); - size_t m_pos = format_line.find("M="); - - if (n_pos != string::npos) - { - size_t comma_pos = format_line.find(',', n_pos); - if (comma_pos != string::npos) - { - N = stoi(format_line.substr(n_pos + 2, comma_pos - n_pos - 2)); - } - } - - if (m_pos != string::npos) - { - M = stoi(format_line.substr(m_pos + 2)); - } - - cout << "解析结果: N=" << N << ", M=" << M << endl; - - // 模式4: 读取键值对数据 - cout << "\n模式4: 读取键值对 (例如: name=John, age=25, city=NYC)" << endl; - cout << "请输入键值对: "; - - string kv_line; - getline(cin, kv_line); - - map kv_pairs; - stringstream kv_ss(kv_line); - string pair; - - while (getline(kv_ss, pair, ',')) - { - size_t equal_pos = pair.find('='); - if (equal_pos != string::npos) - { - string key = pair.substr(0, equal_pos); - string value = pair.substr(equal_pos + 1); - // 去除前后空格 - key.erase(0, key.find_first_not_of(" \t")); - key.erase(key.find_last_not_of(" \t") + 1); - value.erase(0, value.find_first_not_of(" \t")); - value.erase(value.find_last_not_of(" \t") + 1); - kv_pairs[key] = value; - } - } - - cout << "解析结果:" << endl; - for (const auto &kv : kv_pairs) - { - cout << " " << kv.first << " = " << kv.second << endl; - } - - // 模式5: 读取混合数据类型 - cout << "\n模式5: 读取混合数据类型 (例如: 字符串 数字 字符串)" << endl; - cout << "请输入混合数据: "; - - string mixed_line; - getline(cin, mixed_line); - stringstream mixed_ss(mixed_line); - - string str1, str2; - int number; - - mixed_ss >> str1 >> number >> str2; - - cout << "解析结果: 字符串1=" << str1 << ", 数字=" << number << ", 字符串2=" << str2 << endl; - - return 0; -} \ No newline at end of file +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + cout << "=== 算法题中常见的行解析模式 ===" << endl; + + // 模式1: 读取一行包含多个整数的数据 + cout << "\n模式1: 读取一行多个整数" << endl; + cout << "示例输入: 1 2 3 4 5" << endl; + cout << "请输入一行整数: "; + + string line; + getline(cin, line); + stringstream ss(line); + vector nums; + int x; + while (ss >> x) + { + nums.push_back(x); + } + + cout << "解析结果: "; + for (int num : nums) + { + cout << num << " "; + } + cout << endl; + + // 模式2: 读取一行字符串数组 + cout << "\n模式2: 读取一行字符串数组" << endl; + cout << "示例输入: apple banana orange" << endl; + cout << "请输入一行字符串: "; + + string str_line; + getline(cin, str_line); + stringstream str_ss(str_line); + vector strings; + string word; + while (str_ss >> word) + { + strings.push_back(word); + } + + cout << "解析结果: "; + for (const string &s : strings) + { + cout << "[" << s << "] "; + } + cout << endl; + + // 模式3: 读取特定格式的数据 + cout << "\n模式3: 读取特定格式 (例如: N=5, M=10)" << endl; + cout << "请输入格式: N=数字, M=数字: "; + + string format_line; + getline(cin, format_line); + + int N = 0, M = 0; + size_t n_pos = format_line.find("N="); + size_t m_pos = format_line.find("M="); + + if (n_pos != string::npos) + { + size_t comma_pos = format_line.find(',', n_pos); + if (comma_pos != string::npos) + { + N = stoi(format_line.substr(n_pos + 2, comma_pos - n_pos - 2)); + } + } + + if (m_pos != string::npos) + { + M = stoi(format_line.substr(m_pos + 2)); + } + + cout << "解析结果: N=" << N << ", M=" << M << endl; + + // 模式4: 读取键值对数据 + cout << "\n模式4: 读取键值对 (例如: name=John, age=25, city=NYC)" << endl; + cout << "请输入键值对: "; + + string kv_line; + getline(cin, kv_line); + + map kv_pairs; + stringstream kv_ss(kv_line); + string pair; + + while (getline(kv_ss, pair, ',')) + { + size_t equal_pos = pair.find('='); + if (equal_pos != string::npos) + { + string key = pair.substr(0, equal_pos); + string value = pair.substr(equal_pos + 1); + // 去除前后空格 + key.erase(0, key.find_first_not_of(" \t")); + key.erase(key.find_last_not_of(" \t") + 1); + value.erase(0, value.find_first_not_of(" \t")); + value.erase(value.find_last_not_of(" \t") + 1); + kv_pairs[key] = value; + } + } + + cout << "解析结果:" << endl; + for (const auto &kv : kv_pairs) + { + cout << " " << kv.first << " = " << kv.second << endl; + } + + // 模式5: 读取混合数据类型 + cout << "\n模式5: 读取混合数据类型 (例如: 字符串 数字 字符串)" << endl; + cout << "请输入混合数据: "; + + string mixed_line; + getline(cin, mixed_line); + stringstream mixed_ss(mixed_line); + + string str1, str2; + int number; + + mixed_ss >> str1 >> number >> str2; + + cout << "解析结果: 字符串1=" << str1 << ", 数字=" << number << ", 字符串2=" << str2 << endl; + + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 413/B - cat 2/line_parsing_examples.cpp b/atcoder/AtCoder Beginner Contest 413/B - cat 2/line_parsing_examples.cpp index 8d84f662..3e8b211f 100644 --- a/atcoder/AtCoder Beginner Contest 413/B - cat 2/line_parsing_examples.cpp +++ b/atcoder/AtCoder Beginner Contest 413/B - cat 2/line_parsing_examples.cpp @@ -1,90 +1,90 @@ -#include -#include -#include -#include -#include - -using namespace std; - -int main() -{ - cout << "=== C++ 行读取和解析示例 ===" << endl; - - // 方法1: 使用 getline() 读取整行 - cout << "\n方法1: 使用 getline() 读取整行" << endl; - string line; - cout << "请输入一行文本: "; - getline(cin, line); - cout << "读取到的行: " << line << endl; - - // 方法2: 使用 stringstream 解析空格分隔的数据 - cout << "\n方法2: 解析空格分隔的整数" << endl; - cout << "请输入一行整数(空格分隔): "; - string numbers_line; - getline(cin, numbers_line); - - stringstream ss(numbers_line); - vector numbers; - int num; - while (ss >> num) - { - numbers.push_back(num); - } - - cout << "解析出的整数: "; - for (int n : numbers) - { - cout << n << " "; - } - cout << endl; - - // 方法3: 使用 stringstream 解析逗号分隔的数据 - cout << "\n方法3: 解析逗号分隔的字符串" << endl; - cout << "请输入一行字符串(逗号分隔): "; - string csv_line; - getline(cin, csv_line); - - vector csv_data; - stringstream csv_ss(csv_line); - string item; - while (getline(csv_ss, item, ',')) - { - csv_data.push_back(item); - } - - cout << "解析出的字符串: "; - for (const string &s : csv_data) - { - cout << "[" << s << "] "; - } - cout << endl; - - // 方法4: 手动解析特定格式 - cout << "\n方法4: 手动解析特定格式" << endl; - cout << "请输入格式: 姓名:年龄:城市 (例如: 张三:25:北京): "; - string format_line; - getline(cin, format_line); - - size_t pos1 = format_line.find(':'); - size_t pos2 = format_line.find(':', pos1 + 1); - - if (pos1 != string::npos && pos2 != string::npos) - { - string name = format_line.substr(0, pos1); - string age_str = format_line.substr(pos1 + 1, pos2 - pos1 - 1); - string city = format_line.substr(pos2 + 1); - - cout << "姓名: " << name << endl; - cout << "年龄: " << age_str << endl; - cout << "城市: " << city << endl; - } - - // 方法5: 使用 cin.getline() 读取固定长度 - cout << "\n方法5: 读取固定长度的行" << endl; - char buffer[100]; - cout << "请输入一行文本(最多99字符): "; - cin.getline(buffer, 100); - cout << "读取到的内容: " << buffer << endl; - - return 0; -} \ No newline at end of file +#include +#include +#include +#include +#include + +using namespace std; + +int main() +{ + cout << "=== C++ 行读取和解析示例 ===" << endl; + + // 方法1: 使用 getline() 读取整行 + cout << "\n方法1: 使用 getline() 读取整行" << endl; + string line; + cout << "请输入一行文本: "; + getline(cin, line); + cout << "读取到的行: " << line << endl; + + // 方法2: 使用 stringstream 解析空格分隔的数据 + cout << "\n方法2: 解析空格分隔的整数" << endl; + cout << "请输入一行整数(空格分隔): "; + string numbers_line; + getline(cin, numbers_line); + + stringstream ss(numbers_line); + vector numbers; + int num; + while (ss >> num) + { + numbers.push_back(num); + } + + cout << "解析出的整数: "; + for (int n : numbers) + { + cout << n << " "; + } + cout << endl; + + // 方法3: 使用 stringstream 解析逗号分隔的数据 + cout << "\n方法3: 解析逗号分隔的字符串" << endl; + cout << "请输入一行字符串(逗号分隔): "; + string csv_line; + getline(cin, csv_line); + + vector csv_data; + stringstream csv_ss(csv_line); + string item; + while (getline(csv_ss, item, ',')) + { + csv_data.push_back(item); + } + + cout << "解析出的字符串: "; + for (const string &s : csv_data) + { + cout << "[" << s << "] "; + } + cout << endl; + + // 方法4: 手动解析特定格式 + cout << "\n方法4: 手动解析特定格式" << endl; + cout << "请输入格式: 姓名:年龄:城市 (例如: 张三:25:北京): "; + string format_line; + getline(cin, format_line); + + size_t pos1 = format_line.find(':'); + size_t pos2 = format_line.find(':', pos1 + 1); + + if (pos1 != string::npos && pos2 != string::npos) + { + string name = format_line.substr(0, pos1); + string age_str = format_line.substr(pos1 + 1, pos2 - pos1 - 1); + string city = format_line.substr(pos2 + 1); + + cout << "姓名: " << name << endl; + cout << "年龄: " << age_str << endl; + cout << "城市: " << city << endl; + } + + // 方法5: 使用 cin.getline() 读取固定长度 + cout << "\n方法5: 读取固定长度的行" << endl; + char buffer[100]; + cout << "请输入一行文本(最多99字符): "; + cin.getline(buffer, 100); + cout << "读取到的内容: " << buffer << endl; + + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 413/C - Large Queue/1.cpp b/atcoder/AtCoder Beginner Contest 413/C - Large Queue/1.cpp index 8f8b05f0..040c0945 100644 --- a/atcoder/AtCoder Beginner Contest 413/C - Large Queue/1.cpp +++ b/atcoder/AtCoder Beginner Contest 413/C - Large Queue/1.cpp @@ -1,70 +1,70 @@ -#include -#include -#include - -using namespace std; - -struct Element -{ - long long value; // 元素值 - long long count; // 连续数量 - - Element(long long v, long long c) : value(v), count(c) {} -}; - -int main() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int Q; - cin >> Q; - - queue q; - - for (int i = 0; i < Q; i++) - { - int type; - cin >> type; - - if (type == 1) - { - // Type 1: 添加c个值为x的元素 - long long c, x; - cin >> c >> x; - q.push(Element(x, c)); - } - else - { - // Type 2: 移除前k个元素并输出和 - long long k; - cin >> k; - - long long sum = 0; - - while (k > 0 && !q.empty()) - { - Element &front = q.front(); - - if (front.count <= k) - { - // 移除整个块 - sum += front.value * front.count; - k -= front.count; - q.pop(); - } - else - { - // 只移除部分块 - sum += front.value * k; - front.count -= k; - k = 0; - } - } - - cout << sum << endl; - } - } - - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +struct Element +{ + long long value; // 元素值 + long long count; // 连续数量 + + Element(long long v, long long c) : value(v), count(c) {} +}; + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int Q; + cin >> Q; + + queue q; + + for (int i = 0; i < Q; i++) + { + int type; + cin >> type; + + if (type == 1) + { + // Type 1: 添加c个值为x的元素 + long long c, x; + cin >> c >> x; + q.push(Element(x, c)); + } + else + { + // Type 2: 移除前k个元素并输出和 + long long k; + cin >> k; + + long long sum = 0; + + while (k > 0 && !q.empty()) + { + Element &front = q.front(); + + if (front.count <= k) + { + // 移除整个块 + sum += front.value * front.count; + k -= front.count; + q.pop(); + } + else + { + // 只移除部分块 + sum += front.value * k; + front.count -= k; + k = 0; + } + } + + cout << sum << endl; + } + } + + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 413/D - Make Geometric Sequence/1.cpp b/atcoder/AtCoder Beginner Contest 413/D - Make Geometric Sequence/1.cpp index 895699a8..658102f5 100644 --- a/atcoder/AtCoder Beginner Contest 413/D - Make Geometric Sequence/1.cpp +++ b/atcoder/AtCoder Beginner Contest 413/D - Make Geometric Sequence/1.cpp @@ -1,122 +1,122 @@ -#include -#include -#include -#include -#include -#include - -using namespace std; - -// 检查两个浮点数是否相等(考虑精度误差) -bool isEqual(double a, double b, double eps = 1e-9) -{ - return abs(a - b) < eps; -} - -// 检查序列是否能重排成等比数列 -bool canFormGeometricSequence(const vector &A) -{ - int n = A.size(); - - // 统计每个元素出现的次数 - map count; - for (int x : A) - { - count[x]++; - } - - // 如果序列长度为1,任何序列都是等比数列 - if (n == 1) - return true; - - // 计算所有可能的公比值 - set ratios; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - if (i != j && A[j] != 0) - { - double ratio = (double)A[i] / A[j]; - ratios.insert(ratio); - } - } - } - - // 对每个公比值进行验证 - for (double r : ratios) - { - // 尝试以每个元素作为第一个元素 - for (int first : A) - { - vector sequence; - sequence.push_back(first); - - // 根据公比计算后续元素 - bool valid = true; - map temp_count = count; - temp_count[first]--; - if (temp_count[first] == 0) - temp_count.erase(first); - - for (int i = 1; i < n; i++) - { - double next_val = first * pow(r, i); - - // 找到最接近的整数值 - int next_int = round(next_val); - - // 检查这个整数值是否在剩余元素中 - if (temp_count.find(next_int) == temp_count.end() || temp_count[next_int] == 0) - { - valid = false; - break; - } - - temp_count[next_int]--; - if (temp_count[next_int] == 0) - temp_count.erase(next_int); - sequence.push_back(next_int); - } - - if (valid) - { - return true; - } - } - } - - return false; -} - -int main() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); - - int T; - cin >> T; - - while (T--) - { - int N; - cin >> N; - - vector A(N); - for (int i = 0; i < N; i++) - { - cin >> A[i]; - } - - if (canFormGeometricSequence(A)) - { - cout << "Yes" << endl; - } - else - { - cout << "No" << endl; - } - } - - return 0; -} +#include +#include +#include +#include +#include +#include + +using namespace std; + +// 检查两个浮点数是否相等(考虑精度误差) +bool isEqual(double a, double b, double eps = 1e-9) +{ + return abs(a - b) < eps; +} + +// 检查序列是否能重排成等比数列 +bool canFormGeometricSequence(const vector &A) +{ + int n = A.size(); + + // 统计每个元素出现的次数 + map count; + for (int x : A) + { + count[x]++; + } + + // 如果序列长度为1,任何序列都是等比数列 + if (n == 1) + return true; + + // 计算所有可能的公比值 + set ratios; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i != j && A[j] != 0) + { + double ratio = (double)A[i] / A[j]; + ratios.insert(ratio); + } + } + } + + // 对每个公比值进行验证 + for (double r : ratios) + { + // 尝试以每个元素作为第一个元素 + for (int first : A) + { + vector sequence; + sequence.push_back(first); + + // 根据公比计算后续元素 + bool valid = true; + map temp_count = count; + temp_count[first]--; + if (temp_count[first] == 0) + temp_count.erase(first); + + for (int i = 1; i < n; i++) + { + double next_val = first * pow(r, i); + + // 找到最接近的整数值 + int next_int = round(next_val); + + // 检查这个整数值是否在剩余元素中 + if (temp_count.find(next_int) == temp_count.end() || temp_count[next_int] == 0) + { + valid = false; + break; + } + + temp_count[next_int]--; + if (temp_count[next_int] == 0) + temp_count.erase(next_int); + sequence.push_back(next_int); + } + + if (valid) + { + return true; + } + } + } + + return false; +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int T; + cin >> T; + + while (T--) + { + int N; + cin >> N; + + vector A(N); + for (int i = 0; i < N; i++) + { + cin >> A[i]; + } + + if (canFormGeometricSequence(A)) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/1.cpp b/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/1.cpp index bd558f8b..a89ddfda 100644 --- a/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/1.cpp +++ b/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/1.cpp @@ -1,11 +1,11 @@ -#include -using namespace std; -int main() { - ios::sync_with_stdio(false), cin.tie(0); - int n, m; - cin >> n >> m; - for (int i = 1; i <= n; ++i) { - cout << (i <= m ? "OK" : "Too Many Requests") << "\n"; - } - return 0; -} \ No newline at end of file +#include +using namespace std; +int main() { + ios::sync_with_stdio(false), cin.tie(0); + int n, m; + cin >> n >> m; + for (int i = 1; i <= n; ++i) { + cout << (i <= m ? "OK" : "Too Many Requests") << "\n"; + } + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/2.cpp b/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/2.cpp index c86c0df2..6961bb64 100644 --- a/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/2.cpp +++ b/atcoder/AtCoder Beginner Contest 429/A.Too Many Requests/2.cpp @@ -1,23 +1,23 @@ - -#include -using namespace std; - -typedef long long ll; - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - - int n, m; - cin >> n >> m; - for (int i = 0; i < n; ++i) { - if (i < m) { - cout << "OK\n"; - } - else { - cout << "Too Many Requests\n"; - } - } - - return 0; -} - + +#include +using namespace std; + +typedef long long ll; + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + + int n, m; + cin >> n >> m; + for (int i = 0; i < n; ++i) { + if (i < m) { + cout << "OK\n"; + } + else { + cout << "Too Many Requests\n"; + } + } + + return 0; +} + diff --git a/atcoder/AtCoder Beginner Contest 429/B - N - 1/1.cpp b/atcoder/AtCoder Beginner Contest 429/B - N - 1/1.cpp index a70edabe..18fe3019 100644 --- a/atcoder/AtCoder Beginner Contest 429/B - N - 1/1.cpp +++ b/atcoder/AtCoder Beginner Contest 429/B - N - 1/1.cpp @@ -1,26 +1,26 @@ - -#include -using namespace std; - -typedef long long ll; - -unordered_set us; - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - - int n, m; - cin >> n >> m; - ll sum = 0; - for (int i = 0; i < n; ++i) { - int x; - cin >> x; - sum += x; - us.insert(x); - } - - if (us.find(sum - m) != us.end())cout << "Yes\n"; - else cout << "No\n"; - - return 0; -} \ No newline at end of file + +#include +using namespace std; + +typedef long long ll; + +unordered_set us; + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + + int n, m; + cin >> n >> m; + ll sum = 0; + for (int i = 0; i < n; ++i) { + int x; + cin >> x; + sum += x; + us.insert(x); + } + + if (us.find(sum - m) != us.end())cout << "Yes\n"; + else cout << "No\n"; + + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 429/C - Odd One Subsequence/2.cpp b/atcoder/AtCoder Beginner Contest 429/C - Odd One Subsequence/2.cpp index 249773ac..09670b71 100644 --- a/atcoder/AtCoder Beginner Contest 429/C - Odd One Subsequence/2.cpp +++ b/atcoder/AtCoder Beginner Contest 429/C - Odd One Subsequence/2.cpp @@ -1,24 +1,24 @@ -#include -using namespace std; - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - - int n; - cin >> n; - - unordered_map cnt; - for (int i = 0; i < n; ++i) { - int a; - cin >> a; - cnt[a]++; - } - - long long ans = 0; - for (auto& [val, c] : cnt) { - ans += c * (c - 1) / 2 * (n - c); - } - - cout << ans << '\n'; - return 0; -} \ No newline at end of file +#include +using namespace std; + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + + int n; + cin >> n; + + unordered_map cnt; + for (int i = 0; i < n; ++i) { + int a; + cin >> a; + cnt[a]++; + } + + long long ans = 0; + for (auto& [val, c] : cnt) { + ans += c * (c - 1) / 2 * (n - c); + } + + cout << ans << '\n'; + return 0; +} diff --git a/atcoder/AtCoder Beginner Contest 433/A.py b/atcoder/AtCoder Beginner Contest 433/A.py index 80d6b2fe..cb3024d0 100644 --- a/atcoder/AtCoder Beginner Contest 433/A.py +++ b/atcoder/AtCoder Beginner Contest 433/A.py @@ -25,4 +25,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git a/atcoder/AtCoder Beginner Contest 433/B.py b/atcoder/AtCoder Beginner Contest 433/B.py index 0f8c98a9..9db05f6f 100644 --- a/atcoder/AtCoder Beginner Contest 433/B.py +++ b/atcoder/AtCoder Beginner Contest 433/B.py @@ -22,4 +22,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git a/atcoder/AtCoder Beginner Contest 433/C.py b/atcoder/AtCoder Beginner Contest 433/C.py index 2423e3fa..b90421bf 100644 --- a/atcoder/AtCoder Beginner Contest 433/C.py +++ b/atcoder/AtCoder Beginner Contest 433/C.py @@ -28,7 +28,7 @@ def solve(): d1, L = runs[i] # 第i段的字符和长度 d2, R = runs[i + 1] # 第i+1段的字符和长度 # 如果后一个字符的ASCII码正好比前一个大1(如'a'和'b') - if ord(d2) == ord(d1) + 1: + if ord(d2) == ord(d1) + 1: m = min(L, R) # 能成对的最大数量是两段长度较小值 # 对于所有长度为k(1到m)的配对计算组合数 # Σ_{k=1..m} (L - k + 1) diff --git a/atcoder/AtCoder Beginner Contest 444/B.py b/atcoder/AtCoder Beginner Contest 444/B.py index 7789bea4..5fb86920 100644 --- a/atcoder/AtCoder Beginner Contest 444/B.py +++ b/atcoder/AtCoder Beginner Contest 444/B.py @@ -23,7 +23,7 @@ def solve(): while current_n > 0: digit_sum += current_n % 10 current_n //= 10 - + if digit_sum == K: ans += 1 diff --git a/atcoder/AtCoder Beginner Contest 444/C.py b/atcoder/AtCoder Beginner Contest 444/C.py index 41fba57a..358ef667 100644 --- a/atcoder/AtCoder Beginner Contest 444/C.py +++ b/atcoder/AtCoder Beginner Contest 444/C.py @@ -28,10 +28,10 @@ def solve(): for L in sorted_candidates: if tot % L != 0: continue - + current_counts = cnts.copy() possible = True - + for u in unique_elements: c = current_counts[u] if c == 0: @@ -58,7 +58,7 @@ def solve(): break current_counts[u] = 0 current_counts[target] -= c - + if possible: vaild_L.append(L) diff --git a/atcoder/abc394-2/abc394_b/abc394_b.cpp b/atcoder/abc394-2/abc394_b/abc394_b.cpp index 40b7eb81..d1e3ca5e 100644 --- a/atcoder/abc394-2/abc394_b/abc394_b.cpp +++ b/atcoder/abc394-2/abc394_b/abc394_b.cpp @@ -1,3 +1,4 @@ +#include // 包含C++标准库的头文件 #include diff --git a/atcoder/abc394-2/abc394_d/abc394_d.cpp b/atcoder/abc394-2/abc394_d/abc394_d.cpp index 1efc9417..33954cf1 100644 --- a/atcoder/abc394-2/abc394_d/abc394_d.cpp +++ b/atcoder/abc394-2/abc394_d/abc394_d.cpp @@ -65,4 +65,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git a/atcoder/abc394/A.cpp b/atcoder/abc394/A.cpp index e4c372a4..4c8d29e3 100644 --- a/atcoder/abc394/A.cpp +++ b/atcoder/abc394/A.cpp @@ -1,15 +1,15 @@ -#include - -using namespace std; - -int main(){ - string s; - cin >> s; - int n = s.size(); - for(int i = 0; i < n; i++){ - if(s[i] == '2'){ - cout << 2 ; - } - } - return 0; -} +#include + +using namespace std; + +int main(){ + string s; + cin >> s; + int n = s.size(); + for(int i = 0; i < n; i++){ + if(s[i] == '2'){ + cout << 2 ; + } + } + return 0; +} diff --git a/atcoder/abc394/B.cpp b/atcoder/abc394/B.cpp index badccd30..2b303960 100644 --- a/atcoder/abc394/B.cpp +++ b/atcoder/abc394/B.cpp @@ -1,35 +1,36 @@ -// 包含C++标准库的头文件 -#include - -// 使用标准命名空间 -using namespace std; - -int main(){ - // 声明变量n用于存储输入的字符串个数 - int n; - cin >> n; - // 创建一个大小为n的字符串向量s用于存储输入的字符串 - vector s(n); - // 循环读入n个字符串 - for(int i = 0; i < n; i++){ - cin >> s[i]; - } - // 创建一个pair向量a,用于存储字符串及其长度 - vector> a; - // 遍历字符串向量s,将每个字符串及其长度作为pair存入向量a - for(int i = 0; i < n; i++){ - a.push_back(make_pair(s[i],s[i].size())); - } - // 使用lambda表达式对向量a按照字符串长度进行升序排序 - // sort(a.begin(), a.end(), [](const pair& x, const pair& y){ - // return x.second < y.second; - // }); - // const 和 & 是为了提高效率,可以暂时不用太关注 - sort(a.begin(), a.end(), [](pair x, pair y) - { return x.second < y.second; }); - // 按顺序输出排序后的字符串 - for(int i = 0; i < n; i++){ - cout << a[i].first; - } - return 0; -} +#include +// 包含C++标准库的头文件 +#include + +// 使用标准命名空间 +using namespace std; + +int main(){ + // 声明变量n用于存储输入的字符串个数 + int n; + cin >> n; + // 创建一个大小为n的字符串向量s用于存储输入的字符串 + vector s(n); + // 循环读入n个字符串 + for(int i = 0; i < n; i++){ + cin >> s[i]; + } + // 创建一个pair向量a,用于存储字符串及其长度 + vector> a; + // 遍历字符串向量s,将每个字符串及其长度作为pair存入向量a + for(int i = 0; i < n; i++){ + a.push_back(make_pair(s[i],s[i].size())); + } + // 使用lambda表达式对向量a按照字符串长度进行升序排序 + // sort(a.begin(), a.end(), [](const pair& x, const pair& y){ + // return x.second < y.second; + // }); + // const 和 & 是为了提高效率,可以暂时不用太关注 + sort(a.begin(), a.end(), [](pair x, pair y) + { return x.second < y.second; }); + // 按顺序输出排序后的字符串 + for(int i = 0; i < n; i++){ + cout << a[i].first; + } + return 0; +} diff --git a/atcoder/abc394/C.cpp b/atcoder/abc394/C.cpp index 1408dfa8..f984fa97 100644 --- a/atcoder/abc394/C.cpp +++ b/atcoder/abc394/C.cpp @@ -1,37 +1,38 @@ -#include - -using namespace std; - -int main() -{ - string s; - cin >> s; - - vector chars(s.begin(), s.end()); - bool changed = true; - - while (changed) - { - changed = false; - vector next = chars; - - for (int i = 0; i < chars.size() - 1; i++) - { - if (next[i] == 'W' && next[i + 1] == 'A') - { - next[i] = 'A'; - next[i + 1] = 'C'; - changed = true; - i++; // 跳过下一个字符,因为已经处理过了 - } - } - chars = next; - } - - for (char c : chars) - { - cout << c; - } - cout << endl; - return 0; -} +#include +#include + +using namespace std; + +int main() +{ + string s; + cin >> s; + + vector chars(s.begin(), s.end()); + bool changed = true; + + while (changed) + { + changed = false; + vector next = chars; + + for (int i = 0; i < chars.size() - 1; i++) + { + if (next[i] == 'W' && next[i + 1] == 'A') + { + next[i] = 'A'; + next[i + 1] = 'C'; + changed = true; + i++; // 跳过下一个字符,因为已经处理过了 + } + } + chars = next; + } + + for (char c : chars) + { + cout << c; + } + cout << endl; + return 0; +} diff --git a/atcoder/abc394/D.cpp b/atcoder/abc394/D.cpp index 5aae6e66..33954cf1 100644 --- a/atcoder/abc394/D.cpp +++ b/atcoder/abc394/D.cpp @@ -1,68 +1,68 @@ -#include - -using namespace std; - -// 将函数定义移到 main 函数外部 -int bracket_match(string &s1, string &s2) -{ - if (s1 == "(" && s2 == ")") - { - return 1; - } - else if (s1 == "[" && s2 == "]") - { - return 1; - } - else if (s1 == "{" && s2 == "}") - { - return 1; - } - return 0; -} - -int main() -{ - string bracket; - cin >> bracket; - - stack bracket_stack; - - for (int i = 0; i < bracket.size(); i++) - { - string current(1, bracket[i]); // 将 char 转换为 string - - if (current == "(" || current == "[" || current == "{") - { - bracket_stack.push(current); - } - else if (current == ")" || current == "]" || current == "}") - { - if (bracket_stack.empty()) - { - cout << "No" << endl; - return 0; - } - if (bracket_match(bracket_stack.top(), current)) - { - bracket_stack.pop(); - } - else - { - cout << "No" << endl; - return 0; - } - } - } - - // 最后检查栈是否为空 - if (!bracket_stack.empty()) - { - cout << "No" << endl; - } - else - { - cout << "Yes" << endl; - } - - return 0; -} \ No newline at end of file +#include + +using namespace std; + +// 将函数定义移到 main 函数外部 +int bracket_match(string &s1, string &s2) +{ + if (s1 == "(" && s2 == ")") + { + return 1; + } + else if (s1 == "[" && s2 == "]") + { + return 1; + } + else if (s1 == "{" && s2 == "}") + { + return 1; + } + return 0; +} + +int main() +{ + string bracket; + cin >> bracket; + + stack bracket_stack; + + for (int i = 0; i < bracket.size(); i++) + { + string current(1, bracket[i]); // 将 char 转换为 string + + if (current == "(" || current == "[" || current == "{") + { + bracket_stack.push(current); + } + else if (current == ")" || current == "]" || current == "}") + { + if (bracket_stack.empty()) + { + cout << "No" << endl; + return 0; + } + if (bracket_match(bracket_stack.top(), current)) + { + bracket_stack.pop(); + } + else + { + cout << "No" << endl; + return 0; + } + } + } + + // 最后检查栈是否为空 + if (!bracket_stack.empty()) + { + cout << "No" << endl; + } + else + { + cout << "Yes" << endl; + } + + return 0; +} diff --git a/atcoder/abc394/E.cpp b/atcoder/abc394/E.cpp index d1e1d120..5dc46956 100644 --- a/atcoder/abc394/E.cpp +++ b/atcoder/abc394/E.cpp @@ -1,73 +1,74 @@ -#include -// 定义一个简化的循环宏 -#define rep(i, n) for (int i = 0; i < (n); i++) -using namespace std; - -// 定义一个表示无穷大的常量 -int inf = 1000000010; - -int main() -{ - // 读入矩阵大小n - int n; - cin >> n; - // 创建n*n的字符矩阵c用于存储输入 - vector> c(n, vector(n)); - rep(i, n) rep(j, n) cin >> c[i][j]; - - // 创建n*n的距离矩阵a,初始化为无穷大 - vector> a(n, vector(n, inf)); - // 创建队列用于BFS搜索 - queue> que; - - // 对角线元素距离初始化为0 - rep(i, n) - { - que.push({i, i}); - a[i][i] = 0; - } - - // 直接相连的元素距离初始化为1 - rep(i, n) rep(j, n) - { - if (i == j or c[i][j] == '-') - continue; - que.push({i, j}); - a[i][j] = 1; - } - - // BFS搜索计算所有点对之间的最短距离 - while (!que.empty()) // 当队列不为空时继续搜索 - { - auto q = que.front(); // 获取队首元素 - que.pop(); // 弹出队首元素 - int i = q.first, j = q.second; // 获取当前处理的点对(i,j)的坐标 - - // 遍历所有可能的新点对(k,l) - rep(k, n) rep(l, n) - { - // 判断是否可以通过当前点对(i,j)构建到新点对(k,l)的路径: - // 1. c[k][i]不是'-' - 表示k到i有边 - // 2. c[j][l]不是'-' - 表示j到l有边 - // 3. c[k][i] == c[j][l] - 两条边的字符相同 - // 4. a[k][l] == inf - 点对(k,l)还未被访问过 - if (c[k][i] != '-' && c[j][l] != '-' && c[k][i] == c[j][l] && a[k][l] == inf) - { - // 新路径的距离 = 当前点对的距离 + 2 - // +2是因为需要经过两条新的边(k->i和j->l) - a[k][l] = a[i][j] + 2; - que.push({k, l}); // 将新的点对加入队列以继续搜索 - } - } - } - - // 输出结果矩阵 - rep(i, n) - { - rep(j, n) - { - // 如果距离仍为无穷大,输出-1,否则输出实际距离 - cout << (a[i][j] == inf ? -1 : a[i][j]) << " \n"[j == n - 1]; - } - } -} +#include +#include +// 定义一个简化的循环宏 +#define rep(i, n) for (int i = 0; i < (n); i++) +using namespace std; + +// 定义一个表示无穷大的常量 +int inf = 1000000010; + +int main() +{ + // 读入矩阵大小n + int n; + cin >> n; + // 创建n*n的字符矩阵c用于存储输入 + vector> c(n, vector(n)); + rep(i, n) rep(j, n) cin >> c[i][j]; + + // 创建n*n的距离矩阵a,初始化为无穷大 + vector> a(n, vector(n, inf)); + // 创建队列用于BFS搜索 + queue> que; + + // 对角线元素距离初始化为0 + rep(i, n) + { + que.push({i, i}); + a[i][i] = 0; + } + + // 直接相连的元素距离初始化为1 + rep(i, n) rep(j, n) + { + if (i == j or c[i][j] == '-') + continue; + que.push({i, j}); + a[i][j] = 1; + } + + // BFS搜索计算所有点对之间的最短距离 + while (!que.empty()) // 当队列不为空时继续搜索 + { + auto q = que.front(); // 获取队首元素 + que.pop(); // 弹出队首元素 + int i = q.first, j = q.second; // 获取当前处理的点对(i,j)的坐标 + + // 遍历所有可能的新点对(k,l) + rep(k, n) rep(l, n) + { + // 判断是否可以通过当前点对(i,j)构建到新点对(k,l)的路径: + // 1. c[k][i]不是'-' - 表示k到i有边 + // 2. c[j][l]不是'-' - 表示j到l有边 + // 3. c[k][i] == c[j][l] - 两条边的字符相同 + // 4. a[k][l] == inf - 点对(k,l)还未被访问过 + if (c[k][i] != '-' && c[j][l] != '-' && c[k][i] == c[j][l] && a[k][l] == inf) + { + // 新路径的距离 = 当前点对的距离 + 2 + // +2是因为需要经过两条新的边(k->i和j->l) + a[k][l] = a[i][j] + 2; + que.push({k, l}); // 将新的点对加入队列以继续搜索 + } + } + } + + // 输出结果矩阵 + rep(i, n) + { + rep(j, n) + { + // 如果距离仍为无穷大,输出-1,否则输出实际距离 + cout << (a[i][j] == inf ? -1 : a[i][j]) << " \n"[j == n - 1]; + } + } +} diff --git a/atcoder/abc394/F.cpp b/atcoder/abc394/F.cpp index 9e3fef0e..cfe9dcef 100644 --- a/atcoder/abc394/F.cpp +++ b/atcoder/abc394/F.cpp @@ -1,87 +1,88 @@ -#include // 包含C++标准库 - -using namespace std; // 使用标准命名空间 - -#define mp make_pair // 定义make_pair的简写 -#define pb push_back // 定义push_back的简写 -#define fi first // 定义first的简写 -#define se second // 定义second的简写 -#define li long long // 定义long long的简写 -#define pii pair // 定义pair的简写 -#define vi vector // 定义vector的简写 - -#define forn(i, n) for (int i = 0; i < (int)n; i++) // 定义从0到n-1的循环宏 -#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++) // 定义从b到e的循环宏 -#define all(x) (x).begin(), (x).end() // 定义容器的begin和end迭代器 - -int ans = 0; // 存储最终答案 -vector edges; // 存储图的邻接表 -vi f; // 存储每个节点的状态值 - -void dfs(int v, int p) // 深度优先搜索函数,v是当前节点,p是父节点 -{ - for (int u : edges[v]) // 遍历当前节点的所有邻接点 - { - if (u != p) // 如果邻接点不是父节点 - { - dfs(u, v); // 递归处理子节点 - } - } - f[v] = 1; // 初始化当前节点的状态值为1 - if (edges[v].size() >= 4) // 如果当前节点的度数大于等于4 - { - vi children; // 存储子节点的状态值 - for (int u : edges[v]) // 遍历所有邻接点 - { - if (u != p) // 如果不是父节点 - { - children.pb(f[u]); // 添加子节点的状态值 - } - else - { - children.pb(1); // 父节点贡献值为1 - } - } - sort(all(children), greater()); // 对子节点状态值降序排序 - forn(j, 3) // 选择前3大的值 - { - f[v] += children[j]; // 累加到当前节点的状态值 - } - int here = 1; // 计算包含4个子节点的路径长度 - forn(j, 4) - { - here += children[j]; - } - ans = max(ans, here); // 更新最大答案 - } - else if (p == -1) // 如果是根节点 - { - for (int u : edges[v]) // 遍历所有子节点 - { - f[v] = max(f[v], f[u] + 1); // 更新根节点状态值 - } - } - // printf("f[%d] = %d edges count = %d\n", v, f[v], (int)edges[v].size()); - ans = max(ans, f[v] + (p == -1 ? 0 : 1)); // 更新最大答案 -} - -int main() -{ - int n; // 节点数量 - cin >> n; - edges.resize(n + 1); // 初始化邻接表大小 - forn(i, n - 1) // 读入n-1条边 - { - int u, v; - cin >> u >> v; - edges[u].pb(v); // 添加无向边 - edges[v].pb(u); - } - f.resize(n + 1); // 初始化状态数组大小 - dfs(1, -1); // 从节点1开始深度优先搜索 - if (ans < 5) // 如果最大路径长度小于5 - { - ans = -1; // 输出-1 - } - cout << ans; // 输出答案 -} +#include +#include // 包含C++标准库 + +using namespace std; // 使用标准命名空间 + +#define mp make_pair // 定义make_pair的简写 +#define pb push_back // 定义push_back的简写 +#define fi first // 定义first的简写 +#define se second // 定义second的简写 +#define li long long // 定义long long的简写 +#define pii pair // 定义pair的简写 +#define vi vector // 定义vector的简写 + +#define forn(i, n) for (int i = 0; i < (int)n; i++) // 定义从0到n-1的循环宏 +#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++) // 定义从b到e的循环宏 +#define all(x) (x).begin(), (x).end() // 定义容器的begin和end迭代器 + +int ans = 0; // 存储最终答案 +vector edges; // 存储图的邻接表 +vi f; // 存储每个节点的状态值 + +void dfs(int v, int p) // 深度优先搜索函数,v是当前节点,p是父节点 +{ + for (int u : edges[v]) // 遍历当前节点的所有邻接点 + { + if (u != p) // 如果邻接点不是父节点 + { + dfs(u, v); // 递归处理子节点 + } + } + f[v] = 1; // 初始化当前节点的状态值为1 + if (edges[v].size() >= 4) // 如果当前节点的度数大于等于4 + { + vi children; // 存储子节点的状态值 + for (int u : edges[v]) // 遍历所有邻接点 + { + if (u != p) // 如果不是父节点 + { + children.pb(f[u]); // 添加子节点的状态值 + } + else + { + children.pb(1); // 父节点贡献值为1 + } + } + sort(all(children), greater()); // 对子节点状态值降序排序 + forn(j, 3) // 选择前3大的值 + { + f[v] += children[j]; // 累加到当前节点的状态值 + } + int here = 1; // 计算包含4个子节点的路径长度 + forn(j, 4) + { + here += children[j]; + } + ans = max(ans, here); // 更新最大答案 + } + else if (p == -1) // 如果是根节点 + { + for (int u : edges[v]) // 遍历所有子节点 + { + f[v] = max(f[v], f[u] + 1); // 更新根节点状态值 + } + } + // printf("f[%d] = %d edges count = %d\n", v, f[v], (int)edges[v].size()); + ans = max(ans, f[v] + (p == -1 ? 0 : 1)); // 更新最大答案 +} + +int main() +{ + int n; // 节点数量 + cin >> n; + edges.resize(n + 1); // 初始化邻接表大小 + forn(i, n - 1) // 读入n-1条边 + { + int u, v; + cin >> u >> v; + edges[u].pb(v); // 添加无向边 + edges[v].pb(u); + } + f.resize(n + 1); // 初始化状态数组大小 + dfs(1, -1); // 从节点1开始深度优先搜索 + if (ans < 5) // 如果最大路径长度小于5 + { + ans = -1; // 输出-1 + } + cout << ans; // 输出答案 +} diff --git a/atcoder/abc394/G.cpp b/atcoder/abc394/G.cpp index f9859898..30331418 100644 --- a/atcoder/abc394/G.cpp +++ b/atcoder/abc394/G.cpp @@ -1,98 +1,99 @@ -// 包含C++标准库和atcoder的并查集库 -#include -#include - -// 使用标准命名空间和atcoder命名空间 -using namespace std; -using namespace atcoder; - -// 定义常量 -#define H 500 // 最大高度 -#define W 500 // 最大宽度 -#define Q (int)2e+5 // 最大查询次数 -#define F (int)1e+6 // 最大权值 - -int main(void) -{ - int h, w, q; // h,w为网格大小,q为查询次数 - int f[H][W]; // f[i][j]存储网格中每个位置的权值 - int a[Q], b[Q], y[Q]; // 每个查询的起点坐标(a,b)和权值y - int c[Q], d[Q], z[Q]; // 每个查询的终点坐标(c,d)和权值z - int l[Q], r[Q]; // 二分查找的左右边界 - vector> e[F + 1]; // e[i]存储权值为i的边 - vector check[F + 1]; // check[i]存储需要在权值i处检查的查询 - - // 读入网格大小 - cin >> h >> w; - // 读入网格中的权值 - for (int i = 0; i < h; i++) - for (int j = 0; j < w; j++) - cin >> f[i][j]; - // 读入查询次数和查询信息 - cin >> q; - for (int i = 0; i < q; i++) - { - cin >> a[i] >> b[i] >> y[i] >> c[i] >> d[i] >> z[i]; - a[i]--, b[i]--, c[i]--, d[i]--; // 坐标从0开始 - } - - // 构建边集合 - // 添加竖直方向的边 - for (int i = 0; i < h - 1; i++) - for (int j = 0; j < w; j++) - e[min(f[i][j], f[i + 1][j])].push_back({i * w + j, (i + 1) * w + j}); - // 添加水平方向的边 - for (int i = 0; i < h; i++) - for (int j = 0; j < w - 1; j++) - e[min(f[i][j], f[i][j + 1])].push_back({i * w + j, i * w + (j + 1)}); - // 初始化二分查找的边界 - for (int i = 0; i < q; i++) - l[i] = 1, r[i] = F + 1; - - // 二分查找过程 - while (true) - { - bool flag = true; - // 清空check数组 - for (int i = 0; i <= F; i++) - check[i].clear(); - // 对每个查询,如果二分区间未收敛,则加入check数组 - for (int i = 0; i < q; i++) - { - if (r[i] - l[i] > 1) - { - check[(l[i] + r[i]) / 2].push_back(i); - flag = false; - } - } - if (flag) break; // 如果所有查询都已收敛,退出循环 - - // 使用并查集检查连通性 - dsu uf(h * w); - // 从大到小遍历权值 - for (int i = F; i >= 0; i--) - { - // 合并权值为i的所有边 - int sz = e[i].size(); - for (int j = 0; j < sz; j++) - uf.merge(e[i][j].first, e[i][j].second); - // 检查需要在权值i处判断的查询 - sz = check[i].size(); - for (int j = 0; j < sz; j++) - { - int idx_s = a[check[i][j]] * w + b[check[i][j]]; // 起点在一维数组中的索引 - int idx_t = c[check[i][j]] * w + d[check[i][j]]; // 终点在一维数组中的索引 - // 根据连通性更新二分边界 - if (uf.same(idx_s, idx_t)) - l[check[i][j]] = i; - else - r[check[i][j]] = i; - } - } - } - - // 输出每个查询的结果 - for (int i = 0; i < q; i++) - cout << (y[i] + z[i] - 2 * min(l[i], min(y[i], z[i]))) << endl; - return 0; -} +#include +// 包含C++标准库和atcoder的并查集库 +#include +#include + +// 使用标准命名空间和atcoder命名空间 +using namespace std; +using namespace atcoder; + +// 定义常量 +#define H 500 // 最大高度 +#define W 500 // 最大宽度 +#define Q (int)2e+5 // 最大查询次数 +#define F (int)1e+6 // 最大权值 + +int main(void) +{ + int h, w, q; // h,w为网格大小,q为查询次数 + int f[H][W]; // f[i][j]存储网格中每个位置的权值 + int a[Q], b[Q], y[Q]; // 每个查询的起点坐标(a,b)和权值y + int c[Q], d[Q], z[Q]; // 每个查询的终点坐标(c,d)和权值z + int l[Q], r[Q]; // 二分查找的左右边界 + vector> e[F + 1]; // e[i]存储权值为i的边 + vector check[F + 1]; // check[i]存储需要在权值i处检查的查询 + + // 读入网格大小 + cin >> h >> w; + // 读入网格中的权值 + for (int i = 0; i < h; i++) + for (int j = 0; j < w; j++) + cin >> f[i][j]; + // 读入查询次数和查询信息 + cin >> q; + for (int i = 0; i < q; i++) + { + cin >> a[i] >> b[i] >> y[i] >> c[i] >> d[i] >> z[i]; + a[i]--, b[i]--, c[i]--, d[i]--; // 坐标从0开始 + } + + // 构建边集合 + // 添加竖直方向的边 + for (int i = 0; i < h - 1; i++) + for (int j = 0; j < w; j++) + e[min(f[i][j], f[i + 1][j])].push_back({i * w + j, (i + 1) * w + j}); + // 添加水平方向的边 + for (int i = 0; i < h; i++) + for (int j = 0; j < w - 1; j++) + e[min(f[i][j], f[i][j + 1])].push_back({i * w + j, i * w + (j + 1)}); + // 初始化二分查找的边界 + for (int i = 0; i < q; i++) + l[i] = 1, r[i] = F + 1; + + // 二分查找过程 + while (true) + { + bool flag = true; + // 清空check数组 + for (int i = 0; i <= F; i++) + check[i].clear(); + // 对每个查询,如果二分区间未收敛,则加入check数组 + for (int i = 0; i < q; i++) + { + if (r[i] - l[i] > 1) + { + check[(l[i] + r[i]) / 2].push_back(i); + flag = false; + } + } + if (flag) break; // 如果所有查询都已收敛,退出循环 + + // 使用并查集检查连通性 + dsu uf(h * w); + // 从大到小遍历权值 + for (int i = F; i >= 0; i--) + { + // 合并权值为i的所有边 + int sz = e[i].size(); + for (int j = 0; j < sz; j++) + uf.merge(e[i][j].first, e[i][j].second); + // 检查需要在权值i处判断的查询 + sz = check[i].size(); + for (int j = 0; j < sz; j++) + { + int idx_s = a[check[i][j]] * w + b[check[i][j]]; // 起点在一维数组中的索引 + int idx_t = c[check[i][j]] * w + d[check[i][j]]; // 终点在一维数组中的索引 + // 根据连通性更新二分边界 + if (uf.same(idx_s, idx_t)) + l[check[i][j]] = i; + else + r[check[i][j]] = i; + } + } + } + + // 输出每个查询的结果 + for (int i = 0; i < q; i++) + cout << (y[i] + z[i] - 2 * min(l[i], min(y[i], z[i]))) << endl; + return 0; +} diff --git a/atcoder/abc413/abc413_a/abc413_a.cpp b/atcoder/abc413/abc413_a/abc413_a.cpp index 7ab400de..e161ffe4 100644 --- a/atcoder/abc413/abc413_a/abc413_a.cpp +++ b/atcoder/abc413/abc413_a/abc413_a.cpp @@ -14,4 +14,4 @@ int main(){ if(sum <= m)cout << "Yes" << endl; else cout << "No" << endl; return 0; -} \ No newline at end of file +} diff --git a/atcoder/abc413/abc413_b/abc413_b.cpp b/atcoder/abc413/abc413_b/abc413_b.cpp index 493d4483..0ef682a3 100644 --- a/atcoder/abc413/abc413_b/abc413_b.cpp +++ b/atcoder/abc413/abc413_b/abc413_b.cpp @@ -37,4 +37,4 @@ int main() cout << result.size() << endl; return 0; -} \ No newline at end of file +} diff --git "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1-2.cpp" "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1-2.cpp" index ba014a5a..81f909e5 100644 --- "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1-2.cpp" +++ "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1-2.cpp" @@ -1,30 +1,30 @@ -#include -#include -using namespace std; - -int main() { - int ans = 0; - for (int i = 1; i <= 100000000; ++i) { - vector digits; - int x = i; - while (x) { - digits.push_back(x % 10); - x /= 10; - } - int j = digits.size(); - if (j % 2 != 0) continue; - - int s1 = 0, s2 = 0; - for (int k = 0; k < j / 2; ++k) { - s1 += digits[k]; - } - for (int k = j / 2; k < j; ++k) { - s2 += digits[k]; - } - if (s1 == s2) { - ++ans; - } - } - cout << ans << endl; - return 0; -} \ No newline at end of file +#include +#include +using namespace std; + +int main() { + int ans = 0; + for (int i = 1; i <= 100000000; ++i) { + vector digits; + int x = i; + while (x) { + digits.push_back(x % 10); + x /= 10; + } + int j = digits.size(); + if (j % 2 != 0) continue; + + int s1 = 0, s2 = 0; + for (int k = 0; k < j / 2; ++k) { + s1 += digits[k]; + } + for (int k = j / 2; k < j; ++k) { + s2 += digits[k]; + } + if (s1 == s2) { + ++ans; + } + } + cout << ans << endl; + return 0; +} diff --git "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1.cpp" "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1.cpp" index ea78cb7a..4d07a452 100644 --- "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1.cpp" +++ "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/A/1.cpp" @@ -1,33 +1,33 @@ -#include -#include - -using namespace std; - -int main(){ - int cnt = 0; - for (int i = 11; i <= 100000000; i++) - { - int x = i; - vector weishu; - while(x){ - weishu.push_back(x % 10); - x /= 10; - } - if(weishu.size() % 2 == 1) - continue; - int sum1 = 0; - int sum2 = 0; - int front = 0; - int back = weishu.size() - 1; - while(front < back){ - sum1 += weishu[front]; - sum2 += weishu[back]; - front++; - back--; - } - if(sum1 == sum2) - cnt++; - } - cout << cnt << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +int main(){ + int cnt = 0; + for (int i = 11; i <= 100000000; i++) + { + int x = i; + vector weishu; + while(x){ + weishu.push_back(x % 10); + x /= 10; + } + if(weishu.size() % 2 == 1) + continue; + int sum1 = 0; + int sum2 = 0; + int front = 0; + int back = weishu.size() - 1; + while(front < back){ + sum1 += weishu[front]; + sum2 += weishu[back]; + front++; + back--; + } + if(sum1 == sum2) + cnt++; + } + cout << cnt << endl; + return 0; +} diff --git "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-2.cpp" "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-2.cpp" index 5b1a524c..e9a56d26 100644 --- "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-2.cpp" +++ "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-2.cpp" @@ -1,59 +1,59 @@ -#include -#include -#include - -// 遍历所有可能的二进制数。 -// 检查每个二进制数中连续的“1”对应的“10”相加是否等于70。 -// 使用集合确保每个满足条件的组合是唯一的。 -// 最后输出满足条件的组合数量。 - -// 使用标准命名空间 -using namespace std; - -// 定义最大值常量 (2^30) -const int N = 1 << 30; - -// 初始化答案计数器和存储唯一值的集合 -int ans; -set s; - -int main() -{ - // 遍历所有可能的组合,从0到N-1 - for (int i = 0, j; i < N; ++i) - { - int cur = 0; // 当前连续'1'的和 - // 检查每个位置,从0到29 - for (j = 0; j < 30; ++j) - { - // 如果i的第j位是1 - if (i & (1 << j)) - { - // 当前和加10 - if ((cur += 10) == 100) - break; // 如果和达到100,停止检查 - } - else - cur = 0; // 如果位是0,重置当前和 - - // 检查当前和是否等于70 - if (cur == 70) - { - // 获取当前位置之前的所有位 - int pre = i & ((1 << (j + 1)) - 1); - // 如果这个组合之前没有被计数 - if (!s.count(pre)) - { - ++ans; // 增加答案计数 - s.insert(pre); // 将组合插入集合中 - // 取消下面注释可以输出调试信息 - // printf("Case %d:\n",ans); - // for (int k=0;k<=j;++k) printf("%d ",(i>>k)&1); - // putchar('\n'); - } - } - } - } - // 打印最终答案并返回 - return printf("%d\n", ans), 0; -} \ No newline at end of file +#include +#include +#include + +// 遍历所有可能的二进制数。 +// 检查每个二进制数中连续的“1”对应的“10”相加是否等于70。 +// 使用集合确保每个满足条件的组合是唯一的。 +// 最后输出满足条件的组合数量。 + +// 使用标准命名空间 +using namespace std; + +// 定义最大值常量 (2^30) +const int N = 1 << 30; + +// 初始化答案计数器和存储唯一值的集合 +int ans; +set s; + +int main() +{ + // 遍历所有可能的组合,从0到N-1 + for (int i = 0, j; i < N; ++i) + { + int cur = 0; // 当前连续'1'的和 + // 检查每个位置,从0到29 + for (j = 0; j < 30; ++j) + { + // 如果i的第j位是1 + if (i & (1 << j)) + { + // 当前和加10 + if ((cur += 10) == 100) + break; // 如果和达到100,停止检查 + } + else + cur = 0; // 如果位是0,重置当前和 + + // 检查当前和是否等于70 + if (cur == 70) + { + // 获取当前位置之前的所有位 + int pre = i & ((1 << (j + 1)) - 1); + // 如果这个组合之前没有被计数 + if (!s.count(pre)) + { + ++ans; // 增加答案计数 + s.insert(pre); // 将组合插入集合中 + // 取消下面注释可以输出调试信息 + // printf("Case %d:\n",ans); + // for (int k=0;k<=j;++k) printf("%d ",(i>>k)&1); + // putchar('\n'); + } + } + } + } + // 打印最终答案并返回 + return printf("%d\n", ans), 0; +} diff --git "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-3.cpp" "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-3.cpp" index 48df49f5..06353a45 100644 --- "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-3.cpp" +++ "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2-3.cpp" @@ -1,33 +1,33 @@ -#include -#include -#include - -using namespace std; - -const int N = 1 << 30; - -int ans; -set s; - -int main(){ - for (int i = 0, j; i < N;++i){ - int cur = 0; - for (j = 0; j < 30;++j){ - if(i & ( 1 << j)){ - if(cur + 10 == 100) - break; - } - else - cur = 0; - if(cur == 70){ - int pre = i & ((1 << (j + 1)) - 1); - if(!s.count(pre)){ - ++ans; - s.insert(pre); - } - } - } - } - - return cout << ans << endl, 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +const int N = 1 << 30; + +int ans; +set s; + +int main(){ + for (int i = 0, j; i < N;++i){ + int cur = 0; + for (j = 0; j < 30;++j){ + if(i & ( 1 << j)){ + if(cur + 10 == 100) + break; + } + else + cur = 0; + if(cur == 70){ + int pre = i & ((1 << (j + 1)) - 1); + if(!s.count(pre)){ + ++ans; + s.insert(pre); + } + } + } + } + + return cout << ans << endl, 0; +} diff --git "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2.cpp" "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2.cpp" index 6798bf21..19dec96e 100644 --- "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2.cpp" +++ "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/2.cpp" @@ -1,33 +1,33 @@ -#include - -using namespace std; -typedef long long ll; -typedef pair pii; -#define int ll -const int dx[9]={-1, 0, 1, 0, 1, -1, 1, -1,0}; -const int dy[9]={0, 1, 0, -1, -1, 1, 1, -1,0}; -#define INF 0x3f3f3f3f -const int N=2e5+10; -int ans=0; -void dfs(int grade,int index) -{ - if(grade==70) ans++; - if(grade==100) return; - if(index==30) return; - dfs(grade+10,index+1); - dfs(0,index+1); -} -void solve() -{ - dfs(0,0); - cout << ans << endl; - return; -} - -signed main() -{ - ios::sync_with_stdio(0); - cin.tie(0); - solve(); - return 0; -} \ No newline at end of file +#include + +using namespace std; +typedef long long ll; +typedef pair pii; +#define int ll +const int dx[9]={-1, 0, 1, 0, 1, -1, 1, -1,0}; +const int dy[9]={0, 1, 0, -1, -1, 1, 1, -1,0}; +#define INF 0x3f3f3f3f +const int N=2e5+10; +int ans=0; +void dfs(int grade,int index) +{ + if(grade==70) ans++; + if(grade==100) return; + if(index==30) return; + dfs(grade+10,index+1); + dfs(0,index+1); +} +void solve() +{ + dfs(0,0); + cout << ans << endl; + return; +} + +signed main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + solve(); + return 0; +} diff --git "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/tempCodeRunnerFile.cpp" "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/tempCodeRunnerFile.cpp" index 61fb6993..700d3964 100644 --- "a/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/tempCodeRunnerFile.cpp" +++ "b/blue_braige/2023\345\271\264\347\234\201\350\265\233/B/tempCodeRunnerFile.cpp" @@ -1,3 +1,3 @@ - printf("Case %d:\n",ans); - for (RI k=0;k<=j;++k) printf("%d ",(i>>k)&1); - putchar('\n'); \ No newline at end of file + printf("Case %d:\n",ans); + for (RI k=0;k<=j;++k) printf("%d ",(i>>k)&1); + putchar('\n'); diff --git "a/blue_braige/2024\345\271\264\347\234\201\350\265\233/A.cpp" "b/blue_braige/2024\345\271\264\347\234\201\350\265\233/A.cpp" index 937bf429..da0747a6 100644 --- "a/blue_braige/2024\345\271\264\347\234\201\350\265\233/A.cpp" +++ "b/blue_braige/2024\345\271\264\347\234\201\350\265\233/A.cpp" @@ -1,69 +1,69 @@ -#include -using namespace std; - -// 数字对应的笔画数 -const int cnt[10] = {13, 1, 2, 3, 5, 4, 4, 2, 2, 2}; - -// 判断是否为闰年 -bool leap(int year) -{ - // 闰年条件:能被400整除,或者能被4整除但不能被100整除 - return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); -} - -// 获取某年某月的最大天数 -int maxdays(int year, int month) -{ - // 大月有31天 - if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) - return 31; - // 小月有30天 - if (month == 4 || month == 6 || month == 9 || month == 11) - return 30; - // 二月根据是否为闰年决定天数 - return leap(year) ? 29 : 28; -} - -int main() -{ - int ans = 0; // 记录符合条件的天数 - int year = 2000, month = 1, day = 1; // 从2000年1月1日开始 - - while (1) - { - // 到达2024年4月14日时停止 - if (year == 2024 && month == 4 && day == 14) - break; - - int tot = 0; // 记录当前日期的总笔画数 - // 计算年份的笔画数 - tot += cnt[year / 1000] + cnt[year / 100 % 10] + cnt[year / 10 % 10] + cnt[year % 10]; - // 计算月份的笔画数 - tot += cnt[month / 10] + cnt[month % 10]; - // 计算日期的笔画数 - tot += cnt[day / 10] + cnt[day % 10]; - - // 如果总笔画数大于50,计数加1 - if (tot > 50) - ans++; - - // 日期递增 - if (day < maxdays(year, month)) - day++; - else - { - day = 1; // 重置为1号 - if (month < 12) - month++; // 月份递增 - else - { - year++; // 年份递增 - month = 1; // 重置为1月 - } - } - } - - // 输出符合条件的天数 - cout << ans; - return 0; -} +#include +using namespace std; + +// 数字对应的笔画数 +const int cnt[10] = {13, 1, 2, 3, 5, 4, 4, 2, 2, 2}; + +// 判断是否为闰年 +bool leap(int year) +{ + // 闰年条件:能被400整除,或者能被4整除但不能被100整除 + return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); +} + +// 获取某年某月的最大天数 +int maxdays(int year, int month) +{ + // 大月有31天 + if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) + return 31; + // 小月有30天 + if (month == 4 || month == 6 || month == 9 || month == 11) + return 30; + // 二月根据是否为闰年决定天数 + return leap(year) ? 29 : 28; +} + +int main() +{ + int ans = 0; // 记录符合条件的天数 + int year = 2000, month = 1, day = 1; // 从2000年1月1日开始 + + while (1) + { + // 到达2024年4月14日时停止 + if (year == 2024 && month == 4 && day == 14) + break; + + int tot = 0; // 记录当前日期的总笔画数 + // 计算年份的笔画数 + tot += cnt[year / 1000] + cnt[year / 100 % 10] + cnt[year / 10 % 10] + cnt[year % 10]; + // 计算月份的笔画数 + tot += cnt[month / 10] + cnt[month % 10]; + // 计算日期的笔画数 + tot += cnt[day / 10] + cnt[day % 10]; + + // 如果总笔画数大于50,计数加1 + if (tot > 50) + ans++; + + // 日期递增 + if (day < maxdays(year, month)) + day++; + else + { + day = 1; // 重置为1号 + if (month < 12) + month++; // 月份递增 + else + { + year++; // 年份递增 + month = 1; // 重置为1月 + } + } + } + + // 输出符合条件的天数 + cout << ans; + return 0; +} diff --git "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260.cpp" "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260.cpp" index 878e1384..48730fc3 100644 --- "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260.cpp" +++ "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260.cpp" @@ -1,37 +1,37 @@ -#include - -using namespace std; - -long long Lim = 1000000; - -int main() -{ - int cnt = 1; - - for (int i = 3; i <= Lim; ++i) - { - if (i % 2 == 0) - continue; - bool flag = true; - for (int j = 3; j * j <= i; j += 2) - { - if (i % j == 0) - { - flag = false; - break; // 找到因子后可以直接退出 - } - } - - if (flag) - { - cnt++; - if (cnt == 2025) // 找到第5个质数 - { - cout << i; - return 0; // 找到后直接退出程序 - } - } - } - - return 0; -} +#include + +using namespace std; + +long long Lim = 1000000; + +int main() +{ + int cnt = 1; + + for (int i = 3; i <= Lim; ++i) + { + if (i % 2 == 0) + continue; + bool flag = true; + for (int j = 3; j * j <= i; j += 2) + { + if (i % j == 0) + { + flag = false; + break; // 找到因子后可以直接退出 + } + } + + if (flag) + { + cnt++; + if (cnt == 2025) // 找到第5个质数 + { + cout << i; + return 0; // 找到后直接退出程序 + } + } + } + + return 0; +} diff --git "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260_1.cpp" "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260_1.cpp" index 80b64702..19b4aff3 100644 --- "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260_1.cpp" +++ "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/A\345\257\273\346\211\276\350\264\250\346\225\260_1.cpp" @@ -1,29 +1,29 @@ -#include - -using namespace std; - -int cnt = 1; - -bool check(int x){ - if(x % 2 == 0) - return false; - for (int i = 3; i * i <= x;i+=2){ - if(x % i == 0) - return false; - } - return true; -} - -int main(){ - int cnt = 1; - for (int i = 3; i <= 1000000;++i){ - if(check(i)){ - cnt++; - if(cnt == 2025){ - cout << i; - return 0; - } - } - } - return 0; -} \ No newline at end of file +#include + +using namespace std; + +int cnt = 1; + +bool check(int x){ + if(x % 2 == 0) + return false; + for (int i = 3; i * i <= x;i+=2){ + if(x % i == 0) + return false; + } + return true; +} + +int main(){ + int cnt = 1; + for (int i = 3; i <= 1000000;++i){ + if(check(i)){ + cnt++; + if(cnt == 2025){ + cout << i; + return 0; + } + } + } + return 0; +} diff --git "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/B\351\273\221\347\231\275\346\243\213.cpp" "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/B\351\273\221\347\231\275\346\243\213.cpp" index 48ede4c9..7fe3f2ac 100644 --- "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/B\351\273\221\347\231\275\346\243\213.cpp" +++ "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/B\351\273\221\347\231\275\346\243\213.cpp" @@ -1,9 +1,9 @@ -#include - -using namespace std; - -int main(){ - - - return 0; -} +#include + +using namespace std; + +int main(){ + + + return 0; +} diff --git "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/D\347\272\242\351\273\221\346\240\221.cpp" "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/D\347\272\242\351\273\221\346\240\221.cpp" index 0920badd..23bb8d02 100644 --- "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/D\347\272\242\351\273\221\346\240\221.cpp" +++ "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/D\347\272\242\351\273\221\346\240\221.cpp" @@ -1,27 +1,27 @@ -#include -using namespace std; - -bool digit_parity(int n) -{ - int cnt = 0; - while (n) - { - cnt += n & 1; - n >>= 1; - } - if (cnt % 2 == 0) - { - return false; - } -} - -int main() -{ - int m, k; - cin >> m; - for (int i = 0; i < m; i++) - { - cin >> k >> k; - cout << (digit_parity(k - 1) ? "BLACK\n" : "RED\n"); - } -} \ No newline at end of file +#include +using namespace std; + +bool digit_parity(int n) +{ + int cnt = 0; + while (n) + { + cnt += n & 1; + n >>= 1; + } + if (cnt % 2 == 0) + { + return false; + } +} + +int main() +{ + int m, k; + cin >> m; + for (int i = 0; i < m; i++) + { + cin >> k >> k; + cout << (digit_parity(k - 1) ? "BLACK\n" : "RED\n"); + } +} diff --git "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/G\345\234\260\351\233\267\351\230\265.cpp" "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/G\345\234\260\351\233\267\351\230\265.cpp" index 4340ab97..58ccb178 100644 --- "a/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/G\345\234\260\351\233\267\351\230\265.cpp" +++ "b/blue_braige/2025\345\271\264\347\234\201\350\265\233CA/G\345\234\260\351\233\267\351\230\265.cpp" @@ -1,75 +1,76 @@ -/* - * 地雷阵问题 - 计算地雷覆盖的总角度范围 - * - * 相关知识: - * 1. 极坐标与直角坐标转换 - * - 使用atan2(y,x)计算点(x,y)与x轴的夹角 - * - 使用sqrt(x^2 + y^2)计算点到原点的距离 - * - * 2. 三角函数 - * - asin(x)计算x的反正弦值 - * - 用于计算地雷覆盖的扇形角度 - * - * 3. 区间合并 - * - 将重叠的区间合并为更大的区间 - * - 使用sort对区间按起始角度排序 - * - 遍历区间,合并重叠部分 - * - * 4. 浮点数输出控制 - * - 使用fixed和setprecision控制输出精度 - */ - -#include - -using namespace std; - -int main(){ - // 输入地雷数量 - int n; - cin >> n; - - // 存储每个地雷的覆盖角度范围 [起始角度, 结束角度] - vector> radians; - - // 处理每个地雷 - for (int i = 0; i < n;++i){ - // 输入地雷坐标(a,b)和半径r - double a, b, r; - cin >> a >> b >> r; - - // 计算地雷中心与原点连线的角度 - double theta = atan2(b,a); - // 计算地雷到原点的距离 - double len = sqrt(a * a + b * b); - // 计算地雷覆盖的扇形角度的一半 - double phi = asin(r / len); - - // 存储该地雷的覆盖角度范围 - radians.emplace_back(theta - phi, theta + phi); // 直接在尾部“就地”构造元素,而不是先创建一个临时对象再拷贝或移动进去。 - } - - // 按起始角度排序 - sort(radians.begin(), radians.end()); - - // 初始化合并后的角度范围 - double start = 0, end = 0, sum = 0; - - // 合并重叠的角度范围 - for (auto &[l,r]:radians){ - if(l <= end){ - // 当前范围与前一个范围不重叠,累加前一个范围 - end = max(end, r); - }else{ - sum += end - start; - start = l; - end = r; - } - } - - // 累加最后一个范围 - sum += end - start; - - // 输出结果,保留两位小数 - cout << fixed << setprecision(3) << 1 - sum / atan2(1, 0) << endl; - return 0; -} +#include +/* + * 地雷阵问题 - 计算地雷覆盖的总角度范围 + * + * 相关知识: + * 1. 极坐标与直角坐标转换 + * - 使用atan2(y,x)计算点(x,y)与x轴的夹角 + * - 使用sqrt(x^2 + y^2)计算点到原点的距离 + * + * 2. 三角函数 + * - asin(x)计算x的反正弦值 + * - 用于计算地雷覆盖的扇形角度 + * + * 3. 区间合并 + * - 将重叠的区间合并为更大的区间 + * - 使用sort对区间按起始角度排序 + * - 遍历区间,合并重叠部分 + * + * 4. 浮点数输出控制 + * - 使用fixed和setprecision控制输出精度 + */ + +#include + +using namespace std; + +int main(){ + // 输入地雷数量 + int n; + cin >> n; + + // 存储每个地雷的覆盖角度范围 [起始角度, 结束角度] + vector> radians; + + // 处理每个地雷 + for (int i = 0; i < n;++i){ + // 输入地雷坐标(a,b)和半径r + double a, b, r; + cin >> a >> b >> r; + + // 计算地雷中心与原点连线的角度 + double theta = atan2(b,a); + // 计算地雷到原点的距离 + double len = sqrt(a * a + b * b); + // 计算地雷覆盖的扇形角度的一半 + double phi = asin(r / len); + + // 存储该地雷的覆盖角度范围 + radians.emplace_back(theta - phi, theta + phi); // 直接在尾部“就地”构造元素,而不是先创建一个临时对象再拷贝或移动进去。 + } + + // 按起始角度排序 + sort(radians.begin(), radians.end()); + + // 初始化合并后的角度范围 + double start = 0, end = 0, sum = 0; + + // 合并重叠的角度范围 + for (auto &[l,r]:radians){ + if(l <= end){ + // 当前范围与前一个范围不重叠,累加前一个范围 + end = max(end, r); + }else{ + sum += end - start; + start = l; + end = r; + } + } + + // 累加最后一个范围 + sum += end - start; + + // 输出结果,保留两位小数 + cout << fixed << setprecision(3) << 1 - sum / atan2(1, 0) << endl; + return 0; +} diff --git a/blue_braige/base/1.cpp b/blue_braige/base/1.cpp index d495f301..aaf34d26 100644 --- a/blue_braige/base/1.cpp +++ b/blue_braige/base/1.cpp @@ -1,41 +1,41 @@ -#include - -using namespace std; - -int main(){ - ios::sync_with_stdio(false); - cin.tie(0); - cout.tie(0); - int cnt = 0; - bool flag = false; - for (int year = 2014;year <= 2020;year++) - for (int month = 1; month <= 12;month++) - for (int day = 1; day <= 31;day++) - { - if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); - else if(month == 2) - { - if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ - if(day > 29) - break; - } - else - if(day > 28) - break; - } - else - if(day > 30) - break; - if(year == 2014 && month == 11 && day == 9){ - flag = true; - } - if(flag){ - cnt++; - if(cnt == 1001){ - cout << setw(4) << setfill('0') << year << "-" << setw(2) << setfill('0') << month << "-" << setw(2) << setfill('0') << day << endl; - return 0; - } - } - } - return 0; -} \ No newline at end of file +#include + +using namespace std; + +int main(){ + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + int cnt = 0; + bool flag = false; + for (int year = 2014;year <= 2020;year++) + for (int month = 1; month <= 12;month++) + for (int day = 1; day <= 31;day++) + { + if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); + else if(month == 2) + { + if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ + if(day > 29) + break; + } + else + if(day > 28) + break; + } + else + if(day > 30) + break; + if(year == 2014 && month == 11 && day == 9){ + flag = true; + } + if(flag){ + cnt++; + if(cnt == 1001){ + cout << setw(4) << setfill('0') << year << "-" << setw(2) << setfill('0') << month << "-" << setw(2) << setfill('0') << day << endl; + return 0; + } + } + } + return 0; +} diff --git a/blue_braige/base/2.cpp b/blue_braige/base/2.cpp index 90494949..c12af892 100644 --- a/blue_braige/base/2.cpp +++ b/blue_braige/base/2.cpp @@ -1,45 +1,45 @@ -#include - -using namespace std; - -int main() -{ - ios::sync_with_stdio(false); - cin.tie(0); - cout.tie(0); - int cnt = 0; - bool flag = false; - for (int year = 1921; year <= 2020; year++) - for (int month = 1; month <= 12; month++) - for (int day = 1; day <= 31; day++) - { - if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); - else if (month == 2) - { - if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) - { - if (day > 29) - break; - } - else if (day > 28) - break; - } - else if (day > 30) - break; - if (year == 1921 && month == 7 && day == 23) - { - flag = true; - cnt = 0; - } - if (flag) - { - if (year == 2020 && month == 7 && day == 1) - { - cout << cnt*24*60 << endl; - return 0; - } - cnt++; - } - } - return 0; -} \ No newline at end of file +#include + +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + int cnt = 0; + bool flag = false; + for (int year = 1921; year <= 2020; year++) + for (int month = 1; month <= 12; month++) + for (int day = 1; day <= 31; day++) + { + if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); + else if (month == 2) + { + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + { + if (day > 29) + break; + } + else if (day > 28) + break; + } + else if (day > 30) + break; + if (year == 1921 && month == 7 && day == 23) + { + flag = true; + cnt = 0; + } + if (flag) + { + if (year == 2020 && month == 7 && day == 1) + { + cout << cnt*24*60 << endl; + return 0; + } + cnt++; + } + } + return 0; +} diff --git a/blue_braige/base/3.cpp b/blue_braige/base/3.cpp index 38bad34b..bdeb04dc 100644 --- a/blue_braige/base/3.cpp +++ b/blue_braige/base/3.cpp @@ -1,19 +1,19 @@ -#include - -using namespace std; - -int main() -{ - ios::sync_with_stdio(false); - cin.tie(0); - cout.tie(0); - map mp; - mp.emplace("Alex", 15); - mp.emplace("Bob", 20); - mp.emplace("Fiona", 5); - map::iterator it; - for(it = mp.begin();it != mp.end();++it){ - cout << it->first << " " << it->second << endl; - } - return 0; -} \ No newline at end of file +#include + +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + map mp; + mp.emplace("Alex", 15); + mp.emplace("Bob", 20); + mp.emplace("Fiona", 5); + map::iterator it; + for(it = mp.begin();it != mp.end();++it){ + cout << it->first << " " << it->second << endl; + } + return 0; +} diff --git "a/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/1.cpp" "b/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/1.cpp" index b442517e..03dcd54a 100644 --- "a/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/1.cpp" +++ "b/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/1.cpp" @@ -1,38 +1,38 @@ -#include -#include -#include -#include - -using namespace std; - -const int N = 100010; - -int T, n; -int c[N]; -int mem[N]; -int sum = 0; - -int dfs(int x){ - if(mem[x]) - return mem[x]; - if(x > n) - sum = 0; - else - sum = max(dfs(x + 1), dfs(x + 2) + c[x]); -} - -int main(){ - - vector c(N); - scanf("%d", &T); - while(T--){ - scanf("%d", &n); - for (int i = 0; i < n;++i){ - scanf("%d", &c[i]); - } - memset(mem, 0, sizeof(mem)); - int res = dfs(0); - printf("%d", res); - } - return 0; -} \ No newline at end of file +#include +#include +#include +#include + +using namespace std; + +const int N = 100010; + +int T, n; +int c[N]; +int mem[N]; +int sum = 0; + +int dfs(int x){ + if(mem[x]) + return mem[x]; + if(x > n) + sum = 0; + else + sum = max(dfs(x + 1), dfs(x + 2) + c[x]); +} + +int main(){ + + vector c(N); + scanf("%d", &T); + while(T--){ + scanf("%d", &n); + for (int i = 0; i < n;++i){ + scanf("%d", &c[i]); + } + memset(mem, 0, sizeof(mem)); + int res = dfs(0); + printf("%d", res); + } + return 0; +} diff --git "a/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/DFS/1.cpp" "b/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/DFS/1.cpp" index 6a52d723..fd129a0b 100644 --- "a/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/DFS/1.cpp" +++ "b/blue_braige/\345\212\250\346\200\201\350\247\204\345\210\222/DFS/1.cpp" @@ -1,47 +1,47 @@ -#include -#include -using namespace std; - -const int N = 20; -int n; -bool st[N]; // 使用bool数组来标记是否选择某个数字 - -// DFS生成所有子集 -void dfs(int x) -{ - // 到达叶子节点,输出当前选择的数字 - if (x > n) - { - for (int i = 1; i <= n; ++i) - { - if (st[i]) - cout << i << " "; - } - cout << endl; - return; - } - - // 不选择当前数字 - st[x] = false; - dfs(x + 1); - - // 选择当前数字 - st[x] = true; - dfs(x + 1); - st[x] = false; // 回溯时恢复状态 -} - -int main() -{ - cin >> n; - - // 输入验证 - if (n <= 0 || n >= N) - { - cout << "Invalid input!" << endl; - return 1; - } - - dfs(1); - return 0; -} \ No newline at end of file +#include +#include +using namespace std; + +const int N = 20; +int n; +bool st[N]; // 使用bool数组来标记是否选择某个数字 + +// DFS生成所有子集 +void dfs(int x) +{ + // 到达叶子节点,输出当前选择的数字 + if (x > n) + { + for (int i = 1; i <= n; ++i) + { + if (st[i]) + cout << i << " "; + } + cout << endl; + return; + } + + // 不选择当前数字 + st[x] = false; + dfs(x + 1); + + // 选择当前数字 + st[x] = true; + dfs(x + 1); + st[x] = false; // 回溯时恢复状态 +} + +int main() +{ + cin >> n; + + // 输入验证 + if (n <= 0 || n >= N) + { + cout << "Invalid input!" << endl; + return 1; + } + + dfs(1); + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/DFS/\345\207\221\347\256\227\345\274\217/1.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/DFS/\345\207\221\347\256\227\345\274\217/1.cpp" index 5ff02901..d575c2a8 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/DFS/\345\207\221\347\256\227\345\274\217/1.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/DFS/\345\207\221\347\256\227\345\274\217/1.cpp" @@ -1,44 +1,44 @@ -#include -using namespace std; - -bool st[15]; // 标记数组,标记数字是否已经被使用过 -int num[15], ans; // 数组num存储排列的数字,ans记录满足条件的排列数量 - -// 深度优先搜索函数,用于生成排列并判断是否满足条件 -void dfs(int n) -{ - // 如果已经生成了一个排列 - if (n == 10) - { - // 计算出数字x和y - int x = num[4] * 100 + num[5] * 10 + num[6]; - int y = num[7] * 100 + num[8] * 10 + num[9]; - - // 判断是否满足条件 - if (num[1] * num[3] * y + num[2] * y + x * num[3] == 10 * num[3] * y) - { - ans++; // 满足条件的排列数量加一 - return; - } - } - - // 遍历可能的数字 - for (int i = 1; i <= 9; ++i) - { - // 如果数字已经被使用过,则跳过 - if (st[i]) - continue; - - st[i] = 1; // 标记当前数字已经被使用 - num[n] = i; // 将当前数字加入排列中 - dfs(n + 1); // 递归生成下一个位置的数字 - st[i] = 0; // 回溯,撤销当前数字的使用标记 - } -} - -int main() -{ - dfs(1); // 从第一个位置开始生成排列 - cout << ans; // 输出满足条件的排列数量 - return 0; -} \ No newline at end of file +#include +using namespace std; + +bool st[15]; // 标记数组,标记数字是否已经被使用过 +int num[15], ans; // 数组num存储排列的数字,ans记录满足条件的排列数量 + +// 深度优先搜索函数,用于生成排列并判断是否满足条件 +void dfs(int n) +{ + // 如果已经生成了一个排列 + if (n == 10) + { + // 计算出数字x和y + int x = num[4] * 100 + num[5] * 10 + num[6]; + int y = num[7] * 100 + num[8] * 10 + num[9]; + + // 判断是否满足条件 + if (num[1] * num[3] * y + num[2] * y + x * num[3] == 10 * num[3] * y) + { + ans++; // 满足条件的排列数量加一 + return; + } + } + + // 遍历可能的数字 + for (int i = 1; i <= 9; ++i) + { + // 如果数字已经被使用过,则跳过 + if (st[i]) + continue; + + st[i] = 1; // 标记当前数字已经被使用 + num[n] = i; // 将当前数字加入排列中 + dfs(n + 1); // 递归生成下一个位置的数字 + st[i] = 0; // 回溯,撤销当前数字的使用标记 + } +} + +int main() +{ + dfs(1); // 从第一个位置开始生成排列 + cout << ans; // 输出满足条件的排列数量 + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\344\271\230\347\247\257\346\225\260\351\207\217/1.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\344\271\230\347\247\257\346\225\260\351\207\217/1.cpp" index 65253702..72033b86 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\344\271\230\347\247\257\346\225\260\351\207\217/1.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\344\271\230\347\247\257\346\225\260\351\207\217/1.cpp" @@ -1,26 +1,26 @@ -#include -#include -#define ll long long - -using namespace std; - -int main() -{ - ios::sync_with_stdio(false); - cin.tie(0); - cout.tie(0); - - ll N; - cin >> N; - ll res = 0; - for (ll i = 1; i * i * i <= N; ++i) - { - for (ll j = i; j * j <= N / i; ++j) - { - if (j <= N / (i * j)) - res += (N / (i * j) - j + 1); - } - } - cout << res << endl; - return 0; -} \ No newline at end of file +#include +#include +#define ll long long + +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + ll N; + cin >> N; + ll res = 0; + for (ll i = 1; i * i * i <= N; ++i) + { + for (ll j = i; j * j <= N / i; ++j) + { + if (j <= N / (i * j)) + res += (N / (i * j) - j + 1); + } + } + cout << res << endl; + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\346\225\260\347\232\204\345\210\206\350\247\243/1.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\346\225\260\347\232\204\345\210\206\350\247\243/1.cpp" index 4e537195..5804207d 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\346\225\260\347\232\204\345\210\206\350\247\243/1.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\346\225\260\347\232\204\345\210\206\350\247\243/1.cpp" @@ -1,41 +1,41 @@ -#include -#include - -using namespace std; - -bool check(int x) { - while (x) { - if (x % 10 == 2 || x % 10 == 4) { - return false; - } - x /= 10; - } - return true; -} - -int main() { - int res = 0; - int num = 2019; - vector validNumbers; - - // Precompute valid numbers - for (int i = 1; i < num; ++i) { - if (check(i)) { - validNumbers.push_back(i); - } - } - - // Use three nested loops to find combinations - for (size_t i = 0; i < validNumbers.size(); ++i) { - for (size_t j = i + 1; j < validNumbers.size(); ++j) { - for (size_t k = j + 1; k < validNumbers.size(); ++k) { - if (validNumbers[i] + validNumbers[j] + validNumbers[k] == num) { - res++; - } - } - } - } - - cout << res << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +bool check(int x) { + while (x) { + if (x % 10 == 2 || x % 10 == 4) { + return false; + } + x /= 10; + } + return true; +} + +int main() { + int res = 0; + int num = 2019; + vector validNumbers; + + // Precompute valid numbers + for (int i = 1; i < num; ++i) { + if (check(i)) { + validNumbers.push_back(i); + } + } + + // Use three nested loops to find combinations + for (size_t i = 0; i < validNumbers.size(); ++i) { + for (size_t j = i + 1; j < validNumbers.size(); ++j) { + for (size_t k = j + 1; k < validNumbers.size(); ++k) { + if (validNumbers[i] + validNumbers[j] + validNumbers[k] == num) { + res++; + } + } + } + } + + cout << res << endl; + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/1.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/1.cpp" index 44564704..a9f2b691 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/1.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/1.cpp" @@ -1,22 +1,22 @@ -#include -#include - -using namespace std; - -int main(){ - int n; - cin >> n; - long long sum = 0; - for (int i = 1; i <= n;++i){ - int j = i; - while(j){ - if(j % 10 == 2 || j % 10 == 0 || j % 10 == 1 || j % 10 == 9){ - sum += i; - break; - } - j /= 10; - } - } - cout << sum << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +int main(){ + int n; + cin >> n; + long long sum = 0; + for (int i = 1; i <= n;++i){ + int j = i; + while(j){ + if(j % 10 == 2 || j % 10 == 0 || j % 10 == 1 || j % 10 == 9){ + sum += i; + break; + } + j /= 10; + } + } + cout << sum << endl; + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/tempCodeRunnerFile.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/tempCodeRunnerFile.cpp" index a99ac211..1547e21a 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/tempCodeRunnerFile.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\347\211\271\345\210\253\346\225\260\347\232\204\345\222\214/tempCodeRunnerFile.cpp" @@ -1,2 +1,2 @@ - - } \ No newline at end of file + + } diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/1.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/1.cpp" index 4e932036..821d795a 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/1.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/1.cpp" @@ -1,26 +1,26 @@ -#include -// 偏分版本 - -using namespace std; - -// 1 2 6 7 15 16 28 -// 3 5 8 14 17 27 -// 4 9 13 18 26 -// 10 12 19 25 -// 11 20 24 -// 21 23 -// 22 - -// 1 5 13 25 -// 1 -// 1+4 -// 1+4+8 -// 1+4+8+12 -int main(){ - int sum = 1; - for (int i = 1; i < 20;++i){ - sum += i * 4; - } - cout << sum << endl; - return 0; -} +#include +// 偏分版本 + +using namespace std; + +// 1 2 6 7 15 16 28 +// 3 5 8 14 17 27 +// 4 9 13 18 26 +// 10 12 19 25 +// 11 20 24 +// 21 23 +// 22 + +// 1 5 13 25 +// 1 +// 1+4 +// 1+4+8 +// 1+4+8+12 +int main(){ + int sum = 1; + for (int i = 1; i < 20;++i){ + sum += i * 4; + } + cout << sum << endl; + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/2.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/2.cpp" index aebc716e..34e9748f 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/2.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2601/2.cpp" @@ -1,37 +1,37 @@ -#include -using namespace std; - -int main() -{ - int m = 20, n = 20, num = 0; - int cnt = m + n - 1; // 确定斜对角线层级 - - // 累加前cnt-1层的数字 - for (int i = 1; i < cnt; i++) - { - num += i; - } - - // 判断斜对角线方向 - if (cnt % 2 == 0) - { // 偶数层:右上→左下方向 - int row = 1; - while (row <= m) - { - num++; - row++; - } - } - else - { // 奇数层:左下→右上方向 - int row = cnt; - while (row >= m) - { - num++; - row--; - } - } - - cout << num << endl; - return 0; -} \ No newline at end of file +#include +using namespace std; + +int main() +{ + int m = 20, n = 20, num = 0; + int cnt = m + n - 1; // 确定斜对角线层级 + + // 累加前cnt-1层的数字 + for (int i = 1; i < cnt; i++) + { + num += i; + } + + // 判断斜对角线方向 + if (cnt % 2 == 0) + { // 偶数层:右上→左下方向 + int row = 1; + while (row <= m) + { + num++; + row++; + } + } + else + { // 奇数层:左下→右上方向 + int row = cnt; + while (row >= m) + { + num++; + row--; + } + } + + cout << num << endl; + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2602/1.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2602/1.cpp" index 917faa39..85ce3729 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2602/1.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\350\233\207\345\275\242\345\241\253\346\225\2602/1.cpp" @@ -1,33 +1,33 @@ -#include -#include -#include -using namespace std; - -// int a[110][110]; - -int main() { - int i, j, tot, x, y, n; - cin >> n; - int a[n+2][n+2]; - memset(a, 0, sizeof(a)); - x = 0; - y = 0; - a[x][y] = 1; - tot = 1; - - while (tot < n * n) { // 顺时针填写数字 - while (y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot; // 上方一行从左到右 - while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot; // 右方一列从上到下 - while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot; // 下方一行从右到左 - while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot; // 左方一列从下到上 - } - - for (i = 0; i < n; i++) { - for (j = 0; j < n; j++) - printf("%3d", a[i][j]); - cout << endl; - } - - cout << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +using namespace std; + +// int a[110][110]; + +int main() { + int i, j, tot, x, y, n; + cin >> n; + int a[n+2][n+2]; + memset(a, 0, sizeof(a)); + x = 0; + y = 0; + a[x][y] = 1; + tot = 1; + + while (tot < n * n) { // 顺时针填写数字 + while (y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot; // 上方一行从左到右 + while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot; // 右方一列从上到下 + while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot; // 下方一行从右到左 + while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot; // 左方一列从下到上 + } + + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) + printf("%3d", a[i][j]); + cout << endl; + } + + cout << endl; + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/1.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/1.cpp" index 184d5fb0..f2d058de 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/1.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/1.cpp" @@ -1,31 +1,31 @@ -#include -#include - -using namespace std; - -string unzip(){ - string str1 ="",x =""; - char c; - int num; - while(cin >> c){ - if(c == '['){ - cin >> num; - x = unzip(); - while(num--){ - str1 += x; - } - } - else if(c == ']'){ - return str1; - } - else{ - str1 += c; - } - } - return str1; -} - -int main(){ - cout << unzip(); - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +string unzip(){ + string str1 ="",x =""; + char c; + int num; + while(cin >> c){ + if(c == '['){ + cin >> num; + x = unzip(); + while(num--){ + str1 += x; + } + } + else if(c == ']'){ + return str1; + } + else{ + str1 += c; + } + } + return str1; +} + +int main(){ + cout << unzip(); + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/2.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/2.cpp" index 55cb24c8..73730179 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/2.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/2.cpp" @@ -1,19 +1,19 @@ -#include -#include - -using namespace std; - -int feibo(int n){ - if(n == 1) - return 1; - if(n == 2) - return 2; - return feibo(n - 1) + feibo(n - 2); -} - -int main(){ - int n; - scanf("%d", &n); - printf("%d", feibo(n)); - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +int feibo(int n){ + if(n == 1) + return 1; + if(n == 2) + return 2; + return feibo(n - 1) + feibo(n - 2); +} + +int main(){ + int n; + scanf("%d", &n); + printf("%d", feibo(n)); + return 0; +} diff --git "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/3.cpp" "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/3.cpp" index 8a9a39ed..1af52e80 100644 --- "a/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/3.cpp" +++ "b/blue_braige/\346\232\264\345\212\233\346\236\232\344\270\276/\351\200\222\345\275\222/3.cpp" @@ -1,23 +1,23 @@ -#include -#include - -using namespace std; - - -int main(){ - int n; - scanf("%d",&n); - int f[n]; - f[1] = 1; - f[2] = 2; - if(n == 1 || n == 2){ - printf("%d", n); - return 0; - } - for(int i = 3; i <= n; i++){ - f[i] = f[i - 1] + f[i -2]; - } - printf("%d",f[n]); - - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + + +int main(){ + int n; + scanf("%d",&n); + int f[n]; + f[1] = 1; + f[2] = 2; + if(n == 1 || n == 2){ + printf("%d", n); + return 0; + } + for(int i = 3; i <= n; i++){ + f[i] = f[i - 1] + f[i -2]; + } + printf("%d",f[n]); + + return 0; +} diff --git "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/1.cpp" "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/1.cpp" index edf058e8..db0298e9 100644 --- "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/1.cpp" +++ "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/1.cpp" @@ -1,21 +1,21 @@ -#include -using namespace std; - -int main() { - long long a = 1, b = 1; - int n = 2; - - while (b <= 20211001) { - long long temp = a + b; - a = b; - b = temp; - ++n; - if(b > 20211001){ - cout << b; - break; - } - } - - cout << n << endl; - return 0; -} +#include +using namespace std; + +int main() { + long long a = 1, b = 1; + int n = 2; + + while (b <= 20211001) { + long long temp = a + b; + a = b; + b = temp; + ++n; + if(b > 20211001){ + cout << b; + break; + } + } + + cout << n << endl; + return 0; +} diff --git "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/2.cpp" "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/2.cpp" index 4f61bdea..31a91a8c 100644 --- "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/2.cpp" +++ "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/2.cpp" @@ -1,23 +1,23 @@ -#include -using namespace std; - -int main() { - int n=0,a = 0,b = 0; - cin >> n; - char vote; - while (n--) { - cin >> vote; - if(vote == 'A') { - a++; - }else{ - b++; - } - } - if(a > b) { - cout << "A"; - }else{ - cout << "B"; - } - - return 0; -} +#include +using namespace std; + +int main() { + int n=0,a = 0,b = 0; + cin >> n; + char vote; + while (n--) { + cin >> vote; + if(vote == 'A') { + a++; + }else{ + b++; + } + } + if(a > b) { + cout << "A"; + }else{ + cout << "B"; + } + + return 0; +} diff --git "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/3.cpp" "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/3.cpp" index dbfdf3b5..f2cbd5c6 100644 --- "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/3.cpp" +++ "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/3.cpp" @@ -1,34 +1,34 @@ -#include - -using namespace std; - -int sum = 0; -int n,m; -int matrix[100][26]; - -void add(int row,int col) { - sum += matrix[row][col]; -} - - -// A - 65 -int main() { - cin >> n >> m; - for(int i = 0; i < n; i++) { - for(int j = 0; j < m; j++) { - cin >> matrix[i][j]; - } - } - string formula; - cin >> formula; - int col,row; - for(int i = 1; i < formula.length(); i++) { - if( formula[i] >= 'A'&& formula[i] <= 'Z'){ - col = formula[i]-'A'; - row = formula[++i]-'1'; - add(row,col); - } - } - cout << sum << endl; - return 0; -} +#include + +using namespace std; + +int sum = 0; +int n,m; +int matrix[100][26]; + +void add(int row,int col) { + sum += matrix[row][col]; +} + + +// A - 65 +int main() { + cin >> n >> m; + for(int i = 0; i < n; i++) { + for(int j = 0; j < m; j++) { + cin >> matrix[i][j]; + } + } + string formula; + cin >> formula; + int col,row; + for(int i = 1; i < formula.length(); i++) { + if( formula[i] >= 'A'&& formula[i] <= 'Z'){ + col = formula[i]-'A'; + row = formula[++i]-'1'; + add(row,col); + } + } + cout << sum << endl; + return 0; +} diff --git "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_1.cpp" "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_1.cpp" index 13873aaa..c36fcef0 100644 --- "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_1.cpp" +++ "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_1.cpp" @@ -1,26 +1,27 @@ -#include - -using namespace std; - -int res= 0; -int n=0; - -int main() { - cin >> n; - vector v(n); - for(int i = 0; i < n; ++i) { - cin >> v[i]; - } - for(int i = 0; i < n; ++i) { - for(int j = i+1; j < n; ++j) { - for(int k = j+1; k < n; ++k) { - if(v[i] != v[j] && v[j] != v[k] && v[i] != v[k]) { - ++res; - } - } - } - } - cout << res << endl; - - return 0; -} +#include +#include + +using namespace std; + +int res= 0; +int n=0; + +int main() { + cin >> n; + vector v(n); + for(int i = 0; i < n; ++i) { + cin >> v[i]; + } + for(int i = 0; i < n; ++i) { + for(int j = i+1; j < n; ++j) { + for(int k = j+1; k < n; ++k) { + if(v[i] != v[j] && v[j] != v[k] && v[i] != v[k]) { + ++res; + } + } + } + } + cout << res << endl; + + return 0; +} diff --git "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_2.cpp" "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_2.cpp" index 0b245f46..6dd6d66d 100644 --- "a/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_2.cpp" +++ "b/blue_braige/\346\250\241\346\213\237\346\257\224\350\265\233/4_2.cpp" @@ -1,24 +1,25 @@ -#include - -using namespace std; - -int res= 0; -int n=0; - -int main() { - cin >> n; - vector v(n); - for(int i = 0; i < n; ++i) { - cin >> v[i]; - } - sort(v.begin(),v.end()); - for(int i = 0; i < n; ++i) { - for(int j = i+1; j < n; ++j) { - if(v[j] == v[j+1])++j; - res += i*(v.size()-j-1); - } - } - cout << res << endl; - - return 0; -} +#include +#include + +using namespace std; + +int res= 0; +int n=0; + +int main() { + cin >> n; + vector v(n); + for(int i = 0; i < n; ++i) { + cin >> v[i]; + } + sort(v.begin(),v.end()); + for(int i = 0; i < n; ++i) { + for(int j = i+1; j < n; ++j) { + if(v[j] == v[j+1])++j; + res += i*(v.size()-j-1); + } + } + cout << res << endl; + + return 0; +} diff --git "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2.cpp" "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2.cpp" index efd298e5..bea5f0c9 100644 --- "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2.cpp" +++ "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2.cpp" @@ -93,4 +93,4 @@ int main() // cout << "YES\n"; // return 0; -// } \ No newline at end of file +// } diff --git "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2_1.cpp" "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2_1.cpp" index 4f1953b3..adc4b9c6 100644 --- "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2_1.cpp" +++ "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/2_1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -36,13 +37,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/3.cpp" "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/3.cpp" index 1d07e7e5..a9d3da8e 100644 --- "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/3.cpp" +++ "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/3.cpp" @@ -48,4 +48,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/4.cpp" "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/4.cpp" index 4fd94542..bf629e32 100644 --- "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/4.cpp" +++ "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/4.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -57,4 +58,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/5.cpp" "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/5.cpp" index 5d1b7b75..edcdfdc4 100644 --- "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/5.cpp" +++ "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/5.cpp" @@ -54,4 +54,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6.cpp" "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6.cpp" index 341991b6..85df724a 100644 --- "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6.cpp" +++ "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6.cpp" @@ -48,4 +48,4 @@ int main() cout << dp[n] << endl; return 0; -} \ No newline at end of file +} diff --git "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6_1.cpp" "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6_1.cpp" index 8100813a..2b3c8866 100644 --- "a/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6_1.cpp" +++ "b/blue_braige/\347\254\25432\345\234\272\350\223\235\346\241\245\302\267\347\256\227\346\263\225\345\205\245\351\227\250\350\265\233\302\267\347\231\276\346\240\241\350\201\224\350\265\233/6_1.cpp" @@ -43,4 +43,4 @@ int main() cout << dp[n] << endl; return 0; -} \ No newline at end of file +} diff --git "a/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/1.cpp" "b/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/1.cpp" index 92c7ec5b..fac8e1b8 100644 --- "a/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/1.cpp" +++ "b/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/1.cpp" @@ -1,48 +1,48 @@ -#include -#include - -using namespace std; - -class Solution { -private: - vector> results; - vector d; - - bool isLegal(int row, int col, int n) { - for (int i = 0; i < row; ++i) { - if (d[i] == col || i - d[i] == row - col || i + d[i] == row + col) { - return false; - } - } - return true; - } - - void searchChilds(int row, int n, vector& board) { - if (row == n) { - results.push_back(board); - return; - } - - for (int col = 0; col < n; ++col) { - if (isLegal(row, col, n)) { - d[row] = col; - board[row][col] = 'Q'; - searchChilds(row + 1, n, board); - board[row][col] = '.'; - d[row] = 0; - } - } - } - -public: - vector> solveNQueens(int n) { - results.clear(); - d.resize(n, 0); - - vector board(n, string(n, '.')); - - searchChilds(0, n, board); - - return results; - } -}; \ No newline at end of file +#include +#include + +using namespace std; + +class Solution { +private: + vector> results; + vector d; + + bool isLegal(int row, int col, int n) { + for (int i = 0; i < row; ++i) { + if (d[i] == col || i - d[i] == row - col || i + d[i] == row + col) { + return false; + } + } + return true; + } + + void searchChilds(int row, int n, vector& board) { + if (row == n) { + results.push_back(board); + return; + } + + for (int col = 0; col < n; ++col) { + if (isLegal(row, col, n)) { + d[row] = col; + board[row][col] = 'Q'; + searchChilds(row + 1, n, board); + board[row][col] = '.'; + d[row] = 0; + } + } + } + +public: + vector> solveNQueens(int n) { + results.clear(); + d.resize(n, 0); + + vector board(n, string(n, '.')); + + searchChilds(0, n, board); + + return results; + } +}; diff --git "a/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/2.cpp" "b/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/2.cpp" index 48650e5a..be0057e0 100644 --- "a/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/2.cpp" +++ "b/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/N_Queen/2.cpp" @@ -1,55 +1,55 @@ -#include -#include - -using namespace std; - -int N; -int d[10010]; -char result[10010][10010]; -int num = 0; - -bool isLegal(int row, int col) { - bool judge = true; - for (int i = 1; i < row; ++i) { - if (d[i] == col || i - d[i] == row - col || i + d[i] == row + col) { - judge = false; - break; - } - } - return judge; -} - -void searchChilds(int row) { - if (row == N + 1) { - ++num; - for (int i = 1; i <= N; ++i) - cout << result[i] << endl; - cout << endl; - return; - } - - for (int col = 1; col <= N; ++col) { - if (isLegal(row, col)) { - d[row] = col; - result[row][col - 1] = 'Q'; - searchChilds(row + 1); - result[row][col - 1] = '.'; - d[row] = 0; - } - } -} - -int main() { - cin >> N; - - for (int i = 1; i <= N; ++i) { - for (int j = 0; j < N; ++j) { - result[i][j] = '.'; - } - } - - searchChilds(1); - cout << num; - - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +int N; +int d[10010]; +char result[10010][10010]; +int num = 0; + +bool isLegal(int row, int col) { + bool judge = true; + for (int i = 1; i < row; ++i) { + if (d[i] == col || i - d[i] == row - col || i + d[i] == row + col) { + judge = false; + break; + } + } + return judge; +} + +void searchChilds(int row) { + if (row == N + 1) { + ++num; + for (int i = 1; i <= N; ++i) + cout << result[i] << endl; + cout << endl; + return; + } + + for (int col = 1; col <= N; ++col) { + if (isLegal(row, col)) { + d[row] = col; + result[row][col - 1] = 'Q'; + searchChilds(row + 1); + result[row][col - 1] = '.'; + d[row] = 0; + } + } +} + +int main() { + cin >> N; + + for (int i = 1; i <= N; ++i) { + for (int j = 0; j < N; ++j) { + result[i][j] = '.'; + } + } + + searchChilds(1); + cout << num; + + return 0; +} diff --git "a/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/\350\277\207\346\262\263\345\215\222\351\227\256\351\242\230/1.cpp" "b/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/\350\277\207\346\262\263\345\215\222\351\227\256\351\242\230/1.cpp" index 46ec01c6..7dc19f81 100644 --- "a/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/\350\277\207\346\262\263\345\215\222\351\227\256\351\242\230/1.cpp" +++ "b/blue_braige/\347\256\227\346\263\225\345\255\246\344\271\240/\350\277\207\346\262\263\345\215\222\351\227\256\351\242\230/1.cpp" @@ -1,41 +1,42 @@ -#include - -using namespace std; - -int n = 0, m = 0; -int i = 0, j = 0; - -int main(){ - cin >> n >> m >> i >> j; - vector> a(n+1,vector(m+1,0)); - - a[0][0] = 1; - for(int i = 1;i<=n;++i){ - a[i][0] = 1; - } - for(int i = 1;i<=m;++i){ - a[0][i] = 1; - } - if(0 <= i && i < n && j >= 0 && j < m)a[i][j] = 0; - if(i-2 >= 0 && j-1 >= 0)a[i-2][j-1]=0; - if(i-2 >= 0 && j+1 <= m)a[i-2][j+1]=0; - if(i+2 <= n && j-1 >= 0)a[i+2][j-1]=0; - if(i+2 <= n && j+1 <= m)a[i+2][j+1]=0; - if(i-1 >= 0 && j-2 >= 0)a[i-1][j-2]=0; - if(i-1 >= 0 && j+2 <= m)a[i-1][j+2]=0; - if(i+1 <= n && j+2 <= m)a[i+1][j+2]=0; - if(i+1 <= n && j-2 >= 0)a[i+1][j-2]=0; - for(int x = 1;x<=n;++x){ - for(int y = 1;y<=m;++y){ - if(a[x][y] != 0){a[x][y] = a[x-1][y]+a[x][y-1];} - } - } - // for(int x = 0;x +#include + +using namespace std; + +int n = 0, m = 0; +int i = 0, j = 0; + +int main(){ + cin >> n >> m >> i >> j; + vector> a(n+1,vector(m+1,0)); + + a[0][0] = 1; + for(int i = 1;i<=n;++i){ + a[i][0] = 1; + } + for(int i = 1;i<=m;++i){ + a[0][i] = 1; + } + if(0 <= i && i < n && j >= 0 && j < m)a[i][j] = 0; + if(i-2 >= 0 && j-1 >= 0)a[i-2][j-1]=0; + if(i-2 >= 0 && j+1 <= m)a[i-2][j+1]=0; + if(i+2 <= n && j-1 >= 0)a[i+2][j-1]=0; + if(i+2 <= n && j+1 <= m)a[i+2][j+1]=0; + if(i-1 >= 0 && j-2 >= 0)a[i-1][j-2]=0; + if(i-1 >= 0 && j+2 <= m)a[i-1][j+2]=0; + if(i+1 <= n && j+2 <= m)a[i+1][j+2]=0; + if(i+1 <= n && j-2 >= 0)a[i+1][j-2]=0; + for(int x = 1;x<=n;++x){ + for(int y = 1;y<=m;++y){ + if(a[x][y] != 0){a[x][y] = a[x-1][y]+a[x][y-1];} + } + } + // for(int x = 0;xstr: diff --git a/book/hello algorithm/ch2/recursion.py b/book/hello algorithm/ch2/recursion.py index 8546d83e..1baf908f 100644 --- a/book/hello algorithm/ch2/recursion.py +++ b/book/hello algorithm/ch2/recursion.py @@ -13,7 +13,7 @@ def tail_recur(n,res): # 尾递归我感觉就是输出的结果放在了递归函数的参数中,不断的更新 if n == 0: return res - + tail_recur(n-1,res+n) def fib(n:int)->int: @@ -22,7 +22,7 @@ def fib(n:int)->int: return 0 elif n == 2: return 1 - + ans = fib(n-1) + fib(n-2) return ans diff --git "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\345\255\227\347\254\246\344\270\262/KMP \347\256\227\346\263\225/1.cpp" "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\345\255\227\347\254\246\344\270\262/KMP \347\256\227\346\263\225/1.cpp" index e69de29b..8b137891 100644 --- "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\345\255\227\347\254\246\344\270\262/KMP \347\256\227\346\263\225/1.cpp" +++ "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\345\255\227\347\254\246\344\270\262/KMP \347\256\227\346\263\225/1.cpp" @@ -0,0 +1 @@ + diff --git "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\225\260\347\273\204/\344\272\214\345\210\206\346\237\245\346\211\276/1.cpp" "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\225\260\347\273\204/\344\272\214\345\210\206\346\237\245\346\211\276/1.cpp" index fbd62731..6a9acdfb 100644 --- "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\225\260\347\273\204/\344\272\214\345\210\206\346\237\245\346\211\276/1.cpp" +++ "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\225\260\347\273\204/\344\272\214\345\210\206\346\237\245\346\211\276/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-03-22 17:44:27 diff --git "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" index 92dc979c..22c22041 100644 --- "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" +++ "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" @@ -35,13 +35,13 @@ class MyStack { public: queue q1,q2; //q2用于备份 MyStack() { - + } - + void push(int x) { q1.push(x); } - + int pop() { int n = q1.size(); if(n < 1)return -1; @@ -58,7 +58,7 @@ class MyStack { while(!q2.empty())q2.pop(); return res; } - + int top() { int n = q1.size(); if(n < 1)return -1; @@ -75,7 +75,7 @@ class MyStack { while(!q2.empty())q2.pop(); return res; } - + bool empty() { return q1.empty(); } diff --git "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" index 1dac5b00..2ed11ad1 100644 --- "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" +++ "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" @@ -34,15 +34,15 @@ using namespace std; // @lc code=start class MyStack { public: - queue q1; + queue q1; MyStack() { - + } - + void push(int x) { q1.push(x); } - + int pop() { int n = q1.size(); if(n < 1)return -1; @@ -56,7 +56,7 @@ class MyStack { q1.pop(); return res; } - + int top() { int n = q1.size(); if(n < 1)return -1; @@ -71,7 +71,7 @@ class MyStack { q1.pop(); return res; } - + bool empty() { return q1.empty(); } diff --git "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" index 4d9bac01..94662e38 100644 --- "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" +++ "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" @@ -42,13 +42,13 @@ class MyQueue { public: stack stackIn,stackOut; MyQueue() { - + } - + void push(int x) { stackIn.push(x); } - + int pop() { if(empty())return -1; if(stackOut.empty()){ @@ -61,7 +61,7 @@ class MyQueue { stackOut.pop(); return res; } - + int peek() { if(empty())return -1; if(stackOut.empty()){ @@ -72,7 +72,7 @@ class MyQueue { } return stackOut.top(); } - + bool empty() { return stackIn.empty() && stackOut.empty(); } diff --git "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/239. \346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274/1.cpp" "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/239. \346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274/1.cpp" index 225071b1..62408233 100644 --- "a/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/239. \346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274/1.cpp" +++ "b/book/\344\273\243\347\240\201\351\232\217\346\203\263\345\275\225/\346\240\210\345\222\214\351\230\237\345\210\227/239. \346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274/1.cpp" @@ -37,7 +37,7 @@ class Solution { class MyQueue{ public: deque q; - + void pop(int x){ if(!q.empty() && q.front() == x)q.pop_front(); //队首是当前窗口的最大值,与 front 对比不能和 back 对比 } diff --git a/code360/BSC/Search In Infinite Sorted 0-1 Array/1.cpp b/code360/BSC/Search In Infinite Sorted 0-1 Array/1.cpp index 7299ae10..9e5ac45d 100644 --- a/code360/BSC/Search In Infinite Sorted 0-1 Array/1.cpp +++ b/code360/BSC/Search In Infinite Sorted 0-1 Array/1.cpp @@ -1,28 +1,28 @@ -#include - -using namespace std; - -/************************************************************ - - Use get function that returns the value at index i - in the infinite sorted binary array. - - get(i) - { - - } - - -************************************************************/ - -long long firstOne() -{ - // Write your code here. - int n = 0,i = 0; - while(n = get(i)) -} - -int main(){ - - return 0; -} \ No newline at end of file +#include + +using namespace std; + +/************************************************************ + + Use get function that returns the value at index i + in the infinite sorted binary array. + + get(i) + { + + } + + +************************************************************/ + +long long firstOne() +{ + // Write your code here. + int n = 0,i = 0; + while(n = get(i)) +} + +int main(){ + + return 0; +} diff --git "a/code360/BSC/\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\242\221\347\216\207/1.cpp" "b/code360/BSC/\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\242\221\347\216\207/1.cpp" index f17bd2df..5803063a 100644 --- "a/code360/BSC/\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\242\221\347\216\207/1.cpp" +++ "b/code360/BSC/\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\242\221\347\216\207/1.cpp" @@ -1,23 +1,23 @@ -#include -#include - -using namespace std; - -int countOccurrences(vector arr, int x) -{ - // Write your code here. - int count = 0; - for (int i = 0; i < arr.size();++i){ - if(arr[i] == x){ - count++; - } - } - return count; -} - -int main(){ - vector arr = {1, 2, 3, 4, 5}; - int x = 3; - cout << countOccurrences(arr, x) << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +int countOccurrences(vector arr, int x) +{ + // Write your code here. + int count = 0; + for (int i = 0; i < arr.size();++i){ + if(arr[i] == x){ + count++; + } + } + return count; +} + +int main(){ + vector arr = {1, 2, 3, 4, 5}; + int x = 3; + cout << countOccurrences(arr, x) << endl; + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/A. A + B \351\227\256\351\242\230/A. A + B \351\227\256\351\242\230.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/A. A + B \351\227\256\351\242\230/A. A + B \351\227\256\351\242\230.cpp" index c1a2ed1f..2bbc26f6 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/A. A + B \351\227\256\351\242\230/A. A + B \351\227\256\351\242\230.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/A. A + B \351\227\256\351\242\230/A. A + B \351\227\256\351\242\230.cpp" @@ -1,12 +1,12 @@ -//#include -#include - -//using namespace std; - -int main() -{ - int a, b; - if (scanf("%d %d", &a, &b)!=2)return -1; - printf("%d", a + b); - return 0; -} \ No newline at end of file +//#include +#include + +//using namespace std; + +int main() +{ + int a, b; + if (scanf("%d %d", &a, &b)!=2)return -1; + printf("%d", a + b); + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/B. \345\276\210\351\253\230\345\205\264\346\235\245\345\210\260\345\215\227\351\200\232\345\244\247\345\255\246/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/B. \345\276\210\351\253\230\345\205\264\346\235\245\345\210\260\345\215\227\351\200\232\345\244\247\345\255\246/1.cpp" index 76a5f840..409bad58 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/B. \345\276\210\351\253\230\345\205\264\346\235\245\345\210\260\345\215\227\351\200\232\345\244\247\345\255\246/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/B. \345\276\210\351\253\230\345\205\264\346\235\245\345\210\260\345\215\227\351\200\232\345\244\247\345\255\246/1.cpp" @@ -1,9 +1,9 @@ -#include - -using namespace std; - -int main() -{ - cout << "Nice to be at Nantong University." << endl; - return 0; -} \ No newline at end of file +#include + +using namespace std; + +int main() +{ + cout << "Nice to be at Nantong University." << endl; + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/C. \345\244\247\345\255\246\347\224\237\347\250\213\345\272\217\350\256\276\350\256\241\347\253\236\350\265\233/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/C. \345\244\247\345\255\246\347\224\237\347\250\213\345\272\217\350\256\276\350\256\241\347\253\236\350\265\233/1.cpp" index 083a6b77..244771f8 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/C. \345\244\247\345\255\246\347\224\237\347\250\213\345\272\217\350\256\276\350\256\241\347\253\236\350\265\233/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/C. \345\244\247\345\255\246\347\224\237\347\250\213\345\272\217\350\256\276\350\256\241\347\253\236\350\265\233/1.cpp" @@ -1,38 +1,38 @@ -#include -#include -using namespace std; - -void showICPC() { - cout << "I love ICPC!" << endl; - cout << "-------------------------" << endl; - cout << "-#####-#####-#####-#####-" << endl; - cout << "---#---#-----#---#-#-----" << endl; - cout << "---#---#-----#####-#-----" << endl; - cout << "-#####-#####-#-----#####-" << endl; - cout << "-------------------------" << endl; -} - -void showCCPC() { - cout << "I love CCPC!" << endl; - cout << "-------------------------" << endl; - cout << "-#####-#####-#####-#####-" << endl; - cout << "-#-----#-----#---#-#-----" << endl; - cout << "-#-----#-----#####-#-----" << endl; - cout << "-#####-#####-#-----#####-" << endl; - cout << "-------------------------" << endl; -} - - -int main() -{ - string judge; - cin >> judge; - if (judge == "ICPC") - showICPC(); - if (judge == "CCPC") - showCCPC(); - else { - cout << "input error!"; - } - return 0; -} \ No newline at end of file +#include +#include +using namespace std; + +void showICPC() { + cout << "I love ICPC!" << endl; + cout << "-------------------------" << endl; + cout << "-#####-#####-#####-#####-" << endl; + cout << "---#---#-----#---#-#-----" << endl; + cout << "---#---#-----#####-#-----" << endl; + cout << "-#####-#####-#-----#####-" << endl; + cout << "-------------------------" << endl; +} + +void showCCPC() { + cout << "I love CCPC!" << endl; + cout << "-------------------------" << endl; + cout << "-#####-#####-#####-#####-" << endl; + cout << "-#-----#-----#---#-#-----" << endl; + cout << "-#-----#-----#####-#-----" << endl; + cout << "-#####-#####-#-----#####-" << endl; + cout << "-------------------------" << endl; +} + + +int main() +{ + string judge; + cin >> judge; + if (judge == "ICPC") + showICPC(); + if (judge == "CCPC") + showCCPC(); + else { + cout << "input error!"; + } + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/D. \345\245\275\345\244\232 A + B \351\227\256\351\242\230/1.py" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/D. \345\245\275\345\244\232 A + B \351\227\256\351\242\230/1.py" index 47224a77..39ce03b9 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/D. \345\245\275\345\244\232 A + B \351\227\256\351\242\230/1.py" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/D. \345\245\275\345\244\232 A + B \351\227\256\351\242\230/1.py" @@ -1,15 +1,15 @@ -n = int(input()) - -# method 1 -while n>0: - a,b = [int(x) for x in input().split()] - ans = a+b - print(ans) - n -= 1 - -# method 2 -while n>0: - a,b = map(int,input().split()) - ans = a+b - print(ans) - n-=1 \ No newline at end of file +n = int(input()) + +# method 1 +while n>0: + a,b = [int(x) for x in input().split()] + ans = a+b + print(ans) + n -= 1 + +# method 2 +while n>0: + a,b = map(int,input().split()) + ans = a+b + print(ans) + n-=1 diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/E.\345\245\275\345\244\232\346\225\260\346\261\202\345\222\214\351\227\256\351\242\230/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/E.\345\245\275\345\244\232\346\225\260\346\261\202\345\222\214\351\227\256\351\242\230/1.cpp" index e6136623..c3336bed 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/E.\345\245\275\345\244\232\346\225\260\346\261\202\345\222\214\351\227\256\351\242\230/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/E.\345\245\275\345\244\232\346\225\260\346\261\202\345\222\214\351\227\256\351\242\230/1.cpp" @@ -1,22 +1,22 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int n; - cin >> n; - long sum = 0; - while(n--){ - int x; - cin >> x; - sum += x; - } - cout << sum << endl; - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + long sum = 0; + while(n--){ + int x; + cin >> x; + sum += x; + } + cout << sum << endl; + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.cpp" index ad1dbf9f..36228c31 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.cpp" @@ -1,16 +1,16 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int c,a; - cin >> c >> a; - cout << (c == a) << endl; - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int c,a; + cin >> c >> a; + cout << (c == a) << endl; + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.py" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.py" index 8f089bd2..a694d0ff 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.py" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/1.py" @@ -1,3 +1,3 @@ -c,a = map(int,input().split()) - -print(int(c==a)) \ No newline at end of file +c,a = map(int,input().split()) + +print(int(c==a)) diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/2.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/2.cpp" index e69de29b..8b137891 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/2.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/F. \346\234\211\346\225\210\347\224\265\345\271\263/2.cpp" @@ -0,0 +1 @@ + diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/G. \347\254\254\345\207\240\344\270\252\345\255\227\346\257\215/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/G. \347\254\254\345\207\240\344\270\252\345\255\227\346\257\215/1.cpp" index 21d920dd..e4c70c11 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/G. \347\254\254\345\207\240\344\270\252\345\255\227\346\257\215/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/G. \347\254\254\345\207\240\344\270\252\345\255\227\346\257\215/1.cpp" @@ -1,16 +1,16 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int n; - cin >> n; - cout << char(n - 1 + 'A') << endl; - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + cout << char(n - 1 + 'A') << endl; + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/H. \344\270\211\345\200\274\351\200\273\350\276\221/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/H. \344\270\211\345\200\274\351\200\273\350\276\221/1.cpp" index 90742240..e9f6ebb6 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/H. \344\270\211\345\200\274\351\200\273\350\276\221/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/H. \344\270\211\345\200\274\351\200\273\350\276\221/1.cpp" @@ -1,31 +1,31 @@ -#include -using namespace std; - -int main() { - char a, b; - cin >> a >> b; - - // A && B - char and_result; - if (a == 'F' || b == 'F') { - and_result = 'F'; - } else if (a == 'T' && b == 'T') { - and_result = 'T'; - } else { - and_result = '0'; - } - - // A || B - char or_result; - if (a == 'T' || b == 'T') { - or_result = 'T'; - } else if (a == 'F' && b == 'F') { - or_result = 'F'; - } else { - or_result = '0'; - } - - cout << and_result << " " << or_result << endl; - - return 0; -} \ No newline at end of file +#include +using namespace std; + +int main() { + char a, b; + cin >> a >> b; + + // A && B + char and_result; + if (a == 'F' || b == 'F') { + and_result = 'F'; + } else if (a == 'T' && b == 'T') { + and_result = 'T'; + } else { + and_result = '0'; + } + + // A || B + char or_result; + if (a == 'T' || b == 'T') { + or_result = 'T'; + } else if (a == 'F' && b == 'F') { + or_result = 'F'; + } else { + or_result = '0'; + } + + cout << and_result << " " << or_result << endl; + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/1.cpp" index ba48d2fa..3e6cf113 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/1.cpp" @@ -1,55 +1,56 @@ -#include -using namespace std; - -long long add(long long a,long long b){ - return a+b; -} - -long long mul(long long a,long long b){ - return a*b; -} - -long long sub(long long a,long long b){ - return a-b; -} - -vector division(int64_t a,int64_t b){ - vector result(2); - result[0] = a/b; - result[1] = a%b; - return result; -} - -int main() { - int n; - cin >> n; - cin.ignore(); // Ignore the newline character after the integer input - while(n--){ - string line; - getline(cin,line); - stringstream ss(line); - int num1,num2; - char op; - ss >> num1 >> op >> num2; - switch(op){ - case '+': - cout << add(num1,num2) << endl; - break; - case '-': - cout << sub(num1,num2) << endl; - break; - case '*': - cout << mul(num1,num2) << endl; - break; - case '/':{ - vector result = division(num1,num2); - cout << result[0] << " " << result[1] << endl; - break; - } - default: - break; - } - } - - return 0; -} +#include +#include +using namespace std; + +long long add(long long a,long long b){ + return a+b; +} + +long long mul(long long a,long long b){ + return a*b; +} + +long long sub(long long a,long long b){ + return a-b; +} + +vector division(int64_t a,int64_t b){ + vector result(2); + result[0] = a/b; + result[1] = a%b; + return result; +} + +int main() { + int n; + cin >> n; + cin.ignore(); // Ignore the newline character after the integer input + while(n--){ + string line; + getline(cin,line); + stringstream ss(line); + int num1,num2; + char op; + ss >> num1 >> op >> num2; + switch(op){ + case '+': + cout << add(num1,num2) << endl; + break; + case '-': + cout << sub(num1,num2) << endl; + break; + case '*': + cout << mul(num1,num2) << endl; + break; + case '/':{ + vector result = division(num1,num2); + cout << result[0] << " " << result[1] << endl; + break; + } + default: + break; + } + } + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/2.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/2.cpp" index 3f0c2443..439248cf 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/2.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/I. \345\244\232\345\212\237\350\203\275\350\256\241\347\256\227\345\231\250/2.cpp" @@ -1,52 +1,52 @@ -#include -using namespace std; - -long long add(long long a,long long b){ - return a+b; -} - -long long mul(long long a,long long b){ - return a*b; -} - -long long sub(long long a,long long b){ - return a-b; -} - -pair division(long long a,long long b){ - return {a/b, a%b}; -} - -int main() { - int n; - cin >> n; - cin.ignore(); // Ignore the newline character after the integer input - while(n--){ - string line; - getline(cin,line); - stringstream ss(line); - int num1,num2; - char op; - ss >> num1 >> op >> num2; - switch(op){ - case '+': - cout << add(num1,num2) << endl; - break; - case '-': - cout << sub(num1,num2) << endl; - break; - case '*': - cout << mul(num1,num2) << endl; - break; - case '/':{ - pair result = division(num1,num2); - cout << result.first << " " << result.second << endl; - break; - } - default: - break; - } - } - - return 0; -} +#include +using namespace std; + +long long add(long long a,long long b){ + return a+b; +} + +long long mul(long long a,long long b){ + return a*b; +} + +long long sub(long long a,long long b){ + return a-b; +} + +pair division(long long a,long long b){ + return {a/b, a%b}; +} + +int main() { + int n; + cin >> n; + cin.ignore(); // Ignore the newline character after the integer input + while(n--){ + string line; + getline(cin,line); + stringstream ss(line); + int num1,num2; + char op; + ss >> num1 >> op >> num2; + switch(op){ + case '+': + cout << add(num1,num2) << endl; + break; + case '-': + cout << sub(num1,num2) << endl; + break; + case '*': + cout << mul(num1,num2) << endl; + break; + case '/':{ + pair result = division(num1,num2); + cout << result.first << " " << result.second << endl; + break; + } + default: + break; + } + } + + return 0; +} diff --git a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/J. 10^N/1.cpp b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/J. 10^N/1.cpp index 7eb46eb3..7e77ef86 100644 --- a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/J. 10^N/1.cpp +++ b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/J. 10^N/1.cpp @@ -1,23 +1,23 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int n; - cin >> n; - if(n == 0){ - cout << '1'; - return 0; - } - cout << '1'; - while(n--){ - cout << '0'; - } - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + if(n == 0){ + cout << '1'; + return 0; + } + cout << '1'; + while(n--){ + cout << '0'; + } + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/K. \345\245\207\346\225\260\344\271\213\345\222\214/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/K. \345\245\207\346\225\260\344\271\213\345\222\214/1.cpp" index a85d74b9..142d183f 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/K. \345\245\207\346\225\260\344\271\213\345\222\214/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/K. \345\245\207\346\225\260\344\271\213\345\222\214/1.cpp" @@ -1,23 +1,23 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int n; - cin >> n; - long long sum = 0; - while(n--){ - int x; - cin >> x; - if(x%2 == 1)sum+=x; - } - - cout << sum << '\n'; - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + long long sum = 0; + while(n--){ + int x; + cin >> x; + if(x%2 == 1)sum+=x; + } + + cout << sum << '\n'; + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/L. \346\225\260\345\205\203\347\264\240/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/L. \346\225\260\345\205\203\347\264\240/1.cpp" index 527a3746..36129156 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/L. \346\225\260\345\205\203\347\264\240/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/L. \346\225\260\345\205\203\347\264\240/1.cpp" @@ -1,28 +1,28 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int t; - cin >> t; - while(t--){ - int n,m; - cin >> n >> m; - int large=0,equal = 0,less = 0; - for(int i = 0;i> x; - if(x > m) large++; - else if(x == m) equal++; - else less++; - } - cout << equal << ' ' << large << ' ' << less << '\n'; - } - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int t; + cin >> t; + while(t--){ + int n,m; + cin >> n >> m; + int large=0,equal = 0,less = 0; + for(int i = 0;i> x; + if(x > m) large++; + else if(x == m) equal++; + else less++; + } + cout << equal << ' ' << large << ' ' << less << '\n'; + } + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/M. \347\234\274\347\235\233\350\277\233\344\272\206\346\262\231\345\255\220/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/M. \347\234\274\347\235\233\350\277\233\344\272\206\346\262\231\345\255\220/1.cpp" index a2bf9784..69069bd2 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/M. \347\234\274\347\235\233\350\277\233\344\272\206\346\262\231\345\255\220/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/M. \347\234\274\347\235\233\350\277\233\344\272\206\346\262\231\345\255\220/1.cpp" @@ -1,24 +1,24 @@ -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int t; - cin >> t; - while(t--){ - vector s(3); - for(int i = 0; i < 3; ++i) { - cin >> s[i]; - } - swap(s[0], s[2]); - cout << s[0] << ' ' << s[1] << ' ' << s[2] << '\n'; - } - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int t; + cin >> t; + while(t--){ + vector s(3); + for(int i = 0; i < 3; ++i) { + cin >> s[i]; + } + swap(s[0], s[2]); + cout << s[0] << ' ' << s[1] << ' ' << s[2] << '\n'; + } + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/N. \342\200\234\344\274\240\345\243\260\347\255\222\342\200\235\357\274\214\344\275\206\346\230\257\350\277\207\346\273\244/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/N. \342\200\234\344\274\240\345\243\260\347\255\222\342\200\235\357\274\214\344\275\206\346\230\257\350\277\207\346\273\244/1.cpp" index cc2f7823..b94ecd4d 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/N. \342\200\234\344\274\240\345\243\260\347\255\222\342\200\235\357\274\214\344\275\206\346\230\257\350\277\207\346\273\244/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/N. \342\200\234\344\274\240\345\243\260\347\255\222\342\200\235\357\274\214\344\275\206\346\230\257\350\277\207\346\273\244/1.cpp" @@ -1,21 +1,21 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - int t; - cin >> t; - while(t--){ - string str; - cin >> str; - if(str.find("like") != string::npos || str.find("love") != string::npos) continue; - else cout << str << endl; - } - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int t; + cin >> t; + while(t--){ + string str; + cin >> str; + if(str.find("like") != string::npos || str.find("love") != string::npos) continue; + else cout << str << endl; + } + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/O. \345\255\227\347\254\246\344\270\262\344\272\244\346\215\242/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/O. \345\255\227\347\254\246\344\270\262\344\272\244\346\215\242/1.cpp" index 9f3e7f57..02fd3d0f 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/O. \345\255\227\347\254\246\344\270\262\344\272\244\346\215\242/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/O. \345\255\227\347\254\246\344\270\262\344\272\244\346\215\242/1.cpp" @@ -1,30 +1,30 @@ -#include -#include -#include -using namespace std; - -int main() { - int n, m; - cin >> n >> m; - - vector vec(n+1); - for(int i = 1; i < n+1; ++i) { - cin >> vec[i]; - } - - while(m--) { - int x, y; - cin >> x >> y; - - if(x >= 1 && x <= n && y >= 1 && y <= n) { - swap(vec[x], vec[y]); - } - } - - // 只输出 n 个元素 - for(int i = 1; i < n+1; ++i) { - cout << vec[i] << '\n'; - } - - return 0; -} +#include +#include +#include +using namespace std; + +int main() { + int n, m; + cin >> n >> m; + + vector vec(n+1); + for(int i = 1; i < n+1; ++i) { + cin >> vec[i]; + } + + while(m--) { + int x, y; + cin >> x >> y; + + if(x >= 1 && x <= n && y >= 1 && y <= n) { + swap(vec[x], vec[y]); + } + } + + // 只输出 n 个元素 + for(int i = 1; i < n+1; ++i) { + cout << vec[i] << '\n'; + } + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/P. \346\225\260\345\255\227\346\211\223\345\255\227\346\234\272/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/P. \346\225\260\345\255\227\346\211\223\345\255\227\346\234\272/1.cpp" index c893b315..6182962e 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/P. \346\225\260\345\255\227\346\211\223\345\255\227\346\234\272/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/P. \346\225\260\345\255\227\346\211\223\345\255\227\346\234\272/1.cpp" @@ -1,36 +1,36 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { - - int n; - cin >> n; - while(n--){ - int m; - cin >> m; - int total = m; - int count = 0; - while(m--){ - int x; - cin >> x; - string word(x,' '); - for(int i = 0;i> c; - word[i] = char(c + 'A'-1); - } - cout << word; - count++; - if(count != total) cout << ' '; - } - cout << '\n'; - } - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + + int n; + cin >> n; + while(n--){ + int m; + cin >> m; + int total = m; + int count = 0; + while(m--){ + int x; + cin >> x; + string word(x,' '); + for(int i = 0;i> c; + word[i] = char(c + 'A'-1); + } + cout << word; + count++; + if(count != total) cout << ' '; + } + cout << '\n'; + } + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/Q.\346\227\213\350\275\254\347\202\271/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/Q.\346\227\213\350\275\254\347\202\271/1.cpp" index 19e4f0f5..71a80163 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/Q.\346\227\213\350\275\254\347\202\271/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/Q.\346\227\213\350\275\254\347\202\271/1.cpp" @@ -1,29 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -typedef long long ll; -using namespace std; - -const double PI = acos(-1.0); - -int main() { - ios::sync_with_stdio(false),cin.tie(0); - cout << fixed << setprecision(10); - int t; - cin >> t; - while(t--){ - int r,a; - cin >> r >> a; - cout << cos(a*PI/180.0)*r << ' ' << r*sin(a*PI/180.0) <<'\n'; - } - - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +typedef long long ll; +using namespace std; + +const double PI = acos(-1.0); + +int main() { + ios::sync_with_stdio(false),cin.tie(0); + cout << fixed << setprecision(10); + int t; + cin >> t; + while(t--){ + int r,a; + cin >> r >> a; + cout << cos(a*PI/180.0)*r << ' ' << r*sin(a*PI/180.0) <<'\n'; + } + + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/R. \346\210\221\347\232\204\350\256\260\345\277\206\350\242\253\346\211\223\344\271\261\344\272\206/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/R. \346\210\221\347\232\204\350\256\260\345\277\206\350\242\253\346\211\223\344\271\261\344\272\206/1.cpp" index 23c3eae1..2c56afe4 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/R. \346\210\221\347\232\204\350\256\260\345\277\206\350\242\253\346\211\223\344\271\261\344\272\206/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/R. \346\210\221\347\232\204\350\256\260\345\277\206\350\242\253\346\211\223\344\271\261\344\272\206/1.cpp" @@ -1,30 +1,30 @@ -#include -#include -#include -#include -#include -#include -#include -#include -typedef long long ll; -using namespace std; - -vectora; - -int main() { - ios::sync_with_stdio(false),cin.tie(0); - int n,m; - cin >> n >> m; - a.resize(n); - for(int i = 0;i> a[i]; - - while(m--){ - int i; - cin >> i; - cout << a[i-1]; - if(m>0)cout << ' '; - } - cout << '\n'; - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +#include +typedef long long ll; +using namespace std; + +vectora; + +int main() { + ios::sync_with_stdio(false),cin.tie(0); + int n,m; + cin >> n >> m; + a.resize(n); + for(int i = 0;i> a[i]; + + while(m--){ + int i; + cin >> i; + cout << a[i-1]; + if(m>0)cout << ' '; + } + cout << '\n'; + + return 0; +} diff --git "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/S. 0-1 \346\216\222\345\272\217/1.cpp" "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/S. 0-1 \346\216\222\345\272\217/1.cpp" index 84785ed3..1baa3caa 100644 --- "a/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/S. 0-1 \346\216\222\345\272\217/1.cpp" +++ "b/hydro_oj/NTU_2025_Freshman_Algo_Comp_OuterField/S. 0-1 \346\216\222\345\272\217/1.cpp" @@ -1,41 +1,41 @@ -#include -#include -#include -#include -#include -#include -#include -#include -typedef long long ll; -using namespace std; - -string s; - -int main() { - ios::sync_with_stdio(false),cin.tie(0); - int t; - cin >> t; - int cnt_0 = 0; - for(int i = 0;i> x; - if(x == 0)cnt_0++; - } - - if(cnt_0 == t){ - for(int i = 0;i +#include +#include +#include +#include +#include +#include +#include +typedef long long ll; +using namespace std; + +string s; + +int main() { + ios::sync_with_stdio(false),cin.tie(0); + int t; + cin >> t; + int cnt_0 = 0; + for(int i = 0;i> x; + if(x == 0)cnt_0++; + } + + if(cnt_0 == t){ + for(int i = 0;i - -// 只包含必要的头文件 - -int main() { - // 优化输入输出 - std::ios_base::sync_with_stdio(false); - std::cin.tie(nullptr); - - int t; - std::cin >> t; - - int cnt_0 = 0; - // 使用一个临时变量 n 来保存原始的 t 值 - int n = t; - while (t--) { - int x; - std::cin >> x; - if (x == 0) { - cnt_0++; - } - } - - // 使用一个循环完成所有输出 - for (int i = 0; i < n; ++i) { - // 在输出每个数字之前,先判断是不是第一个数 - // 如果不是第一个数,就先输出一个空格 - if (i > 0) { - std::cout << ' '; - } - - // 根据当前是第几个数来决定输出 0 还是 1 - if (i < cnt_0) { - std::cout << 0; - } else { - std::cout << 1; - } - } - std::cout << '\n'; // 最后输出一个换行符 - - return 0; -} +#include + +// 只包含必要的头文件 + +int main() { + // 优化输入输出 + std::ios_base::sync_with_stdio(false); + std::cin.tie(nullptr); + + int t; + std::cin >> t; + + int cnt_0 = 0; + // 使用一个临时变量 n 来保存原始的 t 值 + int n = t; + while (t--) { + int x; + std::cin >> x; + if (x == 0) { + cnt_0++; + } + } + + // 使用一个循环完成所有输出 + for (int i = 0; i < n; ++i) { + // 在输出每个数字之前,先判断是不是第一个数 + // 如果不是第一个数,就先输出一个空格 + if (i > 0) { + std::cout << ' '; + } + + // 根据当前是第几个数来决定输出 0 还是 1 + if (i < cnt_0) { + std::cout << 0; + } else { + std::cout << 1; + } + } + std::cout << '\n'; // 最后输出一个换行符 + + return 0; +} diff --git a/intro/cpp/sort/sort.cpp b/intro/cpp/sort/sort.cpp index ec86f99c..6362b580 100644 --- a/intro/cpp/sort/sort.cpp +++ b/intro/cpp/sort/sort.cpp @@ -1,42 +1,42 @@ -#define FILE_IO -#define bob_sort -#include -#include - -int a[1005]; - -int main(){ - #ifdef FILE_IO - freopen("test.in", "r", stdin); - freopen("test.out", "w", stdout); - #endif - // get input to arrays - int n,size = 0; - memset(a, 0, sizeof(a)); - while (scanf("%d", &n) != EOF) - { - a[size] = n; - size++; - } - - #ifdef bob_sort - for (int i = 0; i < size;++i) - { - for (int j = size-1; j > i;--j){ - if(a[j] +#include + +int a[1005]; + +int main(){ + #ifdef FILE_IO + freopen("test.in", "r", stdin); + freopen("test.out", "w", stdout); + #endif + // get input to arrays + int n,size = 0; + memset(a, 0, sizeof(a)); + while (scanf("%d", &n) != EOF) + { + a[size] = n; + size++; + } + + #ifdef bob_sort + for (int i = 0; i < size;++i) + { + for (int j = size-1; j > i;--j){ + if(a[j] /* * @Author: tkzzzzzz6 * @Date: 2026-04-11 10:32:07 diff --git "a/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.cpp" "b/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.cpp" index b607840a..884c3acd 100644 --- "a/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.cpp" +++ "b/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.cpp" @@ -1,21 +1,21 @@ -#include -#include - -#include - -using namespace std; - -class Solution { -public: - int maxArea(vector& height) { - int left = 0, right = height.size() - 1, ans = 0; - while (left < right) - { - int area = (right - left) * min(height[left], height[right]); - if (area > ans) ans = area; - if (height[left] < height[right])left++; - else right--; - } - return ans; - } -}; \ No newline at end of file +#include +#include + +#include + +using namespace std; + +class Solution { +public: + int maxArea(vector& height) { + int left = 0, right = height.size() - 1, ans = 0; + while (left < right) + { + int area = (right - left) * min(height[left], height[right]); + if (area > ans) ans = area; + if (height[left] < height[right])left++; + else right--; + } + return ans; + } +}; diff --git "a/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.py" "b/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.py" index 1073fce4..79124ad2 100644 --- "a/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.py" +++ "b/leetcode/Study Plan/11. \347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/1.py" @@ -1,14 +1,14 @@ -class Solution : - def maxArea(self, height: List[int]) -> int : - left = 0 - right = len(height)-1 - ans = 0 - while left < right: - area = (right - left) * min(height[right],height[left]) - if area > ans: - ans = area - if height[left] < height[right]: - left += 1 - else: - right -= 1 - return ans \ No newline at end of file +class Solution : + def maxArea(self, height: List[int]) -> int : + left = 0 + right = len(height)-1 + ans = 0 + while left < right: + area = (right - left) * min(height[right],height[left]) + if area > ans: + ans = area + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return ans diff --git "a/leetcode/Study Plan/1262. \345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214/1.cpp" "b/leetcode/Study Plan/1262. \345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214/1.cpp" index 3ff02164..59ace884 100644 --- "a/leetcode/Study Plan/1262. \345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214/1.cpp" +++ "b/leetcode/Study Plan/1262. \345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: int maxSumDivThree(vector& nums) { @@ -12,7 +13,7 @@ class Solution { for(int num: nums) { a[num % 3].push_back(num); } - + // 对余数为1和2的数组排序,方便后续取最小值 sort(a[1].begin(), a[1].end()); sort(a[2].begin(), a[2].end()); @@ -22,16 +23,16 @@ class Solution { if(s % 3 == 2) { swap(a[1], a[2]); } - + // 方案1:移除1个最小的余数为1的元素 int ans = a[1].size() ? s - a[1][0] : 0; - + // 方案2:移除2个最小的余数为2的元素(因为2+2=4,4%3=1,相当于移除1) // 取两种方案的最大值 if(a[2].size() > 1) { ans = max(ans, s - a[2][0] - a[2][1]); } - + return ans; } }; diff --git "a/leetcode/Study Plan/1292. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\346\255\243\346\226\271\345\275\242\347\232\204\346\234\200\345\244\247\350\276\271\351\225\277/1.py" "b/leetcode/Study Plan/1292. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\346\255\243\346\226\271\345\275\242\347\232\204\346\234\200\345\244\247\350\276\271\351\225\277/1.py" index 0dcea71c..8b4a2b32 100644 --- "a/leetcode/Study Plan/1292. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\346\255\243\346\226\271\345\275\242\347\232\204\346\234\200\345\244\247\350\276\271\351\225\277/1.py" +++ "b/leetcode/Study Plan/1292. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\346\255\243\346\226\271\345\275\242\347\232\204\346\234\200\345\244\247\350\276\271\351\225\277/1.py" @@ -1,10 +1,10 @@ -""" +""" ABC DEF GHI """ -""" +""" 要得到 EF HI diff --git "a/leetcode/Study Plan/1295_\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227/1.cpp" "b/leetcode/Study Plan/1295_\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227/1.cpp" index c71a4bc8..c7b3791a 100644 --- "a/leetcode/Study Plan/1295_\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227/1.cpp" +++ "b/leetcode/Study Plan/1295_\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227/1.cpp" @@ -1,23 +1,23 @@ -#include -#include - -using namespace std; - -class Solution -{ -public: - int findNumbers(vector &nums) - { - int cnt = 0; - for(int num:nums){ - if(to_string(num).length()%2==0){ - cnt++; - } - } - return cnt; - } -}; - -int main(){ - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +class Solution +{ +public: + int findNumbers(vector &nums) + { + int cnt = 0; + for(int num:nums){ + if(to_string(num).length()%2==0){ + cnt++; + } + } + return cnt; + } +}; + +int main(){ + return 0; +} diff --git "a/leetcode/Study Plan/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" "b/leetcode/Study Plan/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" index ac6aecbe..b1caab37 100644 --- "a/leetcode/Study Plan/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" +++ "b/leetcode/Study Plan/13. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-13 10:20:31 @@ -7,6 +8,6 @@ class Solution { public: int duplicateInArray(vector& nums) { - + } }; diff --git a/leetcode/Study Plan/13/13.cpp b/leetcode/Study Plan/13/13.cpp index 3862b68f..09799ed3 100644 --- a/leetcode/Study Plan/13/13.cpp +++ b/leetcode/Study Plan/13/13.cpp @@ -1,42 +1,42 @@ -#include -#include - -class Solution { -public: - int romanToInt(string s) { - // 创建一个字典,将罗马数字与对应的整数值进行映射 - std::unordered_map roman_to_int = { - {'I', 1}, - {'V', 5}, - {'X', 10}, - {'L', 50}, - {'C', 100}, - {'D', 500}, - {'M', 1000} - }; - - // 初始化结果和前一个字符的值 - int result = 0; - int prev_value = 0; - - // 遍历字符串中的每个字符 - for (char c : s) { - // 获取当前字符对应的整数值 - int current_value = roman_to_int[c]; - - // 如果当前值大于前一个值,说明出现了减法情况,需要减去两倍的前一个值 - if (current_value > prev_value) { - result += current_value - 2 * prev_value; - } else { - // 否则,直接将当前值加到结果中 - result += current_value; - } - - // 更新前一个值为当前值 - prev_value = current_value; - } - - // 返回最终结果 - return result; - } -}; \ No newline at end of file +#include +#include + +class Solution { +public: + int romanToInt(string s) { + // 创建一个字典,将罗马数字与对应的整数值进行映射 + std::unordered_map roman_to_int = { + {'I', 1}, + {'V', 5}, + {'X', 10}, + {'L', 50}, + {'C', 100}, + {'D', 500}, + {'M', 1000} + }; + + // 初始化结果和前一个字符的值 + int result = 0; + int prev_value = 0; + + // 遍历字符串中的每个字符 + for (char c : s) { + // 获取当前字符对应的整数值 + int current_value = roman_to_int[c]; + + // 如果当前值大于前一个值,说明出现了减法情况,需要减去两倍的前一个值 + if (current_value > prev_value) { + result += current_value - 2 * prev_value; + } else { + // 否则,直接将当前值加到结果中 + result += current_value; + } + + // 更新前一个值为当前值 + prev_value = current_value; + } + + // 返回最终结果 + return result; + } +}; diff --git a/leetcode/Study Plan/13/13_2.cpp b/leetcode/Study Plan/13/13_2.cpp index ac8b6066..a96962cc 100644 --- a/leetcode/Study Plan/13/13_2.cpp +++ b/leetcode/Study Plan/13/13_2.cpp @@ -1,31 +1,31 @@ -#include -#include - -class Solution { -private: - unordered_map symbolValues = { - {'I', 1}, - {'V', 5}, - {'X', 10}, - {'L', 50}, - {'C', 100}, - {'D', 500}, - {'M', 1000} - }; - - -public: - int romanToInt(string s) { - int result = 0; - int n = s.length(); - for (int i = 0;i +#include + +class Solution { +private: + unordered_map symbolValues = { + {'I', 1}, + {'V', 5}, + {'X', 10}, + {'L', 50}, + {'C', 100}, + {'D', 500}, + {'M', 1000} + }; + + +public: + int romanToInt(string s) { + int result = 0; + int n = s.length(); + for (int i = 0;i int: - # 创建一个字典,将罗马数字与对应的整数值进行映射 - roman_to_int = { - 'I': 1, - 'V': 5, - 'X': 10, - 'L': 50, - 'C': 100, - 'D': 500, - 'M': 1000 - } - - # 初始化结果和前一个字符的值 - result = 0 - prev_value = 0 - - # 遍历字符串中的每个字符 - for char in s: - # 获取当前字符对应的整数值 - current_value = roman_to_int[char] - - # 如果当前值大于前一个值,说明出现了减法情况,需要减去两倍的前一个值 - if current_value > prev_value: - result += current_value - 2 * prev_value - else: - # 否则,直接将当前值加到结果中 - result += current_value - - # 更新前一个值为当前值 - prev_value = current_value - - # 返回最终结果 - return result \ No newline at end of file +class Solution: + def romanToInt(self, s: str) -> int: + # 创建一个字典,将罗马数字与对应的整数值进行映射 + roman_to_int = { + 'I': 1, + 'V': 5, + 'X': 10, + 'L': 50, + 'C': 100, + 'D': 500, + 'M': 1000 + } + + # 初始化结果和前一个字符的值 + result = 0 + prev_value = 0 + + # 遍历字符串中的每个字符 + for char in s: + # 获取当前字符对应的整数值 + current_value = roman_to_int[char] + + # 如果当前值大于前一个值,说明出现了减法情况,需要减去两倍的前一个值 + if current_value > prev_value: + result += current_value - 2 * prev_value + else: + # 否则,直接将当前值加到结果中 + result += current_value + + # 更新前一个值为当前值 + prev_value = current_value + + # 返回最终结果 + return result diff --git a/leetcode/Study Plan/1342/1.cpp b/leetcode/Study Plan/1342/1.cpp index bfefb738..d30d0408 100644 --- a/leetcode/Study Plan/1342/1.cpp +++ b/leetcode/Study Plan/1342/1.cpp @@ -1,32 +1,32 @@ -#include -#include - -using namespace std; - -class Solution -{ -public: - int numberOfSteps(int num) - { - int cnt = 0; - while(num != 0){ - if(num % 2 == 0){ - num /=2; - cnt++; - } - else{ - num--; - cnt++; - } - } - return cnt; - } -}; - -int main() -{ - Solution s; - int num = 14; - cout << s.numberOfSteps(num) << " "; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +class Solution +{ +public: + int numberOfSteps(int num) + { + int cnt = 0; + while(num != 0){ + if(num % 2 == 0){ + num /=2; + cnt++; + } + else{ + num--; + cnt++; + } + } + return cnt; + } +}; + +int main() +{ + Solution s; + int num = 14; + cout << s.numberOfSteps(num) << " "; + return 0; +} diff --git "a/leetcode/Study Plan/1394. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1.cpp" "b/leetcode/Study Plan/1394. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1.cpp" index cdbff24b..ee52b394 100644 --- "a/leetcode/Study Plan/1394. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1.cpp" +++ "b/leetcode/Study Plan/1394. \346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1.cpp" @@ -1,21 +1,21 @@ -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - int findLucky(vector &arr) - { - unordered_map map; - for (int i = 0; i < arr.size();++i) - map[arr[i]]++; - int max = -1; - for (auto &[key, value] : map) - if (key == value) - max = max > key ? max : key; - return max; - } -}; \ No newline at end of file +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + int findLucky(vector &arr) + { + unordered_map map; + for (int i = 0; i < arr.size();++i) + map[arr[i]]++; + int max = -1; + for (auto &[key, value] : map) + if (key == value) + max = max > key ? max : key; + return max; + } +}; diff --git "a/leetcode/Study Plan/14/14._\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" "b/leetcode/Study Plan/14/14._\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" index 70e3bd78..b4aeeb99 100644 --- "a/leetcode/Study Plan/14/14._\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" +++ "b/leetcode/Study Plan/14/14._\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" @@ -1,119 +1,119 @@ -#纵向扫描法_1 -from typing import List - -class Solution: - def longestCommonPrefix(self, strs: List[str]) -> str: - - # 取第一个字符串作为初始公共前缀 - for i in range(len(strs[0])): - char = strs[0][i] # 当前字符 - for j in range(1, len(strs)): - # 如果当前字符超出当前字符串长度或不匹配 - if i == len(strs[j]) or strs[j][i] != char: - return strs[0][:i] # 返回公共前缀 - - return strs[0] # 所有字符串都相同 - -#纵向扫描法_2 -class Solution: - def longestCommonPrefix(self, strs: List[str]) -> str: - - if not strs: - return "" - - # 取第一个字符串作为初始公共前缀 - prefix,count = strs[0],len(strs) - - for i in range(1,count): - prefix = self.lcp(prefix,strs[i]) - if not prefix: - break - return prefix - - def lcp(self,str1,str2): - length,index = min(len(str1),len(str2)),0 - while index < length and str1[index] == str2[index]: - index += 1 - - return str1[:index] - - -#横向扫描法_1 -from typing import List - -class Solution: - def longestCommonPrefix(self, strs: List[str]) -> str: - - if not strs: - return "" - - prefix = strs[0] # 取第一个字符串作为初始公共前缀 - for i in range(1, len(strs)): - prefix = self.lcp(prefix, strs[i]) # 求当前字符串与当前公共前缀的最长公共前缀 - if not prefix: # 如果当前公共前缀为空 - break - - return prefix - - def lcp(self, str1: str, str2: str) -> str: - length, index = min(len(str1), len(str2)), 0 - while index < length and str1[index] == str2[index]: - index += 1 - return str1[:index] # 返回最长公共前缀 - -#纵向扫描法_2 -class Solution: - def longestCommonPrefix(self, strs: List[str]) -> str: - - if not strs: - return "" - - length,count = len(strs[0]),len(strs) - - for i in range(length): - char = strs[0][i] - if any(i == len(strs[j]) or strs[j][i] != char for j in range(1,count)): - return strs[0][:i] - - return strs[0] - - -#分治法 -class Solution: - def longestCommonPrefix(self, strs: List[str]) -> str: - def lcp(start,end): - if start == end: - return strs[start] - - mid = (start + end) // 2 - left_lcp,right_lcp = lcp(start,mid),lcp(mid+1,end) - min_len = min(len(left_lcp),len(right_lcp)) - for i in range(min_len): - if left_lcp[i] != right_lcp[i]: - return left_lcp[:i] - - return left_lcp[:min_len] - - return "" if not strs else lcp(0,len(strs)-1) - -#二分查找法 -class Solution: - def longestCommonPrefix(self, strs: List[str]) -> str: - - def isCommonPrefix(length): - str0,count = strs[0][:length],len(strs) - return all(strs[i][:length] == str0 for i in range(1,count)) - - if not strs: - return "" - - min_len = min(len(s) for s in strs) - low,high = 0,min_len - while low < high: - mid = (high - low + 1)//2 + low - if isCommonPrefix(mid): - low = mid - else: - high = mid - 1 - - return strs[0][:low] \ No newline at end of file +#纵向扫描法_1 +from typing import List + +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + # 取第一个字符串作为初始公共前缀 + for i in range(len(strs[0])): + char = strs[0][i] # 当前字符 + for j in range(1, len(strs)): + # 如果当前字符超出当前字符串长度或不匹配 + if i == len(strs[j]) or strs[j][i] != char: + return strs[0][:i] # 返回公共前缀 + + return strs[0] # 所有字符串都相同 + +#纵向扫描法_2 +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + if not strs: + return "" + + # 取第一个字符串作为初始公共前缀 + prefix,count = strs[0],len(strs) + + for i in range(1,count): + prefix = self.lcp(prefix,strs[i]) + if not prefix: + break + return prefix + + def lcp(self,str1,str2): + length,index = min(len(str1),len(str2)),0 + while index < length and str1[index] == str2[index]: + index += 1 + + return str1[:index] + + +#横向扫描法_1 +from typing import List + +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + if not strs: + return "" + + prefix = strs[0] # 取第一个字符串作为初始公共前缀 + for i in range(1, len(strs)): + prefix = self.lcp(prefix, strs[i]) # 求当前字符串与当前公共前缀的最长公共前缀 + if not prefix: # 如果当前公共前缀为空 + break + + return prefix + + def lcp(self, str1: str, str2: str) -> str: + length, index = min(len(str1), len(str2)), 0 + while index < length and str1[index] == str2[index]: + index += 1 + return str1[:index] # 返回最长公共前缀 + +#纵向扫描法_2 +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + if not strs: + return "" + + length,count = len(strs[0]),len(strs) + + for i in range(length): + char = strs[0][i] + if any(i == len(strs[j]) or strs[j][i] != char for j in range(1,count)): + return strs[0][:i] + + return strs[0] + + +#分治法 +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + def lcp(start,end): + if start == end: + return strs[start] + + mid = (start + end) // 2 + left_lcp,right_lcp = lcp(start,mid),lcp(mid+1,end) + min_len = min(len(left_lcp),len(right_lcp)) + for i in range(min_len): + if left_lcp[i] != right_lcp[i]: + return left_lcp[:i] + + return left_lcp[:min_len] + + return "" if not strs else lcp(0,len(strs)-1) + +#二分查找法 +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + def isCommonPrefix(length): + str0,count = strs[0][:length],len(strs) + return all(strs[i][:length] == str0 for i in range(1,count)) + + if not strs: + return "" + + min_len = min(len(s) for s in strs) + low,high = 0,min_len + while low < high: + mid = (high - low + 1)//2 + low + if isCommonPrefix(mid): + low = mid + else: + high = mid - 1 + + return strs[0][:low] diff --git a/leetcode/Study Plan/14/14.cpp b/leetcode/Study Plan/14/14.cpp index 6965ad41..6ab5e88f 100644 --- a/leetcode/Study Plan/14/14.cpp +++ b/leetcode/Study Plan/14/14.cpp @@ -1,30 +1,30 @@ -#include -#include -#include - -using namespace std; - -string longestCommonPrefix(vector& strs) { - if (strs.empty()) return ""; - - for (int i = 0; i < strs[0].size(); ++i) { - char c = strs[0][i]; - for (int j = 1; j < strs.size(); ++j) { - if (i == strs[j].size() || strs[j][i] != c) { - return strs[0].substr(0, i); - } - } - } - - return strs[0]; -} - -int main() { - vector strs1 = {"flower", "flow", "flight"}; - cout << "Longest Common Prefix: " << longestCommonPrefix(strs1) << endl; // 输出 "fl" - - vector strs2 = {"dog", "racecar", "car"}; - cout << "Longest Common Prefix: " << longestCommonPrefix(strs2) << endl; // 输出 "" - - return 0; -} +#include +#include +#include + +using namespace std; + +string longestCommonPrefix(vector& strs) { + if (strs.empty()) return ""; + + for (int i = 0; i < strs[0].size(); ++i) { + char c = strs[0][i]; + for (int j = 1; j < strs.size(); ++j) { + if (i == strs[j].size() || strs[j][i] != c) { + return strs[0].substr(0, i); + } + } + } + + return strs[0]; +} + +int main() { + vector strs1 = {"flower", "flow", "flight"}; + cout << "Longest Common Prefix: " << longestCommonPrefix(strs1) << endl; // 输出 "fl" + + vector strs2 = {"dog", "racecar", "car"}; + cout << "Longest Common Prefix: " << longestCommonPrefix(strs2) << endl; // 输出 "" + + return 0; +} diff --git a/leetcode/Study Plan/144/144.cpp b/leetcode/Study Plan/144/144.cpp index ea37a8c5..815cb164 100644 --- a/leetcode/Study Plan/144/144.cpp +++ b/leetcode/Study Plan/144/144.cpp @@ -1,68 +1,68 @@ -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * struct TreeNode *left; - * struct TreeNode *right; - * }; - */ -/** - * Note: The returned array must be malloced, assume caller calls free(). - */ -// 辅助函数,用于前序遍历二叉树 -void test(struct TreeNode *root, int *res, int *returnSize) -{ - // 如果当前节点为空,直接返回 - if (root == 0) - { - return; - } - - // 将当前节点的值存入结果数组,并增加数组大小 - res[(*returnSize)++] = root->val; - // 递归遍历左子树 - test(root->left, res, returnSize); - // 递归遍历右子树 - test(root->right, res, returnSize); -} - -// 主函数:前序遍历二叉树并返回遍历结果 -int *preorderTraversal(struct TreeNode *root, int *returnSize) -{ - // 处理空树情况 - if (root == NULL) - { - *returnSize = 0; - return (int *)malloc(sizeof(int)); // 返回空数组 - } - - // 分配内存(根据实际需求调整大小) - int *res = (int *)malloc(sizeof(int) * 100); - if (res == NULL) - { // 检查内存分配是否成功 - *returnSize = 0; - return NULL; - } - - *returnSize = 0; // 初始化大小 - - // 辅助函数(在函数内部定义) - void preorder(struct TreeNode * node, int *arr, int *size) - { - if (node == NULL) - return; - if (*size >= 100) - return; // 防止数组越界 - - arr[*size] = node->val; - (*size)++; - - preorder(node->left, arr, size); - preorder(node->right, arr, size); - } - - // 执行遍历 - preorder(root, res, returnSize); - - return res; -} \ No newline at end of file +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * struct TreeNode *left; + * struct TreeNode *right; + * }; + */ +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +// 辅助函数,用于前序遍历二叉树 +void test(struct TreeNode *root, int *res, int *returnSize) +{ + // 如果当前节点为空,直接返回 + if (root == 0) + { + return; + } + + // 将当前节点的值存入结果数组,并增加数组大小 + res[(*returnSize)++] = root->val; + // 递归遍历左子树 + test(root->left, res, returnSize); + // 递归遍历右子树 + test(root->right, res, returnSize); +} + +// 主函数:前序遍历二叉树并返回遍历结果 +int *preorderTraversal(struct TreeNode *root, int *returnSize) +{ + // 处理空树情况 + if (root == NULL) + { + *returnSize = 0; + return (int *)malloc(sizeof(int)); // 返回空数组 + } + + // 分配内存(根据实际需求调整大小) + int *res = (int *)malloc(sizeof(int) * 100); + if (res == NULL) + { // 检查内存分配是否成功 + *returnSize = 0; + return NULL; + } + + *returnSize = 0; // 初始化大小 + + // 辅助函数(在函数内部定义) + void preorder(struct TreeNode * node, int *arr, int *size) + { + if (node == NULL) + return; + if (*size >= 100) + return; // 防止数组越界 + + arr[*size] = node->val; + (*size)++; + + preorder(node->left, arr, size); + preorder(node->right, arr, size); + } + + // 执行遍历 + preorder(root, res, returnSize); + + return res; +} diff --git a/leetcode/Study Plan/1480/1.cpp b/leetcode/Study Plan/1480/1.cpp index 681972af..991d6796 100644 --- a/leetcode/Study Plan/1480/1.cpp +++ b/leetcode/Study Plan/1480/1.cpp @@ -1,29 +1,29 @@ -#include -#include - -using namespace std; - -class Solution -{ -public: - vector runningSum(vector &nums) - { - for (int i = 1; i < nums.size(); i++) - { - nums[i] += nums[i-1]; - } - return nums; - } -}; - -int main() -{ - Solution s; - vector nums = {1, 2,3,4}; - vector result = s.runningSum(nums); - for (int i = 0; i < result.size(); i++) - { - cout << result[i] << " "; - } - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +class Solution +{ +public: + vector runningSum(vector &nums) + { + for (int i = 1; i < nums.size(); i++) + { + nums[i] += nums[i-1]; + } + return nums; + } +}; + +int main() +{ + Solution s; + vector nums = {1, 2,3,4}; + vector result = s.runningSum(nums); + for (int i = 0; i < result.size(); i++) + { + cout << result[i] << " "; + } + return 0; +} diff --git "a/leetcode/Study Plan/1486. \346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234/1.py" "b/leetcode/Study Plan/1486. \346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234/1.py" index a1928d25..f27eb6d0 100644 --- "a/leetcode/Study Plan/1486. \346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234/1.py" +++ "b/leetcode/Study Plan/1486. \346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234/1.py" @@ -5,4 +5,4 @@ def xorOperation(self, n: int, start: int) -> int: start += 2 ans ^= start return ans - + diff --git "a/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.cpp" "b/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.cpp" index 419bf5f9..40208569 100644 --- "a/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.cpp" +++ "b/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-12 13:35:46 diff --git "a/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.py" "b/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.py" index c212db8d..9edf56dc 100644 --- "a/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.py" +++ "b/leetcode/Study Plan/15. \344\270\211\346\225\260\344\271\213\345\222\214/1.py" @@ -1,36 +1,36 @@ -class Solution: - def threeSum(self, nums: List[int]) -> List[List[int]]: - nums.sort() - n = len(nums) - # 三元组的顺序不重要 - # let i < j < k - # 答案中不可以包含重复的三元组 - ans = [] - for i in range(n-2): - x = nums[i] - if i > 0 and x == nums[i-1]: - continue - # first imporve - if x + nums[i+1] + nums[i+2] > 0: - break - if x + nums[-1] + nums[-2] < 0: - continue - j = i + 1 - k = n - 1 - while j < k: - s = x + nums[j] + nums[k] - if s < 0: - j += 1 - if s > 0: - k -= 1 - if s == 0: - ans.append([x,nums[j],nums[k]]) - j += 1 - while j < k and nums[j] == nums[j-1]: - j+=1 - k -= 1 - while k>j and nums[k] == nums[k+1]: - k-=1 - return ans - - \ No newline at end of file +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + n = len(nums) + # 三元组的顺序不重要 + # let i < j < k + # 答案中不可以包含重复的三元组 + ans = [] + for i in range(n-2): + x = nums[i] + if i > 0 and x == nums[i-1]: + continue + # first imporve + if x + nums[i+1] + nums[i+2] > 0: + break + if x + nums[-1] + nums[-2] < 0: + continue + j = i + 1 + k = n - 1 + while j < k: + s = x + nums[j] + nums[k] + if s < 0: + j += 1 + if s > 0: + k -= 1 + if s == 0: + ans.append([x,nums[j],nums[k]]) + j += 1 + while j < k and nums[j] == nums[j-1]: + j+=1 + k -= 1 + while k>j and nums[k] == nums[k+1]: + k-=1 + return ans + + diff --git "a/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/1.cpp" "b/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/1.cpp" index 2e635b81..95003d59 100644 --- "a/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/1.cpp" +++ "b/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: int numIdenticalPairs(vector& nums) { diff --git "a/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/2.cpp" "b/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/2.cpp" index 318ed98c..b997b17d 100644 --- "a/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/2.cpp" +++ "b/leetcode/Study Plan/1512. \345\245\275\346\225\260\345\257\271\347\232\204\346\225\260\347\233\256/2.cpp" @@ -1,3 +1,4 @@ +#include class Solution{ public: int numIdenticalPairs(vector& nums) { diff --git "a/leetcode/Study Plan/1513. \344\273\205\345\220\253 1 \347\232\204\345\255\220\344\270\262\346\225\260/1.py" "b/leetcode/Study Plan/1513. \344\273\205\345\220\253 1 \347\232\204\345\255\220\344\270\262\346\225\260/1.py" index ad2911ba..b7426e5d 100644 --- "a/leetcode/Study Plan/1513. \344\273\205\345\220\253 1 \347\232\204\345\255\220\344\270\262\346\225\260/1.py" +++ "b/leetcode/Study Plan/1513. \344\273\205\345\220\253 1 \347\232\204\345\255\220\344\270\262\346\225\260/1.py" @@ -18,4 +18,4 @@ def numSub(self, s: str) -> int: # 处理字符串末尾如果是连续的 1 时的情况 total += consecutive * (consecutive + 1) // 2 # 结果需要对 10^9+7 取模 - return total % (10 ** 9 + 7) \ No newline at end of file + return total % (10 ** 9 + 7) diff --git "a/leetcode/Study Plan/1523. \345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256/1.cpp" "b/leetcode/Study Plan/1523. \345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256/1.cpp" index 5a5d7f9e..9fd10a36 100644 --- "a/leetcode/Study Plan/1523. \345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256/1.cpp" +++ "b/leetcode/Study Plan/1523. \345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256/1.cpp" @@ -1,11 +1,11 @@ class Solution { public: - + int countOdds(int low, int high) { return pre(high) - pre(low-1); } int pre(int x) { - return ( x+1) >> 1; + return ( x+1) >> 1; } }; diff --git "a/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/1_\346\236\232\344\270\276.cpp" "b/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/1_\346\236\232\344\270\276.cpp" index 3b9f830e..2b610851 100644 --- "a/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/1_\346\236\232\344\270\276.cpp" +++ "b/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/1_\346\236\232\344\270\276.cpp" @@ -1,37 +1,37 @@ -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - int countGoodTriplets(vector &arr, int a, int b, int c) - { - int cnt = 0; - for (int i = 0; i < arr.size(); ++i) - { - for (int j = i + 1; j < arr.size(); ++j) - { - for (int k = j + 1; k < arr.size(); ++k) - { - bool judge = (abs(arr[i] - arr[j]) <= a) && (abs(arr[k] - arr[j]) <= b) && (abs(arr[i] - arr[k]) <= c); - if (judge) - ++cnt; - } - } - } - return cnt; - } -}; - -int main() -{ - Solution s; - vector arr(6); - arr = {3, 0, 1, 1, 9, 7}; - cout << s.countGoodTriplets(arr, 7, 2, 3) << endl; - cout << (s.countGoodTriplets(arr, 7, 2, 3) == 4) << endl; - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + int countGoodTriplets(vector &arr, int a, int b, int c) + { + int cnt = 0; + for (int i = 0; i < arr.size(); ++i) + { + for (int j = i + 1; j < arr.size(); ++j) + { + for (int k = j + 1; k < arr.size(); ++k) + { + bool judge = (abs(arr[i] - arr[j]) <= a) && (abs(arr[k] - arr[j]) <= b) && (abs(arr[i] - arr[k]) <= c); + if (judge) + ++cnt; + } + } + } + return cnt; + } +}; + +int main() +{ + Solution s; + vector arr(6); + arr = {3, 0, 1, 1, 9, 7}; + cout << s.countGoodTriplets(arr, 7, 2, 3) << endl; + cout << (s.countGoodTriplets(arr, 7, 2, 3) == 4) << endl; + return 0; +} diff --git "a/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/2_\344\274\230\345\214\226\346\236\232\344\270\276.cpp" "b/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/2_\344\274\230\345\214\226\346\236\232\344\270\276.cpp" index 9db1c25a..efc38963 100644 --- "a/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/2_\344\274\230\345\214\226\346\236\232\344\270\276.cpp" +++ "b/leetcode/Study Plan/1534_\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/2_\344\274\230\345\214\226\346\236\232\344\270\276.cpp" @@ -1,54 +1,54 @@ -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - int countGoodTriplets(vector &arr, int a, int b, int c) - { - int ans = 0, n = arr.size(); - vector sum(1001, 0); - // 从j=0开始枚举是因为我们需要构建对应的前缀和数组 - for (int j = 0; j < n; ++j) - { - for (int k = j + 1; k < n; ++k) - { - if (abs(arr[j] - arr[k]) <= b) - { - int lj = arr[j] - a, rj = arr[j] + a; - int lk = arr[k] - c, rk = arr[k] + c; - int l = max(0, max(lj, lk)), r = min(1000, min(rj, rk)); - if (l <= r) - { - if (l == 0) - { - ans += sum[r]; - } - else - { - ans += sum[r] - sum[l - 1]; - } - } - } - } - for (int k = arr[j]; k <= 1000; ++k) - { - ++sum[k]; - } - } - return ans; - } -}; - -int main() -{ - Solution s; - vector arr(6); - arr = {3, 0, 1, 1, 9, 7}; - cout << s.countGoodTriplets(arr, 7, 2, 3) << endl; - cout << (s.countGoodTriplets(arr, 7, 2, 3) == 4) << endl; - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + int countGoodTriplets(vector &arr, int a, int b, int c) + { + int ans = 0, n = arr.size(); + vector sum(1001, 0); + // 从j=0开始枚举是因为我们需要构建对应的前缀和数组 + for (int j = 0; j < n; ++j) + { + for (int k = j + 1; k < n; ++k) + { + if (abs(arr[j] - arr[k]) <= b) + { + int lj = arr[j] - a, rj = arr[j] + a; + int lk = arr[k] - c, rk = arr[k] + c; + int l = max(0, max(lj, lk)), r = min(1000, min(rj, rk)); + if (l <= r) + { + if (l == 0) + { + ans += sum[r]; + } + else + { + ans += sum[r] - sum[l - 1]; + } + } + } + } + for (int k = arr[j]; k <= 1000; ++k) + { + ++sum[k]; + } + } + return ans; + } +}; + +int main() +{ + Solution s; + vector arr(6); + arr = {3, 0, 1, 1, 9, 7}; + cout << s.countGoodTriplets(arr, 7, 2, 3) << endl; + cout << (s.countGoodTriplets(arr, 7, 2, 3) == 4) << endl; + return 0; +} diff --git "a/leetcode/Study Plan/1536. \346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260/1.py" "b/leetcode/Study Plan/1536. \346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260/1.py" index 2a76b76d..d3013683 100644 --- "a/leetcode/Study Plan/1536. \346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260/1.py" +++ "b/leetcode/Study Plan/1536. \346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260/1.py" @@ -15,7 +15,7 @@ def minSwaps(self, grid: List[List[int]]) -> int: break ans = 0 - for i in range(n-1): + for i in range(n-1): need_zeros = n - 1 - i for j in range(i,n): if tail_zeros[j] >= need_zeros: @@ -24,5 +24,5 @@ def minSwaps(self, grid: List[List[int]]) -> int: break else: return -1 - return ans + return ans diff --git "a/leetcode/Study Plan/1578. \344\275\277\347\273\263\345\255\220\345\217\230\346\210\220\345\275\251\350\211\262\347\232\204\346\234\200\347\237\255\346\227\266\351\227\264/1.py" "b/leetcode/Study Plan/1578. \344\275\277\347\273\263\345\255\220\345\217\230\346\210\220\345\275\251\350\211\262\347\232\204\346\234\200\347\237\255\346\227\266\351\227\264/1.py" index d6846781..27b9a7cd 100644 --- "a/leetcode/Study Plan/1578. \344\275\277\347\273\263\345\255\220\345\217\230\346\210\220\345\275\251\350\211\262\347\232\204\346\234\200\347\237\255\346\227\266\351\227\264/1.py" +++ "b/leetcode/Study Plan/1578. \344\275\277\347\273\263\345\255\220\345\217\230\346\210\220\345\275\251\350\211\262\347\232\204\346\234\200\347\237\255\346\227\266\351\227\264/1.py" @@ -18,9 +18,9 @@ def minCost(self, colors: str, neededTime: List[int]) -> int: maxValue = max(maxValue, neededTime[i]) # 删除成本最高的颜色,这里的成本指的是序号的长度 total += neededTime[i] i += 1 - + res += total - maxValue - + return res ======= class Solution: @@ -42,8 +42,8 @@ def minCost(self, colors: str, neededTime: List[int]) -> int: maxValue = max(maxValue, neededTime[i]) # 删除成本最高的颜色,这里的成本指的是序号的长度 total += neededTime[i] i += 1 - + res += total - maxValue - + return res >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 diff --git "a/leetcode/Study Plan/1582. \344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256/2.py" "b/leetcode/Study Plan/1582. \344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256/2.py" index 8d9661ca..8eaa9811 100644 --- "a/leetcode/Study Plan/1582. \344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256/2.py" +++ "b/leetcode/Study Plan/1582. \344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256/2.py" @@ -25,4 +25,4 @@ def numSpecial(self, mat: List[List[int]]) -> int: ans += see1 return ans - + diff --git a/leetcode/Study Plan/1588/1.cpp b/leetcode/Study Plan/1588/1.cpp index abe16afd..7db12f6a 100644 --- a/leetcode/Study Plan/1588/1.cpp +++ b/leetcode/Study Plan/1588/1.cpp @@ -1,22 +1,23 @@ -class Solution -{ -public: - int sumOddLengthSubarrays(vector &arr) - { - int n = arr.size(); - int sum = 0; - int sub_arr_length = 1; - while (sub_arr_length <= n) - { - for (int i = 0; i < n - sub_arr_length + 1; i++) - { - for (int j = i; j < i + sub_arr_length; j++) - { - sum += arr[j]; - } - } - sub_arr_length += 2; - } - return sum; - } -}; \ No newline at end of file +#include +class Solution +{ +public: + int sumOddLengthSubarrays(vector &arr) + { + int n = arr.size(); + int sum = 0; + int sub_arr_length = 1; + while (sub_arr_length <= n) + { + for (int i = 0; i < n - sub_arr_length + 1; i++) + { + for (int j = i; j < i + sub_arr_length; j++) + { + sum += arr[j]; + } + } + sub_arr_length += 2; + } + return sum; + } +}; diff --git "a/leetcode/Study Plan/1590. \344\275\277\346\225\260\347\273\204\345\222\214\350\203\275\350\242\253 P \346\225\264\351\231\244/1.py" "b/leetcode/Study Plan/1590. \344\275\277\346\225\260\347\273\204\345\222\214\350\203\275\350\242\253 P \346\225\264\351\231\244/1.py" index 121f0938..43fadea0 100644 --- "a/leetcode/Study Plan/1590. \344\275\277\346\225\260\347\273\204\345\222\214\350\203\275\350\242\253 P \346\225\264\351\231\244/1.py" +++ "b/leetcode/Study Plan/1590. \344\275\277\346\225\260\347\273\204\345\222\214\350\203\275\350\242\253 P \346\225\264\351\231\244/1.py" @@ -3,35 +3,35 @@ def minSubarray(self, nums: List[int], p: int) -> int: # 计算前缀和数组,s[i] 表示 nums[0] 到 nums[i-1] 的和 # initial=0 表示前缀和数组的第一个元素为 0 s = list(accumulate(nums,initial = 0)) - + # x 表示整个数组和模 p 的余数 # 如果我们需要删除一个子数组使得剩余和能被 p 整除 # 那么被删除的子数组的和模 p 的余数必须等于 x x = s[-1] % p - + # 如果整个数组和已经能被 p 整除,不需要删除任何元素 if x == 0: return 0 - + # ans 记录最短子数组长度,初始化为数组长度 # n 为数组长度 ans = n = len(nums) - + # last 字典记录每个前缀和模 p 的余数最后出现的位置 last = {} - + # 遍历前缀和数组 for i,v in enumerate(s): # 记录当前前缀和模 p 的余数对应的位置 last[v%p] = i - + # 如果我们要删除子数组 nums[j+1...i-1] # 那么删除后的和为 s[n] - (s[i] - s[j]) = s[n] - s[i] + s[j] # 要使这个和能被 p 整除,需要 (s[n] - s[i] + s[j]) % p == 0 # 即 (x - v + s[j]) % p == 0,也就是 s[j] % p == (v - x) % p # 查找是否存在满足条件的前缀和位置 j j = last.get((v-x)%p,-n) - + # 如果找到了满足条件的位置 j,更新最短长度 # 子数组长度为 i - j(因为删除的是 nums[j+1...i-1]) ans = min(ans,i-j) diff --git "a/leetcode/Study Plan/1611. \344\275\277\346\225\264\346\225\260\345\217\230\344\270\272 0 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" "b/leetcode/Study Plan/1611. \344\275\277\346\225\264\346\225\260\345\217\230\344\270\272 0 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" index c99a8bf5..afc20e86 100644 --- "a/leetcode/Study Plan/1611. \344\275\277\346\225\264\346\225\260\345\217\230\344\270\272 0 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" +++ "b/leetcode/Study Plan/1611. \344\275\277\346\225\264\346\225\260\345\217\230\344\270\272 0 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" @@ -7,4 +7,4 @@ def minimumOneBitOperations(self, n: int) -> int: x = len(bin(n)) - 3 # bin(n)形如'0b100...',减去'0b'和第一个'1'前面的位数 # 利用格雷码及其反演性质,递推公式: # (1<<(x+1))-1 表示全1(共x+1位)的数,减去递归剩余部分的答案 - return (1 << (x + 1)) - 1 - self.minimumOneBitOperations(n - (1 << x)) \ No newline at end of file + return (1 << (x + 1)) - 1 - self.minimumOneBitOperations(n - (1 << x)) diff --git "a/leetcode/Study Plan/1653. \344\275\277\345\255\227\347\254\246\344\270\262\345\271\263\350\241\241\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\254\241\346\225\260/1.py" "b/leetcode/Study Plan/1653. \344\275\277\345\255\227\347\254\246\344\270\262\345\271\263\350\241\241\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\254\241\346\225\260/1.py" index b1f2e06c..9e5c287f 100644 --- "a/leetcode/Study Plan/1653. \344\275\277\345\255\227\347\254\246\344\270\262\345\271\263\350\241\241\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\254\241\346\225\260/1.py" +++ "b/leetcode/Study Plan/1653. \344\275\277\345\255\227\347\254\246\344\270\262\345\271\263\350\241\241\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\254\241\346\225\260/1.py" @@ -12,5 +12,5 @@ def minimumDeletions(self, s: str) -> int: delete -= 1 if c == 'a' else -1 if delete < ans: ans = delete - + return ans diff --git a/leetcode/Study Plan/1672/1.cpp b/leetcode/Study Plan/1672/1.cpp index efd3c7a8..e27f986b 100644 --- a/leetcode/Study Plan/1672/1.cpp +++ b/leetcode/Study Plan/1672/1.cpp @@ -1,33 +1,33 @@ -#include -#include - -using namespace std; - -class Solution -{ -public: - int maximumWealth(vector> &accounts) - { - int max = 0; - for (int i = 0; i < accounts.size(); i++) - { - int sum = 0; - for (int j = 0; j < accounts[i].size(); j++) - { - sum = sum + accounts[i][j]; - } - if (sum > max) - max = sum; - } - return max; - } -}; - - -int main() -{ - Solution s; - vector> accounts = {{1, 2, 3}, {3, 2, 1}}; - cout << s.maximumWealth(accounts) << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +class Solution +{ +public: + int maximumWealth(vector> &accounts) + { + int max = 0; + for (int i = 0; i < accounts.size(); i++) + { + int sum = 0; + for (int j = 0; j < accounts[i].size(); j++) + { + sum = sum + accounts[i][j]; + } + if (sum > max) + max = sum; + } + return max; + } +}; + + +int main() +{ + Solution s; + vector> accounts = {{1, 2, 3}, {3, 2, 1}}; + cout << s.maximumWealth(accounts) << endl; + return 0; +} diff --git "a/leetcode/Study Plan/1680. \350\277\236\346\216\245\350\277\236\347\273\255\344\272\214\350\277\233\345\210\266\346\225\260\345\255\227/1.py" "b/leetcode/Study Plan/1680. \350\277\236\346\216\245\350\277\236\347\273\255\344\272\214\350\277\233\345\210\266\346\225\260\345\255\227/1.py" index 30811285..2c3143c5 100644 --- "a/leetcode/Study Plan/1680. \350\277\236\346\216\245\350\277\236\347\273\255\344\272\214\350\277\233\345\210\266\346\225\260\345\255\227/1.py" +++ "b/leetcode/Study Plan/1680. \350\277\236\346\216\245\350\277\236\347\273\255\344\272\214\350\277\233\345\210\266\346\225\260\345\255\227/1.py" @@ -5,4 +5,4 @@ def concatenatedBinary(self, n: int) -> int: for i in range(1,n+1): w = i.bit_length() ans = (ans << w | i)%MOD - return ans \ No newline at end of file + return ans diff --git "a/leetcode/Study Plan/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1.cpp" "b/leetcode/Study Plan/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1.cpp" index b4fc09f2..672c366c 100644 --- "a/leetcode/Study Plan/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1.cpp" +++ "b/leetcode/Study Plan/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1.cpp" @@ -1,5 +1,5 @@ -#include - -using namespace std; - -int main(){} \ No newline at end of file +#include + +using namespace std; + +int main(){} diff --git "a/leetcode/Study Plan/1716. \350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261/1.py" "b/leetcode/Study Plan/1716. \350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261/1.py" index f38b7232..b0585e0f 100644 --- "a/leetcode/Study Plan/1716. \350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261/1.py" +++ "b/leetcode/Study Plan/1716. \350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261/1.py" @@ -1,9 +1,9 @@ - -# 周一到周日之和是 7*(1+2+..+k) + (1+2+3+4+5+6)*k = 7*k*(k+1)//2+21*k - -# 假如k周余下s天。那么需要再加上 k*s+s*(s+1)//2 - -class Solution: - def totalMoney(self, n: int) -> int: - k,s = divmod(n,7) - return 7*k*(k+1)//2 + 21*k + k*s + s*(s+1)//2 + +# 周一到周日之和是 7*(1+2+..+k) + (1+2+3+4+5+6)*k = 7*k*(k+1)//2+21*k + +# 假如k周余下s天。那么需要再加上 k*s+s*(s+1)//2 + +class Solution: + def totalMoney(self, n: int) -> int: + k,s = divmod(n,7) + return 7*k*(k+1)//2 + 21*k + k*s + s*(s+1)//2 diff --git a/leetcode/Study Plan/1768/1.py b/leetcode/Study Plan/1768/1.py index 096eca97..e5adde4d 100644 --- a/leetcode/Study Plan/1768/1.py +++ b/leetcode/Study Plan/1768/1.py @@ -1,12 +1,12 @@ -class Solution: - def mergeAlternately(self, word1: str, word2: str) -> str: - str = "" - for i in range(max(len(word1),len(word2))): - if i < len(word1): - str += word1[i] - if i < len(word2): - str += word2[i] - return str - -s = Solution() -print(s.mergeAlternately("abc", "pqr")) \ No newline at end of file +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + str = "" + for i in range(max(len(word1),len(word2))): + if i < len(word1): + str += word1[i] + if i < len(word2): + str += word2[i] + return str + +s = Solution() +print(s.mergeAlternately("abc", "pqr")) diff --git "a/leetcode/Study Plan/18. \345\233\233\346\225\260\344\271\213\345\222\214/1.cpp" "b/leetcode/Study Plan/18. \345\233\233\346\225\260\344\271\213\345\222\214/1.cpp" index 240d8d63..08655065 100644 --- "a/leetcode/Study Plan/18. \345\233\233\346\225\260\344\271\213\345\222\214/1.cpp" +++ "b/leetcode/Study Plan/18. \345\233\233\346\225\260\344\271\213\345\222\214/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-12 14:05:03 diff --git "a/leetcode/Study Plan/1895. \346\234\200\345\244\247\347\232\204\345\271\273\346\226\271/1.py" "b/leetcode/Study Plan/1895. \346\234\200\345\244\247\347\232\204\345\271\273\346\226\271/1.py" index 5834acaf..234e51a5 100644 --- "a/leetcode/Study Plan/1895. \346\234\200\345\244\247\347\232\204\345\271\273\346\226\271/1.py" +++ "b/leetcode/Study Plan/1895. \346\234\200\345\244\247\347\232\204\345\271\273\346\226\271/1.py" @@ -1,29 +1,29 @@ class Solution: def largestMagicSquare(self, grid: List[List[int]]) -> int: m,n = len(grid),len(grid[0]) - + row_sum = [[0]*(n+1) for _ in range(m)] #向右前缀和 col_sum = [[0]*(n+1) for _ in range(m+1)] #向下前缀和 diag_sum = [[0]*(n+1) for _ in range(m+1)] #主对角线前缀和 anti_sum = [[0]*(n+1) for _ in range(m+1)] #反对角线前缀和 - + for i,row in enumerate(grid): for j,x in enumerate(row): row_sum[i][j+1] = row_sum[i][j] + x - col_sum[i+1][j] = col_sum[i][j] + x + col_sum[i+1][j] = col_sum[i][j] + x diag_sum[i+1][j+1] = diag_sum[i][j] + x anti_sum[i+1][j] = anti_sum[i][j+1] + x - + # k*k子矩阵的左上角为(i-k,j-k),右下角为(i-1,j-1) for k in range(min(m,n),0,-1): for i in range(k,m+1): for j in range(k,n+1): # 子矩阵主对角线和 s = diag_sum[i][j] - diag_sum[i-k][j-k] - + # 子矩阵反对角线和 # 子矩阵每行和每列的和都是 s if anti_sum[i][j-k] - anti_sum[i-k][j] == s and \ all(row_sum[r][j] - row_sum[r][j-k] == s for r in range(i-k,i)) and \ all(col_sum[i][c] - col_sum[i-k][c] == s for c in range(j-k,j)): - return k \ No newline at end of file + return k diff --git a/leetcode/Study Plan/1922/1.cpp b/leetcode/Study Plan/1922/1.cpp index 4bc2e01e..730b0d63 100644 --- a/leetcode/Study Plan/1922/1.cpp +++ b/leetcode/Study Plan/1922/1.cpp @@ -1,18 +1,18 @@ -#include - -using namespace std; - - - -class Solution -{ -public: - bool judgeGoodNumbers(long long num){ - num - } - - int countGoodNumbers(long long n) - { - - } -}; \ No newline at end of file +#include + +using namespace std; + + + +class Solution +{ +public: + bool judgeGoodNumbers(long long num){ + num + } + + int countGoodNumbers(long long n) + { + + } +}; diff --git "a/leetcode/Study Plan/1930. \351\225\277\345\272\246\344\270\272 3 \347\232\204\344\270\215\345\220\214\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/1.py" "b/leetcode/Study Plan/1930. \351\225\277\345\272\246\344\270\272 3 \347\232\204\344\270\215\345\220\214\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/1.py" index 52111bf7..2cca56f4 100644 --- "a/leetcode/Study Plan/1930. \351\225\277\345\272\246\344\270\272 3 \347\232\204\344\270\215\345\220\214\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/1.py" +++ "b/leetcode/Study Plan/1930. \351\225\277\345\272\246\344\270\272 3 \347\232\204\344\270\215\345\220\214\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/1.py" @@ -14,4 +14,4 @@ def countPalindromicSubsequence(self, s: str) -> int: for k in range(l+1,r): charset.add(s[k]) ans += len(charset) - return ans \ No newline at end of file + return ans diff --git "a/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/1.py" "b/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/1.py" index 897a40ed..d87aab40 100644 --- "a/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/1.py" +++ "b/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/1.py" @@ -8,4 +8,4 @@ def dfs(i:int) ->int: return 0 return max(dfs(i - 1),dfs(i-2)+nums[i]) - return dfs(n - 1) \ No newline at end of file + return dfs(n - 1) diff --git "a/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/2.py" "b/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/2.py" index 757854c6..deef4fbb 100644 --- "a/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/2.py" +++ "b/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/2.py" @@ -3,4 +3,4 @@ def rob(self, nums: List[int]) -> int: cache = [0] * (len(nums)+2) for i,x in enumerate(nums): cache[i+2] = max(cache[i+1],cache[i]+x) - return cache[-1] \ No newline at end of file + return cache[-1] diff --git "a/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/3.py" "b/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/3.py" index 599a3b52..653382d0 100644 --- "a/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/3.py" +++ "b/leetcode/Study Plan/198. \346\211\223\345\256\266\345\212\253\350\210\215/3.py" @@ -3,4 +3,4 @@ def rob(self, nums: List[int]) -> int: f0 = f1 = 0 for i,x in enumerate(nums): f0, f1 = f1, max(f1, f0 + x) - return f1 \ No newline at end of file + return f1 diff --git "a/leetcode/Study Plan/20. \346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/20_\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" "b/leetcode/Study Plan/20. \346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/20_\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" index 2a42339e..accfa32c 100644 --- "a/leetcode/Study Plan/20. \346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/20_\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" +++ "b/leetcode/Study Plan/20. \346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/20_\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" @@ -1,37 +1,37 @@ -class Solution: - def isValid(self, s: str) -> bool: - stack = [] - bracket_map = {")": "(", "}": "{", "]": "["} - - for char in s: - if char in bracket_map: # 如果是右括号 - if not stack or stack[-1] != bracket_map[char]: - return False - stack.pop() - else: # 如果是左括号 - stack.append(char) - - return len(stack) == 0 # 栈应该为空,所有左括号都被匹配 - -#leetcode官方解法 -class Solution: - def isValid(self, s: str) -> bool: - if len(s) % 2 == 1: - return False - - pairs = { - ")": "(", - "}": "{", - "]": "[", - } - - stack = list() - for ch in s: - if ch in pairs: - if not stack or stack[-1] != pairs[ch]: - return False - stack.pop() - else: - stack.append(ch) - - return not stack \ No newline at end of file +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_map = {")": "(", "}": "{", "]": "["} + + for char in s: + if char in bracket_map: # 如果是右括号 + if not stack or stack[-1] != bracket_map[char]: + return False + stack.pop() + else: # 如果是左括号 + stack.append(char) + + return len(stack) == 0 # 栈应该为空,所有左括号都被匹配 + +#leetcode官方解法 +class Solution: + def isValid(self, s: str) -> bool: + if len(s) % 2 == 1: + return False + + pairs = { + ")": "(", + "}": "{", + "]": "[", + } + + stack = list() + for ch in s: + if ch in pairs: + if not stack or stack[-1] != pairs[ch]: + return False + stack.pop() + else: + stack.append(ch) + + return not stack diff --git "a/leetcode/Study Plan/2011. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/1.py" "b/leetcode/Study Plan/2011. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/1.py" index 7d7147d4..7eadef7c 100644 --- "a/leetcode/Study Plan/2011. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/1.py" +++ "b/leetcode/Study Plan/2011. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/1.py" @@ -1,11 +1,11 @@ -class Solution: - def finalValueAfterOperations(self, operations: List[str]) -> int: - ans = 0 - for s in operations: - if(s=='++X' or s == 'X++'): - ans+=1 - else: - ans-=1 - - return ans -c \ No newline at end of file +class Solution: + def finalValueAfterOperations(self, operations: List[str]) -> int: + ans = 0 + for s in operations: + if(s=='++X' or s == 'X++'): + ans+=1 + else: + ans-=1 + + return ans +c diff --git "a/leetcode/Study Plan/2043.\347\256\200\346\230\223\351\223\266\350\241\214\347\263\273\347\273\237/1.py" "b/leetcode/Study Plan/2043.\347\256\200\346\230\223\351\223\266\350\241\214\347\263\273\347\273\237/1.py" index d068ac0d..95860352 100644 --- "a/leetcode/Study Plan/2043.\347\256\200\346\230\223\351\223\266\350\241\214\347\263\273\347\273\237/1.py" +++ "b/leetcode/Study Plan/2043.\347\256\200\346\230\223\351\223\266\350\241\214\347\263\273\347\273\237/1.py" @@ -1,31 +1,31 @@ -class Bank: - - def __init__(self, balance: List[int]): - self.balance = balance - - def transfer(self, account1: int, account2: int, money: int) -> bool: - if account1 > len(self.balance) or account2 > len(self.balance) or self.balance[account1 -1] < money: - return False - self.balance[account1 - 1] -= money - self.balance[account2 - 1] += money - return True - - def deposit(self, account: int, money: int) -> bool: #存钱 - if account > len(self.balance): - return False - self.balance[account - 1] += money - return True - - def withdraw(self, account: int, money: int) -> bool: #取钱 - if account > len(self.balance) or self.balance[account - 1] < money: - return False - self.balance[account -1] -= money - return True - - -# Your Bank object will be instantiated and called as such: -# obj = Bank(balance) -# param_1 = obj.transfer(account1,account2,money) -# param_2 = obj.deposit(account,money) -# param_3 = obj.withdraw(account,money) - +class Bank: + + def __init__(self, balance: List[int]): + self.balance = balance + + def transfer(self, account1: int, account2: int, money: int) -> bool: + if account1 > len(self.balance) or account2 > len(self.balance) or self.balance[account1 -1] < money: + return False + self.balance[account1 - 1] -= money + self.balance[account2 - 1] += money + return True + + def deposit(self, account: int, money: int) -> bool: #存钱 + if account > len(self.balance): + return False + self.balance[account - 1] += money + return True + + def withdraw(self, account: int, money: int) -> bool: #取钱 + if account > len(self.balance) or self.balance[account - 1] < money: + return False + self.balance[account -1] -= money + return True + + +# Your Bank object will be instantiated and called as such: +# obj = Bank(balance) +# param_1 = obj.transfer(account1,account2,money) +# param_2 = obj.deposit(account,money) +# param_3 = obj.withdraw(account,money) + diff --git "a/leetcode/Study Plan/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/1.py" "b/leetcode/Study Plan/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/1.py" index b548ef66..b5e87bc3 100644 --- "a/leetcode/Study Plan/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/1.py" +++ "b/leetcode/Study Plan/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/1.py" @@ -1,22 +1,22 @@ -class Solution: - def minSubArrayLen(self, target: int, nums: List[int]) -> int: - n = len(nums) - ans = n+1 - - s = 0 - left = 0 - - for right,x in enumerate(nums): - s+=x - # while(s-target >= 0): - # ans = min(ans,right - left+1) - # s-=nums[left] - # left += 1 - - while s- nums[left] >= target: - s-=nums[left] - left += 1 - if(s-target >= 0): - ans = min(ans,right-left +1) - return ans if ans <= n else 0 - +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + n = len(nums) + ans = n+1 + + s = 0 + left = 0 + + for right,x in enumerate(nums): + s+=x + # while(s-target >= 0): + # ans = min(ans,right - left+1) + # s-=nums[left] + # left += 1 + + while s- nums[left] >= target: + s-=nums[left] + left += 1 + if(s-target >= 0): + ans = min(ans,right-left +1) + return ans if ans <= n else 0 + diff --git "a/leetcode/Study Plan/21/21_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" "b/leetcode/Study Plan/21/21_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" index 83b369f4..8460f25b 100644 --- "a/leetcode/Study Plan/21/21_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" +++ "b/leetcode/Study Plan/21/21_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" @@ -1,41 +1,41 @@ -# 定义单链表的节点类(注释掉的代码) -from typing import Optional -class ListNode: - def __init__(self, val=0, next=None): - self.val = val - self.next = next - -# 定义解决方案类 -class Solution: - # 定义合并两个有序链表的方法 - def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: - # 创建一个哑节点作为合并后链表的起点 - dummy = ListNode() - # 当前节点指向哑节点 - current = dummy - - # 当两个链表都不为空时,进行合并操作 - while list1 and list2: - # 比较两个链表当前节点的值 - if list1.val < list2.val: - # 如果list1的值小于list2的值,将list1的节点连接到当前节点 - current.next = list1 - # 移动list1到下一个节点 - list1 = list1.next - else: - # 如果list2的值小于或等于list1的值,将list2的节点连接到当前节点 - current.next = list2 - # 移动list2到下一个节点 - list2 = list2.next - # 移动当前节点到下一个节点 - current = current.next - - # 如果list1还有剩余节点,将其连接到当前节点 - if list1: - current.next = list1 - # 如果list2还有剩余节点,将其连接到当前节点 - elif list2: - current.next = list2 - - # 返回合并后的链表的起点(哑节点的下一个节点) - return dummy.next \ No newline at end of file +# 定义单链表的节点类(注释掉的代码) +from typing import Optional +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +# 定义解决方案类 +class Solution: + # 定义合并两个有序链表的方法 + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + # 创建一个哑节点作为合并后链表的起点 + dummy = ListNode() + # 当前节点指向哑节点 + current = dummy + + # 当两个链表都不为空时,进行合并操作 + while list1 and list2: + # 比较两个链表当前节点的值 + if list1.val < list2.val: + # 如果list1的值小于list2的值,将list1的节点连接到当前节点 + current.next = list1 + # 移动list1到下一个节点 + list1 = list1.next + else: + # 如果list2的值小于或等于list1的值,将list2的节点连接到当前节点 + current.next = list2 + # 移动list2到下一个节点 + list2 = list2.next + # 移动当前节点到下一个节点 + current = current.next + + # 如果list1还有剩余节点,将其连接到当前节点 + if list1: + current.next = list1 + # 如果list2还有剩余节点,将其连接到当前节点 + elif list2: + current.next = list2 + + # 返回合并后的链表的起点(哑节点的下一个节点) + return dummy.next diff --git "a/leetcode/Study Plan/21/21_\346\232\264\345\212\233\350\247\243\346\263\225_\350\277\255\344\273\243.py" "b/leetcode/Study Plan/21/21_\346\232\264\345\212\233\350\247\243\346\263\225_\350\277\255\344\273\243.py" index e703da12..52fda1dd 100644 --- "a/leetcode/Study Plan/21/21_\346\232\264\345\212\233\350\247\243\346\263\225_\350\277\255\344\273\243.py" +++ "b/leetcode/Study Plan/21/21_\346\232\264\345\212\233\350\247\243\346\263\225_\350\277\255\344\273\243.py" @@ -1,39 +1,39 @@ -from typing import Optional - -# 定义链表节点类 -class ListNode: - def __init__(self, val=0, next=None): - self.val = val # 节点的值 - self.next = next # 指向下一个节点的指针 - -# 定义解决方案类 -class Solution: - # 合并两个有序链表的函数 - def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: - # 创建一个哑节点作为合并后链表的起点 - prehead = ListNode(-1) - - # prev指针用于构建新的链表 - prev = prehead - - # 当两个链表都不为空时,进行合并操作 - while l1 and l2: - # 比较两个链表当前节点的值 - if l1.val <= l2.val: - # 如果l1的值小于或等于l2的值,将l1的节点连接到prev - prev.next = l1 - # 移动l1到下一个节点 - l1 = l1.next - else: - # 如果l2的值小于l1的值,将l2的节点连接到prev - prev.next = l2 - # 移动l2到下一个节点 - l2 = l2.next - # 移动prev到下一个节点 - prev = prev.next - - # 合并后l1和l2最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可 - prev.next = l1 if l1 is not None else l2 - - # 返回合并后的链表的起点(哑节点的下一个节点) - return prehead.next \ No newline at end of file +from typing import Optional + +# 定义链表节点类 +class ListNode: + def __init__(self, val=0, next=None): + self.val = val # 节点的值 + self.next = next # 指向下一个节点的指针 + +# 定义解决方案类 +class Solution: + # 合并两个有序链表的函数 + def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: + # 创建一个哑节点作为合并后链表的起点 + prehead = ListNode(-1) + + # prev指针用于构建新的链表 + prev = prehead + + # 当两个链表都不为空时,进行合并操作 + while l1 and l2: + # 比较两个链表当前节点的值 + if l1.val <= l2.val: + # 如果l1的值小于或等于l2的值,将l1的节点连接到prev + prev.next = l1 + # 移动l1到下一个节点 + l1 = l1.next + else: + # 如果l2的值小于l1的值,将l2的节点连接到prev + prev.next = l2 + # 移动l2到下一个节点 + l2 = l2.next + # 移动prev到下一个节点 + prev = prev.next + + # 合并后l1和l2最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可 + prev.next = l1 if l1 is not None else l2 + + # 返回合并后的链表的起点(哑节点的下一个节点) + return prehead.next diff --git "a/leetcode/Study Plan/21/21_\351\200\222\345\275\222.py" "b/leetcode/Study Plan/21/21_\351\200\222\345\275\222.py" index ed96c290..e1faf65b 100644 --- "a/leetcode/Study Plan/21/21_\351\200\222\345\275\222.py" +++ "b/leetcode/Study Plan/21/21_\351\200\222\345\275\222.py" @@ -26,4 +26,4 @@ def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optio # 递归合并l1和l2的下一个节点,并将结果赋值给l2的下一个节点 l2.next = self.mergeTwoLists(l1, l2.next) # 返回l2作为合并后的链表的头节点 - return l2 \ No newline at end of file + return l2 diff --git a/leetcode/Study Plan/21/test_1.py b/leetcode/Study Plan/21/test_1.py index b1efc512..5ad922c1 100644 --- a/leetcode/Study Plan/21/test_1.py +++ b/leetcode/Study Plan/21/test_1.py @@ -1,8 +1,8 @@ -def hanoi(n, source, target, auxiliary): - if n == 1: - print(f"Move disk 1 from {source} to {target}") - else: - hanoi(n - 1, source, auxiliary, target) - print(f"Move disk {n} from {source} to {target}") - hanoi(n - 1, auxiliary, target, source) -hanoi (3, 'A', 'C', 'B') \ No newline at end of file +def hanoi(n, source, target, auxiliary): + if n == 1: + print(f"Move disk 1 from {source} to {target}") + else: + hanoi(n - 1, source, auxiliary, target) + print(f"Move disk {n} from {source} to {target}") + hanoi(n - 1, auxiliary, target, source) +hanoi (3, 'A', 'C', 'B') diff --git "a/leetcode/Study Plan/2125. \351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/1.py" "b/leetcode/Study Plan/2125. \351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/1.py" index d165540a..7cb1c104 100644 --- "a/leetcode/Study Plan/2125. \351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/1.py" +++ "b/leetcode/Study Plan/2125. \351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/1.py" @@ -17,4 +17,4 @@ def numberOfBeams(self,bank:List[str])->int: ans += last * cnt last = cnt #将前面不为0的一行的安全设备的数量进行记录 >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 - return ans \ No newline at end of file + return ans diff --git "a/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/1.cpp" "b/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/1.cpp" index 1ff43876..ae6fe7ac 100644 --- "a/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/1.cpp" +++ "b/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/1.cpp" @@ -1,20 +1,21 @@ +#include class Solution { public: long long maxRunTime(int n, vector& batteries) { // 计算所有电池的总容量 long long tot = reduce(batteries.begin(),batteries.end(),0LL); - + // 二分查找的左右边界 // 理论上最多能运行 tot/n 分钟(平均分配),所以上界设为 tot/n+1 long long l = 0, r = tot / n + 1; - + // 二分查找:找到最大的运行时间 x,使得 n 台电脑可以同时运行 x 分钟 // 使用左闭右开区间 [l, r) while(l + 1 < r) { // 候选的运行时间 long long x = l + (r - l) / 2; - + // 计算所有电池能提供的总能量 // 对于每个电池,最多只能给一台电脑供电 x 分钟 // 所以每个电池最多贡献 min(b, x) 的能量 @@ -23,13 +24,13 @@ class Solution { { sum += min(b, x); } - + // 判断:如果总能量 sum >= n * x,说明可以让 n 台电脑运行 x 分钟 // 此时可以尝试更大的 x(更新左边界) // 否则需要减小 x(更新右边界) (n * x <= sum ? l : r) = x; } - + // 返回最大的满足条件的运行时间 return l; } diff --git "a/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/2.cpp" "b/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/2.cpp" index 34ea86c3..4e4453de 100644 --- "a/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/2.cpp" +++ "b/leetcode/Study Plan/2141. \345\220\214\346\227\266\350\277\220\350\241\214 N \345\217\260\347\224\265\350\204\221\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264/2.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: long long maxRunTime(int n, vector& batteries) { diff --git "a/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/1.py" "b/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/1.py" index b3b07a08..7f64a6cb 100644 --- "a/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/1.py" +++ "b/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/1.py" @@ -1,4 +1,4 @@ -# out of time +# out of time class Solution: def countOperations(self, num1: int, num2: int) -> int: cnt = 0 @@ -9,4 +9,4 @@ def countOperations(self, num1: int, num2: int) -> int: else: num2 -=num1 cnt+=1 - return cnt \ No newline at end of file + return cnt diff --git "a/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/2.py" "b/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/2.py" index ba7ea130..794e98da 100644 --- "a/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/2.py" +++ "b/leetcode/Study Plan/2169. \345\276\227\345\210\260 0 \347\232\204\346\223\215\344\275\234\346\225\260/2.py" @@ -8,4 +8,4 @@ def countOperations(self, num1: int, num2: int) -> int: else: q,num2 = divmod(num2,num1) cnt += q - return cnt \ No newline at end of file + return cnt diff --git "a/leetcode/Study Plan/2179_\347\273\237\350\256\241\346\225\260\347\273\204\344\270\255\345\245\275\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256/1.cpp" "b/leetcode/Study Plan/2179_\347\273\237\350\256\241\346\225\260\347\273\204\344\270\255\345\245\275\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256/1.cpp" index 622143c8..b9f1fbb5 100644 --- "a/leetcode/Study Plan/2179_\347\273\237\350\256\241\346\225\260\347\273\204\344\270\255\345\245\275\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256/1.cpp" +++ "b/leetcode/Study Plan/2179_\347\273\237\350\256\241\346\225\260\347\273\204\344\270\255\345\245\275\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256/1.cpp" @@ -1,78 +1,79 @@ -// 树状数组类,用于高效处理前缀和查询和单点更新 -class FenwickTree -{ -private: - vector tree; // 树状数组存储结构 - -public: - // 构造函数,初始化树状数组大小为size+1,所有元素初始值为0 - FenwickTree(int size) : tree(size + 1, 0) {} - - // 更新操作:在index位置增加delta值 - void update(int index, int delta) - { - index++; // 树状数组从1开始索引 - while (index < tree.size()) - { - tree[index] += delta; - index += index & -index; // 移动到父节点 - } - } - - // 查询操作:返回[0,index]区间的前缀和 - int query(int index) - { - index++; // 树状数组从1开始索引 - int res = 0; - while (index > 0) - { - res += tree[index]; - index -= index & -index; // 移动到前一个区间 - } - return res; - } -}; - -class Solution -{ -public: - // 计算好三元组的数量 - // 好三元组定义:在nums1和nums2中相对顺序相同的三元组 - long long goodTriplets(vector &nums1, vector &nums2) - { - int n = nums1.size(); - // pos2[i]表示数字i在nums2中的位置 - vector pos2(n); - // reversedIndexMapping[i]表示nums2中第i个位置对应的nums1中的位置 - vector reversedIndexMapping(n); - - // 建立nums2中数字到位置的映射 - for (int i = 0; i < n; i++) - { - pos2[nums2[i]] = i; - } - // 建立nums1和nums2的位置对应关系 - for (int i = 0; i < n; i++) - { - reversedIndexMapping[pos2[nums1[i]]] = i; - } - - FenwickTree tree(n); - long long res = 0; - - // 遍历每个数字,计算以该数字为中间元素的好三元组数量 - for (int value = 0; value < n; value++) - { - int pos = reversedIndexMapping[value]; - // 计算左侧小于当前数字的元素数量 - int left = tree.query(pos); - // 将当前数字加入树状数组 - tree.update(pos, 1); - // 计算右侧大于当前数字的元素数量 - int right = (n - 1 - pos) - (value - left); - // 累加当前数字作为中间元素的好三元组数量 - res += (long long)left * right; - } - return res; - } -}; +#include +// 树状数组类,用于高效处理前缀和查询和单点更新 +class FenwickTree +{ +private: + vector tree; // 树状数组存储结构 + +public: + // 构造函数,初始化树状数组大小为size+1,所有元素初始值为0 + FenwickTree(int size) : tree(size + 1, 0) {} + + // 更新操作:在index位置增加delta值 + void update(int index, int delta) + { + index++; // 树状数组从1开始索引 + while (index < tree.size()) + { + tree[index] += delta; + index += index & -index; // 移动到父节点 + } + } + + // 查询操作:返回[0,index]区间的前缀和 + int query(int index) + { + index++; // 树状数组从1开始索引 + int res = 0; + while (index > 0) + { + res += tree[index]; + index -= index & -index; // 移动到前一个区间 + } + return res; + } +}; + +class Solution +{ +public: + // 计算好三元组的数量 + // 好三元组定义:在nums1和nums2中相对顺序相同的三元组 + long long goodTriplets(vector &nums1, vector &nums2) + { + int n = nums1.size(); + // pos2[i]表示数字i在nums2中的位置 + vector pos2(n); + // reversedIndexMapping[i]表示nums2中第i个位置对应的nums1中的位置 + vector reversedIndexMapping(n); + + // 建立nums2中数字到位置的映射 + for (int i = 0; i < n; i++) + { + pos2[nums2[i]] = i; + } + // 建立nums1和nums2的位置对应关系 + for (int i = 0; i < n; i++) + { + reversedIndexMapping[pos2[nums1[i]]] = i; + } + + FenwickTree tree(n); + long long res = 0; + + // 遍历每个数字,计算以该数字为中间元素的好三元组数量 + for (int value = 0; value < n; value++) + { + int pos = reversedIndexMapping[value]; + // 计算左侧小于当前数字的元素数量 + int left = tree.query(pos); + // 将当前数字加入树状数组 + tree.update(pos, 1); + // 计算右侧大于当前数字的元素数量 + int right = (n - 1 - pos) - (value - left); + // 累加当前数字作为中间元素的好三元组数量 + res += (long long)left * right; + } + return res; + } +}; diff --git "a/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" "b/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" index fcaa26a5..12f34466 100644 --- "a/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" +++ "b/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/1.cpp" @@ -35,13 +35,13 @@ class MyStack { public: queue q1,q2; //q2用于备份 MyStack() { - + } - + void push(int x) { q1.push(x); } - + int pop() { int n = q1.size(); if(n < 1)return -1; @@ -58,7 +58,7 @@ class MyStack { while(!q2.empty())q2.pop(); return res; } - + int top() { int n = q1.size(); if(n < 1)return -1; @@ -75,7 +75,7 @@ class MyStack { while(!q2.empty())q2.pop(); return res; } - + bool empty() { return q1.empty(); } diff --git "a/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" "b/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" index 7e5bb270..0d41e86e 100644 --- "a/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" +++ "b/leetcode/Study Plan/225. \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/2.cpp" @@ -34,15 +34,15 @@ using namespace std; // @lc code=start class MyStack { public: - queue q1; + queue q1; MyStack() { - + } - + void push(int x) { q1.push(x); } - + int pop() { int n = q1.size(); if(n < 1)return -1; @@ -56,7 +56,7 @@ class MyStack { q1.pop(); return res; } - + int top() { int n = q1.size(); if(n < 1)return -1; @@ -71,7 +71,7 @@ class MyStack { q1.pop(); return res; } - + bool empty() { return q1.empty(); } diff --git "a/leetcode/Study Plan/2257. \347\273\237\350\256\241\347\275\221\346\240\274\345\233\276\344\270\255\346\262\241\346\234\211\350\242\253\344\277\235\345\215\253\347\232\204\346\240\274\345\255\220\346\225\260/1.py" "b/leetcode/Study Plan/2257. \347\273\237\350\256\241\347\275\221\346\240\274\345\233\276\344\270\255\346\262\241\346\234\211\350\242\253\344\277\235\345\215\253\347\232\204\346\240\274\345\255\220\346\225\260/1.py" index ee64bf8c..be5c680c 100644 --- "a/leetcode/Study Plan/2257. \347\273\237\350\256\241\347\275\221\346\240\274\345\233\276\344\270\255\346\262\241\346\234\211\350\242\253\344\277\235\345\215\253\347\232\204\346\240\274\345\255\220\346\225\260/1.py" +++ "b/leetcode/Study Plan/2257. \347\273\237\350\256\241\347\275\221\346\240\274\345\233\276\344\270\255\346\262\241\346\234\211\350\242\253\344\277\235\345\215\253\347\232\204\346\240\274\345\255\220\346\225\260/1.py" @@ -1,39 +1,39 @@ -class Solution: - def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int: - grid = [[0] * n for _ in range(m)] # 网格状态数组 - q = deque([]) # 广度优先搜索队列 - # 每个方向的单位向量 - dx = [1, 0, -1, 0] - dy = [0, 1, 0, -1] - - # 标记警卫的位置,并将每个警卫四个方向的初始视线入队 - for i, j in guards: - grid[i][j] = -1 # -1 代表这个格子是警卫 - for k in range(4): - # (i, j, k) 表示从(i, j)出发向第k个方向(0-3)发射视线 - q.append((i, j, k)) - - # 标记墙的位置 - for i, j in walls: - grid[i][j] = -2 # -2 代表这个格子是墙 - - # BFS 遍历所有视线 - while q: - x, y, k = q.popleft() - nx, ny = x + dx[k], y + dy[k] - if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] >= 0: - # 如果当前方向k还没覆盖过(nx, ny)这个格子 - if (grid[nx][ny] & (1 << k)) == 0: - # 用位运算标记:第k方向已被该视线覆盖 - grid[nx][ny] |= (1 << k) - # 将下一个视线状态入队,继续往前推进 - q.append((nx, ny, k)) - - # 统计未被任何视线、警卫或墙覆盖的格子的数量 - res = 0 - for i in range(m): - for j in range(n): - # 如果该格子既不是警卫(-1)、也不是墙(-2),且也未被任何方向的视线覆盖(值==0) - if grid[i][j] == 0: - res += 1 - return res +class Solution: + def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int: + grid = [[0] * n for _ in range(m)] # 网格状态数组 + q = deque([]) # 广度优先搜索队列 + # 每个方向的单位向量 + dx = [1, 0, -1, 0] + dy = [0, 1, 0, -1] + + # 标记警卫的位置,并将每个警卫四个方向的初始视线入队 + for i, j in guards: + grid[i][j] = -1 # -1 代表这个格子是警卫 + for k in range(4): + # (i, j, k) 表示从(i, j)出发向第k个方向(0-3)发射视线 + q.append((i, j, k)) + + # 标记墙的位置 + for i, j in walls: + grid[i][j] = -2 # -2 代表这个格子是墙 + + # BFS 遍历所有视线 + while q: + x, y, k = q.popleft() + nx, ny = x + dx[k], y + dy[k] + if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] >= 0: + # 如果当前方向k还没覆盖过(nx, ny)这个格子 + if (grid[nx][ny] & (1 << k)) == 0: + # 用位运算标记:第k方向已被该视线覆盖 + grid[nx][ny] |= (1 << k) + # 将下一个视线状态入队,继续往前推进 + q.append((nx, ny, k)) + + # 统计未被任何视线、警卫或墙覆盖的格子的数量 + res = 0 + for i in range(m): + for j in range(n): + # 如果该格子既不是警卫(-1)、也不是墙(-2),且也未被任何方向的视线覆盖(值==0) + if grid[i][j] == 0: + res += 1 + return res diff --git a/leetcode/Study Plan/2270/1.cpp b/leetcode/Study Plan/2270/1.cpp index e6f6765f..d73e355a 100644 --- a/leetcode/Study Plan/2270/1.cpp +++ b/leetcode/Study Plan/2270/1.cpp @@ -1,32 +1,32 @@ -#include -#include - -using namespace std; - -class Solution -{ -public: - int waysToSplitArray(vector &nums) - { - int n = nums.size(),cnt = 0; - long long left = 0,right = 0; - for(int i = 0; i < n; i++){ - right += nums[i]; - } - for (int i = 0; i < n - 1;++i){ - left += nums[i]; - right -= nums[i]; - if(left >= right) - ++cnt; - } - return cnt; - } -}; - -int main() -{ - Solution s; - vector nums = {10, 4, -8, 7}; - cout << s.waysToSplitArray(nums) << endl; - return 0; -} +#include +#include + +using namespace std; + +class Solution +{ +public: + int waysToSplitArray(vector &nums) + { + int n = nums.size(),cnt = 0; + long long left = 0,right = 0; + for(int i = 0; i < n; i++){ + right += nums[i]; + } + for (int i = 0; i < n - 1;++i){ + left += nums[i]; + right -= nums[i]; + if(left >= right) + ++cnt; + } + return cnt; + } +}; + +int main() +{ + Solution s; + vector nums = {10, 4, -8, 7}; + cout << s.waysToSplitArray(nums) << endl; + return 0; +} diff --git a/leetcode/Study Plan/2270/2.cpp b/leetcode/Study Plan/2270/2.cpp index 364b8528..e057b090 100644 --- a/leetcode/Study Plan/2270/2.cpp +++ b/leetcode/Study Plan/2270/2.cpp @@ -1,36 +1,36 @@ -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - int waysToSplitArray(vector &nums) - { - int n = nums.size(); - // right使用accumulate函数计算整个数组的和 - // accumulate(开始迭代器, 结束迭代器, 初始值) - // 0LL表示long long类型的0,防止整数溢出 - long long left = 0, right = accumulate(nums.begin(), nums.end(), 0LL); - int ans = 0; - for (int i = 0; i < n - 1; ++i) - { - left += nums[i]; - right -= nums[i]; - if (left >= right) - { - ++ans; - } - } - return ans; - } -}; - -int main() { - Solution s; - vector nums = {10, 4, -8, 7}; - cout << s.waysToSplitArray(nums) << endl; - return 0; -} +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + int waysToSplitArray(vector &nums) + { + int n = nums.size(); + // right使用accumulate函数计算整个数组的和 + // accumulate(开始迭代器, 结束迭代器, 初始值) + // 0LL表示long long类型的0,防止整数溢出 + long long left = 0, right = accumulate(nums.begin(), nums.end(), 0LL); + int ans = 0; + for (int i = 0; i < n - 1; ++i) + { + left += nums[i]; + right -= nums[i]; + if (left >= right) + { + ++ans; + } + } + return ans; + } +}; + +int main() { + Solution s; + vector nums = {10, 4, -8, 7}; + cout << s.waysToSplitArray(nums) << endl; + return 0; +} diff --git a/leetcode/Study Plan/2275/1.cpp b/leetcode/Study Plan/2275/1.cpp index bfd3bb22..bc0de5bd 100644 --- a/leetcode/Study Plan/2275/1.cpp +++ b/leetcode/Study Plan/2275/1.cpp @@ -1,32 +1,33 @@ -class Solution -{ -public: - // 找出数组中按位与(&)运算结果不为0的最大子数组长度 - // 思路: 统计每一位上1的个数,最大的个数即为所求 - // 因为只要这些数在某一位上都为1,按位与的结果在该位上就为1,整体结果就不为0 - int largestCombination(vector &candidates) - { - int result = 0; - // 遍历32位(因为是int型整数) - for (int bit = 0; bit < 32; bit++) - { - int count = 0; - // 遍历数组中的每个数 - for (int num : candidates) - { - // num >> bit 将num右移bit位 - // & 1 判断最低位是否为1 - // 即判断num的第bit位是否为1 - if ((num >> bit) & 1) - { - count++; // 统计当前位上1的个数 - } - } - // 更新最大值 - // 因为我们要找能使按位与不为0的最大子数组 - // 而在某位上为1的数的个数就是可能的子数组大小 - result = max(result, count); - } - return result; // 返回最大子数组长度 - } -}; +#include +class Solution +{ +public: + // 找出数组中按位与(&)运算结果不为0的最大子数组长度 + // 思路: 统计每一位上1的个数,最大的个数即为所求 + // 因为只要这些数在某一位上都为1,按位与的结果在该位上就为1,整体结果就不为0 + int largestCombination(vector &candidates) + { + int result = 0; + // 遍历32位(因为是int型整数) + for (int bit = 0; bit < 32; bit++) + { + int count = 0; + // 遍历数组中的每个数 + for (int num : candidates) + { + // num >> bit 将num右移bit位 + // & 1 判断最低位是否为1 + // 即判断num的第bit位是否为1 + if ((num >> bit) & 1) + { + count++; // 统计当前位上1的个数 + } + } + // 更新最大值 + // 因为我们要找能使按位与不为0的最大子数组 + // 而在某位上为1的数的个数就是可能的子数组大小 + result = max(result, count); + } + return result; // 返回最大子数组长度 + } +}; diff --git "a/leetcode/Study Plan/231. 2 \347\232\204\345\271\202/1.cpp" "b/leetcode/Study Plan/231. 2 \347\232\204\345\271\202/1.cpp" index 2c3da81f..86793a92 100644 --- "a/leetcode/Study Plan/231. 2 \347\232\204\345\271\202/1.cpp" +++ "b/leetcode/Study Plan/231. 2 \347\232\204\345\271\202/1.cpp" @@ -1,6 +1,6 @@ class Solution { public: bool isPowerOfTwo(int n) { - return n > 0 && (n & (n-1)) == 0; + return n > 0 && (n & (n-1)) == 0; } }; diff --git "a/leetcode/Study Plan/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" "b/leetcode/Study Plan/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" index 4d9bac01..94662e38 100644 --- "a/leetcode/Study Plan/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" +++ "b/leetcode/Study Plan/232. \347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/1.cpp" @@ -42,13 +42,13 @@ class MyQueue { public: stack stackIn,stackOut; MyQueue() { - + } - + void push(int x) { stackIn.push(x); } - + int pop() { if(empty())return -1; if(stackOut.empty()){ @@ -61,7 +61,7 @@ class MyQueue { stackOut.pop(); return res; } - + int peek() { if(empty())return -1; if(stackOut.empty()){ @@ -72,7 +72,7 @@ class MyQueue { } return stackOut.top(); } - + bool empty() { return stackIn.empty() && stackOut.empty(); } diff --git "a/leetcode/Study Plan/242. \346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/1.cpp" "b/leetcode/Study Plan/242. \346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/1.cpp" index 0add25eb..c4d42332 100644 --- "a/leetcode/Study Plan/242. \346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/1.cpp" +++ "b/leetcode/Study Plan/242. \346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/1.cpp" @@ -1,20 +1,20 @@ -class Solution { -public: - bool isAnagram(string s, string t) { - int hash[26] = {0}; - if (s.size() != t.size()) // 如果s和t的长度不相同的话,不可能是异位词 - return false; - - for (auto c : s) { - hash[c - 'a']++; - } - for (auto c : t) { - hash[c - 'a']--; - } - for (int i = 0; i < 26; ++i) { - if (hash[i] != 0) - return false; - } - return true; - } -}; +class Solution { +public: + bool isAnagram(string s, string t) { + int hash[26] = {0}; + if (s.size() != t.size()) // 如果s和t的长度不相同的话,不可能是异位词 + return false; + + for (auto c : s) { + hash[c - 'a']++; + } + for (auto c : t) { + hash[c - 'a']--; + } + for (int i = 0; i < 26; ++i) { + if (hash[i] != 0) + return false; + } + return true; + } +}; diff --git "a/leetcode/Study Plan/2586. \347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/1.cpp" "b/leetcode/Study Plan/2586. \347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/1.cpp" index 7888a49c..5188ea20 100644 --- "a/leetcode/Study Plan/2586. \347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/1.cpp" +++ "b/leetcode/Study Plan/2586. \347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include using namespace std; @@ -8,7 +9,7 @@ class Solution { int ans = 0; for(int i = left;i<=right;++i) { - ans += (vowels.find(words[i][0]) != string::npos) + ans += (vowels.find(words[i][0]) != string::npos) && (vowels.find(words[i].back()) != string::npos); } return ans; diff --git "a/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.cpp" "b/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.cpp" index 2aee010e..88226617 100644 --- "a/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.cpp" +++ "b/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.cpp" @@ -1,21 +1,22 @@ -#include -class Solution { -public: - int findSmallestInteger(vector& nums, int value) { - unordered_map mp; - - - for (auto x : nums) { - int mod = ((x % value) + value) % value; - mp[mod]++; - } - - int mex = 0; - while (mp[mex % value] > 0) { - mp[mex % value]--; - mex++; - } - - return mex; - } -}; \ No newline at end of file +#include +#include +class Solution { +public: + int findSmallestInteger(vector& nums, int value) { + unordered_map mp; + + + for (auto x : nums) { + int mod = ((x % value) + value) % value; + mp[mod]++; + } + + int mex = 0; + while (mp[mex % value] > 0) { + mp[mex % value]--; + mex++; + } + + return mex; + } +}; diff --git "a/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.py" "b/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.py" index c06026b3..a7661487 100644 --- "a/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.py" +++ "b/leetcode/Study Plan/2598. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\346\234\200\345\244\247 MEX/1.py" @@ -1,12 +1,12 @@ -class Solution: - def findSmallestInteger(self, nums: List[int], value: int) -> int: - - # frequency count of non-neggative remainders for each number - mp = Counter(x % value for x in nums) - - # greedy search to find MEX - mex = 0 - while mp[mex % value] > 0: - mp[mex % value] -= 1 - mex +=1 - return mex +class Solution: + def findSmallestInteger(self, nums: List[int], value: int) -> int: + + # frequency count of non-neggative remainders for each number + mp = Counter(x % value for x in nums) + + # greedy search to find MEX + mex = 0 + while mp[mex % value] > 0: + mp[mex % value] -= 1 + mex +=1 + return mex diff --git "a/leetcode/Study Plan/26/26_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" "b/leetcode/Study Plan/26/26_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" index b9249b1f..ef382ee8 100644 --- "a/leetcode/Study Plan/26/26_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" +++ "b/leetcode/Study Plan/26/26_\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" @@ -1,19 +1,19 @@ -from typing import List - -class Solution: - def removeDuplicates(self, nums: List[int]) -> int: - if not nums: - return 0 - k = 0 - for i in range(1, len(nums)): - if nums[i] != nums[k]: - k += 1 - nums[k] = nums[i] - return k + 1 - -if __name__ == "__main__": - solution = Solution() - nums = [1, 1, 2, 2, 3, 4, 4, 5] - length = solution.removeDuplicates(nums) - print(f"Length of array after removing duplicates: {length}") - print(f"Array after removing duplicates: {nums[:length]}") \ No newline at end of file +from typing import List + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + if not nums: + return 0 + k = 0 + for i in range(1, len(nums)): + if nums[i] != nums[k]: + k += 1 + nums[k] = nums[i] + return k + 1 + +if __name__ == "__main__": + solution = Solution() + nums = [1, 1, 2, 2, 3, 4, 4, 5] + length = solution.removeDuplicates(nums) + print(f"Length of array after removing duplicates: {length}") + print(f"Array after removing duplicates: {nums[:length]}") diff --git "a/leetcode/Study Plan/2654. \344\275\277\346\225\260\347\273\204\346\211\200\346\234\211\345\205\203\347\264\240\345\217\230\346\210\220 1 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" "b/leetcode/Study Plan/2654. \344\275\277\346\225\260\347\273\204\346\211\200\346\234\211\345\205\203\347\264\240\345\217\230\346\210\220 1 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" index 432cfe03..1b219489 100644 --- "a/leetcode/Study Plan/2654. \344\275\277\346\225\260\347\273\204\346\211\200\346\234\211\345\205\203\347\264\240\345\217\230\346\210\220 1 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" +++ "b/leetcode/Study Plan/2654. \344\275\277\346\225\260\347\273\204\346\211\200\346\234\211\345\205\203\347\264\240\345\217\230\346\210\220 1 \347\232\204\346\234\200\345\260\221\346\223\215\344\275\234\346\254\241\346\225\260/1.py" @@ -31,4 +31,4 @@ def minOperations(self, nums: List[int]) -> int: # 操作数 = 区间内变1的操作数 + 其它变1的操作数 # 区间内最短为min_len,外部剩下n-1个数,再减去1(因为区间内有两个数变1需要min_len-1步,实际上总步数是min_len+n-2) - return min_len + n - 2 \ No newline at end of file + return min_len + n - 2 diff --git a/leetcode/Study Plan/27/1.cpp b/leetcode/Study Plan/27/1.cpp index dbd84685..e52a6482 100644 --- a/leetcode/Study Plan/27/1.cpp +++ b/leetcode/Study Plan/27/1.cpp @@ -1,18 +1,19 @@ -class Solution -{ -public: - int removeElement(vector &nums, int val) - { - int i = 0,j = nums.size() - 1,k=0; - while(i <= j){ - if(nums[i] == val){ - nums[i] = nums[j]; - j--; - } - else{ - i++; - } - } - return i; - } -}; \ No newline at end of file +#include +class Solution +{ +public: + int removeElement(vector &nums, int val) + { + int i = 0,j = nums.size() - 1,k=0; + while(i <= j){ + if(nums[i] == val){ + nums[i] = nums[j]; + j--; + } + else{ + i++; + } + } + return i; + } +}; diff --git a/leetcode/Study Plan/2843/1.cpp b/leetcode/Study Plan/2843/1.cpp index 221faaa4..04d44332 100644 --- a/leetcode/Study Plan/2843/1.cpp +++ b/leetcode/Study Plan/2843/1.cpp @@ -1,48 +1,48 @@ -#include -#include - -using namespace std; - -class Solution { -public: - bool isSymmetricNumber(int num) { - if (num < 0) return false; // 处理负数 - if (num == 0) return true; // 处理0 - - vector digits; - while (num > 0) { - digits.push_back(num % 10); - num /= 10; - } - - // 如果数字位数为奇数,直接返回false - if (digits.size() % 2 != 0) return false; - - int left = 0, right = digits.size() - 1; - int sum = 0; - - while (left < right) { - sum += digits[left] - digits[right]; - left++; - right--; - } - - return sum == 0; - } - - int countSymmetricIntegers(int low, int high) { - int count = 0; - for (int i = low; i <= high; ++i) { - if (isSymmetricNumber(i)) count++; - } - return count; - } -}; - -int main() { - Solution s; - int low, high; - cin >> low >> high; - cout << s.countSymmetricIntegers(low, high) << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +class Solution { +public: + bool isSymmetricNumber(int num) { + if (num < 0) return false; // 处理负数 + if (num == 0) return true; // 处理0 + + vector digits; + while (num > 0) { + digits.push_back(num % 10); + num /= 10; + } + + // 如果数字位数为奇数,直接返回false + if (digits.size() % 2 != 0) return false; + + int left = 0, right = digits.size() - 1; + int sum = 0; + + while (left < right) { + sum += digits[left] - digits[right]; + left++; + right--; + } + + return sum == 0; + } + + int countSymmetricIntegers(int low, int high) { + int count = 0; + for (int i = low; i <= high; ++i) { + if (isSymmetricNumber(i)) count++; + } + return count; + } +}; + +int main() { + Solution s; + int low, high; + cin >> low >> high; + cout << s.countSymmetricIntegers(low, high) << endl; + return 0; +} diff --git "a/leetcode/Study Plan/2943. \346\234\200\345\244\247\345\214\226\347\275\221\346\240\274\345\233\276\344\270\255\346\255\243\346\226\271\345\275\242\347\251\272\346\264\236\347\232\204\351\235\242\347\247\257/1.cpp" "b/leetcode/Study Plan/2943. \346\234\200\345\244\247\345\214\226\347\275\221\346\240\274\345\233\276\344\270\255\346\255\243\346\226\271\345\275\242\347\251\272\346\264\236\347\232\204\351\235\242\347\247\257/1.cpp" index 577bda76..d63af538 100644 --- "a/leetcode/Study Plan/2943. \346\234\200\345\244\247\345\214\226\347\275\221\346\240\274\345\233\276\344\270\255\346\255\243\346\226\271\345\275\242\347\251\272\346\264\236\347\232\204\351\235\242\347\247\257/1.cpp" +++ "b/leetcode/Study Plan/2943. \346\234\200\345\244\247\345\214\226\347\275\221\346\240\274\345\233\276\344\270\255\346\255\243\346\226\271\345\275\242\347\251\272\346\264\236\347\232\204\351\235\242\347\247\257/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { int f(vector &a) { ranges::sort(a); diff --git "a/leetcode/Study Plan/3013. \345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\346\234\200\345\260\217\346\200\273\344\273\243\344\273\267\347\232\204\345\255\220\346\225\260\347\273\204 II/1.py" "b/leetcode/Study Plan/3013. \345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\346\234\200\345\260\217\346\200\273\344\273\243\344\273\267\347\232\204\345\255\220\346\225\260\347\273\204 II/1.py" index dc23871a..6d18252b 100644 --- "a/leetcode/Study Plan/3013. \345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\346\234\200\345\260\217\346\200\273\344\273\243\344\273\267\347\232\204\345\255\220\346\225\260\347\273\204 II/1.py" +++ "b/leetcode/Study Plan/3013. \345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\346\234\200\345\260\217\346\200\273\344\273\243\344\273\267\347\232\204\345\255\220\346\225\260\347\273\204 II/1.py" @@ -19,7 +19,7 @@ def L2R()->None: nonlocal sum_left sum_left -= x R.add(x) - + def R2L()->None: x = R.pop(0) nonlocal sum_left @@ -45,13 +45,13 @@ def R2L()->None: L.add(in_val) else: R.add(in_val) - + # 维护大小 if len(L) == k - 1: R2L() elif len(L) == k + 1: L2R() - + ans = min(ans,sum_left) - return ans + return ans diff --git a/leetcode/Study Plan/3065/1.cpp b/leetcode/Study Plan/3065/1.cpp index d76a8d1f..02d3e238 100644 --- a/leetcode/Study Plan/3065/1.cpp +++ b/leetcode/Study Plan/3065/1.cpp @@ -1,28 +1,28 @@ -#include -#include - -using namespace std; - -class Solution -{ -public: - int minOperations(vector &nums, int k) - { - int cnt=0,i=0; - while(i nums = {2,11,10,1,3}; - int k = 10; - cout << s.minOperations(nums, k) << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +class Solution +{ +public: + int minOperations(vector &nums, int k) + { + int cnt=0,i=0; + while(i nums = {2,11,10,1,3}; + int k = 10; + cout << s.minOperations(nums, k) << endl; + return 0; +} diff --git a/leetcode/Study Plan/3066/1.cpp b/leetcode/Study Plan/3066/1.cpp index 50d5fb4d..cffb4d84 100644 --- a/leetcode/Study Plan/3066/1.cpp +++ b/leetcode/Study Plan/3066/1.cpp @@ -1,35 +1,35 @@ -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - int minOperations(vector &nums, int k) - { - int cnt = 0; - //定义一个基于小根堆的优先队列 - priority_queue, greater> minHeap(nums.begin(), nums.end()); - while (minHeap.size() >= 2 && minHeap.top() < k) - { - long long x = minHeap.top(); - minHeap.pop(); - long long y = minHeap.top(); - minHeap.pop(); - minHeap.push(x * 2 + y); - cnt++; - } - return cnt; - } -}; -int main() -{ - vector nums = {1, 1, 2, 4, 9}; - int k = 20; - Solution s; - int result = s.minOperations(nums, k); - cout << "Minimum operations: " << result << endl; - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + int minOperations(vector &nums, int k) + { + int cnt = 0; + //定义一个基于小根堆的优先队列 + priority_queue, greater> minHeap(nums.begin(), nums.end()); + while (minHeap.size() >= 2 && minHeap.top() < k) + { + long long x = minHeap.top(); + minHeap.pop(); + long long y = minHeap.top(); + minHeap.pop(); + minHeap.push(x * 2 + y); + cnt++; + } + return cnt; + } +}; +int main() +{ + vector nums = {1, 1, 2, 4, 9}; + int k = 20; + Solution s; + int result = s.minOperations(nums, k); + cout << "Minimum operations: " << result << endl; + return 0; +} diff --git "a/leetcode/Study Plan/3070. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216 k \347\232\204\345\255\220\347\237\251\351\230\265\347\232\204\346\225\260\347\233\256/1\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214.cpp" "b/leetcode/Study Plan/3070. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216 k \347\232\204\345\255\220\347\237\251\351\230\265\347\232\204\346\225\260\347\233\256/1\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214.cpp" index 0cd8cd69..f31bdc90 100644 --- "a/leetcode/Study Plan/3070. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216 k \347\232\204\345\255\220\347\237\251\351\230\265\347\232\204\346\225\260\347\233\256/1\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214.cpp" +++ "b/leetcode/Study Plan/3070. \345\205\203\347\264\240\345\222\214\345\260\217\344\272\216\347\255\211\344\272\216 k \347\232\204\345\255\220\347\237\251\351\230\265\347\232\204\346\225\260\347\233\256/1\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214.cpp" @@ -9,7 +9,7 @@ class Solution { int m = grid.size(),n = grid[0].size(); int ans = 0; vector> sum(m+1,vector(n+1)); - + for(int i = 0;i -#include -#include -#include - -class Solution -{ -public: - int minimumSubarrayLength(std::vector nums, int k) - { - int n = nums.size(); - int res = INT_MAX; - for (int i = 0; i < n; i++) - { - int value = 0; - for (int j = i; j < n; j++) - { - value |= nums[j]; - if (value >= k) - { - res = std::min(res, j - i + 1); - break; - } - } - } - return res == INT_MAX ? -1 : res; - } -}; - -int main() { - Solution solution; - std::vector nums = {2, 1, 8}; - int k = 10; - - int result = solution.minimumSubarrayLength(nums, k); - std::cout << "Result: " << result << std::endl; - - return 0; -} +#include +#include +#include +#include + +class Solution +{ +public: + int minimumSubarrayLength(std::vector nums, int k) + { + int n = nums.size(); + int res = INT_MAX; + for (int i = 0; i < n; i++) + { + int value = 0; + for (int j = i; j < n; j++) + { + value |= nums[j]; + if (value >= k) + { + res = std::min(res, j - i + 1); + break; + } + } + } + return res == INT_MAX ? -1 : res; + } +}; + +int main() { + Solution solution; + std::vector nums = {2, 1, 8}; + int k = 10; + + int result = solution.minimumSubarrayLength(nums, k); + std::cout << "Result: " << result << std::endl; + + return 0; +} diff --git a/leetcode/Study Plan/3095/2.cpp b/leetcode/Study Plan/3095/2.cpp index e8afca23..ee6c7420 100644 --- a/leetcode/Study Plan/3095/2.cpp +++ b/leetcode/Study Plan/3095/2.cpp @@ -1,43 +1,44 @@ -class Solution -{ -public: - int minimumSubarrayLength(vector &nums, int k) - { - int n = nums.size(); - int left = 0, right = 0; - int curr_or = 0; - int min_len = n + 1; - - while (right < n) - { - curr_or |= nums[right]; - - // 当找到一个满足条件的窗口时 - while (curr_or >= k) - { - min_len = min(min_len, right - left + 1); - - // 重新计算从left+1到right的或值 - int new_or = 0; - for (int i = left + 1; i <= right; i++) - { - new_or |= nums[i]; - } - - // 如果移除left后仍然满足条件,继续收缩 - if (new_or >= k) - { - curr_or = new_or; - left++; - } - else - { - break; - } - } - right++; - } - - return min_len == n + 1 ? -1 : min_len; - } -}; \ No newline at end of file +#include +class Solution +{ +public: + int minimumSubarrayLength(vector &nums, int k) + { + int n = nums.size(); + int left = 0, right = 0; + int curr_or = 0; + int min_len = n + 1; + + while (right < n) + { + curr_or |= nums[right]; + + // 当找到一个满足条件的窗口时 + while (curr_or >= k) + { + min_len = min(min_len, right - left + 1); + + // 重新计算从left+1到right的或值 + int new_or = 0; + for (int i = left + 1; i <= right; i++) + { + new_or |= nums[i]; + } + + // 如果移除left后仍然满足条件,继续收缩 + if (new_or >= k) + { + curr_or = new_or; + left++; + } + else + { + break; + } + } + right++; + } + + return min_len == n + 1 ? -1 : min_len; + } +}; diff --git a/leetcode/Study Plan/3095/3.cpp b/leetcode/Study Plan/3095/3.cpp index 910ffe6d..3627a764 100644 --- a/leetcode/Study Plan/3095/3.cpp +++ b/leetcode/Study Plan/3095/3.cpp @@ -1,68 +1,69 @@ -#include - -// 滑动窗口 - -class Solution -{ -public: - int minimumSubarrayLength(vector &nums, int k) - { - int n = nums.size(); - // 用于统计每个二进制位上1的个数,bits[i]表示第i位上1的个数 - vector bits(30); - - // 计算当前窗口内所有数字按位或的结果 - auto calc = [](vector &bits) -> int - { - int ans = 0; - for (int i = 0; i < bits.size(); i++) - { - // 如果第i位上有1,则将结果的第i位置为1 - if (bits[i] > 0) - { - ans |= 1 << i; - } - } - return ans; - }; - - // 记录最小子数组长度 - int res = INT_MAX; - // 滑动窗口,left为左边界,right为右边界 - for (int left = 0, right = 0; right < n; right++) - { - // 将右边界的数加入窗口,更新每个二进制位上1的个数 - for (int i = 0; i < 30; i++) - { - bits[i] += (nums[right] >> i) & 1; - } - // 当窗口内的数按位或结果大于等于k时,尝试缩小窗口 - while (left <= right && calc(bits) >= k) - { - // 更新最小长度 - res = min(res, right - left + 1); - // 将左边界的数移出窗口,更新每个二进制位上1的个数 - for (int i = 0; i < 30; i++) - { - bits[i] -= (nums[left] >> i) & 1; - } - left++; - } - } - - // 如果没有找到满足条件的子数组,返回-1 - return res == INT_MAX ? -1 : res; - } -}; - -int main() -{ - Solution solution; - std::vector nums = {2, 1, 8}; - int k = 10; - - int result = solution.minimumSubarrayLength(nums, k); - std::cout << "Result: " << result << std::endl; - - return 0; -} +#include +#include + +// 滑动窗口 + +class Solution +{ +public: + int minimumSubarrayLength(vector &nums, int k) + { + int n = nums.size(); + // 用于统计每个二进制位上1的个数,bits[i]表示第i位上1的个数 + vector bits(30); + + // 计算当前窗口内所有数字按位或的结果 + auto calc = [](vector &bits) -> int + { + int ans = 0; + for (int i = 0; i < bits.size(); i++) + { + // 如果第i位上有1,则将结果的第i位置为1 + if (bits[i] > 0) + { + ans |= 1 << i; + } + } + return ans; + }; + + // 记录最小子数组长度 + int res = INT_MAX; + // 滑动窗口,left为左边界,right为右边界 + for (int left = 0, right = 0; right < n; right++) + { + // 将右边界的数加入窗口,更新每个二进制位上1的个数 + for (int i = 0; i < 30; i++) + { + bits[i] += (nums[right] >> i) & 1; + } + // 当窗口内的数按位或结果大于等于k时,尝试缩小窗口 + while (left <= right && calc(bits) >= k) + { + // 更新最小长度 + res = min(res, right - left + 1); + // 将左边界的数移出窗口,更新每个二进制位上1的个数 + for (int i = 0; i < 30; i++) + { + bits[i] -= (nums[left] >> i) & 1; + } + left++; + } + } + + // 如果没有找到满足条件的子数组,返回-1 + return res == INT_MAX ? -1 : res; + } +}; + +int main() +{ + Solution solution; + std::vector nums = {2, 1, 8}; + int k = 10; + + int result = solution.minimumSubarrayLength(nums, k); + std::cout << "Result: " << result << std::endl; + + return 0; +} diff --git "a/leetcode/Study Plan/3289. \346\225\260\345\255\227\345\260\217\351\225\207\344\270\255\347\232\204\346\215\243\350\233\213\351\254\274/1.py" "b/leetcode/Study Plan/3289. \346\225\260\345\255\227\345\260\217\351\225\207\344\270\255\347\232\204\346\215\243\350\233\213\351\254\274/1.py" index 2590db20..57fcc1b7 100644 --- "a/leetcode/Study Plan/3289. \346\225\260\345\255\227\345\260\217\351\225\207\344\270\255\347\232\204\346\215\243\350\233\213\351\254\274/1.py" +++ "b/leetcode/Study Plan/3289. \346\225\260\345\255\227\345\260\217\351\225\207\344\270\255\347\232\204\346\215\243\350\233\213\351\254\274/1.py" @@ -5,7 +5,7 @@ def getSneakyNumbers(self, nums: List[int]) -> List[int]: res = [] # 使用字典记录每个数字出现的次数 count = {} - + # 遍历数组中的每个数字 for x in nums: # 更新数字x的出现次数(如果不存在则初始化为0,然后加1) @@ -13,7 +13,7 @@ def getSneakyNumbers(self, nums: List[int]) -> List[int]: # 当某个数字第二次出现时,将其加入结果列表 if count[x] == 2: res.append(x) - + # 返回所有出现两次的数字 return res @@ -24,7 +24,7 @@ def getSneakyNumbers(self, nums: List[int]) -> List[int]: res = [] # 使用字典记录每个数字出现的次数 count = {} - + # 遍历数组中的每个数字 for x in nums: # 更新数字x的出现次数(如果不存在则初始化为0,然后加1) @@ -32,7 +32,7 @@ def getSneakyNumbers(self, nums: List[int]) -> List[int]: # 当某个数字第二次出现时,将其加入结果列表 if count[x] == 2: res.append(x) - + # 返回所有出现两次的数字 return res diff --git a/leetcode/Study Plan/3310/1.cpp b/leetcode/Study Plan/3310/1.cpp index acd5d8cc..e5385178 100644 --- a/leetcode/Study Plan/3310/1.cpp +++ b/leetcode/Study Plan/3310/1.cpp @@ -1,36 +1,36 @@ -/* - * @lc app=leetcode.cn id=3110 lang=cpp - * - * [3110] 字符串的分数 - */ - -#include -#include -#include - -// @lc code=start -class Solution -{ -public: - int scoreOfString(std::string s) - { - int ans = 0; - for (int i = 0, j = 1; j < s.size(); ++i, ++j) - { - int n = s[i] - '0'; - int m = s[j] - '0'; - ans += abs(n - m); - } - return ans; - } -}; -// @lc code=end - -int main() -{ - Solution solution; - // std::cout << int('h' - '0') << std::endl; - std::string s = "hello"; - std::cout << solution.scoreOfString(s) << std::endl; - return 0; -} +/* + * @lc app=leetcode.cn id=3110 lang=cpp + * + * [3110] 字符串的分数 + */ + +#include +#include +#include + +// @lc code=start +class Solution +{ +public: + int scoreOfString(std::string s) + { + int ans = 0; + for (int i = 0, j = 1; j < s.size(); ++i, ++j) + { + int n = s[i] - '0'; + int m = s[j] - '0'; + ans += abs(n - m); + } + return ans; + } +}; +// @lc code=end + +int main() +{ + Solution solution; + // std::cout << int('h' - '0') << std::endl; + std::string s = "hello"; + std::cout << solution.scoreOfString(s) << std::endl; + return 0; +} diff --git "a/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/1.py" "b/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/1.py" index 153ab714..127a9479 100644 --- "a/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/1.py" +++ "b/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/1.py" @@ -1,5 +1,5 @@ -""" -x1 = 100100 +""" +x1 = 100100 100100|100101 = 100101 x2 = 100111 100111|101000 = 101111 diff --git "a/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/2.py" "b/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/2.py" index 6a443077..990daa4a 100644 --- "a/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/2.py" +++ "b/leetcode/Study Plan/3314. \346\236\204\351\200\240\346\234\200\345\260\217\344\275\215\350\277\220\347\256\227\346\225\260\347\273\204 I/2.py" @@ -1,5 +1,5 @@ -""" -x1 = 100100 +""" +x1 = 100100 100100|100101 = 100101 x2 = 100111 100111|101000 = 101111 diff --git "a/leetcode/Study Plan/3318. \350\256\241\347\256\227\345\255\220\346\225\260\347\273\204\347\232\204 x-sum I/1.py" "b/leetcode/Study Plan/3318. \350\256\241\347\256\227\345\255\220\346\225\260\347\273\204\347\232\204 x-sum I/1.py" index 3d7b717e..0168e8ef 100644 --- "a/leetcode/Study Plan/3318. \350\256\241\347\256\227\345\255\220\346\225\260\347\273\204\347\232\204 x-sum I/1.py" +++ "b/leetcode/Study Plan/3318. \350\256\241\347\256\227\345\255\220\346\225\260\347\273\204\347\232\204 x-sum I/1.py" @@ -67,4 +67,4 @@ def findXSum(self, nums: List[int], k: int, x: int) -> List[int]: arr = sorted(cnt.items(), key = lambda t: (-t[1], -t[0])) res[i - k + 1] = sum(t * y for t, y in arr[:x]) >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 - return res \ No newline at end of file + return res diff --git a/leetcode/Study Plan/3340/1.cpp b/leetcode/Study Plan/3340/1.cpp index c14057e2..d4f354d9 100644 --- a/leetcode/Study Plan/3340/1.cpp +++ b/leetcode/Study Plan/3340/1.cpp @@ -1,22 +1,22 @@ -#include - -class Solution -{ -public: - bool isBalanced(string num) - { - int sum = 0; - for (int i = 0; i < num.size(); i++) - { - if (i % 2 == 0) - { - sum += (num[i] - '0'); - } - else - { - sum -= (num[i] - '0'); - } - } - return sum == 0; - } -}; \ No newline at end of file +#include + +class Solution +{ +public: + bool isBalanced(string num) + { + int sum = 0; + for (int i = 0; i < num.size(); i++) + { + if (i % 2 == 0) + { + sum += (num[i] - '0'); + } + else + { + sum -= (num[i] - '0'); + } + } + return sum == 0; + } +}; diff --git "a/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/2.cpp" "b/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/2.cpp" index 1e556e48..1a787413 100644 --- "a/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/2.cpp" +++ "b/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/2.cpp" @@ -1,57 +1,57 @@ -#include -#include -#include - -using namespace std; - -class Solution -{ - struct State{ - int x, y, dis; - State(int x, int y, int dis) : x(x), y(y), dis(dis) {}; - bool operator<(const State &rth) const{ - return dis > rth.dis; - } - }; - -public: - const int inf = 0x3f3f3f3f; - int minTimeToReach(vector> &moveTime) - { - int n = moveTime.size(), m = moveTime[0].size(); - - vector> d(n, vector(m, inf)); - vector> v(n, vector(m, 0)); - - // 定义方向 - int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; - d[0][0] = 0; - priority_queue q; - q.push(State(0, 0, 0)); - - while(q.size()){ - State s = q.top(); - q.pop(); - //如果该点已经访问 - if(v[s.x][s.y]) - continue; - //标记为已经访问 - v[s.x][s.y] = 1; - - //遍历四个方向 - for (int i = 0; i < 4; ++i) { - int nx = s.x + dirs[i][0]; - int ny = s.y + dirs[i][1]; - //检查是否越界 - if (nx < 0 || nx >= n || ny < 0 || ny >= m)continue; - //计算到达新点的时间 - } - - - - //找到更短时间,加入优先队列 - - //返回最短时间 - } - } -}; \ No newline at end of file +#include +#include +#include + +using namespace std; + +class Solution +{ + struct State{ + int x, y, dis; + State(int x, int y, int dis) : x(x), y(y), dis(dis) {}; + bool operator<(const State &rth) const{ + return dis > rth.dis; + } + }; + +public: + const int inf = 0x3f3f3f3f; + int minTimeToReach(vector> &moveTime) + { + int n = moveTime.size(), m = moveTime[0].size(); + + vector> d(n, vector(m, inf)); + vector> v(n, vector(m, 0)); + + // 定义方向 + int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + d[0][0] = 0; + priority_queue q; + q.push(State(0, 0, 0)); + + while(q.size()){ + State s = q.top(); + q.pop(); + //如果该点已经访问 + if(v[s.x][s.y]) + continue; + //标记为已经访问 + v[s.x][s.y] = 1; + + //遍历四个方向 + for (int i = 0; i < 4; ++i) { + int nx = s.x + dirs[i][0]; + int ny = s.y + dirs[i][1]; + //检查是否越界 + if (nx < 0 || nx >= n || ny < 0 || ny >= m)continue; + //计算到达新点的时间 + } + + + + //找到更短时间,加入优先队列 + + //返回最短时间 + } + } +}; diff --git "a/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/Dijkstra's shortest path.cpp" "b/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/Dijkstra's shortest path.cpp" index 81d78534..85285ed5 100644 --- "a/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/Dijkstra's shortest path.cpp" +++ "b/leetcode/Study Plan/3341_\345\210\260\350\276\276\346\234\200\345\220\216\344\270\200\344\270\252\346\210\277\351\227\264\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264_I/Dijkstra's shortest path.cpp" @@ -1,65 +1,66 @@ -class Solution -{ - // 定义状态结构体,用于优先队列 - struct State - { - int x; // 当前x坐标 - int y; // 当前y坐标 - int dis; // 到达该点的最短时间 - State(int x, int y, int dis) : x(x), y(y), dis(dis) {}; - // 重载小于运算符,用于优先队列排序(小根堆) - bool operator<(const State &rth) const - { - return dis > rth.dis; - } - }; - -public: - const int inf = 0x3f3f3f3f; // 定义无穷大值 - int minTimeToReach(vector> &moveTime) - { - int n = moveTime.size(), m = moveTime[0].size(); - // d数组存储到达每个点的最短时间 - vector> d(n, vector(m, inf)); - // v数组标记点是否已访问 - vector> v(n, vector(m, 0)); - - // 定义四个方向的移动:下、上、右、左 - int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; - d[0][0] = 0; // 起点时间为0 - priority_queue q; // 优先队列,按时间排序 - q.push(State(0, 0, 0)); // 将起点加入队列 - - while (q.size()) - { - State s = q.top(); - q.pop(); - // 如果该点已访问,跳过 - if (v[s.x][s.y]) - { - continue; - } - v[s.x][s.y] = 1; // 标记为已访问 - // 遍历四个方向 - for (int i = 0; i < 4; i++) - { - int nx = s.x + dirs[i][0]; - int ny = s.y + dirs[i][1]; - // 检查是否越界 - if (nx < 0 || nx >= n || ny < 0 || ny >= m) - { - continue; - } - // 计算到达新点的时间:当前点时间、新点移动时间的较大值 + 1 - int dist = max(d[s.x][s.y], moveTime[nx][ny]) + 1; - // 如果找到更短的时间,更新并加入队列 - if (d[nx][ny] > dist) - { - d[nx][ny] = dist; - q.push(State(nx, ny, dist)); - } - } - } - return d[n - 1][m - 1]; // 返回到达终点的最短时间 - } -}; +#include +class Solution +{ + // 定义状态结构体,用于优先队列 + struct State + { + int x; // 当前x坐标 + int y; // 当前y坐标 + int dis; // 到达该点的最短时间 + State(int x, int y, int dis) : x(x), y(y), dis(dis) {}; + // 重载小于运算符,用于优先队列排序(小根堆) + bool operator<(const State &rth) const + { + return dis > rth.dis; + } + }; + +public: + const int inf = 0x3f3f3f3f; // 定义无穷大值 + int minTimeToReach(vector> &moveTime) + { + int n = moveTime.size(), m = moveTime[0].size(); + // d数组存储到达每个点的最短时间 + vector> d(n, vector(m, inf)); + // v数组标记点是否已访问 + vector> v(n, vector(m, 0)); + + // 定义四个方向的移动:下、上、右、左 + int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + d[0][0] = 0; // 起点时间为0 + priority_queue q; // 优先队列,按时间排序 + q.push(State(0, 0, 0)); // 将起点加入队列 + + while (q.size()) + { + State s = q.top(); + q.pop(); + // 如果该点已访问,跳过 + if (v[s.x][s.y]) + { + continue; + } + v[s.x][s.y] = 1; // 标记为已访问 + // 遍历四个方向 + for (int i = 0; i < 4; i++) + { + int nx = s.x + dirs[i][0]; + int ny = s.y + dirs[i][1]; + // 检查是否越界 + if (nx < 0 || nx >= n || ny < 0 || ny >= m) + { + continue; + } + // 计算到达新点的时间:当前点时间、新点移动时间的较大值 + 1 + int dist = max(d[s.x][s.y], moveTime[nx][ny]) + 1; + // 如果找到更短的时间,更新并加入队列 + if (d[nx][ny] > dist) + { + d[nx][ny] = dist; + q.push(State(nx, ny, dist)); + } + } + } + return d[n - 1][m - 1]; // 返回到达终点的最短时间 + } +}; diff --git "a/leetcode/Study Plan/3347. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\345\205\203\347\264\240\347\232\204\346\234\200\351\253\230\351\242\221\347\216\207 II/1.py" "b/leetcode/Study Plan/3347. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\345\205\203\347\264\240\347\232\204\346\234\200\351\253\230\351\242\221\347\216\207 II/1.py" index cdf8d19f..909f2d41 100644 --- "a/leetcode/Study Plan/3347. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\345\205\203\347\264\240\347\232\204\346\234\200\351\253\230\351\242\221\347\216\207 II/1.py" +++ "b/leetcode/Study Plan/3347. \346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\345\205\203\347\264\240\347\232\204\346\234\200\351\253\230\351\242\221\347\216\207 II/1.py" @@ -1,75 +1,75 @@ -class Solution: - def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int: - # 排序数组,便于后续二分查找和统计连续相同元素 - nums.sort() - - # 记录最终答案:某个值能达到的最大出现频率 - ans = 0 - - # 统计原数组中每个数字的出现次数 - num_count = defaultdict(int) - - # 候选模式集合:存储所有可能成为高频目标值的数字 - modes = set() - - def add_mode(value): - """ - 将value及其相关候选值加入modes集合 - - 思路:如果要让某个数字出现次数最多,有三种可能的目标值: - 1. value本身 - 2. value - k:范围内的其他数可以通过 +k 操作变成 value - 3. value + k:范围内的其他数可以通过 -k 操作变成 value - """ - modes.add(value) - # 只有当 value-k 在数组范围内时才加入 - if value - k >= nums[0]: - modes.add(value - k) - # 只有当 value+k 在数组范围内时才加入 - if value + k <= nums[-1]: - modes.add(value + k) - - # 记录当前处理的相同数字的起始位置 - last_num_index = 0 - - # 第一次遍历:统计每个不同数字的出现次数 - for i in range(len(nums)): - # 当遇到不同的数字时 - if nums[i] != nums[last_num_index]: - # 记录前一个数字的出现次数(从last_num_index到i-1) - num_count[nums[last_num_index]] = i - last_num_index - # 更新答案(不操作时的最大频率) - ans = max(ans, i - last_num_index) - # 将该数字及其相关候选值加入modes - add_mode(nums[last_num_index]) - # 更新起始位置为当前位置 - last_num_index = i - - # 处理最后一组相同的数字 - num_count[nums[last_num_index]] = len(nums) - last_num_index - ans = max(ans, len(nums) - last_num_index) - add_mode(nums[last_num_index]) - - # 第二次遍历:对每个候选模式计算能达到的最大频率 - for mode in sorted(modes): - # 使用二分查找找到 [mode-k, mode+k] 范围内的所有元素 - # l: 第一个 >= mode-k 的元素位置 - l = bisect.bisect_left(nums, mode - k) - # r: 最后一个 <= mode+k 的元素位置 - r = bisect.bisect_right(nums, mode + k) - 1 - - # 计算以mode为目标值时能达到的最大频率 - if mode in num_count: - # 情况1:mode原本就存在于数组中 - # 最大频率 = min(范围内总元素数, 原有数量 + 允许操作次数) - temp_ans = min(r - l + 1, num_count[mode] + numOperations) - else: - # 情况2:mode不在原数组中 - # 最大频率 = min(范围内总元素数, 允许操作次数) - # 因为每个变成mode的元素都需要一次操作 - temp_ans = min(r - l + 1, numOperations) - - # 更新全局最大值 - ans = max(ans, temp_ans) - - return ans \ No newline at end of file +class Solution: + def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int: + # 排序数组,便于后续二分查找和统计连续相同元素 + nums.sort() + + # 记录最终答案:某个值能达到的最大出现频率 + ans = 0 + + # 统计原数组中每个数字的出现次数 + num_count = defaultdict(int) + + # 候选模式集合:存储所有可能成为高频目标值的数字 + modes = set() + + def add_mode(value): + """ + 将value及其相关候选值加入modes集合 + + 思路:如果要让某个数字出现次数最多,有三种可能的目标值: + 1. value本身 + 2. value - k:范围内的其他数可以通过 +k 操作变成 value + 3. value + k:范围内的其他数可以通过 -k 操作变成 value + """ + modes.add(value) + # 只有当 value-k 在数组范围内时才加入 + if value - k >= nums[0]: + modes.add(value - k) + # 只有当 value+k 在数组范围内时才加入 + if value + k <= nums[-1]: + modes.add(value + k) + + # 记录当前处理的相同数字的起始位置 + last_num_index = 0 + + # 第一次遍历:统计每个不同数字的出现次数 + for i in range(len(nums)): + # 当遇到不同的数字时 + if nums[i] != nums[last_num_index]: + # 记录前一个数字的出现次数(从last_num_index到i-1) + num_count[nums[last_num_index]] = i - last_num_index + # 更新答案(不操作时的最大频率) + ans = max(ans, i - last_num_index) + # 将该数字及其相关候选值加入modes + add_mode(nums[last_num_index]) + # 更新起始位置为当前位置 + last_num_index = i + + # 处理最后一组相同的数字 + num_count[nums[last_num_index]] = len(nums) - last_num_index + ans = max(ans, len(nums) - last_num_index) + add_mode(nums[last_num_index]) + + # 第二次遍历:对每个候选模式计算能达到的最大频率 + for mode in sorted(modes): + # 使用二分查找找到 [mode-k, mode+k] 范围内的所有元素 + # l: 第一个 >= mode-k 的元素位置 + l = bisect.bisect_left(nums, mode - k) + # r: 最后一个 <= mode+k 的元素位置 + r = bisect.bisect_right(nums, mode + k) - 1 + + # 计算以mode为目标值时能达到的最大频率 + if mode in num_count: + # 情况1:mode原本就存在于数组中 + # 最大频率 = min(范围内总元素数, 原有数量 + 允许操作次数) + temp_ans = min(r - l + 1, num_count[mode] + numOperations) + else: + # 情况2:mode不在原数组中 + # 最大频率 = min(范围内总元素数, 允许操作次数) + # 因为每个变成mode的元素都需要一次操作 + temp_ans = min(r - l + 1, numOperations) + + # 更新全局最大值 + ans = max(ans, temp_ans) + + return ans diff --git "a/leetcode/Study Plan/3350. \346\243\200\346\265\213\347\233\270\351\202\273\351\200\222\345\242\236\345\255\220\346\225\260\347\273\204 II/2.py" "b/leetcode/Study Plan/3350. \346\243\200\346\265\213\347\233\270\351\202\273\351\200\222\345\242\236\345\255\220\346\225\260\347\273\204 II/2.py" index 2f074307..99f0e629 100644 --- "a/leetcode/Study Plan/3350. \346\243\200\346\265\213\347\233\270\351\202\273\351\200\222\345\242\236\345\255\220\346\225\260\347\273\204 II/2.py" +++ "b/leetcode/Study Plan/3350. \346\243\200\346\265\213\347\233\270\351\202\273\351\200\222\345\242\236\345\255\220\346\225\260\347\273\204 II/2.py" @@ -1,19 +1,19 @@ -class Solution(object): - def maxIncreasingSubarrays(self, nums:list[int])->int: - """ - :type nums: List[int] - :rtype: int - """ - n = len(nums) - - pre,current,ans = 1,0,0 - - for i in range(n): - if nums[i] > nums[i-1]: - current +=1 - else: - pre,current = current,1 - ans = max(ans,min(pre,current)) - ans = max(ans,current//2) - - return ans \ No newline at end of file +class Solution(object): + def maxIncreasingSubarrays(self, nums:list[int])->int: + """ + :type nums: List[int] + :rtype: int + """ + n = len(nums) + + pre,current,ans = 1,0,0 + + for i in range(n): + if nums[i] > nums[i-1]: + current +=1 + else: + pre,current = current,1 + ans = max(ans,min(pre,current)) + ans = max(ans,current//2) + + return ans diff --git "a/leetcode/Study Plan/3370. \344\273\205\345\220\253\347\275\256\344\275\215\344\275\215\347\232\204\346\234\200\345\260\217\346\225\264\346\225\260/1.py" "b/leetcode/Study Plan/3370. \344\273\205\345\220\253\347\275\256\344\275\215\344\275\215\347\232\204\346\234\200\345\260\217\346\225\264\346\225\260/1.py" index dabc628b..f47ae3a3 100644 --- "a/leetcode/Study Plan/3370. \344\273\205\345\220\253\347\275\256\344\275\215\344\275\215\347\232\204\346\234\200\345\260\217\346\225\264\346\225\260/1.py" +++ "b/leetcode/Study Plan/3370. \344\273\205\345\220\253\347\275\256\344\275\215\344\275\215\347\232\204\346\234\200\345\260\217\346\225\264\346\225\260/1.py" @@ -4,7 +4,7 @@ def smallestNumber(self, n: int) -> int: x = 1 while x < n: x <<= 1 - x += 1 + x += 1 return x ======= @@ -13,7 +13,7 @@ def smallestNumber(self, n: int) -> int: x = 1 while x < n: x <<= 1 - x += 1 + x += 1 return x >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 diff --git "a/leetcode/Study Plan/3381. \351\225\277\345\272\246\345\217\257\350\242\253 K \346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\205\203\347\264\240\345\222\214/2.cpp" "b/leetcode/Study Plan/3381. \351\225\277\345\272\246\345\217\257\350\242\253 K \346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\205\203\347\264\240\345\222\214/2.cpp" index 6f7647b6..a9ef5995 100644 --- "a/leetcode/Study Plan/3381. \351\225\277\345\272\246\345\217\257\350\242\253 K \346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\205\203\347\264\240\345\222\214/2.cpp" +++ "b/leetcode/Study Plan/3381. \351\225\277\345\272\246\345\217\257\350\242\253 K \346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\205\203\347\264\240\345\222\214/2.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: long long maxSubarraySum(vector& nums, int k) { diff --git "a/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/binary.cpp" "b/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/binary.cpp" index 7f743d86..2c7ae29f 100644 --- "a/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/binary.cpp" +++ "b/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/binary.cpp" @@ -1,100 +1,100 @@ -#include -#include -using namespace std; - -class Solution -{ -public: - // 查找第一个等于target的位置 - int findFirst(vector &nums, int target) - { - int left = 0, right = nums.size() - 1; - int result = -1; - - while (left <= right) - { - int mid = left + (right - left) / 2; - - if (nums[mid] == target) - { - result = mid; // 记录当前位置 - right = mid - 1; // 继续在左半部分查找 - } - else if (nums[mid] < target) - { - left = mid + 1; - } - else - { - right = mid - 1; - } - } - - return result; - } - - // 查找最后一个等于target的位置 - int findLast(vector &nums, int target) - { - int left = 0, right = nums.size() - 1; - int result = -1; - - while (left <= right) - { - int mid = left + (right - left) / 2; - - if (nums[mid] == target) - { - result = mid; // 记录当前位置 - left = mid + 1; // 继续在右半部分查找 - } - else if (nums[mid] < target) - { - left = mid + 1; - } - else - { - right = mid - 1; - } - } - - return result; - } - - vector searchRange(vector &nums, int target) - { - int first = findFirst(nums, target); - int last = findLast(nums, target); - - return {first, last}; - } -}; - -// 测试函数 -int main() -{ - Solution solution; - - // 测试用例1 - vector nums1 = {5, 7, 7, 8, 8, 10}; - int target1 = 8; - vector result1 = solution.searchRange(nums1, target1); - cout << "数组: [5,7,7,8,8,10], 目标: " << target1 << endl; - cout << "结果: [" << result1[0] << ", " << result1[1] << "]" << endl; - - // 测试用例2 - vector nums2 = {5, 7, 7, 8, 8, 10}; - int target2 = 6; - vector result2 = solution.searchRange(nums2, target2); - cout << "数组: [5,7,7,8,8,10], 目标: " << target2 << endl; - cout << "结果: [" << result2[0] << ", " << result2[1] << "]" << endl; - - // 测试用例3 - vector nums3 = {}; - int target3 = 0; - vector result3 = solution.searchRange(nums3, target3); - cout << "数组: [], 目标: " << target3 << endl; - cout << "结果: [" << result3[0] << ", " << result3[1] << "]" << endl; - - return 0; -} \ No newline at end of file +#include +#include +using namespace std; + +class Solution +{ +public: + // 查找第一个等于target的位置 + int findFirst(vector &nums, int target) + { + int left = 0, right = nums.size() - 1; + int result = -1; + + while (left <= right) + { + int mid = left + (right - left) / 2; + + if (nums[mid] == target) + { + result = mid; // 记录当前位置 + right = mid - 1; // 继续在左半部分查找 + } + else if (nums[mid] < target) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return result; + } + + // 查找最后一个等于target的位置 + int findLast(vector &nums, int target) + { + int left = 0, right = nums.size() - 1; + int result = -1; + + while (left <= right) + { + int mid = left + (right - left) / 2; + + if (nums[mid] == target) + { + result = mid; // 记录当前位置 + left = mid + 1; // 继续在右半部分查找 + } + else if (nums[mid] < target) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return result; + } + + vector searchRange(vector &nums, int target) + { + int first = findFirst(nums, target); + int last = findLast(nums, target); + + return {first, last}; + } +}; + +// 测试函数 +int main() +{ + Solution solution; + + // 测试用例1 + vector nums1 = {5, 7, 7, 8, 8, 10}; + int target1 = 8; + vector result1 = solution.searchRange(nums1, target1); + cout << "数组: [5,7,7,8,8,10], 目标: " << target1 << endl; + cout << "结果: [" << result1[0] << ", " << result1[1] << "]" << endl; + + // 测试用例2 + vector nums2 = {5, 7, 7, 8, 8, 10}; + int target2 = 6; + vector result2 = solution.searchRange(nums2, target2); + cout << "数组: [5,7,7,8,8,10], 目标: " << target2 << endl; + cout << "结果: [" << result2[0] << ", " << result2[1] << "]" << endl; + + // 测试用例3 + vector nums3 = {}; + int target3 = 0; + vector result3 = solution.searchRange(nums3, target3); + cout << "数组: [], 目标: " << target3 << endl; + cout << "结果: [" << result3[0] << ", " << result3[1] << "]" << endl; + + return 0; +} diff --git "a/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/simple_binary.cpp" "b/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/simple_binary.cpp" index a899fb9f..9eeeebfe 100644 --- "a/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/simple_binary.cpp" +++ "b/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/simple_binary.cpp" @@ -1,85 +1,85 @@ -#include -#include -using namespace std; - -class Solution -{ -public: - vector searchRange(vector &nums, int target) - { - int n = nums.size(); - if (n == 0) - return {-1, -1}; - - // 先找到target的任意一个位置 - int left = 0, right = n - 1; - int found = -1; - - while (left <= right) - { - int mid = left + (right - left) / 2; - if (nums[mid] == target) - { - found = mid; - break; - } - else if (nums[mid] < target) - { - left = mid + 1; - } - else - { - right = mid - 1; - } - } - - if (found == -1) - return {-1, -1}; - - // 从找到的位置向两边扩展 - int first = found, last = found; - - // 向左找第一个位置 - while (first > 0 && nums[first - 1] == target) - { - first--; - } - - // 向右找最后一个位置 - while (last < n - 1 && nums[last + 1] == target) - { - last++; - } - - return {first, last}; - } -}; - -// 测试函数 -int main() -{ - Solution solution; - - // 测试用例1 - vector nums1 = {5, 7, 7, 8, 8, 10}; - int target1 = 8; - vector result1 = solution.searchRange(nums1, target1); - cout << "数组: [5,7,7,8,8,10], 目标: " << target1 << endl; - cout << "结果: [" << result1[0] << ", " << result1[1] << "]" << endl; - - // 测试用例2 - vector nums2 = {5, 7, 7, 8, 8, 10}; - int target2 = 6; - vector result2 = solution.searchRange(nums2, target2); - cout << "数组: [5,7,7,8,8,10], 目标: " << target2 << endl; - cout << "结果: [" << result2[0] << ", " << result2[1] << "]" << endl; - - // 测试用例3 - vector nums3 = {1, 1, 1, 1, 1}; - int target3 = 1; - vector result3 = solution.searchRange(nums3, target3); - cout << "数组: [1,1,1,1,1], 目标: " << target3 << endl; - cout << "结果: [" << result3[0] << ", " << result3[1] << "]" << endl; - - return 0; -} \ No newline at end of file +#include +#include +using namespace std; + +class Solution +{ +public: + vector searchRange(vector &nums, int target) + { + int n = nums.size(); + if (n == 0) + return {-1, -1}; + + // 先找到target的任意一个位置 + int left = 0, right = n - 1; + int found = -1; + + while (left <= right) + { + int mid = left + (right - left) / 2; + if (nums[mid] == target) + { + found = mid; + break; + } + else if (nums[mid] < target) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + if (found == -1) + return {-1, -1}; + + // 从找到的位置向两边扩展 + int first = found, last = found; + + // 向左找第一个位置 + while (first > 0 && nums[first - 1] == target) + { + first--; + } + + // 向右找最后一个位置 + while (last < n - 1 && nums[last + 1] == target) + { + last++; + } + + return {first, last}; + } +}; + +// 测试函数 +int main() +{ + Solution solution; + + // 测试用例1 + vector nums1 = {5, 7, 7, 8, 8, 10}; + int target1 = 8; + vector result1 = solution.searchRange(nums1, target1); + cout << "数组: [5,7,7,8,8,10], 目标: " << target1 << endl; + cout << "结果: [" << result1[0] << ", " << result1[1] << "]" << endl; + + // 测试用例2 + vector nums2 = {5, 7, 7, 8, 8, 10}; + int target2 = 6; + vector result2 = solution.searchRange(nums2, target2); + cout << "数组: [5,7,7,8,8,10], 目标: " << target2 << endl; + cout << "结果: [" << result2[0] << ", " << result2[1] << "]" << endl; + + // 测试用例3 + vector nums3 = {1, 1, 1, 1, 1}; + int target3 = 1; + vector result3 = solution.searchRange(nums3, target3); + cout << "数组: [1,1,1,1,1], 目标: " << target3 << endl; + cout << "结果: [" << result3[0] << ", " << result3[1] << "]" << endl; + + return 0; +} diff --git "a/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/stl_binary.cpp" "b/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/stl_binary.cpp" index 0f652ccd..69ac86b9 100644 --- "a/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/stl_binary.cpp" +++ "b/leetcode/Study Plan/34. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/stl_binary.cpp" @@ -1,58 +1,58 @@ -#include -#include -#include -using namespace std; - -class Solution -{ -public: - vector searchRange(vector &nums, int target) - { - // 使用lower_bound找到第一个大于等于target的位置 - auto first = lower_bound(nums.begin(), nums.end(), target); - - // 如果没找到或者找到的值不等于target - if (first == nums.end() || *first != target) - { - return {-1, -1}; - } - - // 使用upper_bound找到第一个大于target的位置 - auto last = upper_bound(nums.begin(), nums.end(), target); - - // 转换为索引 - int firstIndex = first - nums.begin(); - int lastIndex = (last - nums.begin()) - 1; // 减1是因为upper_bound返回的是第一个大于的位置 - - return {firstIndex, lastIndex}; - } -}; - -// 测试函数 -int main() -{ - Solution solution; - - // 测试用例1 - vector nums1 = {5, 7, 7, 8, 8, 10}; - int target1 = 8; - vector result1 = solution.searchRange(nums1, target1); - cout << "数组: [5,7,7,8,8,10], 目标: " << target1 << endl; - cout << "结果: [" << result1[0] << ", " << result1[1] << "]" << endl; - - // 测试用例2 - vector nums2 = {5, 7, 7, 8, 8, 10}; - int target2 = 6; - vector result2 = solution.searchRange(nums2, target2); - cout << "数组: [5,7,7,8,8,10], 目标: " << target2 << endl; - cout << "结果: [" << result2[0] << ", " << result2[1] << "]" << endl; - - // 测试用例3 - vector nums3 = {1, 1, 1, 1, 1}; - int target3 = 1; - vector result3 = solution.searchRange(nums3, target3); - cout << "数组: [1,1,1,1,1], 目标: " << target3 << endl; - cout << "结果: [" << result3[0] << ", " << result3[1] << "]" << endl; - - return 0; -} \ No newline at end of file +#include +#include +#include +using namespace std; + +class Solution +{ +public: + vector searchRange(vector &nums, int target) + { + // 使用lower_bound找到第一个大于等于target的位置 + auto first = lower_bound(nums.begin(), nums.end(), target); + + // 如果没找到或者找到的值不等于target + if (first == nums.end() || *first != target) + { + return {-1, -1}; + } + + // 使用upper_bound找到第一个大于target的位置 + auto last = upper_bound(nums.begin(), nums.end(), target); + + // 转换为索引 + int firstIndex = first - nums.begin(); + int lastIndex = (last - nums.begin()) - 1; // 减1是因为upper_bound返回的是第一个大于的位置 + + return {firstIndex, lastIndex}; + } +}; + +// 测试函数 +int main() +{ + Solution solution; + + // 测试用例1 + vector nums1 = {5, 7, 7, 8, 8, 10}; + int target1 = 8; + vector result1 = solution.searchRange(nums1, target1); + cout << "数组: [5,7,7,8,8,10], 目标: " << target1 << endl; + cout << "结果: [" << result1[0] << ", " << result1[1] << "]" << endl; + + // 测试用例2 + vector nums2 = {5, 7, 7, 8, 8, 10}; + int target2 = 6; + vector result2 = solution.searchRange(nums2, target2); + cout << "数组: [5,7,7,8,8,10], 目标: " << target2 << endl; + cout << "结果: [" << result2[0] << ", " << result2[1] << "]" << endl; + + // 测试用例3 + vector nums3 = {1, 1, 1, 1, 1}; + int target3 = 1; + vector result3 = solution.searchRange(nums3, target3); + cout << "数组: [1,1,1,1,1], 目标: " << target3 << endl; + cout << "结果: [" << result3[0] << ", " << result3[1] << "]" << endl; + + return 0; +} diff --git "a/leetcode/Study Plan/3432. \347\273\237\350\256\241\345\205\203\347\264\240\345\222\214\345\267\256\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\345\210\206\345\214\272\346\226\271\346\241\210/2.cpp" "b/leetcode/Study Plan/3432. \347\273\237\350\256\241\345\205\203\347\264\240\345\222\214\345\267\256\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\345\210\206\345\214\272\346\226\271\346\241\210/2.cpp" index e69de29b..8b137891 100644 --- "a/leetcode/Study Plan/3432. \347\273\237\350\256\241\345\205\203\347\264\240\345\222\214\345\267\256\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\345\210\206\345\214\272\346\226\271\346\241\210/2.cpp" +++ "b/leetcode/Study Plan/3432. \347\273\237\350\256\241\345\205\203\347\264\240\345\222\214\345\267\256\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\345\210\206\345\214\272\346\226\271\346\241\210/2.cpp" @@ -0,0 +1 @@ + diff --git "a/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.py" "b/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.py" index 649eb5bb..05854fa7 100644 --- "a/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.py" +++ "b/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.py" @@ -12,11 +12,11 @@ def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int curr_t = int(timestamp) # Handle OFFLINE event: user goes offline for 60 seconds if type_ == "OFFLINE": - online_t[int(mention)] = curr_t + 60 + online_t[int(mention)] = curr_t + 60 # Handle ALL mentions: all users are mentioned elif mention == "ALL": for i in range(numberOfUsers): - ans[i] += 1 + ans[i] += 1 # Handle HERE mentions: only currently online users are mentioned elif mention == "HERE": for i,t in enumerate(online_t): diff --git "a/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/1.cpp" "b/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/1.cpp" index 4e43a394..40c6bd4e 100644 --- "a/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/1.cpp" +++ "b/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-12 14:43:32 diff --git "a/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/2.cpp" "b/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/2.cpp" index cea27936..d395e846 100644 --- "a/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/2.cpp" +++ "b/leetcode/Study Plan/344. \345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/2.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-12 14:43:32 diff --git "a/leetcode/Study Plan/3454. \345\210\206\345\211\262\346\255\243\346\226\271\345\275\242 II/1.cpp" "b/leetcode/Study Plan/3454. \345\210\206\345\211\262\346\255\243\346\226\271\345\275\242 II/1.cpp" index 5dc3e0bd..51f2ed88 100644 --- "a/leetcode/Study Plan/3454. \345\210\206\345\211\262\346\255\243\346\226\271\345\275\242 II/1.cpp" +++ "b/leetcode/Study Plan/3454. \345\210\206\345\211\262\346\255\243\346\226\271\345\275\242 II/1.cpp" @@ -1,3 +1,4 @@ +#include class SegmentTree { private: vector count; diff --git "a/leetcode/Study Plan/3461. \345\210\244\346\226\255\346\223\215\344\275\234\345\220\216\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\225\260\345\255\227\346\230\257\345\220\246\347\233\270\347\255\211 I/2.py" "b/leetcode/Study Plan/3461. \345\210\244\346\226\255\346\223\215\344\275\234\345\220\216\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\225\260\345\255\227\346\230\257\345\220\246\347\233\270\347\255\211 I/2.py" index 095e26db..f01e0e62 100644 --- "a/leetcode/Study Plan/3461. \345\210\244\346\226\255\346\223\215\344\275\234\345\220\216\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\225\260\345\255\227\346\230\257\345\220\246\347\233\270\347\255\211 I/2.py" +++ "b/leetcode/Study Plan/3461. \345\210\244\346\226\255\346\223\215\344\275\234\345\220\216\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\225\260\345\255\227\346\230\257\345\220\246\347\233\270\347\255\211 I/2.py" @@ -1,11 +1,11 @@ -class Solution: - def hasSameDigits(self, s: str) -> bool: - n = len(s) - while n > 2: - temp_s = "" - for i in range(len(s) - 1): - temp_s += str((int(s[i]) + int(s[i + 1])) % 10) - s = temp_s - n = len(s) - - return s[0] == s[1] \ No newline at end of file +class Solution: + def hasSameDigits(self, s: str) -> bool: + n = len(s) + while n > 2: + temp_s = "" + for i in range(len(s) - 1): + temp_s += str((int(s[i]) + int(s[i + 1])) % 10) + s = temp_s + n = len(s) + + return s[0] == s[1] diff --git "a/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/1.cpp" "b/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/1.cpp" index c7b9c378..8b78bc30 100644 --- "a/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/1.cpp" +++ "b/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-11 09:39:48 diff --git "a/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/2.cpp" "b/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/2.cpp" index b9fb2ede..5599f7ba 100644 --- "a/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/2.cpp" +++ "b/leetcode/Study Plan/349. \344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/2.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: diff --git "a/leetcode/Study Plan/3507. \347\247\273\351\231\244\346\234\200\345\260\217\346\225\260\345\257\271\344\275\277\346\225\260\347\273\204\346\234\211\345\272\217 I/1.py" "b/leetcode/Study Plan/3507. \347\247\273\351\231\244\346\234\200\345\260\217\346\225\260\345\257\271\344\275\277\346\225\260\347\273\204\346\234\211\345\272\217 I/1.py" index ab3359cf..da108f08 100644 --- "a/leetcode/Study Plan/3507. \347\247\273\351\231\244\346\234\200\345\260\217\346\225\260\345\257\271\344\275\277\346\225\260\347\273\204\346\234\211\345\272\217 I/1.py" +++ "b/leetcode/Study Plan/3507. \347\247\273\351\231\244\346\234\200\345\260\217\346\225\260\345\257\271\344\275\277\346\225\260\347\273\204\346\234\211\345\272\217 I/1.py" @@ -22,7 +22,7 @@ def minimumPairRemoval(self, nums: List[int]) -> int: if minSum > pair_sum: minSum = pair_sum target_idx = i - + if isAscending: break @@ -31,6 +31,6 @@ def minimumPairRemoval(self, nums: List[int]) -> int: nums.pop(target_idx+1) return cnt - - - + + + diff --git "a/leetcode/Study Plan/3567. \345\255\220\347\237\251\351\230\265\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256/1.cpp" "b/leetcode/Study Plan/3567. \345\255\220\347\237\251\351\230\265\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256/1.cpp" index 0dcf48c2..11074172 100644 --- "a/leetcode/Study Plan/3567. \345\255\220\347\237\251\351\230\265\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256/1.cpp" +++ "b/leetcode/Study Plan/3567. \345\255\220\347\237\251\351\230\265\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256/1.cpp" @@ -1,3 +1,4 @@ +#include #include class Solution { public: diff --git "a/leetcode/Study Plan/3577. \347\273\237\350\256\241\350\256\241\347\256\227\346\234\272\350\247\243\351\224\201\351\241\272\345\272\217\346\216\222\345\210\227\346\225\260/1.cpp" "b/leetcode/Study Plan/3577. \347\273\237\350\256\241\350\256\241\347\256\227\346\234\272\350\247\243\351\224\201\351\241\272\345\272\217\346\216\222\345\210\227\346\225\260/1.cpp" index dd0b2f32..738c524c 100644 --- "a/leetcode/Study Plan/3577. \347\273\237\350\256\241\350\256\241\347\256\227\346\234\272\350\247\243\351\224\201\351\241\272\345\272\217\346\216\222\345\210\227\346\225\260/1.cpp" +++ "b/leetcode/Study Plan/3577. \347\273\237\350\256\241\350\256\241\347\256\227\346\234\272\350\247\243\351\224\201\351\241\272\345\272\217\346\216\222\345\210\227\346\225\260/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: int countPermutations(vector& complexity) { diff --git "a/leetcode/Study Plan/3578. \347\273\237\350\256\241\346\236\201\345\267\256\346\234\200\345\244\247\344\270\272 K \347\232\204\345\210\206\345\211\262\346\226\271\345\274\217\346\225\260/2.cpp" "b/leetcode/Study Plan/3578. \347\273\237\350\256\241\346\236\201\345\267\256\346\234\200\345\244\247\344\270\272 K \347\232\204\345\210\206\345\211\262\346\226\271\345\274\217\346\225\260/2.cpp" index c184307a..0865380e 100644 --- "a/leetcode/Study Plan/3578. \347\273\237\350\256\241\346\236\201\345\267\256\346\234\200\345\244\247\344\270\272 K \347\232\204\345\210\206\345\211\262\346\226\271\345\274\217\346\225\260/2.cpp" +++ "b/leetcode/Study Plan/3578. \347\273\237\350\256\241\346\236\201\345\267\256\346\234\200\345\244\247\344\270\272 K \347\232\204\345\210\206\345\211\262\346\226\271\345\274\217\346\225\260/2.cpp" @@ -1,3 +1,4 @@ +#include #include #include using namespace std; diff --git "a/leetcode/Study Plan/3583. \347\273\237\350\256\241\347\211\271\346\256\212\344\270\211\345\205\203\347\273\204/1.cpp" "b/leetcode/Study Plan/3583. \347\273\237\350\256\241\347\211\271\346\256\212\344\270\211\345\205\203\347\273\204/1.cpp" index dbd94dfa..8c409b5f 100644 --- "a/leetcode/Study Plan/3583. \347\273\237\350\256\241\347\211\271\346\256\212\344\270\211\345\205\203\347\273\204/1.cpp" +++ "b/leetcode/Study Plan/3583. \347\273\237\350\256\241\347\211\271\346\256\212\344\270\211\345\205\203\347\273\204/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: int specialTriplets(vector &nums) { diff --git "a/leetcode/Study Plan/3600. \345\215\207\347\272\247\345\220\216\346\234\200\345\244\247\347\224\237\346\210\220\346\240\221\347\250\263\345\256\232\346\200\247/1.py" "b/leetcode/Study Plan/3600. \345\215\207\347\272\247\345\220\216\346\234\200\345\244\247\347\224\237\346\210\220\346\240\221\347\250\263\345\256\232\346\200\247/1.py" index f71720ec..950a1fe6 100644 --- "a/leetcode/Study Plan/3600. \345\215\207\347\272\247\345\220\216\346\234\200\345\244\247\347\224\237\346\210\220\346\240\221\347\250\263\345\256\232\346\200\247/1.py" +++ "b/leetcode/Study Plan/3600. \345\215\207\347\272\247\345\220\216\346\234\200\345\244\247\347\224\237\346\210\220\346\240\221\347\250\263\345\256\232\346\200\247/1.py" @@ -39,7 +39,7 @@ def merge(self,from_:int,to:int)->bool: self._fa[x] = y self.cc -= 1 return True - + class Solution: def maxStability(self, n: int, edges: List[List[int]], k: int) -> int: must_uf = UnionFind(n) @@ -77,5 +77,5 @@ def check(low:int)->bool: if check(mid): left = mid else: - right = mid + right = mid return left diff --git "a/leetcode/Study Plan/3606. \344\274\230\346\203\240\345\210\270\346\240\241\351\252\214\345\231\250/1.cpp" "b/leetcode/Study Plan/3606. \344\274\230\346\203\240\345\210\270\346\240\241\351\252\214\345\231\250/1.cpp" index 316fe2a9..0150a0f9 100644 --- "a/leetcode/Study Plan/3606. \344\274\230\346\203\240\345\210\270\346\240\241\351\252\214\345\231\250/1.cpp" +++ "b/leetcode/Study Plan/3606. \344\274\230\346\203\240\345\210\270\346\240\241\351\252\214\345\231\250/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include @@ -18,7 +19,7 @@ class Solution { } // Sort by priority first, then by code lexicographically sort(ans.begin(), ans.end()); - + // Extract just the codes for the result vector result; for(auto &p : ans) @@ -27,7 +28,7 @@ class Solution { } return result; } - + private: vector vaild_business = { "electronics", @@ -35,7 +36,7 @@ class Solution { "pharmacy", "restaurant" }; - + // Get priority of business line (0 = highest priority) int getBusinessPriority(const string &b) { @@ -43,7 +44,7 @@ class Solution { if(it == vaild_business.end()) return -1; return it - vaild_business.begin(); } - + bool isValid(const string &c, const string &b) { if(find(vaild_business.begin(), vaild_business.end(), b) == vaild_business.end()) @@ -55,7 +56,7 @@ class Solution { bool isAlpha = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); bool isDigit = ch >= '0' && ch <= '9'; bool isUnderscore = ch == '_'; - + if(!isAlpha && !isDigit && !isUnderscore) return false; } diff --git a/leetcode/Study Plan/382/382.cpp b/leetcode/Study Plan/382/382.cpp index 1a6f5e35..691d7bdd 100644 --- a/leetcode/Study Plan/382/382.cpp +++ b/leetcode/Study Plan/382/382.cpp @@ -1,46 +1,46 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * struct ListNode *next; - * }; - */ - - - -typedef struct { - struct ListNode *head; - int length; -} Solution; - - -Solution* solutionCreate(struct ListNode* head) { - Solution *obj = (Solution *)malloc(sizeof(Solution)); - obj->head = head; - obj->length = 0; - for (struct ListNode *p = head;p!=NULL;p = p->next){ - obj->length++; - } - return obj; -} - -int solutionGetRandom(Solution* obj) { - int index = rand() % obj->length; - struct ListNode *cur = obj->head; - for (int i = 0;inext; - } - return cur->val; -} - -void solutionFree(Solution* obj) { - free(obj); -} - -/** - * Your Solution struct will be instantiated and called as such: - * Solution* obj = solutionCreate(head); - * int param_1 = solutionGetRandom(obj); - - * solutionFree(obj); -*/ \ No newline at end of file +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + + + +typedef struct { + struct ListNode *head; + int length; +} Solution; + + +Solution* solutionCreate(struct ListNode* head) { + Solution *obj = (Solution *)malloc(sizeof(Solution)); + obj->head = head; + obj->length = 0; + for (struct ListNode *p = head;p!=NULL;p = p->next){ + obj->length++; + } + return obj; +} + +int solutionGetRandom(Solution* obj) { + int index = rand() % obj->length; + struct ListNode *cur = obj->head; + for (int i = 0;inext; + } + return cur->val; +} + +void solutionFree(Solution* obj) { + free(obj); +} + +/** + * Your Solution struct will be instantiated and called as such: + * Solution* obj = solutionCreate(head); + * int param_1 = solutionGetRandom(obj); + + * solutionFree(obj); +*/ diff --git a/leetcode/Study Plan/383/1.cpp b/leetcode/Study Plan/383/1.cpp index 85d3424a..175910d9 100644 --- a/leetcode/Study Plan/383/1.cpp +++ b/leetcode/Study Plan/383/1.cpp @@ -1,28 +1,28 @@ -#include -#include -using namespace std; - -class Solution -{ -public: - bool canConstruct(string ransomNote, string magazine) - { - for(int i=0;i +#include +using namespace std; + +class Solution +{ +public: + bool canConstruct(string ransomNote, string magazine) + { + for(int i=0;i -#include -#include -using namespace std; - -class Solution -{ -public: - bool canConstruct(string ransomNote, string magazine) - { - if (ransomNote.size() > magazine.size()) - { - return false; - } - vector cnt(26); - for (auto &c : magazine) - { - cnt[c - 'a']++; - } - for (auto &c : ransomNote) - { - cnt[c - 'a']--; - if (cnt[c - 'a'] < 0) - { - return false; - } - } - return true; - } -}; - - -int main() -{ - string ransomNote = "aa"; - string magazine = "aab"; - Solution s; - bool result = s.canConstruct(ransomNote, magazine); - cout << (result ? "true" : "false") << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +using namespace std; + +class Solution +{ +public: + bool canConstruct(string ransomNote, string magazine) + { + if (ransomNote.size() > magazine.size()) + { + return false; + } + vector cnt(26); + for (auto &c : magazine) + { + cnt[c - 'a']++; + } + for (auto &c : ransomNote) + { + cnt[c - 'a']--; + if (cnt[c - 'a'] < 0) + { + return false; + } + } + return true; + } +}; + + +int main() +{ + string ransomNote = "aa"; + string magazine = "aab"; + Solution s; + bool result = s.canConstruct(ransomNote, magazine); + cout << (result ? "true" : "false") << endl; + return 0; +} diff --git a/leetcode/Study Plan/383/3.cpp b/leetcode/Study Plan/383/3.cpp index 37ae16d5..d8f92aaf 100644 --- a/leetcode/Study Plan/383/3.cpp +++ b/leetcode/Study Plan/383/3.cpp @@ -1,31 +1,31 @@ -#include -#include -#include -using namespace std; - -class Solution -{ -public: - bool canConstruct(string ransomNote, string magazine) - { - if(ransomNote.size() > magazine.size())return false; - vector cnt(26); - for(char c : magazine){ - cnt[c - 'a']++; - } - for(auto c:ransomNote){ - if(--cnt[c - 'a'] < 0)return false; - } - return true; - } -}; - -int main() -{ - string ransomNote = "aa"; - string magazine = "aab"; - Solution s; - bool result = s.canConstruct(ransomNote, magazine); - cout << (result ? "true" : "false") << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +using namespace std; + +class Solution +{ +public: + bool canConstruct(string ransomNote, string magazine) + { + if(ransomNote.size() > magazine.size())return false; + vector cnt(26); + for(char c : magazine){ + cnt[c - 'a']++; + } + for(auto c:ransomNote){ + if(--cnt[c - 'a'] < 0)return false; + } + return true; + } +}; + +int main() +{ + string ransomNote = "aa"; + string magazine = "aab"; + Solution s; + bool result = s.canConstruct(ransomNote, magazine); + cout << (result ? "true" : "false") << endl; + return 0; +} diff --git a/leetcode/Study Plan/389/1.cpp b/leetcode/Study Plan/389/1.cpp index 277fd6ca..cf75b27e 100644 --- a/leetcode/Study Plan/389/1.cpp +++ b/leetcode/Study Plan/389/1.cpp @@ -1,32 +1,32 @@ -#include -#include -#include - -using namespace std; - -class Solution { -public: - char findTheDifference(string s, string t) { - vector v; - for(int i=0;i +#include +#include + +using namespace std; + +class Solution { +public: + char findTheDifference(string s, string t) { + vector v; + for(int i=0;i -#include -#include - -using namespace std; - -class Solution -{ -public: - char findTheDifference(string s, string t) - { - vector cnt(26,0); - for(char c:s){ - cnt[c - 'a']++; - } - for(char c:t){ - cnt[c - 'a']--; - } - for(int i = 0; i < 26; i++){ - if(cnt[i] < 0)return 'a' + i; - } - return ' '; - } -}; - -int main() -{ - Solution sol; - string s = "abcd"; - string t = "abcde"; - cout << sol.findTheDifference(s, t) << endl; // 输出:"e" - return 0; -} \ No newline at end of file +//计数 + +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + char findTheDifference(string s, string t) + { + vector cnt(26,0); + for(char c:s){ + cnt[c - 'a']++; + } + for(char c:t){ + cnt[c - 'a']--; + } + for(int i = 0; i < 26; i++){ + if(cnt[i] < 0)return 'a' + i; + } + return ' '; + } +}; + +int main() +{ + Solution sol; + string s = "abcd"; + string t = "abcde"; + cout << sol.findTheDifference(s, t) << endl; // 输出:"e" + return 0; +} diff --git a/leetcode/Study Plan/389/3.cpp b/leetcode/Study Plan/389/3.cpp index e86fbf9d..93e5db25 100644 --- a/leetcode/Study Plan/389/3.cpp +++ b/leetcode/Study Plan/389/3.cpp @@ -1,37 +1,37 @@ -//求和 - -// 计数 - -#include -#include -#include - -using namespace std; - -class Solution -{ -public: -// 注意加法和减法的操作的先后次序,字符串t的字符串ASCII码值更大,应该先做加法,再对s字符串做减法,不然得到的值是负数,无法得到对应的正确结果 - char findTheDifference(string s, string t) - { - int sum = 0; - for (char c : t) - { - sum += c; - } - for (char c : s) - { - sum -= c; - } - return (char)sum; - } -}; - -int main() -{ - Solution sol; - string s = "abcd"; - string t = "abcde"; - cout << sol.findTheDifference(s, t) << endl; - return 0; -} \ No newline at end of file +//求和 + +// 计数 + +#include +#include +#include + +using namespace std; + +class Solution +{ +public: +// 注意加法和减法的操作的先后次序,字符串t的字符串ASCII码值更大,应该先做加法,再对s字符串做减法,不然得到的值是负数,无法得到对应的正确结果 + char findTheDifference(string s, string t) + { + int sum = 0; + for (char c : t) + { + sum += c; + } + for (char c : s) + { + sum -= c; + } + return (char)sum; + } +}; + +int main() +{ + Solution sol; + string s = "abcd"; + string t = "abcde"; + cout << sol.findTheDifference(s, t) << endl; + return 0; +} diff --git a/leetcode/Study Plan/389/4.cpp b/leetcode/Study Plan/389/4.cpp index 6d5997dc..b2cdc94e 100644 --- a/leetcode/Study Plan/389/4.cpp +++ b/leetcode/Study Plan/389/4.cpp @@ -1,45 +1,45 @@ -//位运算 - -// 求和 - -// 计数 - -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - char findTheDifference(string s, string t) - { - // 初始化一个变量来存储异或操作的结果 - char ret = 0; - - // 对字符串s中的所有字符进行异或操作 - // 这将有效地抵消所有成对出现的字符 - for(char c : s){ - ret ^= c; - } - - // 对字符串t中的所有字符进行异或操作 - // 由于t比s多一个字符,这将使我们得到多出的那个字符 - for(char c : t){ - ret ^= c; - } - - // 结果将是字符串t中多出的那个字符 - return ret; - } -}; - -int main() -{ - Solution sol; - string s = "abcd"; - string t = "abcde"; - cout << sol.findTheDifference(s, t) << endl; - return 0; -} \ No newline at end of file +//位运算 + +// 求和 + +// 计数 + +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + char findTheDifference(string s, string t) + { + // 初始化一个变量来存储异或操作的结果 + char ret = 0; + + // 对字符串s中的所有字符进行异或操作 + // 这将有效地抵消所有成对出现的字符 + for(char c : s){ + ret ^= c; + } + + // 对字符串t中的所有字符进行异或操作 + // 由于t比s多一个字符,这将使我们得到多出的那个字符 + for(char c : t){ + ret ^= c; + } + + // 结果将是字符串t中多出的那个字符 + return ret; + } +}; + +int main() +{ + Solution sol; + string s = "abcd"; + string t = "abcde"; + cout << sol.findTheDifference(s, t) << endl; + return 0; +} diff --git a/leetcode/Study Plan/412/1.cpp b/leetcode/Study Plan/412/1.cpp index e06b9323..72ff5910 100644 --- a/leetcode/Study Plan/412/1.cpp +++ b/leetcode/Study Plan/412/1.cpp @@ -1,36 +1,36 @@ -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - vector fizzBuzz(int n) - { - vector result; - for(int i = 1; i <= n; i++){ - if(i % 3 == 0 && i % 5 == 0) - result.push_back("FizzBuzz"); - else if(i % 3 == 0) - result.push_back("Fizz"); - else if(i % 5 == 0) - result.push_back("Buzz"); - else - result.push_back(to_string(i)); - } - return result; - } -}; - -int main() -{ - Solution s; - vector result = s.fizzBuzz(15); - for(const string& str : result) { - cout << str << " "; - } - cout << endl; - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + vector fizzBuzz(int n) + { + vector result; + for(int i = 1; i <= n; i++){ + if(i % 3 == 0 && i % 5 == 0) + result.push_back("FizzBuzz"); + else if(i % 3 == 0) + result.push_back("Fizz"); + else if(i % 5 == 0) + result.push_back("Buzz"); + else + result.push_back(to_string(i)); + } + return result; + } +}; + +int main() +{ + Solution s; + vector result = s.fizzBuzz(15); + for(const string& str : result) { + cout << str << " "; + } + cout << endl; + return 0; +} diff --git "a/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.cpp" "b/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.cpp" index 81aa95b6..72d7c61d 100644 --- "a/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.cpp" +++ "b/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.cpp" @@ -1,26 +1,27 @@ -class Solution { -public: - int trap(vector& height) { - int n = height.size(); - int left = 0, right = n - 1; - int suf_max = 0, pre_max = 0, ans = 0; - while (left <= right) - { - pre_max = max(pre_max, height[left]); - suf_max = max(suf_max, height[right]); - - if (pre_max < suf_max) - { - ans += pre_max - height[left]; - left++; - } - else - { - ans += suf_max - height[right]; - right--; - } - - } - return ans; - } -}; \ No newline at end of file +#include +class Solution { +public: + int trap(vector& height) { + int n = height.size(); + int left = 0, right = n - 1; + int suf_max = 0, pre_max = 0, ans = 0; + while (left <= right) + { + pre_max = max(pre_max, height[left]); + suf_max = max(suf_max, height[right]); + + if (pre_max < suf_max) + { + ans += pre_max - height[left]; + left++; + } + else + { + ans += suf_max - height[right]; + right--; + } + + } + return ans; + } +}; diff --git "a/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.py" "b/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.py" index a1b59dc4..9b9f0abb 100644 --- "a/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.py" +++ "b/leetcode/Study Plan/42. \346\216\245\351\233\250\346\260\264/1.py" @@ -1,24 +1,24 @@ -class Solution: - def trap(self, height: List[int]) -> int: - n = len(height) - - pre_max = [0]*n - pre_max[0] = height[0] - for i in range(1,n,1): - pre_max[i] = max(pre_max[i-1],height[i]) - - suf_max = [0]*n - suf_max[-1] = height[-1] - for i in range(n-2,-1,-1): - suf_max[i] = max(suf_max[i+1],height[i]) - - ans = 0 - # for i in range(1,n): - # capacity = min(pre_max[i],suf_max[i]) - # if capacity - height[i] > 0: - # ans += capacity - height[i] - - for pre,suf,h in zip(pre_max,suf_max,height): - ans += min(pre,suf)-h - - return ans \ No newline at end of file +class Solution: + def trap(self, height: List[int]) -> int: + n = len(height) + + pre_max = [0]*n + pre_max[0] = height[0] + for i in range(1,n,1): + pre_max[i] = max(pre_max[i-1],height[i]) + + suf_max = [0]*n + suf_max[-1] = height[-1] + for i in range(n-2,-1,-1): + suf_max[i] = max(suf_max[i+1],height[i]) + + ans = 0 + # for i in range(1,n): + # capacity = min(pre_max[i],suf_max[i]) + # if capacity - height[i] > 0: + # ans += capacity - height[i] + + for pre,suf,h in zip(pre_max,suf_max,height): + ans += min(pre,suf)-h + + return ans diff --git "a/leetcode/Study Plan/454. \345\233\233\346\225\260\347\233\270\345\212\240 II/1.cpp" "b/leetcode/Study Plan/454. \345\233\233\346\225\260\347\233\270\345\212\240 II/1.cpp" index ebfa73e8..6dbf0419 100644 --- "a/leetcode/Study Plan/454. \345\233\233\346\225\260\347\233\270\345\212\240 II/1.cpp" +++ "b/leetcode/Study Plan/454. \345\233\233\346\225\260\347\233\270\345\212\240 II/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-11 11:15:02 diff --git "a/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/1.cpp" "b/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/1.cpp" index 262ea013..90fe8a9d 100644 --- "a/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/1.cpp" +++ "b/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-03-22 18:32:49 diff --git "a/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/2.cpp" "b/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/2.cpp" index 05d428da..78f554c7 100644 --- "a/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/2.cpp" +++ "b/leetcode/Study Plan/48. \346\227\213\350\275\254\345\233\276\345\203\217/2.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-03-22 18:32:49 diff --git "a/leetcode/Study Plan/49. \345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/1.cpp" "b/leetcode/Study Plan/49. \345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/1.cpp" index f1034d69..23cd3e74 100644 --- "a/leetcode/Study Plan/49. \345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/1.cpp" +++ "b/leetcode/Study Plan/49. \345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include diff --git "a/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\202.cpp" "b/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\202.cpp" index 5fe3c19e..5b54bc2d 100644 --- "a/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\202.cpp" +++ "b/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\202.cpp" @@ -1,33 +1,33 @@ -#include -// 快速幂 -// 递归分治法 -using namespace std; - -class Solution -{ -public: - double powHelper(double x, int n) - { - if (n == 0) - return 1.0; - double half = powHelper(x, n / 2); - return n % 2 == 0 ? half * half : half * half * x; - } - - double myPow(double x, int n) - { - if (n == 0 || x == 1) - return 1.0; - long long N = n; - if (N < 0) - return 1.0 / powHelper(x, -N); - return powHelper(x, N); - } -}; - -int main() -{ - Solution s; - cout << (s.myPow(2.00000, 10) == 1024); - return 0; -} \ No newline at end of file +#include +// 快速幂 +// 递归分治法 +using namespace std; + +class Solution +{ +public: + double powHelper(double x, int n) + { + if (n == 0) + return 1.0; + double half = powHelper(x, n / 2); + return n % 2 == 0 ? half * half : half * half * x; + } + + double myPow(double x, int n) + { + if (n == 0 || x == 1) + return 1.0; + long long N = n; + if (N < 0) + return 1.0 / powHelper(x, -N); + return powHelper(x, N); + } +}; + +int main() +{ + Solution s; + cout << (s.myPow(2.00000, 10) == 1024); + return 0; +} diff --git "a/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\2022.cpp" "b/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\2022.cpp" index 6251ac89..9032f707 100644 --- "a/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\2022.cpp" +++ "b/leetcode/Study Plan/50/\345\277\253\351\200\237\345\271\2022.cpp" @@ -1,39 +1,39 @@ -#include -// 快速幂 -// 递归分治法 -using namespace std; - -class Solution -{ -public: - double powHelper(double x, long long n) - { - double res = 1.0; - double contribute_x = x; - while(n){ - if(n % 2 == 1){ - res *= contribute_x; - } - n /= 2; - contribute_x *= contribute_x; - } - return res; - } - - double myPow(double x, int n) - { - if (n == 0 || x == 1) - return 1.0; - long long N = n; - if (N < 0) - return 1.0 / powHelper(x, -N); - return powHelper(x, N); - } -}; - -int main() -{ - Solution s; - cout << (s.myPow(2.00000, 10) == 1024); - return 0; -} \ No newline at end of file +#include +// 快速幂 +// 递归分治法 +using namespace std; + +class Solution +{ +public: + double powHelper(double x, long long n) + { + double res = 1.0; + double contribute_x = x; + while(n){ + if(n % 2 == 1){ + res *= contribute_x; + } + n /= 2; + contribute_x *= contribute_x; + } + return res; + } + + double myPow(double x, int n) + { + if (n == 0 || x == 1) + return 1.0; + long long N = n; + if (N < 0) + return 1.0 / powHelper(x, -N); + return powHelper(x, N); + } +}; + +int main() +{ + Solution s; + cout << (s.myPow(2.00000, 10) == 1024); + return 0; +} diff --git "a/leetcode/Study Plan/59. \350\236\272\346\227\213\347\237\251\351\230\265/1.cpp" "b/leetcode/Study Plan/59. \350\236\272\346\227\213\347\237\251\351\230\265/1.cpp" index 7b7079ff..fe302576 100644 --- "a/leetcode/Study Plan/59. \350\236\272\346\227\213\347\237\251\351\230\265/1.cpp" +++ "b/leetcode/Study Plan/59. \350\236\272\346\227\213\347\237\251\351\230\265/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-04-05 18:50:13 diff --git a/leetcode/Study Plan/62/62_1.cpp b/leetcode/Study Plan/62/62_1.cpp index dc7f36a4..38557b42 100644 --- a/leetcode/Study Plan/62/62_1.cpp +++ b/leetcode/Study Plan/62/62_1.cpp @@ -1,13 +1,13 @@ -//采用组合数学的方法,从左上角到右下角的过程中,一共需要向下m-1次,向右n-1次,所以一共需要走m+n-2次,其中m-1次向下,所以一共有C(m+n-2,m-1)种走法。 -//时间复杂度O(min(m,n)),空间复杂度O(1) -class Solution { -public: - int uniquePaths(int m, int n) { - int64_t ans = 1; - for (int i =0;i> C(n+m,vector(n+m)); - for (int i = 0;i +//采用杨辉三角的方法,时间复杂度O(n^2),空间复杂度O(n^2) +//虽然时间复杂度高,但是可以将范围内的所有组合数都计算出来,对于需要频繁计算组合数的问题 +//可以使用这种方法 +class Solution { +public: + int uniquePaths(int m, int n) { + vector> C(n+m,vector(n+m)); + for (int i = 0;i - -using namespace std; - -class Solution{ - public: -} \ No newline at end of file +#include + +using namespace std; + +class Solution{ + public: +} diff --git a/leetcode/Study Plan/66/1.cpp b/leetcode/Study Plan/66/1.cpp index 2d21ce56..51764322 100644 --- a/leetcode/Study Plan/66/1.cpp +++ b/leetcode/Study Plan/66/1.cpp @@ -1,24 +1,25 @@ -class Solution -{ -public: - vector plusOne(vector &digits) - { - int len = digits.size(); - // 从后往前遍历,如果当前位是9,则将当前位设置为0,继续遍历 - for (int i = len - 1; i >= 0; i--) - { - if (digits[i] == 9) - { - digits[i] = 0; - } - else - { - digits[i] += 1; - return digits; - } - } - // 如果所有位都是9,则在开头插入1 - digits.insert(digits.begin(), 1); - return digits; - } -}; \ No newline at end of file +#include +class Solution +{ +public: + vector plusOne(vector &digits) + { + int len = digits.size(); + // 从后往前遍历,如果当前位是9,则将当前位设置为0,继续遍历 + for (int i = len - 1; i >= 0; i--) + { + if (digits[i] == 9) + { + digits[i] = 0; + } + else + { + digits[i] += 1; + return digits; + } + } + // 如果所有位都是9,则在开头插入1 + digits.insert(digits.begin(), 1); + return digits; + } +}; diff --git a/leetcode/Study Plan/66/2.cpp b/leetcode/Study Plan/66/2.cpp index 85c317c5..922ca6fd 100644 --- a/leetcode/Study Plan/66/2.cpp +++ b/leetcode/Study Plan/66/2.cpp @@ -1,44 +1,44 @@ -/* - * @lc app=leetcode.cn id=66 lang=cpp - * - * [66] 加一 - */ - -#include -#include - -// @lc code=start -class Solution -{ -public: - std::vector plusOne(std::vector &digits) - { - int n = digits.size(); - for (int i = n - 1; i >= 0; --i) - { - if (digits[i] != 9) - { - digits[i]++; - return digits; - } - digits[i] = 0; - } - // 这里有一个逻辑错误:当所有位都是9时(如[9,9,9]), - // 我们需要在最前面插入1,而不是修改digits[0]并添加0到末尾 - // 修正方法是:创建一个新的vector,第一位为1,后面跟n个0 - std::vector result(n + 1, 0); - result[0] = 1; - return result; - } -}; -// @lc code=end - -int main() -{ - Solution s; - std::vector digits = {9, 9, 9}; - auto res = s.plusOne(digits); - for (auto x : res) - std::cout << x << " "; - return 0; -} +/* + * @lc app=leetcode.cn id=66 lang=cpp + * + * [66] 加一 + */ + +#include +#include + +// @lc code=start +class Solution +{ +public: + std::vector plusOne(std::vector &digits) + { + int n = digits.size(); + for (int i = n - 1; i >= 0; --i) + { + if (digits[i] != 9) + { + digits[i]++; + return digits; + } + digits[i] = 0; + } + // 这里有一个逻辑错误:当所有位都是9时(如[9,9,9]), + // 我们需要在最前面插入1,而不是修改digits[0]并添加0到末尾 + // 修正方法是:创建一个新的vector,第一位为1,后面跟n个0 + std::vector result(n + 1, 0); + result[0] = 1; + return result; + } +}; +// @lc code=end + +int main() +{ + Solution s; + std::vector digits = {9, 9, 9}; + auto res = s.plusOne(digits); + for (auto x : res) + std::cout << x << " "; + return 0; +} diff --git a/leetcode/Study Plan/66/simulate.cpp b/leetcode/Study Plan/66/simulate.cpp index 078eb155..bc0d3453 100644 --- a/leetcode/Study Plan/66/simulate.cpp +++ b/leetcode/Study Plan/66/simulate.cpp @@ -1,37 +1,37 @@ -#include -#include -#include -#include -using namespace std; - -class Solution{ - public: - vector add(vector &a, vector &b){ - int n = a.size(), m = b.size(); - vector c; - int carry = 0; - for (int i = n - 1, j = m - 1; i >= 0 || j >= 0; i--, j--){ - int ai = (i >= 0) ? a[i] : 0; - int bj = (j >= 0) ? b[j] : 0; - int sum = ai + bj + carry; - carry = sum / 10; - c.push_back(sum % 10); - } - if(carry) - c.push_back(carry); - reverse(c.begin(), c.end()); - return c; - } -}; - -int main(){ - Solution s; - string sa, sb; - cin >> sa >> sb; - vector a, b; - for(auto c : sa) a.push_back(c - '0'); - for(auto c : sb) b.push_back(c - '0'); - auto c = s.add(a, b); - for(auto x : c) cout << x; - return 0; -} +#include +#include +#include +#include +using namespace std; + +class Solution{ + public: + vector add(vector &a, vector &b){ + int n = a.size(), m = b.size(); + vector c; + int carry = 0; + for (int i = n - 1, j = m - 1; i >= 0 || j >= 0; i--, j--){ + int ai = (i >= 0) ? a[i] : 0; + int bj = (j >= 0) ? b[j] : 0; + int sum = ai + bj + carry; + carry = sum / 10; + c.push_back(sum % 10); + } + if(carry) + c.push_back(carry); + reverse(c.begin(), c.end()); + return c; + } +}; + +int main(){ + Solution s; + string sa, sb; + cin >> sa >> sb; + vector a, b; + for(auto c : sa) a.push_back(c - '0'); + for(auto c : sb) b.push_back(c - '0'); + auto c = s.add(a, b); + for(auto x : c) cout << x; + return 0; +} diff --git a/leetcode/Study Plan/67/1.cpp b/leetcode/Study Plan/67/1.cpp index 7bf4f46e..ccae76c1 100644 --- a/leetcode/Study Plan/67/1.cpp +++ b/leetcode/Study Plan/67/1.cpp @@ -1,25 +1,25 @@ -class Solution -{ -public: - string addBinary(string a, string b) - { - int len_a = a.size(); - int len_b = b.size(); - int max_len = max(len_a, len_b); - string result = ""; - int carry = 0; - for (int i = 0; i < max_len; i++) - { - int bit_a = i < len_a ? a[len_a - i - 1] - '0' : 0; - int bit_b = i < len_b ? b[len_b - i - 1] - '0' : 0; - int sum = bit_a + bit_b + carry; - result = to_string(sum % 2) + result; - carry = sum / 2; - } - if (carry > 0) - { - result = to_string(carry) + result; - } - return result; - } -}; \ No newline at end of file +class Solution +{ +public: + string addBinary(string a, string b) + { + int len_a = a.size(); + int len_b = b.size(); + int max_len = max(len_a, len_b); + string result = ""; + int carry = 0; + for (int i = 0; i < max_len; i++) + { + int bit_a = i < len_a ? a[len_a - i - 1] - '0' : 0; + int bit_b = i < len_b ? b[len_b - i - 1] - '0' : 0; + int sum = bit_a + bit_b + carry; + result = to_string(sum % 2) + result; + carry = sum / 2; + } + if (carry > 0) + { + result = to_string(carry) + result; + } + return result; + } +}; diff --git a/leetcode/Study Plan/70/1.cpp b/leetcode/Study Plan/70/1.cpp index 33d2a6a8..1ad02415 100644 --- a/leetcode/Study Plan/70/1.cpp +++ b/leetcode/Study Plan/70/1.cpp @@ -1,27 +1,27 @@ -//动态规划,滚动数组 - -#include -#include - -using namespace std; - -class Solution { -public: - int climbStairs(int n) { - - int n1 = 0,n2 = 0,n3 = 1; - for (int i = 1;i<=n;i++){ - n1 = n2; - n2 = n3; - n3 = n2 + n1; - } - return n3; - } -}; -int main() -{ - Solution s; - int n = 2; - cout << s.climbStairs(n); - return 0; -} \ No newline at end of file +//动态规划,滚动数组 + +#include +#include + +using namespace std; + +class Solution { +public: + int climbStairs(int n) { + + int n1 = 0,n2 = 0,n3 = 1; + for (int i = 1;i<=n;i++){ + n1 = n2; + n2 = n3; + n3 = n2 + n1; + } + return n3; + } +}; +int main() +{ + Solution s; + int n = 2; + cout << s.climbStairs(n); + return 0; +} diff --git a/leetcode/Study Plan/70/2.cpp b/leetcode/Study Plan/70/2.cpp index 16c7c5c1..be4982f6 100644 --- a/leetcode/Study Plan/70/2.cpp +++ b/leetcode/Study Plan/70/2.cpp @@ -1,56 +1,56 @@ -// 矩阵快速幂 -#include -#include - -using namespace std; - -class Solution -{ -public: - // 矩阵乘法函数,计算两个2x2矩阵a和b的乘积 - vector> multiply(vector> &a, vector> &b) - { - vector> c(2, vector(2)); // 初始化结果矩阵c - for (int i = 0; i < 2; i++) - { - for (int j = 0; j < 2; j++) - { - // 计算c[i][j]的值 - c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j]; - } - } - return c; // 返回结果矩阵 - } - - // 矩阵快速幂函数,计算矩阵a的n次幂 - vector> matrixPow(vector> a, int n) - { - vector> ret = {{1, 0}, {0, 1}}; // 初始化单位矩阵ret - while (n > 0) - { - if ((n & 1) == 1) // 如果n是奇数 - { - ret = multiply(ret, a); // 将当前矩阵a乘到结果矩阵ret上 - } - n >>= 1; // n右移一位,相当于n除以2 - a = multiply(a, a); // 矩阵a自乘 - } - return ret; // 返回结果矩阵 - } - - // 计算爬楼梯的方法数 - int climbStairs(int n) - { - vector> ret = {{1, 1}, {1, 0}}; // 初始化转移矩阵 - vector> res = matrixPow(ret, n); // 计算转移矩阵的n次幂 - return res[0][0]; // 返回结果,即爬n阶楼梯的方法数 - } -}; - -int main() -{ - Solution s; - int n = 2; // 爬楼梯的阶数 - cout << s.climbStairs(n); // 输出爬n阶楼梯的方法数 - return 0; -} \ No newline at end of file +// 矩阵快速幂 +#include +#include + +using namespace std; + +class Solution +{ +public: + // 矩阵乘法函数,计算两个2x2矩阵a和b的乘积 + vector> multiply(vector> &a, vector> &b) + { + vector> c(2, vector(2)); // 初始化结果矩阵c + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + // 计算c[i][j]的值 + c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j]; + } + } + return c; // 返回结果矩阵 + } + + // 矩阵快速幂函数,计算矩阵a的n次幂 + vector> matrixPow(vector> a, int n) + { + vector> ret = {{1, 0}, {0, 1}}; // 初始化单位矩阵ret + while (n > 0) + { + if ((n & 1) == 1) // 如果n是奇数 + { + ret = multiply(ret, a); // 将当前矩阵a乘到结果矩阵ret上 + } + n >>= 1; // n右移一位,相当于n除以2 + a = multiply(a, a); // 矩阵a自乘 + } + return ret; // 返回结果矩阵 + } + + // 计算爬楼梯的方法数 + int climbStairs(int n) + { + vector> ret = {{1, 1}, {1, 0}}; // 初始化转移矩阵 + vector> res = matrixPow(ret, n); // 计算转移矩阵的n次幂 + return res[0][0]; // 返回结果,即爬n阶楼梯的方法数 + } +}; + +int main() +{ + Solution s; + int n = 2; // 爬楼梯的阶数 + cout << s.climbStairs(n); // 输出爬n阶楼梯的方法数 + return 0; +} diff --git a/leetcode/Study Plan/70/3.cpp b/leetcode/Study Plan/70/3.cpp index 016fb944..a099c572 100644 --- a/leetcode/Study Plan/70/3.cpp +++ b/leetcode/Study Plan/70/3.cpp @@ -1,26 +1,26 @@ -// 通项公式 -#include -#include - -using namespace std; - -class Solution -{ -public: - int climbStairs(int n) - { - double sqrt5 = sqrt(5); - double fibn = pow((1 + sqrt5) / 2, n + 1) - pow((1 - sqrt5) / 2, n + 1); - return (int)round(fibn / sqrt5); - } -}; - - - -int main() -{ - Solution s; - int n = 2; // 爬楼梯的阶数 - cout << s.climbStairs(n); // 输出爬n阶楼梯的方法数 - return 0; -} \ No newline at end of file +// 通项公式 +#include +#include + +using namespace std; + +class Solution +{ +public: + int climbStairs(int n) + { + double sqrt5 = sqrt(5); + double fibn = pow((1 + sqrt5) / 2, n + 1) - pow((1 - sqrt5) / 2, n + 1); + return (int)round(fibn / sqrt5); + } +}; + + + +int main() +{ + Solution s; + int n = 2; // 爬楼梯的阶数 + cout << s.climbStairs(n); // 输出爬n阶楼梯的方法数 + return 0; +} diff --git a/leetcode/Study Plan/70/4.py b/leetcode/Study Plan/70/4.py index 547ced88..48b5ece2 100644 --- a/leetcode/Study Plan/70/4.py +++ b/leetcode/Study Plan/70/4.py @@ -1,11 +1,11 @@ -class Solution: - def climbStairs(self, n: int) -> int: - a, b = 1, 1 - for _ in range(n - 1): - a, b = b, a + b - return b - -if __name__ == "__main__": - s = Solution() - n = 2 - print(s.climbStairs(n)) \ No newline at end of file +class Solution: + def climbStairs(self, n: int) -> int: + a, b = 1, 1 + for _ in range(n - 1): + a, b = b, a + b + return b + +if __name__ == "__main__": + s = Solution() + n = 2 + print(s.climbStairs(n)) diff --git "a/leetcode/Study Plan/704. Binary Search (\344\272\214\345\210\206\346\237\245\346\211\276)/1.cpp" "b/leetcode/Study Plan/704. Binary Search (\344\272\214\345\210\206\346\237\245\346\211\276)/1.cpp" index f01a4f89..51f63bde 100644 --- "a/leetcode/Study Plan/704. Binary Search (\344\272\214\345\210\206\346\237\245\346\211\276)/1.cpp" +++ "b/leetcode/Study Plan/704. Binary Search (\344\272\214\345\210\206\346\237\245\346\211\276)/1.cpp" @@ -1,13 +1,14 @@ -class Solution { -public: - int search(vector& nums, int target) { - int left = 0,right = nums.size()-1; - while (left <= right) { - int mid = (left + right) / 2; - if (nums[mid] == target)return mid; - if (nums[mid] > target)right = mid - 1; - else left = mid + 1; - } - return -1; - } -}; \ No newline at end of file +#include +class Solution { +public: + int search(vector& nums, int target) { + int left = 0,right = nums.size()-1; + while (left <= right) { + int mid = (left + right) / 2; + if (nums[mid] == target)return mid; + if (nums[mid] > target)right = mid - 1; + else left = mid + 1; + } + return -1; + } +}; diff --git a/leetcode/Study Plan/707/707.cpp b/leetcode/Study Plan/707/707.cpp index 8e9838c7..12e2994e 100644 --- a/leetcode/Study Plan/707/707.cpp +++ b/leetcode/Study Plan/707/707.cpp @@ -1,76 +1,76 @@ -#define MAX(a,b) ((a)>(b)?(a):(b)) - -typedef struct{ - int length; - struct ListNode *head; -} MyLinkedList; - -struct ListNode *ListNodeCreat(int val){ - struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode)); - node->val = val; - node->next = NULL; - return node; -} - -MyLinkedList *myLinkedListCreate(){ - MyLinkedList *obj = (MyLinkedList *)malloc(sizeof(MyLinkedList)); - obj->head = ListNodeCreat(0); - obj->length = 0; - return obj; -} - -int myLinkedListGet(MyLinkedList *obj,int index){ - if(index < 0 || index >= obj->length){ - return -1; - } - struct ListNode *cur = obj->head; - for (int i = 0;i<=index;i++){ - cur = cur->next; - } - return cur->val; -} - -void myLinkedListAddAtIndex(MyLinkedList *obj,int index,int val){ - if(index > obj->length){ - return; - } - index = MAX(0,index); - obj->length++; - struct ListNode *pred = obj->head; - for (int i = 0;inext; - } - struct ListNode *to_add = ListNodeCreat(val); - to_add->next = pred->next; - pred->next = to_add; -} - -void myLinkedListAddAtHead(MyLinkedList *obj,int val){ - myLinkedListAddAtIndex(obj,0,val); -} - -void myLinkedListAddAtTail(MyLinkedList *obj,int val){ - myLinkedListAddAtIndex(obj,obj->length,val); -} - -void myLinkedListDeleteAtIndex(MyLinkedList *obj,int index){ - if(index < 0 || index >= obj->length){ - return; - } - obj->length--; - struct ListNode *pred = obj->head; - for (int i = 0;inext; - } - struct ListNode *p = pred->next; - pred->next = pred->next->next; - free(p); -} - -void myLinkedListFree(MyLinkedList *obj){ - struct ListNode *cur = NULL,*tmp = NULL; - for (struct ListNode *p = obj->head;p!=NULL;p = tmp){ - tmp = p->next; - free(p); - } -} +#define MAX(a,b) ((a)>(b)?(a):(b)) + +typedef struct{ + int length; + struct ListNode *head; +} MyLinkedList; + +struct ListNode *ListNodeCreat(int val){ + struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode)); + node->val = val; + node->next = NULL; + return node; +} + +MyLinkedList *myLinkedListCreate(){ + MyLinkedList *obj = (MyLinkedList *)malloc(sizeof(MyLinkedList)); + obj->head = ListNodeCreat(0); + obj->length = 0; + return obj; +} + +int myLinkedListGet(MyLinkedList *obj,int index){ + if(index < 0 || index >= obj->length){ + return -1; + } + struct ListNode *cur = obj->head; + for (int i = 0;i<=index;i++){ + cur = cur->next; + } + return cur->val; +} + +void myLinkedListAddAtIndex(MyLinkedList *obj,int index,int val){ + if(index > obj->length){ + return; + } + index = MAX(0,index); + obj->length++; + struct ListNode *pred = obj->head; + for (int i = 0;inext; + } + struct ListNode *to_add = ListNodeCreat(val); + to_add->next = pred->next; + pred->next = to_add; +} + +void myLinkedListAddAtHead(MyLinkedList *obj,int val){ + myLinkedListAddAtIndex(obj,0,val); +} + +void myLinkedListAddAtTail(MyLinkedList *obj,int val){ + myLinkedListAddAtIndex(obj,obj->length,val); +} + +void myLinkedListDeleteAtIndex(MyLinkedList *obj,int index){ + if(index < 0 || index >= obj->length){ + return; + } + obj->length--; + struct ListNode *pred = obj->head; + for (int i = 0;inext; + } + struct ListNode *p = pred->next; + pred->next = pred->next->next; + free(p); +} + +void myLinkedListFree(MyLinkedList *obj){ + struct ListNode *cur = NULL,*tmp = NULL; + for (struct ListNode *p = obj->head;p!=NULL;p = tmp){ + tmp = p->next; + free(p); + } +} diff --git "a/leetcode/Study Plan/713. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/1.py" "b/leetcode/Study Plan/713. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/1.py" index a04a2271..d1970e43 100644 --- "a/leetcode/Study Plan/713. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/1.py" +++ "b/leetcode/Study Plan/713. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/1.py" @@ -1,20 +1,20 @@ -class Solution: - def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int: - if k <= 1: - return 0 - n = len(nums) - ans = 0 - - left = 0 - prod = 1 - - - - for right,x in enumerate(nums): - prod*= x - while prod-k>=0: - prod/=nums[left] - left+=1 - ans += right-left+1 - - return ans \ No newline at end of file +class Solution: + def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int: + if k <= 1: + return 0 + n = len(nums) + ans = 0 + + left = 0 + prod = 1 + + + + for right,x in enumerate(nums): + prod*= x + while prod-k>=0: + prod/=nums[left] + left+=1 + ans += right-left+1 + + return ans diff --git "a/leetcode/Study Plan/717. 1 \346\257\224\347\211\271\344\270\216 2 \346\257\224\347\211\271\345\255\227\347\254\246/1.py" "b/leetcode/Study Plan/717. 1 \346\257\224\347\211\271\344\270\216 2 \346\257\224\347\211\271\345\255\227\347\254\246/1.py" index 685a0a89..dc67e1eb 100644 --- "a/leetcode/Study Plan/717. 1 \346\257\224\347\211\271\344\270\216 2 \346\257\224\347\211\271\345\255\227\347\254\246/1.py" +++ "b/leetcode/Study Plan/717. 1 \346\257\224\347\211\271\344\270\216 2 \346\257\224\347\211\271\345\255\227\347\254\246/1.py" @@ -3,4 +3,4 @@ def isOneBitCharacter(self, bits: List[int]) -> bool: i ,n = 0,len(bits) while i < n-1: i += bits[i] + 1 #跳过bits[i]为1的情况 - return i == n-1 \ No newline at end of file + return i == n-1 diff --git "a/leetcode/Study Plan/743_\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264/enumerate.cpp" "b/leetcode/Study Plan/743_\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264/enumerate.cpp" index 1b194bac..44bf21f3 100644 --- "a/leetcode/Study Plan/743_\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264/enumerate.cpp" +++ "b/leetcode/Study Plan/743_\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264/enumerate.cpp" @@ -1,60 +1,60 @@ -#include -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - // 使用Dijkstra算法求解网络延迟时间 - // times: 有向图的边列表,每个元素为 [起点,终点,权重] - // n: 节点数量 - // k: 起始节点 - int networkDelayTime(vector> ×, int n, int k) - { - // 定义无穷大值,使用INT_MAX/2避免加法溢出 - const int inf = INT_MAX / 2; - // 构建邻接矩阵表示的图,初始化为无穷大 - vector> g(n, vector(n, inf)); - // 填充图的边权重 - for (auto &t : times) - { - int x = t[0] - 1, y = t[1] - 1; // 节点编号从0开始 - g[x][y] = t[2]; // 设置边的权重 - } - - // dist数组存储从起点k到各个节点的最短距离 - vector dist(n, inf); - dist[k - 1] = 0; // 起点到自身的距离为0 - // used数组标记节点是否已确定最短路径 - vector used(n); - - // Dijkstra算法主循环 - for (int i = 0; i < n; ++i) - { - // 找到未使用节点中距离最小的节点x - int x = -1; - for (int y = 0; y < n; ++y) - { - if (!used[y] && (x == -1 || dist[y] < dist[x])) - { - x = y; - } - } - used[x] = true; // 标记节点x已使用 - - // 更新通过节点x可以到达的其他节点的距离 - for (int y = 0; y < n; ++y) - { - dist[y] = min(dist[y], dist[x] + g[x][y]); - } - } - - // 找到所有节点中的最大延迟时间 - int ans = *max_element(dist.begin(), dist.end()); - // 如果存在无法到达的节点,返回-1 - return ans == inf ? -1 : ans; - } -}; +#include +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + // 使用Dijkstra算法求解网络延迟时间 + // times: 有向图的边列表,每个元素为 [起点,终点,权重] + // n: 节点数量 + // k: 起始节点 + int networkDelayTime(vector> ×, int n, int k) + { + // 定义无穷大值,使用INT_MAX/2避免加法溢出 + const int inf = INT_MAX / 2; + // 构建邻接矩阵表示的图,初始化为无穷大 + vector> g(n, vector(n, inf)); + // 填充图的边权重 + for (auto &t : times) + { + int x = t[0] - 1, y = t[1] - 1; // 节点编号从0开始 + g[x][y] = t[2]; // 设置边的权重 + } + + // dist数组存储从起点k到各个节点的最短距离 + vector dist(n, inf); + dist[k - 1] = 0; // 起点到自身的距离为0 + // used数组标记节点是否已确定最短路径 + vector used(n); + + // Dijkstra算法主循环 + for (int i = 0; i < n; ++i) + { + // 找到未使用节点中距离最小的节点x + int x = -1; + for (int y = 0; y < n; ++y) + { + if (!used[y] && (x == -1 || dist[y] < dist[x])) + { + x = y; + } + } + used[x] = true; // 标记节点x已使用 + + // 更新通过节点x可以到达的其他节点的距离 + for (int y = 0; y < n; ++y) + { + dist[y] = min(dist[y], dist[x] + g[x][y]); + } + } + + // 找到所有节点中的最大延迟时间 + int ans = *max_element(dist.begin(), dist.end()); + // 如果存在无法到达的节点,返回-1 + return ans == inf ? -1 : ans; + } +}; diff --git "a/leetcode/Study Plan/838_\346\216\250\345\244\232\347\261\263\350\257\272/BFS.cpp" "b/leetcode/Study Plan/838_\346\216\250\345\244\232\347\261\263\350\257\272/BFS.cpp" index 7d6227b0..dee8053c 100644 --- "a/leetcode/Study Plan/838_\346\216\250\345\244\232\347\261\263\350\257\272/BFS.cpp" +++ "b/leetcode/Study Plan/838_\346\216\250\345\244\232\347\261\263\350\257\272/BFS.cpp" @@ -1,62 +1,62 @@ -#include -#include -#include -#include - - -using namespace std; - -class Solution -{ -public: - // 创建队列,用于存储当前需要推倒的多米诺 - string pushDominoes(string dominoes) - { - int n = dominoes.size(); - queue q; - vector time(n, -1); //-1表示未被推倒 - vector force(n); //记录每个位置受到的力 - - for(int i = 0;i < n;++i){ - if(dominoes[i] != '.'){ - q.emplace(i); - time[i] = 0; - force[i].push_back(dominoes[i]); - } - } - - string res(n,'.'); - - while(!q.empty()){ - int i = q.front(); - q.pop(); - - if(force[i].size() == 1){ - char f = force[i][0]; - res[i] = f; - int t = time[i]; - int ni = (f == 'L') ? i - 1 : i + 1; - if(ni >= 0 && ni < n){ - if(time[ni] == -1){ - q.emplace(ni); - time[ni] = t + 1; - force[ni].push_back(f); - }else if(time[ni] == t + 1){ - if(force[ni].size() == 1) - force[ni].push_back(f); - } - } - } - - } - return res; - } -}; - -int main() -{ - Solution s; - string dominoes = ".L.R...LR..L.."; - cout << s.pushDominoes(dominoes) << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +#include + + +using namespace std; + +class Solution +{ +public: + // 创建队列,用于存储当前需要推倒的多米诺 + string pushDominoes(string dominoes) + { + int n = dominoes.size(); + queue q; + vector time(n, -1); //-1表示未被推倒 + vector force(n); //记录每个位置受到的力 + + for(int i = 0;i < n;++i){ + if(dominoes[i] != '.'){ + q.emplace(i); + time[i] = 0; + force[i].push_back(dominoes[i]); + } + } + + string res(n,'.'); + + while(!q.empty()){ + int i = q.front(); + q.pop(); + + if(force[i].size() == 1){ + char f = force[i][0]; + res[i] = f; + int t = time[i]; + int ni = (f == 'L') ? i - 1 : i + 1; + if(ni >= 0 && ni < n){ + if(time[ni] == -1){ + q.emplace(ni); + time[ni] = t + 1; + force[ni].push_back(f); + }else if(time[ni] == t + 1){ + if(force[ni].size() == 1) + force[ni].push_back(f); + } + } + } + + } + return res; + } +}; + +int main() +{ + Solution s; + string dominoes = ".L.R...LR..L.."; + cout << s.pushDominoes(dominoes) << endl; + return 0; +} diff --git "a/leetcode/Study Plan/85. \346\234\200\345\244\247\347\237\251\345\275\242/2.cpp" "b/leetcode/Study Plan/85. \346\234\200\345\244\247\347\237\251\345\275\242/2.cpp" index 585b5a60..7ac3a0f5 100644 --- "a/leetcode/Study Plan/85. \346\234\200\345\244\247\347\237\251\345\275\242/2.cpp" +++ "b/leetcode/Study Plan/85. \346\234\200\345\244\247\347\237\251\345\275\242/2.cpp" @@ -1,3 +1,4 @@ +#include // 单调栈 #include diff --git "a/leetcode/Study Plan/867. \350\275\254\347\275\256\347\237\251\351\230\265/1.cpp" "b/leetcode/Study Plan/867. \350\275\254\347\275\256\347\237\251\351\230\265/1.cpp" index 53c1bddd..20cd9c46 100644 --- "a/leetcode/Study Plan/867. \350\275\254\347\275\256\347\237\251\351\230\265/1.cpp" +++ "b/leetcode/Study Plan/867. \350\275\254\347\275\256\347\237\251\351\230\265/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: vector> transpose(vector> &matrix) { diff --git a/leetcode/Study Plan/876/1.cpp b/leetcode/Study Plan/876/1.cpp index b867aadf..1e3e7fab 100644 --- a/leetcode/Study Plan/876/1.cpp +++ b/leetcode/Study Plan/876/1.cpp @@ -1,54 +1,54 @@ -#include -using namespace std; - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -struct ListNode { - int val; - ListNode *next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode *next) : val(x), next(next) {} -}; - -class Solution -{ -public: - ListNode *middleNode(ListNode *head) - { - ListNode *p = head, *q = head; - int cnt = 0; - while(q->next != nullptr){ - q = q->next; - cnt++; - } - for(int i = 0; i < cnt/2; i++){ - p = p->next; - } - if(cnt%2 == 1)p = p->next; - return p; - } -}; - -int main() { - ListNode* head = new ListNode(1); - head->next = new ListNode(2); - head->next->next = new ListNode(3); - head->next->next->next = new ListNode(4); - head->next->next->next->next = new ListNode(5); - head->next->next->next->next->next = new ListNode(6); - - Solution s; - ListNode* middle = s.middleNode(head); - cout << "Middle node value: " << middle->val << endl; - - return 0; -} \ No newline at end of file +#include +using namespace std; + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution +{ +public: + ListNode *middleNode(ListNode *head) + { + ListNode *p = head, *q = head; + int cnt = 0; + while(q->next != nullptr){ + q = q->next; + cnt++; + } + for(int i = 0; i < cnt/2; i++){ + p = p->next; + } + if(cnt%2 == 1)p = p->next; + return p; + } +}; + +int main() { + ListNode* head = new ListNode(1); + head->next = new ListNode(2); + head->next->next = new ListNode(3); + head->next->next->next = new ListNode(4); + head->next->next->next->next = new ListNode(5); + head->next->next->next->next->next = new ListNode(6); + + Solution s; + ListNode* middle = s.middleNode(head); + cout << "Middle node value: " << middle->val << endl; + + return 0; +} diff --git a/leetcode/Study Plan/876/fast_slow_pointer.cpp b/leetcode/Study Plan/876/fast_slow_pointer.cpp index 5448fa36..eadbf65b 100644 --- a/leetcode/Study Plan/876/fast_slow_pointer.cpp +++ b/leetcode/Study Plan/876/fast_slow_pointer.cpp @@ -1,60 +1,60 @@ -#include -using namespace std; - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -struct ListNode -{ - int val; - ListNode *next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode *next) : val(x), next(next) {} -}; - -class Solution -{ -public: - ListNode *middleNode(ListNode *head) - { - if (head == NULL || head->next == NULL) - { - return head; - } - - ListNode *fast = head, *slow = head; - while (fast->next->next != NULL && fast->next != NULL) - { - slow = slow->next; - fast = fast->next->next; - } - if (fast->next != NULL) - slow = slow->next; - - return slow; - } -}; - -int main() -{ - ListNode *head = new ListNode(1); - head->next = new ListNode(2); - head->next->next = new ListNode(3); - head->next->next->next = new ListNode(4); - head->next->next->next->next = new ListNode(5); - head->next->next->next->next->next = new ListNode(6); - - Solution s; - ListNode *middle = s.middleNode(head); - cout << "Middle node value: " << middle->val << endl; - - return 0; -} \ No newline at end of file +#include +using namespace std; + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution +{ +public: + ListNode *middleNode(ListNode *head) + { + if (head == NULL || head->next == NULL) + { + return head; + } + + ListNode *fast = head, *slow = head; + while (fast->next->next != NULL && fast->next != NULL) + { + slow = slow->next; + fast = fast->next->next; + } + if (fast->next != NULL) + slow = slow->next; + + return slow; + } +}; + +int main() +{ + ListNode *head = new ListNode(1); + head->next = new ListNode(2); + head->next->next = new ListNode(3); + head->next->next->next = new ListNode(4); + head->next->next->next->next = new ListNode(5); + head->next->next->next->next->next = new ListNode(6); + + Solution s; + ListNode *middle = s.middleNode(head); + cout << "Middle node value: " << middle->val << endl; + + return 0; +} diff --git a/leetcode/Study Plan/9/9.py b/leetcode/Study Plan/9/9.py index 8dcd30ea..45ea0446 100644 --- a/leetcode/Study Plan/9/9.py +++ b/leetcode/Study Plan/9/9.py @@ -1,8 +1,8 @@ -class Solution: - def isPalindrome(self, x: int) -> bool: - str_x = str(x) - reversed_str_x = str_x[::-1] - if reversed_str_x == str_x: - return True - else: - return False \ No newline at end of file +class Solution: + def isPalindrome(self, x: int) -> bool: + str_x = str(x) + reversed_str_x = str_x[::-1] + if reversed_str_x == str_x: + return True + else: + return False diff --git "a/leetcode/contest/\345\221\250\350\265\233439/1.cpp" "b/leetcode/contest/\345\221\250\350\265\233439/1.cpp" index f5b6ae58..94d90472 100644 --- "a/leetcode/contest/\345\221\250\350\265\233439/1.cpp" +++ "b/leetcode/contest/\345\221\250\350\265\233439/1.cpp" @@ -1,76 +1,76 @@ -#include -#include -#include -#include - -using namespace std; - -class Solution -{ -public: - int largestInteger(vector &nums, int k) - { - int n = nums.size(); - - // 如果 k 大于数组大小,则不存在有效的子数组 - if (k > n) - return -1; - - // 统计每个 k 大小子数组中每个数字的出现次数 - unordered_map subArrayCount; - - // 对于每个可能的 k 大小子数组的起始位置 - for (int i = 0; i <= n - k; i++) - { - // 创建一个集合来跟踪此子数组中的唯一数字 - unordered_map seen; - - // 处理当前子数组中的每个元素 - for (int j = i; j < i + k; j++) - { - int num = nums[j]; - - // 如果这是我们第一次在此子数组中看到这个数字 - if (!seen[num]) - { - seen[num] = true; - subArrayCount[num]++; - } - } - } - - // 找到仅在一个子数组中出现的最大数字 - int result = -1; - for (auto &[num, count] : subArrayCount) - { - if (count == 1 && num > result) - { - result = num; - } - } - - return result; - } -}; - -int main() -{ - Solution s; - - // Example 1 - vector nums1 = {3, 9, 2, 1, 7}; - int k1 = 3; - cout << "Example 1: " << s.largestInteger(nums1, k1) << endl; // Expected: 7 - - // Example 2 - vector nums2 = {3, 9, 7, 2, 1, 7}; - int k2 = 4; - cout << "Example 2: " << s.largestInteger(nums2, k2) << endl; // Expected: 3 - - // Example 3 - vector nums3 = {0, 0}; - int k3 = 1; - cout << "Example 3: " << s.largestInteger(nums3, k3) << endl; // Expected: -1 - - return 0; -} +#include +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + int largestInteger(vector &nums, int k) + { + int n = nums.size(); + + // 如果 k 大于数组大小,则不存在有效的子数组 + if (k > n) + return -1; + + // 统计每个 k 大小子数组中每个数字的出现次数 + unordered_map subArrayCount; + + // 对于每个可能的 k 大小子数组的起始位置 + for (int i = 0; i <= n - k; i++) + { + // 创建一个集合来跟踪此子数组中的唯一数字 + unordered_map seen; + + // 处理当前子数组中的每个元素 + for (int j = i; j < i + k; j++) + { + int num = nums[j]; + + // 如果这是我们第一次在此子数组中看到这个数字 + if (!seen[num]) + { + seen[num] = true; + subArrayCount[num]++; + } + } + } + + // 找到仅在一个子数组中出现的最大数字 + int result = -1; + for (auto &[num, count] : subArrayCount) + { + if (count == 1 && num > result) + { + result = num; + } + } + + return result; + } +}; + +int main() +{ + Solution s; + + // Example 1 + vector nums1 = {3, 9, 2, 1, 7}; + int k1 = 3; + cout << "Example 1: " << s.largestInteger(nums1, k1) << endl; // Expected: 7 + + // Example 2 + vector nums2 = {3, 9, 7, 2, 1, 7}; + int k2 = 4; + cout << "Example 2: " << s.largestInteger(nums2, k2) << endl; // Expected: 3 + + // Example 3 + vector nums3 = {0, 0}; + int k3 = 1; + cout << "Example 3: " << s.largestInteger(nums3, k3) << endl; // Expected: -1 + + return 0; +} diff --git "a/leetcode/contest/\345\221\250\350\265\233439/2.cpp" "b/leetcode/contest/\345\221\250\350\265\233439/2.cpp" index 6d845994..c4d85eec 100644 --- "a/leetcode/contest/\345\221\250\350\265\233439/2.cpp" +++ "b/leetcode/contest/\345\221\250\350\265\233439/2.cpp" @@ -1,77 +1,77 @@ -#include -#include -#include -#include -using namespace std; - -class Solution -{ -public: - int longestPalindromeSubseq(string s, int k) - { - int n = s.length(); - - // dp[i][j][remain] 表示考虑 s[i...j],最多剩余 'remain' 次操作的最长回文子序列的长度 - vector>> dp(n, vector>(n, vector(k + 1, -1))); - - return solve(s, 0, n - 1, k, dp); - } - -private: - int solve(const string &s, int i, int j, int remain, vector>> &dp) - { - // 基本情况 - if (i > j) - return 0; - if (i == j) - return 1; - - // 如果已经计算过 - if (dp[i][j][remain] != -1) - return dp[i][j][remain]; - - int result = 0; - - // 情况 1: 跳过两个字符 - result = max(solve(s, i + 1, j - 1, remain, dp), result); - - // 情况 2: 跳过左字符 - result = max(solve(s, i + 1, j, remain, dp), result); - - // 情况 3: 跳过右字符 - result = max(solve(s, i, j - 1, remain, dp), result); - - // 情况 4: 包含两个字符 - if (s[i] == s[j]) - { - // 如果字符匹配,不需要操作 - result = max(solve(s, i + 1, j - 1, remain, dp) + 2, result); - } - else if (remain > 0) - { - // 尝试通过一次操作使字符匹配 - // 我们可以改变 s[i] 或 s[j] - result = max(solve(s, i + 1, j - 1, remain - 1, dp) + 2, result); - } - - dp[i][j][remain] = result; - return result; - } -}; - -int main() -{ - Solution solution; - - // 示例 1 - string s1 = "abced"; - int k1 = 2; - cout << "示例 1: " << solution.longestPalindromeSubseq(s1, k1) << endl; // 期望: 3 - - // 示例 2 - string s2 = "aaazzz"; - int k2 = 4; - cout << "示例 2: " << solution.longestPalindromeSubseq(s2, k2) << endl; // 期望: 6 - - return 0; -} +#include +#include +#include +#include +using namespace std; + +class Solution +{ +public: + int longestPalindromeSubseq(string s, int k) + { + int n = s.length(); + + // dp[i][j][remain] 表示考虑 s[i...j],最多剩余 'remain' 次操作的最长回文子序列的长度 + vector>> dp(n, vector>(n, vector(k + 1, -1))); + + return solve(s, 0, n - 1, k, dp); + } + +private: + int solve(const string &s, int i, int j, int remain, vector>> &dp) + { + // 基本情况 + if (i > j) + return 0; + if (i == j) + return 1; + + // 如果已经计算过 + if (dp[i][j][remain] != -1) + return dp[i][j][remain]; + + int result = 0; + + // 情况 1: 跳过两个字符 + result = max(solve(s, i + 1, j - 1, remain, dp), result); + + // 情况 2: 跳过左字符 + result = max(solve(s, i + 1, j, remain, dp), result); + + // 情况 3: 跳过右字符 + result = max(solve(s, i, j - 1, remain, dp), result); + + // 情况 4: 包含两个字符 + if (s[i] == s[j]) + { + // 如果字符匹配,不需要操作 + result = max(solve(s, i + 1, j - 1, remain, dp) + 2, result); + } + else if (remain > 0) + { + // 尝试通过一次操作使字符匹配 + // 我们可以改变 s[i] 或 s[j] + result = max(solve(s, i + 1, j - 1, remain - 1, dp) + 2, result); + } + + dp[i][j][remain] = result; + return result; + } +}; + +int main() +{ + Solution solution; + + // 示例 1 + string s1 = "abced"; + int k1 = 2; + cout << "示例 1: " << solution.longestPalindromeSubseq(s1, k1) << endl; // 期望: 3 + + // 示例 2 + string s2 = "aaazzz"; + int k2 = 4; + cout << "示例 2: " << solution.longestPalindromeSubseq(s2, k2) << endl; // 期望: 6 + + return 0; +} diff --git "a/leetcode/contest/\345\221\250\350\265\233472/Q1. \347\274\272\345\244\261\347\232\204\346\234\200\345\260\217\345\200\215\346\225\260/1.py" "b/leetcode/contest/\345\221\250\350\265\233472/Q1. \347\274\272\345\244\261\347\232\204\346\234\200\345\260\217\345\200\215\346\225\260/1.py" index f3c6c6fe..1d9d505b 100644 --- "a/leetcode/contest/\345\221\250\350\265\233472/Q1. \347\274\272\345\244\261\347\232\204\346\234\200\345\260\217\345\200\215\346\225\260/1.py" +++ "b/leetcode/contest/\345\221\250\350\265\233472/Q1. \347\274\272\345\244\261\347\232\204\346\234\200\345\260\217\345\200\215\346\225\260/1.py" @@ -1,8 +1,8 @@ -class Solution: - def missingMultiple(self, nums: List[int], k: int) -> int: - - s = set(nums) - - for i in range(1,len(nums)+2): - if i*k not in s: - return i*k +class Solution: + def missingMultiple(self, nums: List[int], k: int) -> int: + + s = set(nums) + + for i in range(1,len(nums)+2): + if i*k not in s: + return i*k diff --git "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q1. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 I.py" "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q1. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 I.py" index aacf7961..6517bba8 100644 --- "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q1. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 I.py" +++ "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q1. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 I.py" @@ -11,4 +11,4 @@ def sumAndMultiply(self, n: int) -> int: sum += int(c) if sum == 0: return 0 - return int(ans)*sum \ No newline at end of file + return int(ans)*sum diff --git "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q2. \346\234\200\345\244\247\345\271\263\350\241\241\345\274\202\346\210\226\345\255\220\346\225\260\347\273\204\347\232\204\351\225\277\345\272\246.py" "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q2. \346\234\200\345\244\247\345\271\263\350\241\241\345\274\202\346\210\226\345\255\220\346\225\260\347\273\204\347\232\204\351\225\277\345\272\246.py" index 74896464..8088970c 100644 --- "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q2. \346\234\200\345\244\247\345\271\263\350\241\241\345\274\202\346\210\226\345\255\220\346\225\260\347\273\204\347\232\204\351\225\277\345\272\246.py" +++ "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q2. \346\234\200\345\244\247\345\271\263\350\241\241\345\274\202\346\210\226\345\255\220\346\225\260\347\273\204\347\232\204\351\225\277\345\272\246.py" @@ -30,4 +30,4 @@ def maxBalancedSubarray(self, nums: List[int]) -> int: # 没出现过,记录下该状态第一次出现的位置 norivandal[current_state] = k - return max_length # 返回最大长度 \ No newline at end of file + return max_length # 返回最大长度 diff --git "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q3. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 II.py" "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q3. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 II.py" index 20904efa..fd428a96 100644 --- "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q3. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 II.py" +++ "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q3. \350\277\236\346\216\245\351\235\236\351\233\266\346\225\260\345\255\227\345\271\266\344\271\230\344\273\245\345\205\266\346\225\260\345\255\227\345\222\214 II.py" @@ -35,19 +35,19 @@ def sumAndMultiply(self, s: str, queries: List[List[int]]) -> List[int]: if is_nonzero: # 1. 更新 P_count P_count[i + 1] += 1 - - # 2. 更新 P_sum - P_sum[i + 1] = (P_sum[i + 1] + digit_val) % M + + # 2. 更新 P_sum + P_sum[i + 1] = (P_sum[i + 1] + digit_val) % M # P_sum[i+1] 继承了 P_sum[i],故只需加上 digit_val - + # 3. 更新 P_x (左移一位,加上当前数字) # 这里的 P_x[i+1] 在上一步被设置为 P_x[i] P_x[i + 1] = (P_x[i + 1] * 10 + digit_val) % M ans = [] for l, r in queries: - - idx_s = l + + idx_s = l idx_e = r + 1 count_nozero = P_count[idx_e] - P_count[idx_s] @@ -65,7 +65,7 @@ def sumAndMultiply(self, s: str, queries: List[List[int]]) -> List[int]: # 2. 计算 x (非零数字连接形成的大整数) shift = count_nozero - + # P_x[l] * 10^(shift) mod M shift_term = (P_x[idx_s] * pow10[shift]) % M @@ -80,4 +80,4 @@ def sumAndMultiply(self, s: str, queries: List[List[int]]) -> List[int]: ans.append(answer) - return ans \ No newline at end of file + return ans diff --git "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q4. \346\234\211\346\225\210\345\255\220\345\272\217\345\210\227\347\232\204\346\225\260\351\207\217.py" "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q4. \346\234\211\346\225\210\345\255\220\345\272\217\345\210\227\347\232\204\346\225\260\351\207\217.py" index e3a4659a..338600b8 100644 --- "a/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q4. \346\234\211\346\225\210\345\255\220\345\272\217\345\210\227\347\232\204\346\225\260\351\207\217.py" +++ "b/leetcode/contest/\347\254\254 477 \345\234\272\345\221\250\350\265\233/Q4. \346\234\211\346\225\210\345\255\220\345\272\217\345\210\227\347\232\204\346\225\260\351\207\217.py" @@ -4,36 +4,36 @@ class Solution: def countEffective(self, nums: List[int]) -> int: MOD = 10**9 + 7 n = len(nums) - + # 总 OR tot = 0 for x in nums: tot |= x - + # 如果总 OR 为 0,说明全是 0,不可能让 OR 更小 if tot == 0: return 0 - + # 提取 tot 中为 1 的 bit,压缩 bits = [] for i in range(20): # 因为 nums[i] <= 1e6 < 2^20 if (tot >> i) & 1: bits.append(i) - + B = len(bits) allmask = (1 << B) - 1 - + # 统计每种压缩 bitmask 的出现次数 size = 1 << B cnt = [0] * size - + for x in nums: mask = 0 for j, b in enumerate(bits): if (x >> b) & 1: mask |= (1 << j) cnt[mask] += 1 - + # SOS DP:F[s] = sum_{sub ⊆ s} cnt[sub] F = cnt[:] # 拷贝一份 for i in range(B): @@ -41,23 +41,23 @@ def countEffective(self, nums: List[int]) -> int: for mask in range(size): if mask & bit: F[mask] += F[mask ^ bit] - + # 预处理 2^k max_k = n # sum_no_intersect(t) ≤ n pow2 = [1] * (max_k + 1) for i in range(1, max_k + 1): pow2[i] = (pow2[i - 1] * 2) % MOD - + # 容斥求 |E1 ∪ E2 ∪ ...| ans = 0 for t in range(1, size): comp = allmask ^ t sum_no = F[comp] # 与 t 完全不相交的元素个数 ways = pow2[sum_no] - + if bin(t).count("1") % 2 == 1: ans = (ans + ways) % MOD else: ans = (ans - ways) % MOD - + return ans % MOD diff --git "a/leetcode/hot100/1. \344\270\244\346\225\260\344\271\213\345\222\214/1.cpp" "b/leetcode/hot100/1. \344\270\244\346\225\260\344\271\213\345\222\214/1.cpp" index 5597ab1d..187e5cb6 100644 --- "a/leetcode/hot100/1. \344\270\244\346\225\260\344\271\213\345\222\214/1.cpp" +++ "b/leetcode/hot100/1. \344\270\244\346\225\260\344\271\213\345\222\214/1.cpp" @@ -1,3 +1,4 @@ +#include #include class Solution { public: diff --git "a/leetcode/hot100/283. \347\247\273\345\212\250\351\233\266/1.cpp" "b/leetcode/hot100/283. \347\247\273\345\212\250\351\233\266/1.cpp" index 16ab9094..dfe0ccc2 100644 --- "a/leetcode/hot100/283. \347\247\273\345\212\250\351\233\266/1.cpp" +++ "b/leetcode/hot100/283. \347\247\273\345\212\250\351\233\266/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include using namespace std; diff --git a/leetcode/problems/10.regular-expression-matching.cpp b/leetcode/problems/10.regular-expression-matching.cpp index d3d6fe25..b4730fcb 100644 --- a/leetcode/problems/10.regular-expression-matching.cpp +++ b/leetcode/problems/10.regular-expression-matching.cpp @@ -34,7 +34,7 @@ using namespace std; class Solution { public: bool isMatch(string s, string p) { - + } }; // @lc code=end diff --git a/leetcode/problems/198.house-robber.py b/leetcode/problems/198.house-robber.py index 02532f54..997bb7f8 100644 --- a/leetcode/problems/198.house-robber.py +++ b/leetcode/problems/198.house-robber.py @@ -16,7 +16,7 @@ def rob(self, nums: List[int]) -> int: f0 ,f1 = 0,0 for x in nums: f0,f1 = f1,max(f1,f0+x) - + return f1 # @lc code=end diff --git a/leetcode/problems/2452.words-within-two-edits-of-dictionary.cpp b/leetcode/problems/2452.words-within-two-edits-of-dictionary.cpp index c614b450..0df14950 100644 --- a/leetcode/problems/2452.words-within-two-edits-of-dictionary.cpp +++ b/leetcode/problems/2452.words-within-two-edits-of-dictionary.cpp @@ -34,7 +34,7 @@ using namespace std; class Solution { public: vector twoEditWords(vector& queries, vector& dictionary) { - + } }; // @lc code=end diff --git "a/leetcode/problems/LCR 138.\346\234\211\346\225\210\346\225\260\345\255\227.cpp" "b/leetcode/problems/LCR 138.\346\234\211\346\225\210\346\225\260\345\255\227.cpp" index d58e2cd5..b7f36dcd 100644 --- "a/leetcode/problems/LCR 138.\346\234\211\346\225\210\346\225\260\345\255\227.cpp" +++ "b/leetcode/problems/LCR 138.\346\234\211\346\225\210\346\225\260\345\255\227.cpp" @@ -53,7 +53,7 @@ class Solution { eFlag = true; numFlag = false; //e 后面必须有数字,这里需要重置 numFlag }else if((s[i] == '+' || s[i] == '-') && (i == 0 || s[i-1] == 'e' || s[i-1] == 'E')){ - + } else{ return false; diff --git "a/luogu/ProblemSet/P1616 \347\226\257\347\213\202\347\232\204\351\207\207\350\215\257/1.py" "b/luogu/ProblemSet/P1616 \347\226\257\347\213\202\347\232\204\351\207\207\350\215\257/1.py" index db493461..db82c628 100644 --- "a/luogu/ProblemSet/P1616 \347\226\257\347\213\202\347\232\204\351\207\207\350\215\257/1.py" +++ "b/luogu/ProblemSet/P1616 \347\226\257\347\213\202\347\232\204\351\207\207\350\215\257/1.py" @@ -1,22 +1,22 @@ # 输入:t是背包容量,m是物品数量 t, m = map(int, input().split()) -T = [] +T = [] V = [] # 输入m个物品的体积和价值 for i in range(m): e1, e2 = map(int, input().split()) - T.append(e1) - V.append(e2) + T.append(e1) + V.append(e2) # DP数组:dp[j]表示容量为j时的最大价值 -dp = [0] * (t + 1) +dp = [0] * (t + 1) # 完全背包:每个物品可以选多次 for i in range(m): - for j in range(T[i], t + 1): + for j in range(T[i], t + 1): dp[j] = max(dp[j], dp[j - T[i]] + V[i]) # 输出容量为t时的最大价值 -print(dp[t]) \ No newline at end of file +print(dp[t]) diff --git a/luogu/ProblemSet/P4956 [COCI 2017/2018 #6] Davor/1.cpp b/luogu/ProblemSet/P4956 [COCI 2017/2018 #6] Davor/1.cpp index fc7c55e5..5912a81d 100644 --- a/luogu/ProblemSet/P4956 [COCI 2017/2018 #6] Davor/1.cpp +++ b/luogu/ProblemSet/P4956 [COCI 2017/2018 #6] Davor/1.cpp @@ -5,7 +5,7 @@ using namespace std; int main(){ int n,x,k; cin >> n; - + for(x = 100;x>=1;x--) { int remainder = n - 364*n; diff --git a/luogu/algorithm/1-2_Sort/P1271/1.cpp b/luogu/algorithm/1-2_Sort/P1271/1.cpp index 588e08f7..e1f5ccee 100644 --- a/luogu/algorithm/1-2_Sort/P1271/1.cpp +++ b/luogu/algorithm/1-2_Sort/P1271/1.cpp @@ -8,10 +8,10 @@ int main() cin >> n >> m; // 输入n和m for (int i = 0; i < m; i++) cin >> a[i]; // 循环读入m个数到数组a中 - + sort(a, a + m); // 使用sort函数对数组a中的m个数进行升序排序 - + for (int i = 0; i < m; i++) cout << a[i] << " "; // 循环输出排序后的m个数,每个数后面加空格 return 0; -} \ No newline at end of file +} diff --git a/luogu/algorithm/1-2_Sort/P1271/counting_sort.cpp b/luogu/algorithm/1-2_Sort/P1271/counting_sort.cpp index 25dbedc9..c200505d 100644 --- a/luogu/algorithm/1-2_Sort/P1271/counting_sort.cpp +++ b/luogu/algorithm/1-2_Sort/P1271/counting_sort.cpp @@ -31,4 +31,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git a/luogu/algorithm/1-2_Sort/P1271/insert_sort.cpp b/luogu/algorithm/1-2_Sort/P1271/insert_sort.cpp index 24920fad..801bd176 100644 --- a/luogu/algorithm/1-2_Sort/P1271/insert_sort.cpp +++ b/luogu/algorithm/1-2_Sort/P1271/insert_sort.cpp @@ -44,4 +44,4 @@ int main() cout << endl; return 0; -} \ No newline at end of file +} diff --git a/luogu/algorithm/1-3_Brute Force/P2089/1.cpp b/luogu/algorithm/1-3_Brute Force/P2089/1.cpp index 99da9c8d..e5844c04 100644 --- a/luogu/algorithm/1-3_Brute Force/P2089/1.cpp +++ b/luogu/algorithm/1-3_Brute Force/P2089/1.cpp @@ -8,7 +8,7 @@ int n, m; // 将找到的方案转换为字符串形式并计数 void leijia(int a) -{ +{ int b = 262144;//表示处理到第10中配料 while (a) { @@ -44,4 +44,4 @@ int main() f(n, 0); cout << m << endl << tot; return 0; -} \ No newline at end of file +} diff --git a/luogu/algorithm/1-3_Brute Force/P2241/1.py b/luogu/algorithm/1-3_Brute Force/P2241/1.py index 31807fc0..76bdd370 100644 --- a/luogu/algorithm/1-3_Brute Force/P2241/1.py +++ b/luogu/algorithm/1-3_Brute Force/P2241/1.py @@ -11,4 +11,4 @@ cnt_rectangle = sum_all - cnt_square # 输出结果,使用print()函数输出正方形和长方形的数量 -print(cnt_square, cnt_rectangle) \ No newline at end of file +print(cnt_square, cnt_rectangle) diff --git "a/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q1.cpp" "b/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q1.cpp" index 9ca52f57..36638aec 100644 --- "a/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q1.cpp" +++ "b/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q1.cpp" @@ -26,4 +26,4 @@ int main(){ cout << (flag ? "Yes" : "No") << endl; return 0; -} \ No newline at end of file +} diff --git "a/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q2.cpp" "b/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q2.cpp" index 85bfc8d4..80a80ba4 100644 --- "a/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q2.cpp" +++ "b/luogu/contest/\343\200\220LGR-203-Div.4\343\200\221\346\264\233\350\260\267\345\205\245\351\227\250\350\265\233 #28/q2.cpp" @@ -34,8 +34,8 @@ int main(){ } cout << count << endl; - - + + } -} \ No newline at end of file +} diff --git "a/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/A.cpp" "b/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/A.cpp" index b12788a4..aec88a50 100644 --- "a/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/A.cpp" +++ "b/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/A.cpp" @@ -54,14 +54,14 @@ il void solve(){ int main() { fastio - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } @@ -151,7 +151,7 @@ int main() { for (int col = 1; col <= n; ++col) { if (cnt[col] == 0) continue; - + // 构建 S 的差分: // 颜色在 [L[col], R[col]-1] 这一段“间隙”内是活跃的 // 所以在 L[col] 处异或 w,在 R[col] 处再次异或 w(消除) @@ -177,19 +177,19 @@ int main() { for (int i = 1; i <= n; ++i) { // 1. 计算当前位置的结构哈希 S curr_S ^= diff_S[i]; - + // 2. 计算内容前缀和 C curr_C += w[c[i]]; - + // 3. 计算完结前缀和 E curr_E += point_E[i]; - + // 4. 计算校验哈希 K = C - E ull curr_K = curr_C - curr_E; // 记录状态 states.push_back({curr_S, curr_K, i}); - + // 记录权值 v 的前缀和 sum_v[i] = sum_v[i-1] + v[i]; } @@ -221,4 +221,4 @@ int main() { cout << min_val << endl; return 0; -} \ No newline at end of file +} diff --git "a/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/B.cpp" "b/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/B.cpp" index bf8384ae..d9e27050 100644 --- "a/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/B.cpp" +++ "b/luogu/contest/\343\200\220MX-S12\343\200\221\346\242\246\347\206\212 NOIP 2025 \346\250\241\346\213\237\350\265\233 4\357\274\210\345\220\214\346\255\245\350\265\233\357\274\211/B.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -99,7 +100,7 @@ il void solve(){ diff_S[R[col]] ^= W[col]; } - + // constuct the difference of s point_E[R[col]] += W[col] * (ull)cnt[col]; } @@ -163,14 +164,14 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/luogu/learning/P1706/1.cpp b/luogu/learning/P1706/1.cpp index e461c8a8..295767cf 100644 --- a/luogu/learning/P1706/1.cpp +++ b/luogu/learning/P1706/1.cpp @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git a/luogu/month_contest/B3648/1.py b/luogu/month_contest/B3648/1.py index fc7dc385..4eb1c20f 100644 --- a/luogu/month_contest/B3648/1.py +++ b/luogu/month_contest/B3648/1.py @@ -1,3 +1,3 @@ year = int(input()) -print(f"I am {year} years old.") \ No newline at end of file +print(f"I am {year} years old.") diff --git a/luogu/month_contest/B3652/1.py b/luogu/month_contest/B3652/1.py index cf9aded7..8300d612 100644 --- a/luogu/month_contest/B3652/1.py +++ b/luogu/month_contest/B3652/1.py @@ -2,7 +2,7 @@ a = list(map(int,input().split())) - + max_value = max(a) min_value = min(a) diff --git a/luogu/month_contest/T528356/1.py b/luogu/month_contest/T528356/1.py index 34c4e35f..4864bc9b 100644 --- a/luogu/month_contest/T528356/1.py +++ b/luogu/month_contest/T528356/1.py @@ -7,8 +7,8 @@ def Y_function(a): def L_function(a): a *= 3 - a += 5 + a += 5 print(a,end=" ") Y_function(a) -L_function(a) \ No newline at end of file +L_function(a) diff --git a/luogu/month_contest/T528356/4.py b/luogu/month_contest/T528356/4.py index 8c7f10f2..8909c5f0 100644 --- a/luogu/month_contest/T528356/4.py +++ b/luogu/month_contest/T528356/4.py @@ -7,4 +7,4 @@ if a[i] == i print("No") break - if + if diff --git "a/luogu/\345\205\245\351\227\250/P1009/1.cpp" "b/luogu/\345\205\245\351\227\250/P1009/1.cpp" index 16ac47c2..dcff1b85 100644 --- "a/luogu/\345\205\245\351\227\250/P1009/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1009/1.cpp" @@ -18,4 +18,4 @@ int main() { } printf("%llu\n", sum); return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1046/1.cpp" "b/luogu/\345\205\245\351\227\250/P1046/1.cpp" index 69ad7605..65abff3e 100644 --- "a/luogu/\345\205\245\351\227\250/P1046/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1046/1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/luogu/\345\205\245\351\227\250/P1055/1.cpp" "b/luogu/\345\205\245\351\227\250/P1055/1.cpp" index 48510791..4ab36c05 100644 --- "a/luogu/\345\205\245\351\227\250/P1055/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1055/1.cpp" @@ -30,4 +30,4 @@ int main() cout << s; } return 0; // 皮一句,必须要打!否则爆0! -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1055/1.py" "b/luogu/\345\205\245\351\227\250/P1055/1.py" index 39c1020f..0e675783 100644 --- "a/luogu/\345\205\245\351\227\250/P1055/1.py" +++ "b/luogu/\345\205\245\351\227\250/P1055/1.py" @@ -12,12 +12,12 @@ s = 0 for i in range(9): s += m%10*(9-i) - s %= 11 + s %= 11 m //= 10 if s == d: print("Right") -else: +else: if s == 10: print(f"{a}-{b}-{c}-X") else: diff --git "a/luogu/\345\205\245\351\227\250/P1055/tempCodeRunnerFile.cpp" "b/luogu/\345\205\245\351\227\250/P1055/tempCodeRunnerFile.cpp" index 7be1a26e..1893122d 100644 --- "a/luogu/\345\205\245\351\227\250/P1055/tempCodeRunnerFile.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1055/tempCodeRunnerFile.cpp" @@ -1 +1 @@ - // cout << (char)(sum % 11 + '1') << endl; \ No newline at end of file + // cout << (char)(sum % 11 + '1') << endl; diff --git "a/luogu/\345\205\245\351\227\250/P1085/P1085.py" "b/luogu/\345\205\245\351\227\250/P1085/P1085.py" index a6ebc6aa..c6272c19 100644 --- "a/luogu/\345\205\245\351\227\250/P1085/P1085.py" +++ "b/luogu/\345\205\245\351\227\250/P1085/P1085.py" @@ -14,4 +14,4 @@ else: print(0) - \ No newline at end of file + diff --git "a/luogu/\345\205\245\351\227\250/P1217/1.cpp" "b/luogu/\345\205\245\351\227\250/P1217/1.cpp" index a5a8bca3..75daa2d1 100644 --- "a/luogu/\345\205\245\351\227\250/P1217/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1217/1.cpp" @@ -49,4 +49,4 @@ int main() printf("%d\n", i); // printf会比cout快很多 } return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1217/3.cpp" "b/luogu/\345\205\245\351\227\250/P1217/3.cpp" index af8926cd..e37cfdc2 100644 --- "a/luogu/\345\205\245\351\227\250/P1217/3.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1217/3.cpp" @@ -43,4 +43,4 @@ int main() } } return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1255/1.cpp" "b/luogu/\345\205\245\351\227\250/P1255/1.cpp" index 6cb6c409..20f3bb29 100644 --- "a/luogu/\345\205\245\351\227\250/P1255/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1255/1.cpp" @@ -7,15 +7,15 @@ using namespace std; // 使用标准命名空间 int n, len = 1, f[5003][5003]; // f[k][i]表示第k阶台阶所对应的走法数,len用于记录有效位数 // 高精度加法函数,k表示当前阶数 -void hp(int k) +void hp(int k) { int i; // 循环变量 // 计算第k阶的走法数,使用前两阶的走法数 for (i = 1; i <= len; i++) f[k][i] = f[k - 1][i] + f[k - 2][i]; // 套用公式,f[k] = f[k-1] + f[k-2] - + // 处理进位 - for (i = 1; i <= len; i++) + for (i = 1; i <= len; i++) if (f[k][i] >= 10) // 如果当前位数大于等于10 { f[k][i + 1] += f[k][i] / 10; // 进位到下一位 @@ -32,11 +32,11 @@ int main() f[1][1] = 1; // 初始化第1阶的走法数为1 f[2][1] = 2; // 初始化第2阶的走法数为2 // 从第3阶开始计算,避免越界 - for (i = 3; i <= n; i++) + for (i = 3; i <= n; i++) hp(i); // 调用高精度加法函数计算走法数 - + // 逆序输出结果 - for (i = len; i >= 1; i--) + for (i = len; i >= 1; i--) printf("%d", f[n][i]); // 输出第n阶的走法数 return 0; // 返回0表示程序正常结束 -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1307/1.cpp" "b/luogu/\345\205\245\351\227\250/P1307/1.cpp" index d531151c..d31c2c9c 100644 --- "a/luogu/\345\205\245\351\227\250/P1307/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1307/1.cpp" @@ -6,32 +6,32 @@ using namespace std; int main(){ int n; cin >> n; - + // 处理负数的情况 bool isNegative = false; if (n < 0) { isNegative = true; n = -n; } - + // 特殊情况:如果n为0,直接输出0 if (n == 0) { cout << 0 << endl; return 0; } - + // 反转数字 int reversed = 0; while (n > 0) { reversed = reversed * 10 + n % 10; n /= 10; } - + // 输出结果,如果原数是负数,加上负号 if (isNegative) { cout << "-"; } cout << reversed << endl; - + return 0; } diff --git "a/luogu/\345\205\245\351\227\250/P1420/1.cpp" "b/luogu/\345\205\245\351\227\250/P1420/1.cpp" index 21ef7403..34ee10ab 100644 --- "a/luogu/\345\205\245\351\227\250/P1420/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1420/1.cpp" @@ -28,4 +28,4 @@ int main() cout << max; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1421/P1421.cpp" "b/luogu/\345\205\245\351\227\250/P1421/P1421.cpp" index 4e7d4fdc..c82c3dc4 100644 --- "a/luogu/\345\205\245\351\227\250/P1421/P1421.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1421/P1421.cpp" @@ -7,4 +7,4 @@ int main() { cin >> a >> b; cout << (a*10+b)/19; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1421/P1421.py" "b/luogu/\345\205\245\351\227\250/P1421/P1421.py" index 0fd331fd..62664843 100644 --- "a/luogu/\345\205\245\351\227\250/P1421/P1421.py" +++ "b/luogu/\345\205\245\351\227\250/P1421/P1421.py" @@ -2,4 +2,4 @@ pen_amount = (a*10+b) // 19 -print(pen_amount) \ No newline at end of file +print(pen_amount) diff --git "a/luogu/\345\205\245\351\227\250/P1423/2.cpp" "b/luogu/\345\205\245\351\227\250/P1423/2.cpp" index b48e29b5..189594e0 100644 --- "a/luogu/\345\205\245\351\227\250/P1423/2.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1423/2.cpp" @@ -10,4 +10,4 @@ int main() n = ceil(log(1 - 0.01 * s) / log(0.98)); cout << n; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1424/1_branch.cpp" "b/luogu/\345\205\245\351\227\250/P1424/1_branch.cpp" index dd054efd..b8732ec9 100644 --- "a/luogu/\345\205\245\351\227\250/P1424/1_branch.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1424/1_branch.cpp" @@ -24,4 +24,4 @@ int main() } printf("%llu", ans); return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1424/2_loop.py" "b/luogu/\345\205\245\351\227\250/P1424/2_loop.py" index a1f3f359..2497c3ce 100644 --- "a/luogu/\345\205\245\351\227\250/P1424/2_loop.py" +++ "b/luogu/\345\205\245\351\227\250/P1424/2_loop.py" @@ -6,5 +6,5 @@ current_day = (x+day-1)%7+1 if current_day not in [6,7]: work_days += 1 - + print(work_days * 250) diff --git "a/luogu/\345\205\245\351\227\250/P1424/tempCodeRunnerFile.py" "b/luogu/\345\205\245\351\227\250/P1424/tempCodeRunnerFile.py" index c0f02f7d..5a71f3b9 100644 --- "a/luogu/\345\205\245\351\227\250/P1424/tempCodeRunnerFile.py" +++ "b/luogu/\345\205\245\351\227\250/P1424/tempCodeRunnerFile.py" @@ -1,2 +1,2 @@ if i % 7 != 6 or i % 7 != 0: - total_workday += 1 \ No newline at end of file + total_workday += 1 diff --git "a/luogu/\345\205\245\351\227\250/P1425/P1425.cpp" "b/luogu/\345\205\245\351\227\250/P1425/P1425.cpp" index a9ec90aa..c6675a2a 100644 --- "a/luogu/\345\205\245\351\227\250/P1425/P1425.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1425/P1425.cpp" @@ -9,4 +9,4 @@ int main(){ int used_time = (c - a) * 60 + d - b; cout << used_time / 60 << " " << used_time % 60 << endl; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1425/P1425.py" "b/luogu/\345\205\245\351\227\250/P1425/P1425.py" index 33f2f105..dd1a14ff 100644 --- "a/luogu/\345\205\245\351\227\250/P1425/P1425.py" +++ "b/luogu/\345\205\245\351\227\250/P1425/P1425.py" @@ -12,4 +12,4 @@ e = use_time // 60 f = use_time % 60 -print(f"{e} {f}") \ No newline at end of file +print(f"{e} {f}") diff --git "a/luogu/\345\205\245\351\227\250/P1427 \345\260\217\351\261\274\347\232\204\346\225\260\345\255\227\346\270\270\346\210\217/1.cpp" "b/luogu/\345\205\245\351\227\250/P1427 \345\260\217\351\261\274\347\232\204\346\225\260\345\255\227\346\270\270\346\210\217/1.cpp" index 8cb729e6..df09aa0a 100644 --- "a/luogu/\345\205\245\351\227\250/P1427 \345\260\217\351\261\274\347\232\204\346\225\260\345\255\227\346\270\270\346\210\217/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1427 \345\260\217\351\261\274\347\232\204\346\225\260\345\255\227\346\270\270\346\210\217/1.cpp" @@ -20,4 +20,4 @@ int main(){ } return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1720/2.cpp" "b/luogu/\345\205\245\351\227\250/P1720/2.cpp" index e69de29b..8b137891 100644 --- "a/luogu/\345\205\245\351\227\250/P1720/2.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1720/2.cpp" @@ -0,0 +1 @@ + diff --git "a/luogu/\345\205\245\351\227\250/P1720/3.cpp" "b/luogu/\345\205\245\351\227\250/P1720/3.cpp" index 45874a0c..8593af73 100644 --- "a/luogu/\345\205\245\351\227\250/P1720/3.cpp" +++ "b/luogu/\345\205\245\351\227\250/P1720/3.cpp" @@ -12,7 +12,7 @@ double calculate(int n){ for(int i = 1; i <= n; i++){ sum1 *= n1; sum2 *= n2; - } + } return (sum1 - sum2)/sqrt(5); } @@ -21,4 +21,4 @@ int main(){ cin >> n; cout << setprecision(2) << fixed << calculate(n); return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P1909/P1909.py" "b/luogu/\345\205\245\351\227\250/P1909/P1909.py" index deadeb2d..930fbd15 100644 --- "a/luogu/\345\205\245\351\227\250/P1909/P1909.py" +++ "b/luogu/\345\205\245\351\227\250/P1909/P1909.py" @@ -11,4 +11,4 @@ cost = num_packages * price min_cost = min(min_cost, cost) -print(min_cost) \ No newline at end of file +print(min_cost) diff --git "a/luogu/\345\205\245\351\227\250/P2029/P2029.cpp" "b/luogu/\345\205\245\351\227\250/P2029/P2029.cpp" index 2ab69234..c376c7d0 100644 --- "a/luogu/\345\205\245\351\227\250/P2029/P2029.cpp" +++ "b/luogu/\345\205\245\351\227\250/P2029/P2029.cpp" @@ -10,4 +10,4 @@ int main(){ int ans = ceil(20*1e3/v); cout << ans << endl; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P2029/P2029.py" "b/luogu/\345\205\245\351\227\250/P2029/P2029.py" index cc3c6436..99ebb2b4 100644 --- "a/luogu/\345\205\245\351\227\250/P2029/P2029.py" +++ "b/luogu/\345\205\245\351\227\250/P2029/P2029.py" @@ -3,4 +3,4 @@ h,r = map(int,input().split()) v = 3.14 *r*r*h bucket_ammount = 20*1e3/v -print(math.ceil(bucket_ammount)) \ No newline at end of file +print(math.ceil(bucket_ammount)) diff --git "a/luogu/\345\205\245\351\227\250/P2433/P2433.cpp" "b/luogu/\345\205\245\351\227\250/P2433/P2433.cpp" index 0402150c..36ca57c6 100644 --- "a/luogu/\345\205\245\351\227\250/P2433/P2433.cpp" +++ "b/luogu/\345\205\245\351\227\250/P2433/P2433.cpp" @@ -48,7 +48,7 @@ int main() { double PI = 3.141593; double volume = 4.0/3*PI*(1000+64); double prism; - prism = pow(volume,1.0/3); + prism = pow(volume,1.0/3); printf("%.0lf\n",prism); } else if (T == 14) { double a = -1,b=100,c=-2400; @@ -70,4 +70,4 @@ int main() { } } return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P3954/P3954.cpp" "b/luogu/\345\205\245\351\227\250/P3954/P3954.cpp" index fa847a8f..d34be450 100644 --- "a/luogu/\345\205\245\351\227\250/P3954/P3954.cpp" +++ "b/luogu/\345\205\245\351\227\250/P3954/P3954.cpp" @@ -1,10 +1,10 @@ #include -using namespace std; +using namespace std; int main() { int A,B,C; cin >> A >> B >> C; cout << A*0.2+B*0.3+C*0.5; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P3954/P3954.py" "b/luogu/\345\205\245\351\227\250/P3954/P3954.py" index 79a4c799..f11f4d52 100644 --- "a/luogu/\345\205\245\351\227\250/P3954/P3954.py" +++ "b/luogu/\345\205\245\351\227\250/P3954/P3954.py" @@ -2,4 +2,4 @@ score = A*0.2 + B*0.3 + C*0.5 -print(int(score)) \ No newline at end of file +print(int(score)) diff --git "a/luogu/\345\205\245\351\227\250/P4414/1.cpp" "b/luogu/\345\205\245\351\227\250/P4414/1.cpp" index 1a747501..79605064 100644 --- "a/luogu/\345\205\245\351\227\250/P4414/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P4414/1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/luogu/\345\205\245\351\227\250/P5707/P5707.cpp" "b/luogu/\345\205\245\351\227\250/P5707/P5707.cpp" index 4adb9764..3581e21f 100644 --- "a/luogu/\345\205\245\351\227\250/P5707/P5707.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5707/P5707.cpp" @@ -13,7 +13,7 @@ int main(){ int current_hour = 8; int current_minute = 0; - //calculate the latest time + //calculate the latest time int total_minutes = current_hour * 60 + current_minute - t; if (total_minutes < 0){ total_minutes += 24 * 60; diff --git "a/luogu/\345\205\245\351\227\250/P5707/P5707.py" "b/luogu/\345\205\245\351\227\250/P5707/P5707.py" index 751da192..6be69865 100644 --- "a/luogu/\345\205\245\351\227\250/P5707/P5707.py" +++ "b/luogu/\345\205\245\351\227\250/P5707/P5707.py" @@ -14,4 +14,4 @@ # 格式化输出 formatted_time = latest_departure_time.strftime("%H:%M") -print(formatted_time) \ No newline at end of file +print(formatted_time) diff --git "a/luogu/\345\205\245\351\227\250/P5708/P5708.cpp" "b/luogu/\345\205\245\351\227\250/P5708/P5708.cpp" index c06dc156..4ab04ddd 100644 --- "a/luogu/\345\205\245\351\227\250/P5708/P5708.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5708/P5708.cpp" @@ -9,4 +9,4 @@ int main(){ double p = (a+b+c)/2; double s = std::sqrt(p*(p-a)*(p-b)*(p-c)); printf("%.1f",s); -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5708/P5708.py" "b/luogu/\345\205\245\351\227\250/P5708/P5708.py" index 53cde2c2..c89d635a 100644 --- "a/luogu/\345\205\245\351\227\250/P5708/P5708.py" +++ "b/luogu/\345\205\245\351\227\250/P5708/P5708.py" @@ -6,4 +6,4 @@ def calc_s(a, b, c): if a + b > c and a + c > b and b + c > a: print(calc_s(a, b, c)) else: - print("These numbers cannot form a triangle.") \ No newline at end of file + print("These numbers cannot form a triangle.") diff --git "a/luogu/\345\205\245\351\227\250/P5709/P5709.py" "b/luogu/\345\205\245\351\227\250/P5709/P5709.py" index 3dafb1a2..9bbcd698 100644 --- "a/luogu/\345\205\245\351\227\250/P5709/P5709.py" +++ "b/luogu/\345\205\245\351\227\250/P5709/P5709.py" @@ -3,4 +3,4 @@ if s < m*t and t != 0: print(int(m-(s/t))) else: - print(0) \ No newline at end of file + print(0) diff --git "a/luogu/\345\205\245\351\227\250/P5710/P5710.cpp" "b/luogu/\345\205\245\351\227\250/P5710/P5710.cpp" index 17cd2f72..6d5818f8 100644 --- "a/luogu/\345\205\245\351\227\250/P5710/P5710.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5710/P5710.cpp" @@ -33,4 +33,4 @@ int main(){ } cout << n1 << " " << n2 << " " << n3 << " " << n4 << endl; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5710/P5710.py" "b/luogu/\345\205\245\351\227\250/P5710/P5710.py" index fb7ae73c..eeb3e7fe 100644 --- "a/luogu/\345\205\245\351\227\250/P5710/P5710.py" +++ "b/luogu/\345\205\245\351\227\250/P5710/P5710.py" @@ -21,4 +21,4 @@ num4 = 1 # 输出结果 -print(f'{num1} {num2} {num3} {num4}') \ No newline at end of file +print(f'{num1} {num2} {num3} {num4}') diff --git "a/luogu/\345\205\245\351\227\250/P5711/P5711.cpp" "b/luogu/\345\205\245\351\227\250/P5711/P5711.cpp" index dcf68409..5198cbfd 100644 --- "a/luogu/\345\205\245\351\227\250/P5711/P5711.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5711/P5711.cpp" @@ -10,4 +10,4 @@ int main(){ else if (n % 400 == 0 ) cout << 1 << endl; else cout << 0 << endl; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5712/P5712.py" "b/luogu/\345\205\245\351\227\250/P5712/P5712.py" index 1ac2370a..24e41157 100644 --- "a/luogu/\345\205\245\351\227\250/P5712/P5712.py" +++ "b/luogu/\345\205\245\351\227\250/P5712/P5712.py" @@ -3,4 +3,4 @@ if x ==0 or x == 1: print(f"Today, I ate {x} apple.") if x > 1: - print(f"Today, I ate {x} apples.") \ No newline at end of file + print(f"Today, I ate {x} apples.") diff --git "a/luogu/\345\205\245\351\227\250/P5713/P5713.cpp" "b/luogu/\345\205\245\351\227\250/P5713/P5713.cpp" index 031abb7c..a1b73873 100644 --- "a/luogu/\345\205\245\351\227\250/P5713/P5713.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5713/P5713.cpp" @@ -11,4 +11,4 @@ int main(){ }else{ cout << "Luogu"; } -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5713/P5713.py" "b/luogu/\345\205\245\351\227\250/P5713/P5713.py" index cf2d9eb9..e08eb452 100644 --- "a/luogu/\345\205\245\351\227\250/P5713/P5713.py" +++ "b/luogu/\345\205\245\351\227\250/P5713/P5713.py" @@ -2,5 +2,5 @@ if n*5 < 11+3*n: print("Local") -else: - print("Luogu") \ No newline at end of file +else: + print("Luogu") diff --git "a/luogu/\345\205\245\351\227\250/P5714/P5714.cpp" "b/luogu/\345\205\245\351\227\250/P5714/P5714.cpp" index 1c916725..e6764b13 100644 --- "a/luogu/\345\205\245\351\227\250/P5714/P5714.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5714/P5714.cpp" @@ -14,4 +14,4 @@ int main(){ cout << bmi << endl; cout << "Overweight"<> a >> b >> c; compare(a, b, c); // 调用函数 return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5715/P5715_\344\270\211\344\275\215\346\225\260\346\216\222\345\272\217.py" "b/luogu/\345\205\245\351\227\250/P5715/P5715_\344\270\211\344\275\215\346\225\260\346\216\222\345\272\217.py" index d33cc50e..92ad36d2 100644 --- "a/luogu/\345\205\245\351\227\250/P5715/P5715_\344\270\211\344\275\215\346\225\260\346\216\222\345\272\217.py" +++ "b/luogu/\345\205\245\351\227\250/P5715/P5715_\344\270\211\344\275\215\346\225\260\346\216\222\345\272\217.py" @@ -2,4 +2,4 @@ sorted_list = sorted([a,b,c]) -print(sorted_list[0],sorted_list[1],sorted_list[2]) \ No newline at end of file +print(sorted_list[0],sorted_list[1],sorted_list[2]) diff --git "a/luogu/\345\205\245\351\227\250/P5716/P5716.cpp" "b/luogu/\345\205\245\351\227\250/P5716/P5716.cpp" index 9dbe7354..e719738d 100644 --- "a/luogu/\345\205\245\351\227\250/P5716/P5716.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5716/P5716.cpp" @@ -24,4 +24,4 @@ int main() std::cin >> year >> month; std::cout << day_in_month(year, month) << std::endl; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5717/1.py" "b/luogu/\345\205\245\351\227\250/P5717/1.py" index b52aed65..e118313a 100644 --- "a/luogu/\345\205\245\351\227\250/P5717/1.py" +++ "b/luogu/\345\205\245\351\227\250/P5717/1.py" @@ -17,5 +17,5 @@ print("Not triangle") - + diff --git "a/luogu/\345\205\245\351\227\250/P5723/2.cpp" "b/luogu/\345\205\245\351\227\250/P5723/2.cpp" index b1e53820..089cf819 100644 --- "a/luogu/\345\205\245\351\227\250/P5723/2.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5723/2.cpp" @@ -67,4 +67,4 @@ int main(){ go(0,i); //从第0位开始搜 } return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5724/1.cpp" "b/luogu/\345\205\245\351\227\250/P5724/1.cpp" index ba386721..588004d3 100644 --- "a/luogu/\345\205\245\351\227\250/P5724/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5724/1.cpp" @@ -20,4 +20,4 @@ int main(){ } cout << max - min; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\345\205\245\351\227\250/P5733/1.cpp" "b/luogu/\345\205\245\351\227\250/P5733/1.cpp" index d64ab39a..85dde2d3 100644 --- "a/luogu/\345\205\245\351\227\250/P5733/1.cpp" +++ "b/luogu/\345\205\245\351\227\250/P5733/1.cpp" @@ -12,4 +12,4 @@ int main(){ } cout << str; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/1.cpp" "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/1.cpp" index 77b28f14..5ff8a86b 100644 --- "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/1.cpp" +++ "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/1.cpp" @@ -21,4 +21,4 @@ int main() ans += m[a[i]]; cout << ans << endl; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/2.cpp" "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/2.cpp" index 94e6da98..c7628565 100644 --- "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/2.cpp" +++ "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/2.cpp" @@ -22,4 +22,4 @@ int main() } cout << ans << endl; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/sort_binary.cpp" "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/sort_binary.cpp" index cff1c263..05c1ef57 100644 --- "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/sort_binary.cpp" +++ "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/sort_binary.cpp" @@ -21,4 +21,4 @@ int main() } cout << ans; return 0; -} \ No newline at end of file +} diff --git "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/tempCodeRunnerFile.cpp" "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/tempCodeRunnerFile.cpp" index b7b45bdb..dff459a2 100644 --- "a/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/tempCodeRunnerFile.cpp" +++ "b/luogu/\350\223\235\346\241\245\346\235\257\347\234\201\350\265\233\351\242\230\345\215\225/P1102 A-B \346\225\260\345\257\271/tempCodeRunnerFile.cpp" @@ -19,7 +19,7 @@ int main(){ res += pair.second * cnt_map[pair.first + c]; } } - + cout << res << endl; return 0; -} \ No newline at end of file +} diff --git a/naukri/Weekly 186/A.cpp b/naukri/Weekly 186/A.cpp index 143c18b7..7d69eb84 100644 --- a/naukri/Weekly 186/A.cpp +++ b/naukri/Weekly 186/A.cpp @@ -1,38 +1,38 @@ -#include -int goodnessScore(string s) -{ - // Write your code here. - int goodnessScore = 0; - - int count[26] = {0}; - - for (char c : s) - { - if (c >= 'a' && c <= 'z') - { - count[c - 'a']++; - } - else - { - count[c - 'A']--; - } - } - - for (int i = 0; i < 26; ++i) - { - int caseCount = count[i]; - - if (caseCount > 0) - goodnessScore--; - else if (caseCount < 0) - { - goodnessScore++; - } - else - { - continue; - } - } - - return goodnessScore; -} +#include +int goodnessScore(string s) +{ + // Write your code here. + int goodnessScore = 0; + + int count[26] = {0}; + + for (char c : s) + { + if (c >= 'a' && c <= 'z') + { + count[c - 'a']++; + } + else + { + count[c - 'A']--; + } + } + + for (int i = 0; i < 26; ++i) + { + int caseCount = count[i]; + + if (caseCount > 0) + goodnessScore--; + else if (caseCount < 0) + { + goodnessScore++; + } + else + { + continue; + } + } + + return goodnessScore; +} diff --git a/naukri/beginner-contest-90/tk/A Parity Parts.cpp b/naukri/beginner-contest-90/tk/A Parity Parts.cpp index 1765345f..94e93f67 100644 --- a/naukri/beginner-contest-90/tk/A Parity Parts.cpp +++ b/naukri/beginner-contest-90/tk/A Parity Parts.cpp @@ -1,49 +1,49 @@ -#include - -using namespace std; - -string greaterParitySubpart(string &s) -{ - int n = s.size(); - - // Extract even-subpart (characters at even indices) - string even_subpart = ""; - for (int i = 0; i < n; i += 2) - { - even_subpart += s[i]; - } - - // Extract odd-subpart (characters at odd indices) - string odd_subpart = ""; - for (int i = 1; i < n; i += 2) - { - odd_subpart += s[i]; - } - - // Compare lexicographically and return the greater one - if (even_subpart > odd_subpart) - { - return even_subpart; - } - else - { - return odd_subpart; - } -} - -int main() -{ - int T; - cin >> T; - - while (T--) - { - int N; - string S; - cin >> N >> S; - - cout << greaterParitySubpart(S) << endl; - } - - return 0; -} \ No newline at end of file +#include + +using namespace std; + +string greaterParitySubpart(string &s) +{ + int n = s.size(); + + // Extract even-subpart (characters at even indices) + string even_subpart = ""; + for (int i = 0; i < n; i += 2) + { + even_subpart += s[i]; + } + + // Extract odd-subpart (characters at odd indices) + string odd_subpart = ""; + for (int i = 1; i < n; i += 2) + { + odd_subpart += s[i]; + } + + // Compare lexicographically and return the greater one + if (even_subpart > odd_subpart) + { + return even_subpart; + } + else + { + return odd_subpart; + } +} + +int main() +{ + int T; + cin >> T; + + while (T--) + { + int N; + string S; + cin >> N >> S; + + cout << greaterParitySubpart(S) << endl; + } + + return 0; +} diff --git a/naukri/beginner-contest-90/tk/B Maximum Value of F(x) - VI.cpp b/naukri/beginner-contest-90/tk/B Maximum Value of F(x) - VI.cpp index c72db444..706ac3d7 100644 --- a/naukri/beginner-contest-90/tk/B Maximum Value of F(x) - VI.cpp +++ b/naukri/beginner-contest-90/tk/B Maximum Value of F(x) - VI.cpp @@ -1,102 +1,103 @@ -#include - -// 查找第一个等于target的位置 -int findFirst(vector &arr, int target) -{ - int left = 0, right = arr.size() - 1; - int result = -1; - - while (left <= right) - { - int mid = left + (right - left) / 2; - - if (arr[mid] == target) - { - result = mid; - right = mid - 1; // 继续在左半部分查找 - } - else if (arr[mid] < target) - { - left = mid + 1; - } - else - { - right = mid - 1; - } - } - - return result; -} -// 查找最后一个等于target的位置 -int findLast(vector &arr, int target) -{ - int left = 0, right = arr.size() - 1; - int result = -1; - - while (left <= right) - { - int mid = left + (right - left) / 2; - - if (arr[mid] == target) - { - result = mid; - left = mid + 1; // 继续在右半部分查找 - } - else if (arr[mid] < target) - { - left = mid + 1; - } - else - { - right = mid - 1; - } - } - - return result; -} - -// 计算F(X)值 -int calculateFX(vector &arr, int X) -{ - int n = arr.size(); - - // 找到X的第一个和最后一个位置 - int first = findFirst(arr, X); - int last = findLast(arr, X); - - if (first == -1) - return 0; // X不在数组中 - - // S = 严格小于X的元素个数 = first - // G = 严格大于X的元素个数 = n - last - 1 - int S = first; - int G = n - last - 1; - - return S + G; -} - -int maxFXVI(int n, vector &arr) -{ - int maxF = 0; - - // 遍历数组中的每个不同元素 - for (int i = 0; i < n; i++) - { - // 跳过重复元素 - if (i > 0 && arr[i] == arr[i - 1]) - continue; - - int X = arr[i]; - int F = calculateFX(arr, X); - maxF = max(maxF, F); - } - return maxF; -} - - - - - - - - +#include +#include + +// 查找第一个等于target的位置 +int findFirst(vector &arr, int target) +{ + int left = 0, right = arr.size() - 1; + int result = -1; + + while (left <= right) + { + int mid = left + (right - left) / 2; + + if (arr[mid] == target) + { + result = mid; + right = mid - 1; // 继续在左半部分查找 + } + else if (arr[mid] < target) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return result; +} +// 查找最后一个等于target的位置 +int findLast(vector &arr, int target) +{ + int left = 0, right = arr.size() - 1; + int result = -1; + + while (left <= right) + { + int mid = left + (right - left) / 2; + + if (arr[mid] == target) + { + result = mid; + left = mid + 1; // 继续在右半部分查找 + } + else if (arr[mid] < target) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return result; +} + +// 计算F(X)值 +int calculateFX(vector &arr, int X) +{ + int n = arr.size(); + + // 找到X的第一个和最后一个位置 + int first = findFirst(arr, X); + int last = findLast(arr, X); + + if (first == -1) + return 0; // X不在数组中 + + // S = 严格小于X的元素个数 = first + // G = 严格大于X的元素个数 = n - last - 1 + int S = first; + int G = n - last - 1; + + return S + G; +} + +int maxFXVI(int n, vector &arr) +{ + int maxF = 0; + + // 遍历数组中的每个不同元素 + for (int i = 0; i < n; i++) + { + // 跳过重复元素 + if (i > 0 && arr[i] == arr[i - 1]) + continue; + + int X = arr[i]; + int F = calculateFX(arr, X); + maxF = max(maxF, F); + } + return maxF; +} + + + + + + + + diff --git a/naukri/beginner-contest-90/tk/C Longest Sub-array with bitwise AND!.cpp b/naukri/beginner-contest-90/tk/C Longest Sub-array with bitwise AND!.cpp index 27e86e4a..3f169ebf 100644 --- a/naukri/beginner-contest-90/tk/C Longest Sub-array with bitwise AND!.cpp +++ b/naukri/beginner-contest-90/tk/C Longest Sub-array with bitwise AND!.cpp @@ -1,35 +1,36 @@ -#include -vector bitwiseAnd(int n, int q, vector arr, vector queries) -{ - // Write your code here. - vector results; - - for (int X : queries) - { - int maxInd = 0; - int currentAnd = arr[0]; // 注意:arr[0]对应ARR[1] - - // 从索引1开始(对应ARR[1]) - for (int i = 0; i < arr.size(); i++) - { - if (i == 0) - { - currentAnd = arr[i]; - } - else - { - currentAnd &= arr[i]; - } - - // 如果当前前缀按位与 >= X,更新最大索引 - if (currentAnd >= X) - { - maxInd = i + 1; // 转换为1-based索引 - } - } - - results.push_back(maxInd); - } - - return results; -} \ No newline at end of file +#include +#include +vector bitwiseAnd(int n, int q, vector arr, vector queries) +{ + // Write your code here. + vector results; + + for (int X : queries) + { + int maxInd = 0; + int currentAnd = arr[0]; // 注意:arr[0]对应ARR[1] + + // 从索引1开始(对应ARR[1]) + for (int i = 0; i < arr.size(); i++) + { + if (i == 0) + { + currentAnd = arr[i]; + } + else + { + currentAnd &= arr[i]; + } + + // 如果当前前缀按位与 >= X,更新最大索引 + if (currentAnd >= X) + { + maxInd = i + 1; // 转换为1-based索引 + } + } + + results.push_back(maxInd); + } + + return results; +} diff --git a/naukri/beginner-contest-90/tk/D Add Edges.cpp b/naukri/beginner-contest-90/tk/D Add Edges.cpp index 8ded42b8..484969eb 100644 --- a/naukri/beginner-contest-90/tk/D Add Edges.cpp +++ b/naukri/beginner-contest-90/tk/D Add Edges.cpp @@ -1,50 +1,51 @@ -#include -using namespace std; -long long maxEdges(int n, vector> edges) -{ - // Write your code here. - - // 采用临接表的方法构建图 - vector> graph(n + 1); - for(const auto &edge:edges){ - int u = edge.first, v = edge.second; - graph[u].push_back(v); - graph[v].push_back(u); - } - - // 采用dfs对树的节点染色,将节点分成两组 - vector color(n + 1, -1); - long long black = 0, white = 0; - - // 从节点1开始染色 - queue q; - q.push(1); - color[1] = 0; //0黑,1白 - ++black; - - while(!q.empty()){ - int node = q.front(); - q.pop(); - - for (int neighbor:graph[node]){ - if(color[neighbor] == -1){ - color[neighbor] = 1 - color[node]; - if(color[neighbor] == 0){ - ++black; - }else{ - ++white; - } - q.push(neighbor); - } - } - } - - // 计算最大可能边数 - long long maxPossibleEdges = black * white; - - // 减去已存在的边数 - - long long existingEdges = edges.size(); - - return maxPossibleEdges - existingEdges; -} \ No newline at end of file +#include +#include +using namespace std; +long long maxEdges(int n, vector> edges) +{ + // Write your code here. + + // 采用临接表的方法构建图 + vector> graph(n + 1); + for(const auto &edge:edges){ + int u = edge.first, v = edge.second; + graph[u].push_back(v); + graph[v].push_back(u); + } + + // 采用dfs对树的节点染色,将节点分成两组 + vector color(n + 1, -1); + long long black = 0, white = 0; + + // 从节点1开始染色 + queue q; + q.push(1); + color[1] = 0; //0黑,1白 + ++black; + + while(!q.empty()){ + int node = q.front(); + q.pop(); + + for (int neighbor:graph[node]){ + if(color[neighbor] == -1){ + color[neighbor] = 1 - color[node]; + if(color[neighbor] == 0){ + ++black; + }else{ + ++white; + } + q.push(neighbor); + } + } + } + + // 计算最大可能边数 + long long maxPossibleEdges = black * white; + + // 减去已存在的边数 + + long long existingEdges = edges.size(); + + return maxPossibleEdges - existingEdges; +} diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A.cpp" index 00525120..73c4c089 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' @@ -58,6 +59,6 @@ int main() { solve(); } - + return 0; } diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A1.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A1.cpp" index 82bfc3b3..e8843b0f 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A1.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/A1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-03 17:13:52 diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/B.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/B.cpp" index e89a4ba3..8eb10abc 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/B.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/B.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/C.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/C.cpp" index 4d9beaf3..2d8decb8 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/C.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/C.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' @@ -35,13 +36,13 @@ il void solve(){ int main() { fastio; - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/E.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/E.cpp" index 2822fbc2..93b3671d 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/E.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/E.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' @@ -36,13 +37,13 @@ il void solve(){ int main() { fastio; - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/H.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/H.cpp" index 4b6a9285..c02cdc49 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/H.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/H.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-03 17:39:22 diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/K.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/K.cpp" index 48f3cefd..b9246288 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/K.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest1/K.cpp" @@ -21,7 +21,7 @@ const ll N = 5e5 + 5, mod = 1e9 + 7, inf = 2e18; const double eps = 1e-9; const double PI = 3.1415926; -/* +/* 因为每个数都是不相同的,并且数组中所有元素的乘积与和相等,这种情况下只有 case 1: n = 1 case 2: n = 3 diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/B.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/B.cpp" index 4224b85e..c0ce05f4 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/B.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/B.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' @@ -44,13 +45,13 @@ il void solve(){ int main() { fastio; - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/E.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/E.cpp" index 465c070f..f1e6c0f1 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/E.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/E.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-05 16:40:33 diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/H.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/H.cpp" index d9c54d76..46c157db 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/H.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/H.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-05 15:24:28 diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J.cpp" index a5a2a067..6d4cddb7 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' @@ -58,20 +59,20 @@ il void solve(){ for(auto v:bucket[t]) sources.pb(v); - + } } int main() { fastio; - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J1.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J1.cpp" index 1b480ba1..790c0134 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J1.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest2/J1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-05 16:06:12 diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/B.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/B.cpp" index 1b79d21f..12e9024b 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/B.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/B.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/G.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/G.cpp" index d903cfaa..dbd2ba2e 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/G.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest3/G.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-07 16:04:30 diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/A.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/A.cpp" index 8245bea0..63d173c8 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/A.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/A.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/B.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/B.cpp" index ef6625c6..912ae26b 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/B.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/B.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/F.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/F.cpp" index 75e8f7e1..2f6273d2 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/F.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/F.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' diff --git "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/H.cpp" "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/H.cpp" index 7747f7d2..4ede998e 100644 --- "a/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/H.cpp" +++ "b/niuke/contest/2026\347\211\233\345\256\242\345\257\222\345\201\207\347\256\227\346\263\225\345\237\272\347\241\200\351\233\206\350\256\255\350\220\245/contest4/H.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-09 14:49:17 diff --git "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/12.py" "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/12.py" index 451fedfb..4fac43e9 100644 --- "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/12.py" +++ "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/12.py" @@ -44,4 +44,4 @@ def solve(): if __name__ == "__main__": - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/2.py" "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/2.py" index 9980d07a..a0fbbbe9 100644 --- "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/2.py" +++ "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/2.py" @@ -11,4 +11,4 @@ def solve(): j -= 1 if __name__ == "__main__": - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/3.py" "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/3.py" index ac3c232b..f9ad0a88 100644 --- "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/3.py" +++ "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/3.py" @@ -22,7 +22,7 @@ def solve(): if cnt == -1: res += 1 cnt = 1 - + if(cnt > 0): res += cnt // 2 @@ -31,4 +31,4 @@ def solve(): if __name__ == "__main__": - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/4.py" "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/4.py" index a19e1a08..6590d848 100644 --- "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/4.py" +++ "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/4.py" @@ -29,7 +29,7 @@ def solve(): ans = 0 print(ans) - + if __name__ == "__main__": - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/5.py" "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/5.py" index 696b1caa..49333cd2 100644 --- "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/5.py" +++ "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/5.py" @@ -13,10 +13,10 @@ def solve(): op1 = dp[i-1] #skip op2 = dp[i-3] + a[i-2] #take dp[i] = max(op1,op2) - + print(dp[n]) - + if __name__ == "__main__": - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/6.py" "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/6.py" index 2999d450..5669240f 100644 --- "a/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/6.py" +++ "b/niuke/contest/Cloudflare\347\272\252\345\277\265\350\265\233/6.py" @@ -4,7 +4,7 @@ input = lambda: sys.stdin.readline().rstrip() def solve(): - + n = int(input()) s = str(input()) @@ -13,7 +13,7 @@ def solve(): direction[c] = direction.get(c,0) + 1 ans = 0 - + cntR = direction.get("R",0) cntL = direction.get("L",0) cntU = direction.get("U",0) @@ -31,4 +31,4 @@ def solve(): t = 1 t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/1.cpp" index 7beda59a..e64dcc44 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/1.cpp" @@ -1,25 +1,25 @@ -#include -#include -using namespace std; - -int main() { - int a[4], sum = 0; - - for (int i = 0; i < 4; i++) { - cin >> a[i]; - } - - long long result = 1; - for (int i = 0; i < a[1]; i++) { - result *= a[0]; - } - result = result * a[2] + a[3]; - - if (result % 2 == 0) { - cout << "NO"; - } else { - cout << "YES"; - } - - return 0; -} \ No newline at end of file +#include +#include +using namespace std; + +int main() { + int a[4], sum = 0; + + for (int i = 0; i < 4; i++) { + cin >> a[i]; + } + + long long result = 1; + for (int i = 0; i < a[1]; i++) { + result *= a[0]; + } + result = result * a[2] + a[3]; + + if (result % 2 == 0) { + cout << "NO"; + } else { + cout << "YES"; + } + + return 0; +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.cpp" index 418e7969..e683ad36 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.cpp" @@ -1,49 +1,49 @@ -#include -/* -题目要求: -1. 构造n*n的矩阵 -2. 每行每个数最多出现一次 -3. 任意两不同行只有一个交集 -4. 矩阵中每个元素出现次数小于等于2 -5. 集合中元素为1到1E6之间 - -输出格式(n=3): -1 2 3 -3 4 5 -1 5 6 -*/ - -int main() -{ - int n; - scanf("%d", &n); - - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - if (i == 0) - { - // 第一行:1,2,3 - printf("%d ", j + 1); - } - else if (i == 1) - { - // 第二行:3,4,5 - printf("%d ", j + 3); - } - else - { - // 第三行:1,5,6 - if (j == 0) - printf("1 "); - else if (j == 1) - printf("5 "); - else - printf("6 "); - } - } - printf("\n"); - } - return 0; -} \ No newline at end of file +#include +/* +题目要求: +1. 构造n*n的矩阵 +2. 每行每个数最多出现一次 +3. 任意两不同行只有一个交集 +4. 矩阵中每个元素出现次数小于等于2 +5. 集合中元素为1到1E6之间 + +输出格式(n=3): +1 2 3 +3 4 5 +1 5 6 +*/ + +int main() +{ + int n; + scanf("%d", &n); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == 0) + { + // 第一行:1,2,3 + printf("%d ", j + 1); + } + else if (i == 1) + { + // 第二行:3,4,5 + printf("%d ", j + 3); + } + else + { + // 第三行:1,5,6 + if (j == 0) + printf("1 "); + else if (j == 1) + printf("5 "); + else + printf("6 "); + } + } + printf("\n"); + } + return 0; +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.py" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.py" index de959d10..5fdeaded 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.py" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/2.py" @@ -1,23 +1,23 @@ -def solve(n): - # For n=3, we want to output: - # 1 2 3 - # 3 4 5 - # 1 5 6 - if n == 3: - return [ - [1, 2, 3], - [3, 4, 5], - [1, 5, 6] - ] - - # For other n, return None since we don't have a solution - return None - -# Get input -n = int(input()) -result = solve(n) -if result: - for row in result: - print(*row) -else: - print("No solution found") +def solve(n): + # For n=3, we want to output: + # 1 2 3 + # 3 4 5 + # 1 5 6 + if n == 3: + return [ + [1, 2, 3], + [3, 4, 5], + [1, 5, 6] + ] + + # For other n, return None since we don't have a solution + return None + +# Get input +n = int(input()) +result = solve(n) +if result: + for row in result: + print(*row) +else: + print("No solution found") diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/3.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/3.cpp" index b0d6ef21..008f823b 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/3.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/3.cpp" @@ -1,61 +1,61 @@ -#include -using namespace std; - -const int MAXN = 505; -int n; -int a[MAXN][MAXN]; - -bool check() -{ - // 检查每个位置是否可以通过操作变成0 - for (int i = 1; i <= n; i++) - { - for (int j = 1; j <= n; j++) - { - // 如果是正数,必须能减到0 - if (a[i][j] > 0) - { - // 检查是否存在一个相邻位置的数大于等于当前位置 - if (a[i - 1][j] >= a[i][j] || a[i + 1][j] >= a[i][j] || - a[i][j - 1] >= a[i][j] || a[i][j + 1] >= a[i][j]) - { - return false; - } - } - // 如果是负数,必须能加到0 - else if (a[i][j] < 0) - { - // 检查是否存在一个相邻位置的数小于等于当前位置 - if (a[i - 1][j] <= a[i][j] || a[i + 1][j] <= a[i][j] || - a[i][j - 1] <= a[i][j] || a[i][j + 1] <= a[i][j]) - { - return false; - } - } - } - } - return true; -} - -int main() -{ - cin >> n; - - // 初始化边界为0 - for (int i = 0; i <= n + 1; i++) - { - a[0][i] = a[n + 1][i] = a[i][0] = a[i][n + 1] = 0; - } - - // 读入数据 - for (int i = 1; i <= n; i++) - { - for (int j = 1; j <= n; j++) - { - cin >> a[i][j]; - } - } - - cout << (check() ? "YES" : "NO") << endl; - return 0; -} \ No newline at end of file +#include +using namespace std; + +const int MAXN = 505; +int n; +int a[MAXN][MAXN]; + +bool check() +{ + // 检查每个位置是否可以通过操作变成0 + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + // 如果是正数,必须能减到0 + if (a[i][j] > 0) + { + // 检查是否存在一个相邻位置的数大于等于当前位置 + if (a[i - 1][j] >= a[i][j] || a[i + 1][j] >= a[i][j] || + a[i][j - 1] >= a[i][j] || a[i][j + 1] >= a[i][j]) + { + return false; + } + } + // 如果是负数,必须能加到0 + else if (a[i][j] < 0) + { + // 检查是否存在一个相邻位置的数小于等于当前位置 + if (a[i - 1][j] <= a[i][j] || a[i + 1][j] <= a[i][j] || + a[i][j - 1] <= a[i][j] || a[i][j + 1] <= a[i][j]) + { + return false; + } + } + } + } + return true; +} + +int main() +{ + cin >> n; + + // 初始化边界为0 + for (int i = 0; i <= n + 1; i++) + { + a[0][i] = a[n + 1][i] = a[i][0] = a[i][n + 1] = 0; + } + + // 读入数据 + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + cin >> a[i][j]; + } + } + + cout << (check() ? "YES" : "NO") << endl; + return 0; +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/4.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/4.cpp" index 9e8868d9..5c7d0d7c 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/4.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/4.cpp" @@ -1,82 +1,82 @@ -#include -#include -#include -#include -using namespace std; - -const int MAXN = 2e5 + 5; -const int MAXK = 205; -const int INF = 0x3f3f3f3f; - -struct State { - int pos, k, dist; - State(int p, int kk, int d) : pos(p), k(kk), dist(d) {} -}; - -int n, K, a, b, x, y; -int vis[MAXN][MAXK]; - -int bfs() { - memset(vis, 0, sizeof(vis)); - queue q; - - // 检查输入范围 - if(a < 1 || a > n || b < 1 || b > n || K < 0 || K >= MAXK) { - return -1; - } - - q.push(State(a, K, 0)); - vis[a][K] = 1; - - while(!q.empty()) { - State cur = q.front(); - q.pop(); - - int pos = cur.pos; - int k = cur.k; - int dist = cur.dist; - - if(pos == b) return dist; - - // 顺时针移动x - int next_pos = (pos + x - 1 + n) % n + 1; - if(next_pos >= 1 && next_pos <= n && k >= 0 && k < MAXK && !vis[next_pos][k]) { - vis[next_pos][k] = 1; - q.push(State(next_pos, k, dist + 1)); - } - - // 逆时针移动y - next_pos = ((pos - y - 1 + n) % n) + 1; - if(next_pos >= 1 && next_pos <= n && k >= 0 && k < MAXK && !vis[next_pos][k]) { - vis[next_pos][k] = 1; - q.push(State(next_pos, k, dist + 1)); - } - - // 使用技能 - if(k > 0) { - next_pos = (pos + n/2 - 1 + n) % n + 1; - if(next_pos >= 1 && next_pos <= n && (k-1) >= 0 && (k-1) < MAXK && !vis[next_pos][k-1]) { - vis[next_pos][k-1] = 1; - q.push(State(next_pos, k-1, dist + 1)); - } - } - } - - return -1; -} - -int main() { - ios::sync_with_stdio(false); - cin.tie(nullptr); - - cin >> n >> K >> a >> b >> x >> y; - - // 检查输入范围 - if(n <= 0 || n >= MAXN) { - cout << -1 << endl; - return 0; - } - - cout << bfs() << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +#include +using namespace std; + +const int MAXN = 2e5 + 5; +const int MAXK = 205; +const int INF = 0x3f3f3f3f; + +struct State { + int pos, k, dist; + State(int p, int kk, int d) : pos(p), k(kk), dist(d) {} +}; + +int n, K, a, b, x, y; +int vis[MAXN][MAXK]; + +int bfs() { + memset(vis, 0, sizeof(vis)); + queue q; + + // 检查输入范围 + if(a < 1 || a > n || b < 1 || b > n || K < 0 || K >= MAXK) { + return -1; + } + + q.push(State(a, K, 0)); + vis[a][K] = 1; + + while(!q.empty()) { + State cur = q.front(); + q.pop(); + + int pos = cur.pos; + int k = cur.k; + int dist = cur.dist; + + if(pos == b) return dist; + + // 顺时针移动x + int next_pos = (pos + x - 1 + n) % n + 1; + if(next_pos >= 1 && next_pos <= n && k >= 0 && k < MAXK && !vis[next_pos][k]) { + vis[next_pos][k] = 1; + q.push(State(next_pos, k, dist + 1)); + } + + // 逆时针移动y + next_pos = ((pos - y - 1 + n) % n) + 1; + if(next_pos >= 1 && next_pos <= n && k >= 0 && k < MAXK && !vis[next_pos][k]) { + vis[next_pos][k] = 1; + q.push(State(next_pos, k, dist + 1)); + } + + // 使用技能 + if(k > 0) { + next_pos = (pos + n/2 - 1 + n) % n + 1; + if(next_pos >= 1 && next_pos <= n && (k-1) >= 0 && (k-1) < MAXK && !vis[next_pos][k-1]) { + vis[next_pos][k-1] = 1; + q.push(State(next_pos, k-1, dist + 1)); + } + } + } + + return -1; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> K >> a >> b >> x >> y; + + // 检查输入范围 + if(n <= 0 || n >= MAXN) { + cout << -1 << endl; + return 0; + } + + cout << bfs() << endl; + return 0; +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/1.cpp" index 955de861..25244ed0 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/1.cpp" @@ -1,20 +1,20 @@ -#include -int max(int x, int y) -{ - int z; - if (x > y) - z = x; - else - z = y; - return x,y; -} - -int main() -{ - int max(int x, int y); - int a, b, c; - printf("please enter two numbers:"); - scanf("%d,%d", &a, &b); - c = max(a, b); - printf("max is c=%d\n", c); -} +#include +int max(int x, int y) +{ + int z; + if (x > y) + z = x; + else + z = y; + return x,y; +} + +int main() +{ + int max(int x, int y); + int a, b, c; + printf("please enter two numbers:"); + scanf("%d,%d", &a, &b); + c = max(a, b); + printf("max is c=%d\n", c); +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/2.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/2.cpp" index 93391034..b1aa499a 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/2.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/2.cpp" @@ -1,21 +1,21 @@ -#include -// void func(int x[5]) -// { -// int i; -// for (i = 0; i < 5; i++) -// x[i] = i + 6; -// } -void func(int x[]) -{ - int i; - for (i = 0; i < 5; i++) - x[i] = i + 6; -} - -int main() -{ - int i, a[5] = {1, 2, 3, 4, 5}; - func(a); - for (i = 0; i < 5; i++) - printf("%d ", a[i]); -} +#include +// void func(int x[5]) +// { +// int i; +// for (i = 0; i < 5; i++) +// x[i] = i + 6; +// } +void func(int x[]) +{ + int i; + for (i = 0; i < 5; i++) + x[i] = i + 6; +} + +int main() +{ + int i, a[5] = {1, 2, 3, 4, 5}; + func(a); + for (i = 0; i < 5; i++) + printf("%d ", a[i]); +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/4.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/4.cpp" index 7b4f1137..69c1b104 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/4.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/4.cpp" @@ -1,19 +1,19 @@ -#include -void swap(int *p1, int *p2) -{ - int *t; - *t = *p1; - *p1 = *p2; - *p2 = *t; -} - -int main() -{ - int a, b, *p1, *p2; - scanf("%d,%d", &a, &b); - p1 = &a; - p2 = &b; - swap(p1, p2); - printf("a=%d,b=%d\n", a, b); - printf("*p1=%d,*p2=%d", *p1, *p2); -} +#include +void swap(int *p1, int *p2) +{ + int *t; + *t = *p1; + *p1 = *p2; + *p2 = *t; +} + +int main() +{ + int a, b, *p1, *p2; + scanf("%d,%d", &a, &b); + p1 = &a; + p2 = &b; + swap(p1, p2); + printf("a=%d,b=%d\n", a, b); + printf("*p1=%d,*p2=%d", *p1, *p2); +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/6.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/6.cpp" index 1477614d..10b222cc 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/6.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/class/6.cpp" @@ -1,2 +1,2 @@ -#include - +#include + diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/1.cpp" index 9ec55cbe..81c0275a 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/1.cpp" @@ -1,21 +1,21 @@ -#include -using namespace std; -int main(){ - int n,a,b,c; - int cnt = 0; - cin>>n>>a>>b>>c; - while(n>=a){ - cnt++; - n -= a; - } - while(n>=b){ - cnt++; - n -= b; - } - while(n>=c){ - cnt++; - n -= c; - } - cout< +using namespace std; +int main(){ + int n,a,b,c; + int cnt = 0; + cin>>n>>a>>b>>c; + while(n>=a){ + cnt++; + n -= a; + } + while(n>=b){ + cnt++; + n -= b; + } + while(n>=c){ + cnt++; + n -= c; + } + cout< -#include - -using namespace std; - -int main() -{ - int n; - cin >> n; - vector a(n); - for (int i = 0; i < n; i++) - { - cin >> a[i]; - } - - // 只需要计算一次最大最小值 - int max_val = *max_element(a.begin(), a.end()); - int min_val = *min_element(a.begin(), a.end()); - - // 直接计算最终平均值和操作次数 - int average = (max_val + min_val + 1) / 2; - long long cnt = 0; // 使用long long防止溢出 - - // 一次性计算所有需要的操作次数 - for (int i = 0; i < n; i++) - { - if (a[i] < average) - { - cnt = 2 * (average - a[i]) - 1; - } - else if (a[i] > average) - { - cnt = 2 * (a[i] - average); - } - } - - cout << cnt << " " << average << endl; - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + vector a(n); + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + + // 只需要计算一次最大最小值 + int max_val = *max_element(a.begin(), a.end()); + int min_val = *min_element(a.begin(), a.end()); + + // 直接计算最终平均值和操作次数 + int average = (max_val + min_val + 1) / 2; + long long cnt = 0; // 使用long long防止溢出 + + // 一次性计算所有需要的操作次数 + for (int i = 0; i < n; i++) + { + if (a[i] < average) + { + cnt = 2 * (average - a[i]) - 1; + } + else if (a[i] > average) + { + cnt = 2 * (a[i] - average); + } + } + + cout << cnt << " " << average << endl; + return 0; +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/2.py" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/2.py" index 3bed7a50..350f7437 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/2.py" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/2.py" @@ -1,25 +1,25 @@ -import sys - -def main(): - import sys - import math - - input = sys.stdin.read - data = input().split() - n = int(data[0]) - a = list(map(int, data[1:n+1])) - min_a = min(a) - max_a = max(a) - # Compute x as ceil((min_a + max_a)/2) - x = (min_a + max_a +1) //2 - A = x - min_a - B = max_a -x - if A <0: - A=0 - if B <0: - B=0 - total_operations = max(2*A -1, 2*B) if (A >0 or B >0) else 0 - print(total_operations, x) - -if __name__ == "__main__": - main() +import sys + +def main(): + import sys + import math + + input = sys.stdin.read + data = input().split() + n = int(data[0]) + a = list(map(int, data[1:n+1])) + min_a = min(a) + max_a = max(a) + # Compute x as ceil((min_a + max_a)/2) + x = (min_a + max_a +1) //2 + A = x - min_a + B = max_a -x + if A <0: + A=0 + if B <0: + B=0 + total_operations = max(2*A -1, 2*B) if (A >0 or B >0) else 0 + print(total_operations, x) + +if __name__ == "__main__": + main() diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/3.py" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/3.py" index 3afc9fc2..991bc607 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/3.py" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/3.py" @@ -1,76 +1,76 @@ -import sys - -def main(): - sys.setrecursionlimit(1 << 25) - - def input(): - return sys.stdin.read() - - data = input().strip().split() - n = int(data[0]) - m = int(data[1]) - k = int(data[2]) - seed = int(data[3]) - - class RNG: - def __init__(self, seed): - self.seed = seed - - def rnd(self): - ret = self.seed - self.seed = (self.seed ^ ((self.seed << 13) & 0xFFFFFFFF)) & 0xFFFFFFFF - self.seed = (self.seed ^ ((self.seed >> 17) & 0xFFFFFFFF)) & 0xFFFFFFFF - self.seed = (self.seed ^ ((self.seed << 5) & 0xFFFFFFFF)) & 0xFFFFFFFF - return ret - - rng = RNG(seed) - - # 使用字典存储操作,减少空间复杂度 - row_ops = {} # {row_index: (time, value)} - col_ops = {} # {col_index: (time, value)} - - # 记录最后一次操作的时间 - last_time = 0 - - # 记录所有操作 - for t in range(1, k + 1): - r = rng.rnd() - op = (r % 2) + 1 - if op == 1: - r_i = (rng.rnd() % n) + 1 - x = (rng.rnd() % (n * m)) + 1 - row_ops[r_i] = (t, x) - else: - c_j = (rng.rnd() % m) + 1 - b = (rng.rnd() % (n * m)) + 1 - col_ops[c_j] = (t, b) - last_time = t - - total = 0 - - # 预处理列操作的总和 - col_sum = 0 - latest_cols = 0 # 记录有多少列有操作 - for c in col_ops.values(): - col_sum += c[1] - latest_cols += 1 - - # 对每一行计算贡献 - for r in range(1, n + 1): - if r not in row_ops: - # 如果这一行没有行操作,直接用列和计算 - total += r * col_sum - else: - row_time, row_val = row_ops[r] - # 计算被列操作覆盖的部分 - for c, (col_time, col_val) in col_ops.items(): - if col_time > row_time: - total += r * col_val - # 计算未被覆盖的部分 - remaining_cells = m - sum(1 for _, (t, _) in col_ops.items() if t > row_time) - total += r * row_val * remaining_cells - - print(total) - -if __name__ == "__main__": - main() \ No newline at end of file +import sys + +def main(): + sys.setrecursionlimit(1 << 25) + + def input(): + return sys.stdin.read() + + data = input().strip().split() + n = int(data[0]) + m = int(data[1]) + k = int(data[2]) + seed = int(data[3]) + + class RNG: + def __init__(self, seed): + self.seed = seed + + def rnd(self): + ret = self.seed + self.seed = (self.seed ^ ((self.seed << 13) & 0xFFFFFFFF)) & 0xFFFFFFFF + self.seed = (self.seed ^ ((self.seed >> 17) & 0xFFFFFFFF)) & 0xFFFFFFFF + self.seed = (self.seed ^ ((self.seed << 5) & 0xFFFFFFFF)) & 0xFFFFFFFF + return ret + + rng = RNG(seed) + + # 使用字典存储操作,减少空间复杂度 + row_ops = {} # {row_index: (time, value)} + col_ops = {} # {col_index: (time, value)} + + # 记录最后一次操作的时间 + last_time = 0 + + # 记录所有操作 + for t in range(1, k + 1): + r = rng.rnd() + op = (r % 2) + 1 + if op == 1: + r_i = (rng.rnd() % n) + 1 + x = (rng.rnd() % (n * m)) + 1 + row_ops[r_i] = (t, x) + else: + c_j = (rng.rnd() % m) + 1 + b = (rng.rnd() % (n * m)) + 1 + col_ops[c_j] = (t, b) + last_time = t + + total = 0 + + # 预处理列操作的总和 + col_sum = 0 + latest_cols = 0 # 记录有多少列有操作 + for c in col_ops.values(): + col_sum += c[1] + latest_cols += 1 + + # 对每一行计算贡献 + for r in range(1, n + 1): + if r not in row_ops: + # 如果这一行没有行操作,直接用列和计算 + total += r * col_sum + else: + row_time, row_val = row_ops[r] + # 计算被列操作覆盖的部分 + for c, (col_time, col_val) in col_ops.items(): + if col_time > row_time: + total += r * col_val + # 计算未被覆盖的部分 + remaining_cells = m - sum(1 for _, (t, _) in col_ops.items() if t > row_time) + total += r * row_val * remaining_cells + + print(total) + +if __name__ == "__main__": + main() diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.cpp" index afca6eb4..f6cbed5b 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.cpp" @@ -1,2 +1,2 @@ - - cnt += max(2 * A - 1, 2 * B) \ No newline at end of file + + cnt += max(2 * A - 1, 2 * B) diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.py" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.py" index d3f5a12f..8b137891 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.py" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233106/xiaobai2/tempCodeRunnerFile.py" @@ -1 +1 @@ - + diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/A.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/A.cpp" index 4f6d38fd..eae81be9 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/A.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/A.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; @@ -12,7 +13,7 @@ int main() { ios::sync_with_stdio(false); cin.tie(nullptr); - + int t,x; cin >> t; while(t--) @@ -37,6 +38,6 @@ int main() } } - + return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/B.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/B.cpp" index 1fbdc455..bb7f03e4 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/B.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/B.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; @@ -33,4 +34,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/C.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/C.cpp" index f4e69486..df436908 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/C.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/C.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; @@ -7,7 +8,7 @@ int main() { ios::sync_with_stdio(false); cin.tie(nullptr); - + int t = 0; cin >> t; while(t--) @@ -94,7 +95,7 @@ int main() cout << ans << endl; } - + return 0; } diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/b1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/b1.cpp" index e260278f..025c0a54 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/b1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/b1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -28,13 +29,13 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + int t = 0; cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/c1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/c1.cpp" index cd793cb1..e2fe2732 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/c1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/c1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; using ll = long long; @@ -106,4 +107,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/d1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/d1.cpp" index 13f04ab2..81fecdf3 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/d1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/d1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/e1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/e1.cpp" index 3b84b1ad..d11ee7fc 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/e1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/e1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; using ll = long long; diff --git "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/f1.cpp" "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/f1.cpp" index 6a91db79..54a738a6 100644 --- "a/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/f1.cpp" +++ "b/niuke/contest/\345\260\217\347\231\275\346\234\210\350\265\233123/f1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/niuke/contest/\347\211\233\345\256\2422025\345\271\2641024\347\250\213\345\272\217\345\221\230\350\212\202\345\250\261\344\271\220\350\265\233/A\347\211\233\345\217\257\344\271\220\346\211\223\346\200\252/1.cpp" "b/niuke/contest/\347\211\233\345\256\2422025\345\271\2641024\347\250\213\345\272\217\345\221\230\350\212\202\345\250\261\344\271\220\350\265\233/A\347\211\233\345\217\257\344\271\220\346\211\223\346\200\252/1.cpp" index 2b41c287..d47f96c5 100644 --- "a/niuke/contest/\347\211\233\345\256\2422025\345\271\2641024\347\250\213\345\272\217\345\221\230\350\212\202\345\250\261\344\271\220\350\265\233/A\347\211\233\345\217\257\344\271\220\346\211\223\346\200\252/1.cpp" +++ "b/niuke/contest/\347\211\233\345\256\2422025\345\271\2641024\347\250\213\345\272\217\345\221\230\350\212\202\345\250\261\344\271\220\350\265\233/A\347\211\233\345\217\257\344\271\220\346\211\223\346\200\252/1.cpp" @@ -1,29 +1,29 @@ -#include -using namespace std; -typedef long long ll; - -int main() { - ios::sync_with_stdio(false); - cin.tie(nullptr); - - ll a, n; - cin >> a >> n; - - - int k_min = (n + a - 1) / a; - - ll dmg1 = (n + k_min - 1) / k_min; - ll total1 = k_min * dmg1; - - ll dmg2 = (n + k_min) / (k_min + 1); - ll total2 = (k_min + 1) * dmg2; - - ll best_dmg; - if (total1 < total2)best_dmg = dmg1; - else if (total1 > total2)best_dmg = dmg2; - else { - best_dmg = max(dmg1, dmg2); - } - cout << a - best_dmg; - return 0; -} +#include +using namespace std; +typedef long long ll; + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll a, n; + cin >> a >> n; + + + int k_min = (n + a - 1) / a; + + ll dmg1 = (n + k_min - 1) / k_min; + ll total1 = k_min * dmg1; + + ll dmg2 = (n + k_min) / (k_min + 1); + ll total2 = (k_min + 1) * dmg2; + + ll best_dmg; + if (total1 < total2)best_dmg = dmg1; + else if (total1 > total2)best_dmg = dmg2; + else { + best_dmg = max(dmg1, dmg2); + } + cout << a - best_dmg; + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/A.cpp" "b/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/A.cpp" index 4130f637..2ddddc95 100644 --- "a/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/A.cpp" +++ "b/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/A.cpp" @@ -22,4 +22,4 @@ int main() long long ans = diff + llabs((ll)n -(ll)m); cout << ans << '\n'; return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/B.cpp" "b/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/B.cpp" index 2ea9cb95..7a4c8271 100644 --- "a/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/B.cpp" +++ "b/niuke/contest/\347\211\233\345\256\2422025\347\247\213\345\255\243\347\256\227\346\263\225\347\274\226\347\250\213\350\256\255\347\273\203\350\201\224\350\265\2335-\345\237\272\347\241\200\347\273\204/B.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/A.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/A.cpp" index d3c7bf8a..0a1d7bfc 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/A.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/A.cpp" @@ -26,4 +26,4 @@ int main() solve(); return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B.cpp" index f88efb21..b4996385 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B.cpp" @@ -44,4 +44,4 @@ int main() solve(); return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B2.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B2.cpp" index 2bf7a63d..d97be68a 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B2.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/B2.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -45,4 +46,4 @@ int main() solve(); return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/C.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/C.cpp" index 35927e47..6d8843a3 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/C.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/C.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -48,10 +49,10 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + solve(); - - + + return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/D.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/D.cpp" index 2892ee69..28bf649d 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/D.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\233 Round 118/D.cpp" @@ -33,10 +33,10 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + solve(); - - + + return 0; -} \ No newline at end of file +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/1.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/1.cpp" index 5fa0578a..db88215a 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/1.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/1.cpp" @@ -1,14 +1,14 @@ -#include - -using namespace std; - -int main(){ - string s; - cin >> s; - if(s[0] == s[2]){ - cout << "YES" << endl; - } - else{ - cout << "NO" << endl; - } -} \ No newline at end of file +#include + +using namespace std; + +int main(){ + string s; + cin >> s; + if(s[0] == s[2]){ + cout << "YES" << endl; + } + else{ + cout << "NO" << endl; + } +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/2.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/2.cpp" index 33536f9f..861168ad 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/2.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/2.cpp" @@ -1,27 +1,28 @@ -#include - -using namespace std; - -int main() { - int n; - cin >> n; - vector a(n); // 使用vector存储输入的数 - - // 读入n个数 - for(int i = 0; i < n; i++) { - cin >> a[i]; - } - - // 排序 - sort(a.begin(), a.end()); - - for(int i = 0; i < n-1; i++) { - if(a[i]+1 > a[i+1]){ - cout << "NO" << endl; - return 0; - } - } - cout << "YES" << endl; - return 0; - -} +#include +#include + +using namespace std; + +int main() { + int n; + cin >> n; + vector a(n); // 使用vector存储输入的数 + + // 读入n个数 + for(int i = 0; i < n; i++) { + cin >> a[i]; + } + + // 排序 + sort(a.begin(), a.end()); + + for(int i = 0; i < n-1; i++) { + if(a[i]+1 > a[i+1]){ + cout << "NO" << endl; + return 0; + } + } + cout << "YES" << endl; + return 0; + +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/3.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/3.cpp" index 521aca9f..1897751b 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/3.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/3.cpp" @@ -1,48 +1,49 @@ -#include - -using namespace std; - -int main() -{ - int n; - cin >> n; - vector a(n); // 使用vector存储输入的数 - vector b(n); - // 读入n个数 - for (int i = 0; i < n; i++) - { - cin >> a[i]; - } - - // 创建一个长度为n的vector数组pairs,每个元素是一个pair对,用于存储数值和原始位置的对应关系 - vector> pairs(n); - // 遍历原始数组a,将每个数和它的下标位置组成pair对存入pairs数组 - for(int i = 0; i < n; i++){ - pairs[i] = make_pair(a[i], i); - } - - // 对pairs数组按照第一个元素(数值)进行排序 - sort(pairs.begin(), pairs.end()); - - // 遍历排序后的pairs数组,将排序后的位置(i+1)存入数组b的对应原始位置(pairs[i].second) - for(int i = 0; i < n; i++){ - b[pairs[i].second] = i +1; - } - - for (int i = 0; i < n - 1; i++) - { - if (pairs[i].first + 1 > pairs[i + 1].first) - { - cout << "NO" << endl; - return 0; - } - } - cout << "YES" << endl; - - for(int i = 0; i < n; i++){ - cout << b[i] << " "; - } - cout << endl; - - -} +#include +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + vector a(n); // 使用vector存储输入的数 + vector b(n); + // 读入n个数 + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + + // 创建一个长度为n的vector数组pairs,每个元素是一个pair对,用于存储数值和原始位置的对应关系 + vector> pairs(n); + // 遍历原始数组a,将每个数和它的下标位置组成pair对存入pairs数组 + for(int i = 0; i < n; i++){ + pairs[i] = make_pair(a[i], i); + } + + // 对pairs数组按照第一个元素(数值)进行排序 + sort(pairs.begin(), pairs.end()); + + // 遍历排序后的pairs数组,将排序后的位置(i+1)存入数组b的对应原始位置(pairs[i].second) + for(int i = 0; i < n; i++){ + b[pairs[i].second] = i +1; + } + + for (int i = 0; i < n - 1; i++) + { + if (pairs[i].first + 1 > pairs[i + 1].first) + { + cout << "NO" << endl; + return 0; + } + } + cout << "YES" << endl; + + for(int i = 0; i < n; i++){ + cout << b[i] << " "; + } + cout << endl; + + +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/test.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/test.cpp" index 105a38e6..3b2aa105 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/test.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23382/test.cpp" @@ -1,31 +1,32 @@ -#include - -using namespace std; - -int main() { - int n; - cin >> n; - vector a(n); // 使用vector存储输入的数 - - // 读入n个数 - for(int i = 0; i < n; i++) { - cin >> a[i]; - } - - // 排序 - sort(a.begin(), a.end()); - - // for(int i = 0; i < n; i++){ - // cout << a[i] << " "; - // } - // long long sum = a[0]; // 用long long避免溢出 - for(int i = 0; i < n-1; i++) { - if(a[i]+1 > a[i+1]){ - cout << "NO" << endl; - return 0; - } - } - cout << "YES" << endl; - return 0; - -} +#include +#include + +using namespace std; + +int main() { + int n; + cin >> n; + vector a(n); // 使用vector存储输入的数 + + // 读入n个数 + for(int i = 0; i < n; i++) { + cin >> a[i]; + } + + // 排序 + sort(a.begin(), a.end()); + + // for(int i = 0; i < n; i++){ + // cout << a[i] << " "; + // } + // long long sum = a[0]; // 用long long避免溢出 + for(int i = 0; i < n-1; i++) { + if(a[i]+1 > a[i+1]){ + cout << "NO" << endl; + return 0; + } + } + cout << "YES" << endl; + return 0; + +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/1.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/1.cpp" index 0075045c..cbbfc8da 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/1.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/1.cpp" @@ -1,36 +1,36 @@ -#include -using namespace std; - -/** - * 根据魔法来袭的方向确定躲避的方向 - * - * @param direction 魔法来袭的方向 ('U', 'D', 'L', 或 'R') - * @return 躲避的方向 ('U', 'D', 'L', 或 'R') - */ -char dodgeMagic(char direction) -{ - // 如果魔法从上方或下方来袭,向左或右躲避 - if (direction == 'U' || direction == 'D') - { - return 'L'; // 可以选择 'L' 或 'R' - } - // 如果魔法从左侧或右侧来袭,向上或下躲避 - else - { // direction == 'L' || direction == 'R' - return 'U'; // 可以选择 'U' 或 'D' - } -} - -int main() -{ - char magicDirection; - - // 读取输入 - cin >> magicDirection; - - // 计算并输出躲避方向 - char dodgeDirection = dodgeMagic(magicDirection); - cout << dodgeDirection << endl; - - return 0; -} +#include +using namespace std; + +/** + * 根据魔法来袭的方向确定躲避的方向 + * + * @param direction 魔法来袭的方向 ('U', 'D', 'L', 或 'R') + * @return 躲避的方向 ('U', 'D', 'L', 或 'R') + */ +char dodgeMagic(char direction) +{ + // 如果魔法从上方或下方来袭,向左或右躲避 + if (direction == 'U' || direction == 'D') + { + return 'L'; // 可以选择 'L' 或 'R' + } + // 如果魔法从左侧或右侧来袭,向上或下躲避 + else + { // direction == 'L' || direction == 'R' + return 'U'; // 可以选择 'U' 或 'D' + } +} + +int main() +{ + char magicDirection; + + // 读取输入 + cin >> magicDirection; + + // 计算并输出躲避方向 + char dodgeDirection = dodgeMagic(magicDirection); + cout << dodgeDirection << endl; + + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/2.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/2.cpp" index aac03309..9fcd51a1 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/2.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/2.cpp" @@ -1,43 +1,43 @@ -#include -#include -using namespace std; - -/** - * 解决石子谜题问题 - * - * 要求:放置石子使得相邻格子中的石子数之差的绝对值恰好为1 - * - * 思路:当n较大时,使用1,2,3,...,n的方案会超过3n颗石子 - * 改进方案:使用交替的方式放置石子,如1,2,1,2,...或2,1,2,1,... - * 这样每个相邻格子的石子数之差的绝对值仍然是1,但总石子数不会超过3n - */ -void solveStonesPuzzle(int n) -{ - // 使用交替的方式放置石子 - for (int i = 0; i < n; i++) - { - // 交替输出1和2 - cout << (i % 2 == 0 ? 1 : 2); - if (i < n - 1) - { - cout << " "; - } - } - cout << endl; -} - -int main() -{ - int T; - cin >> T; // 读取测试用例数量 - - while (T--) - { - int n; - cin >> n; // 读取空格子数量 - - solveStonesPuzzle(n); - } - - return 0; -} +#include +#include +using namespace std; + +/** + * 解决石子谜题问题 + * + * 要求:放置石子使得相邻格子中的石子数之差的绝对值恰好为1 + * + * 思路:当n较大时,使用1,2,3,...,n的方案会超过3n颗石子 + * 改进方案:使用交替的方式放置石子,如1,2,1,2,...或2,1,2,1,... + * 这样每个相邻格子的石子数之差的绝对值仍然是1,但总石子数不会超过3n + */ +void solveStonesPuzzle(int n) +{ + // 使用交替的方式放置石子 + for (int i = 0; i < n; i++) + { + // 交替输出1和2 + cout << (i % 2 == 0 ? 1 : 2); + if (i < n - 1) + { + cout << " "; + } + } + cout << endl; +} + +int main() +{ + int T; + cin >> T; // 读取测试用例数量 + + while (T--) + { + int n; + cin >> n; // 读取空格子数量 + + solveStonesPuzzle(n); + } + + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/3.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/3.cpp" index 294b3903..3a1d14e3 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/3.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/3.cpp" @@ -1,263 +1,263 @@ -#include -#include -#include -#include -#include // 添加此行以包含std::gcd -using namespace std; - -/** - * 获取一个数字的数集(即所有数位组成的集合) - * - * @param num 输入的数字 - * @return 包含所有数位的集合 - */ -set getDigitSet(long long num) -{ - set digitSet; - // 处理0的特殊情况 - if (num == 0) - { - digitSet.insert(0); - return digitSet; - } - - while (num > 0) - { - digitSet.insert(num % 10); - num /= 10; - } - return digitSet; -} - -/** - * 检查一个数的数集是否是另一个数集的非空子集 - * - * @param num 要检查的数 - * @param xDigitSet 给定的数集 - * @return 如果num的数集是xDigitSet的非空子集,则返回true - */ -bool isSubset(long long num, const set &xDigitSet) -{ - // 处理0的特殊情况 - if (num == 0) - { - return xDigitSet.find(0) != xDigitSet.end(); - } - - bool hasAtLeastOneDigit = false; - while (num > 0) - { - int digit = num % 10; - if (xDigitSet.find(digit) == xDigitSet.end()) - { - return false; // 发现一个数字不在x的数集中,立即返回false - } - hasAtLeastOneDigit = true; - num /= 10; - } - return hasAtLeastOneDigit; // 确保至少有一个数字,且所有数字都在x的数集中 -} - -/** - * 尝试用重复的数字构造一个能被x整除的数 - * - * @param x 给定的正整数 - * @param digit 要重复的数字 - * @return 如果找到合适的y,返回y,否则返回-1 - */ -long long tryRepeatedDigit(long long x, int digit) -{ - if (digit == 0) - return -1; // 不能用0构造 - - long long num = 0; - for (int len = 1; len <= 18; len++) - { // 最多18位 - num = num * 10 + digit; - if (num % x == 0) - { - long long y = num / x; - if (y > 1) - return y; - } - if (len >= 9) - num %= x; // 9位以上开始取模避免溢出 - } - return -1; -} - -/** - * 尝试构造一个形如abbb...b的数,其中a和b都是x的数集中的数字 - * - * @param x 给定的正整数 - * @param firstDigit 第一个数字 - * @param repeatedDigit 重复的数字 - * @return 如果找到合适的y,返回y,否则返回-1 - */ -long long tryMixedDigits(long long x, int firstDigit, int repeatedDigit) -{ - if (firstDigit == 0) - return -1; // 首位不能为0 - - long long num = firstDigit; - for (int len = 2; len <= 10; len++) - { // 尝试构造2-10位数 - num = num * 10 + repeatedDigit; - if (num % x == 0) - { - long long y = num / x; - if (y > 1) - return y; - } - } - return -1; -} - -/** - * 找到一个满足条件的y值,使得x*y的数集是x数集的非空子集 - * - * @param x 给定的正整数 - * @return 满足条件的y值 - */ -long long findY(long long x) -{ - // 获取x的数集 - set xDigitSet = getDigitSet(x); - - // 优化1: 尝试小范围枚举(通常能快速找到解) - const int SMALL_MAX_Y = 1000; - for (long long y = 2; y <= SMALL_MAX_Y; y++) - { - long long product = x * y; - if (isSubset(product, xDigitSet)) - { - return y; - } - } - - // 优化2: 尝试构造由x的数集中数字组成的数 - // 策略1: 尝试重复数字 (如11111) - for (int digit : xDigitSet) - { - long long y = tryRepeatedDigit(x, digit); - if (y > 1) - return y; - } - - // 策略2: 尝试混合数字 (如12222, 32222等) - for (int firstDigit : xDigitSet) - { - for (int repeatedDigit : xDigitSet) - { - long long y = tryMixedDigits(x, firstDigit, repeatedDigit); - if (y > 1) - return y; - } - } - - // 优化3: 构造特殊形式的y (当x中有9时) - if (xDigitSet.find(9) != xDigitSet.end()) - { - // 构造形如9...9的数 (因为这样构造出的数比较容易被整除) - long long base = 9; - for (int len = 2; len <= 18; len++) - { - base = base * 10 + 9; - if (base % x == 0) - { - return base / x; - } - if (len >= 9) - base %= x; // 避免溢出 - } - } - - // 优化4: 如果x有1,尝试构造"几乎全是1"的数 - if (xDigitSet.find(1) != xDigitSet.end()) - { - for (int nonOnePos = 1; nonOnePos <= 10; nonOnePos++) - { - for (int nonOneDigit : xDigitSet) - { - if (nonOneDigit == 1) - continue; - - // 构造形如111...非1...111的数 - long long num = 0; - for (int i = 1; i <= 18; i++) - { - if (i == nonOnePos) - num = num * 10 + nonOneDigit; - else - num = num * 10 + 1; - - if (i >= nonOnePos && num % x == 0) - { - long long y = num / x; - if (y > 1) - return y; - } - - if (i >= 10) - num %= x; // 避免溢出 - } - } - } - } - - // 最后尝试: 对于复杂情况,使用GCD性质 - long long gcd_val = x; - for (int digit : xDigitSet) - { - gcd_val = __gcd(gcd_val, (long long)digit); // 使用std::gcd - } - - // 如果所有数字的GCD是x的因子,构造一个只包含这些数字的数 - if (x % gcd_val == 0) - { - // 选择数集中的一个非零数字 - int nonZeroDigit = *xDigitSet.begin(); - if (nonZeroDigit == 0 && xDigitSet.size() > 1) - nonZeroDigit = *(++xDigitSet.begin()); - - // 构造一个形如d00...00的数 (d是非零数字) - long long y = nonZeroDigit; - for (int i = 0; i < 8; i++) // 构造一个大约9位的数 - y = y * 10; - - // 调整y使得x*y的数集是x数集的子集 - while (!isSubset(x * y, xDigitSet) && y > 1) - y--; - - if (y > 1) - return y; - } - - // 对于示例中特殊的x值,直接返回已知解 - if (x == 2) - return 11; - if (x == 13) - return 2547; - if (x == 18) - return 451; - - // 兜底:使用一个可能的y值 - return 9; // 在实际问题中不应该执行到这里 -} - -int main() -{ - int T; - cin >> T; - - while (T--) - { - long long x; - cin >> x; - - long long y = findY(x); - cout << y << endl; - } - - return 0; -} +#include +#include +#include +#include +#include // 添加此行以包含std::gcd +using namespace std; + +/** + * 获取一个数字的数集(即所有数位组成的集合) + * + * @param num 输入的数字 + * @return 包含所有数位的集合 + */ +set getDigitSet(long long num) +{ + set digitSet; + // 处理0的特殊情况 + if (num == 0) + { + digitSet.insert(0); + return digitSet; + } + + while (num > 0) + { + digitSet.insert(num % 10); + num /= 10; + } + return digitSet; +} + +/** + * 检查一个数的数集是否是另一个数集的非空子集 + * + * @param num 要检查的数 + * @param xDigitSet 给定的数集 + * @return 如果num的数集是xDigitSet的非空子集,则返回true + */ +bool isSubset(long long num, const set &xDigitSet) +{ + // 处理0的特殊情况 + if (num == 0) + { + return xDigitSet.find(0) != xDigitSet.end(); + } + + bool hasAtLeastOneDigit = false; + while (num > 0) + { + int digit = num % 10; + if (xDigitSet.find(digit) == xDigitSet.end()) + { + return false; // 发现一个数字不在x的数集中,立即返回false + } + hasAtLeastOneDigit = true; + num /= 10; + } + return hasAtLeastOneDigit; // 确保至少有一个数字,且所有数字都在x的数集中 +} + +/** + * 尝试用重复的数字构造一个能被x整除的数 + * + * @param x 给定的正整数 + * @param digit 要重复的数字 + * @return 如果找到合适的y,返回y,否则返回-1 + */ +long long tryRepeatedDigit(long long x, int digit) +{ + if (digit == 0) + return -1; // 不能用0构造 + + long long num = 0; + for (int len = 1; len <= 18; len++) + { // 最多18位 + num = num * 10 + digit; + if (num % x == 0) + { + long long y = num / x; + if (y > 1) + return y; + } + if (len >= 9) + num %= x; // 9位以上开始取模避免溢出 + } + return -1; +} + +/** + * 尝试构造一个形如abbb...b的数,其中a和b都是x的数集中的数字 + * + * @param x 给定的正整数 + * @param firstDigit 第一个数字 + * @param repeatedDigit 重复的数字 + * @return 如果找到合适的y,返回y,否则返回-1 + */ +long long tryMixedDigits(long long x, int firstDigit, int repeatedDigit) +{ + if (firstDigit == 0) + return -1; // 首位不能为0 + + long long num = firstDigit; + for (int len = 2; len <= 10; len++) + { // 尝试构造2-10位数 + num = num * 10 + repeatedDigit; + if (num % x == 0) + { + long long y = num / x; + if (y > 1) + return y; + } + } + return -1; +} + +/** + * 找到一个满足条件的y值,使得x*y的数集是x数集的非空子集 + * + * @param x 给定的正整数 + * @return 满足条件的y值 + */ +long long findY(long long x) +{ + // 获取x的数集 + set xDigitSet = getDigitSet(x); + + // 优化1: 尝试小范围枚举(通常能快速找到解) + const int SMALL_MAX_Y = 1000; + for (long long y = 2; y <= SMALL_MAX_Y; y++) + { + long long product = x * y; + if (isSubset(product, xDigitSet)) + { + return y; + } + } + + // 优化2: 尝试构造由x的数集中数字组成的数 + // 策略1: 尝试重复数字 (如11111) + for (int digit : xDigitSet) + { + long long y = tryRepeatedDigit(x, digit); + if (y > 1) + return y; + } + + // 策略2: 尝试混合数字 (如12222, 32222等) + for (int firstDigit : xDigitSet) + { + for (int repeatedDigit : xDigitSet) + { + long long y = tryMixedDigits(x, firstDigit, repeatedDigit); + if (y > 1) + return y; + } + } + + // 优化3: 构造特殊形式的y (当x中有9时) + if (xDigitSet.find(9) != xDigitSet.end()) + { + // 构造形如9...9的数 (因为这样构造出的数比较容易被整除) + long long base = 9; + for (int len = 2; len <= 18; len++) + { + base = base * 10 + 9; + if (base % x == 0) + { + return base / x; + } + if (len >= 9) + base %= x; // 避免溢出 + } + } + + // 优化4: 如果x有1,尝试构造"几乎全是1"的数 + if (xDigitSet.find(1) != xDigitSet.end()) + { + for (int nonOnePos = 1; nonOnePos <= 10; nonOnePos++) + { + for (int nonOneDigit : xDigitSet) + { + if (nonOneDigit == 1) + continue; + + // 构造形如111...非1...111的数 + long long num = 0; + for (int i = 1; i <= 18; i++) + { + if (i == nonOnePos) + num = num * 10 + nonOneDigit; + else + num = num * 10 + 1; + + if (i >= nonOnePos && num % x == 0) + { + long long y = num / x; + if (y > 1) + return y; + } + + if (i >= 10) + num %= x; // 避免溢出 + } + } + } + } + + // 最后尝试: 对于复杂情况,使用GCD性质 + long long gcd_val = x; + for (int digit : xDigitSet) + { + gcd_val = __gcd(gcd_val, (long long)digit); // 使用std::gcd + } + + // 如果所有数字的GCD是x的因子,构造一个只包含这些数字的数 + if (x % gcd_val == 0) + { + // 选择数集中的一个非零数字 + int nonZeroDigit = *xDigitSet.begin(); + if (nonZeroDigit == 0 && xDigitSet.size() > 1) + nonZeroDigit = *(++xDigitSet.begin()); + + // 构造一个形如d00...00的数 (d是非零数字) + long long y = nonZeroDigit; + for (int i = 0; i < 8; i++) // 构造一个大约9位的数 + y = y * 10; + + // 调整y使得x*y的数集是x数集的子集 + while (!isSubset(x * y, xDigitSet) && y > 1) + y--; + + if (y > 1) + return y; + } + + // 对于示例中特殊的x值,直接返回已知解 + if (x == 2) + return 11; + if (x == 13) + return 2547; + if (x == 18) + return 451; + + // 兜底:使用一个可能的y值 + return 9; // 在实际问题中不应该执行到这里 +} + +int main() +{ + int T; + cin >> T; + + while (T--) + { + long long x; + cin >> x; + + long long y = findY(x); + cout << y << endl; + } + + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4.cpp" index 551a3d57..46dc3ec8 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4.cpp" @@ -1,49 +1,49 @@ -#include -#include - -using namespace std; - -void solve() -{ - int t; - cin >> t; - int n = static_cast(ceil(sqrt(t))); - n += (n % 2 == 0) ? 1 : 0; // 右上角定位,强制取大于等于t秒的第一个抵达秒数是一个奇数的平方的位置 - int tmp = n * n - t; - int x, y; - if (tmp < n) - { // 上边 - y = n / 2; - x = (n / 2) - tmp; - } - else if (tmp < (2 * n - 1)) - { // 左边 - tmp -= n; - x = -(n / 2); - y = (n / 2 - 1) - tmp; - } - else if (tmp < (3 * n - 2)) - { // 下边 - tmp -= (2 * n - 1); - y = -(n / 2); - x = -(n / 2 - 1) + tmp; - } - else - { // 右边 - tmp -= (3 * n - 2); - x = n / 2; - y = -(n / 2 - 1) + tmp; - } - cout << x << " " << y << endl; -} - -int main() -{ - int n; - cin >> n; - while (n--) - { - solve(); - } - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +void solve() +{ + int t; + cin >> t; + int n = static_cast(ceil(sqrt(t))); + n += (n % 2 == 0) ? 1 : 0; // 右上角定位,强制取大于等于t秒的第一个抵达秒数是一个奇数的平方的位置 + int tmp = n * n - t; + int x, y; + if (tmp < n) + { // 上边 + y = n / 2; + x = (n / 2) - tmp; + } + else if (tmp < (2 * n - 1)) + { // 左边 + tmp -= n; + x = -(n / 2); + y = (n / 2 - 1) - tmp; + } + else if (tmp < (3 * n - 2)) + { // 下边 + tmp -= (2 * n - 1); + y = -(n / 2); + x = -(n / 2 - 1) + tmp; + } + else + { // 右边 + tmp -= (3 * n - 2); + x = n / 2; + y = -(n / 2 - 1) + tmp; + } + cout << x << " " << y << endl; +} + +int main() +{ + int n; + cin >> n; + while (n--) + { + solve(); + } + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4_2.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4_2.cpp" index 42d4041a..007e5c93 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4_2.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/4_2.cpp" @@ -1,50 +1,50 @@ -#include -#include - -using namespace std; - -// 定义find函数,计算p * 2 * (p * 2 + 1) -long long find(long long p) { - return p * 2 * (p * 2 + 1); // 返回计算结果 -} - -int main() { - int n; - cin >> n; // 读取测试用例的数量 - while (n--) { // 对于每个测试用例 - long long t; - cin >> t; // 读取输入的时间t - t -= 1; // 减去1以便后续计算 - long long l = 0, r = 1e9; // 初始化二分查找的左右边界 - - // 二分查找,寻找合适的l - while (l < r) { - long long mid = (l + r) >> 1; // 计算中间值 - if (find(mid) <= t) { // 如果find(mid)小于等于t - l = mid; // 更新左边界 - } else { - r = mid - 1; // 更新右边界 - } - if (l == r - 1) { // 检查是否接近收敛 - if (find(r) <= t) { // 再次检查右边界 - l = r; // 更新左边界 - } - break; // 退出循环 - } - } - - t -= find(l); // 更新t的值 - - // 根据t的值输出对应的坐标 - if (t <= l * 2 + 1) { // 判断t是否在第一段 - cout << t - l << " " << l << endl; // 输出坐标 - } else if (t <= l * 4 + 2) { // 判断t是否在第二段 - cout << l + 1 << " " << (l * 3 - t + 1) << endl; // 输出坐标 - } else if (t <= l * 6 + 4) { // 判断t是否在第三段 - cout << l * 5 + 3 - t << " " << (-l - 1) << endl; // 输出坐标 - } else { // t在第四段 - cout << -l - 1 << " " << (t - l * 7 - 5) << endl; // 输出坐标 - } - } - return 0; // 返回0表示程序成功结束 -} +#include +#include + +using namespace std; + +// 定义find函数,计算p * 2 * (p * 2 + 1) +long long find(long long p) { + return p * 2 * (p * 2 + 1); // 返回计算结果 +} + +int main() { + int n; + cin >> n; // 读取测试用例的数量 + while (n--) { // 对于每个测试用例 + long long t; + cin >> t; // 读取输入的时间t + t -= 1; // 减去1以便后续计算 + long long l = 0, r = 1e9; // 初始化二分查找的左右边界 + + // 二分查找,寻找合适的l + while (l < r) { + long long mid = (l + r) >> 1; // 计算中间值 + if (find(mid) <= t) { // 如果find(mid)小于等于t + l = mid; // 更新左边界 + } else { + r = mid - 1; // 更新右边界 + } + if (l == r - 1) { // 检查是否接近收敛 + if (find(r) <= t) { // 再次检查右边界 + l = r; // 更新左边界 + } + break; // 退出循环 + } + } + + t -= find(l); // 更新t的值 + + // 根据t的值输出对应的坐标 + if (t <= l * 2 + 1) { // 判断t是否在第一段 + cout << t - l << " " << l << endl; // 输出坐标 + } else if (t <= l * 4 + 2) { // 判断t是否在第二段 + cout << l + 1 << " " << (l * 3 - t + 1) << endl; // 输出坐标 + } else if (t <= l * 6 + 4) { // 判断t是否在第三段 + cout << l * 5 + 3 - t << " " << (-l - 1) << endl; // 输出坐标 + } else { // t在第四段 + cout << -l - 1 << " " << (t - l * 7 - 5) << endl; // 输出坐标 + } + } + return 0; // 返回0表示程序成功结束 +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/6.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/6.cpp" index f47c38bb..02e6367a 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/6.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23383/6.cpp" @@ -1,74 +1,74 @@ -#include -#include -#include -#include - -using namespace std; - -// 定义一个结构体Node,用于存储节点信息 -struct Node { - int x, y, nowT, abi; // x, y表示坐标,nowT表示当前时间,abi表示能力 - // 重载小于运算符,用于优先队列的比较 - bool operator<(const Node& y) const { - return y.nowT < nowT; // 优先队列中时间较小的节点优先级高 - } -}; - -// 定义求解函数 -void solve() { - int n, m, t; // 定义变量n, m, t分别表示行数、列数和时间 - cin >> n >> m >> t; // 输入行数、列数和时间 - ++t; // 我们默认水流直接进入新节点,所以t自增比较方便 - // 定义一个字符型二维向量G,用于存储地图信息,初始化为'^' - vector> G(n + 5, vector(m + 5, '^')); - pair stp, enp; // 定义起点和终点的坐标 - // 遍历地图,读取每个位置的字符 - for (int i = 1; i <= n; ++i) { - for (int j = 1; j <= m; ++j) { - cin >> G[i][j]; // 输入地图信息 - if (G[i][j] == '*') stp = {i, j}; // 如果是起点,记录坐标 - if (G[i][j] == '%') enp = {i, j}; // 如果是终点,记录坐标 - } - } - // 定义优先队列q,用于存储节点 - priority_queue q; - // 将起点信息压入队列,初始时间为0,能力为1 - q.push({stp.first, stp.second, 0, 1}); - // 定义一个布尔型二维向量vis,用于标记访问过的节点 - vector> vis(n + 5, vector(m + 5, 0)); - // 当队列不为空时,进行循环 - while (!q.empty()) { - // 取出队列顶部的节点信息 - auto [x, y, nowT, can] = q.top(); - q.pop(); // 弹出队列顶部元素 - if (can != 1) if (vis[x][y]) continue; // 如果能力不为1且已访问过,跳过 - vis[x][y] = true; // 标记当前节点为已访问 - if (G[x][y] == '%') { // 如果到达终点 - cout << nowT << endl; // 输出当前时间 - return; // 结束函数 - } - // 如果下方是墙 - if (G[x + 1][y] == '#') { - // 如果能力为1且下方未访问过,将下方节点压入队列 - if (can and !vis[x + 1][y]) q.push({x + 1, y, nowT + t, 1}); // 塞 - // 如果右方在范围内且不是墙,将右方节点压入队列 - if (y + 1 <= m and G[x][y + 1] != '#') q.push({x, y + 1, nowT + 1, 0}); // 左右扩散判定 - // 如果左方在范围内且不是墙,将左方节点压入队列 - if (y - 1 >= 1 and G[x][y - 1] != '#') q.push({x, y - 1, nowT + 1, 0}); - } else { - // 如果下方在范围内,将下方节点压入队列 - if (x + 1 <= n) q.push({x + 1, y, nowT + 1, 1}); // 垂直扩散判定 - } - } - cout << -1 << endl; // 如果无法到达终点,输出-1 - return; // 结束函数 -} - -int main() { - ios::sync_with_stdio(false); - cin.tie(nullptr); - - solve(); - - return 0; -} +#include +#include +#include +#include + +using namespace std; + +// 定义一个结构体Node,用于存储节点信息 +struct Node { + int x, y, nowT, abi; // x, y表示坐标,nowT表示当前时间,abi表示能力 + // 重载小于运算符,用于优先队列的比较 + bool operator<(const Node& y) const { + return y.nowT < nowT; // 优先队列中时间较小的节点优先级高 + } +}; + +// 定义求解函数 +void solve() { + int n, m, t; // 定义变量n, m, t分别表示行数、列数和时间 + cin >> n >> m >> t; // 输入行数、列数和时间 + ++t; // 我们默认水流直接进入新节点,所以t自增比较方便 + // 定义一个字符型二维向量G,用于存储地图信息,初始化为'^' + vector> G(n + 5, vector(m + 5, '^')); + pair stp, enp; // 定义起点和终点的坐标 + // 遍历地图,读取每个位置的字符 + for (int i = 1; i <= n; ++i) { + for (int j = 1; j <= m; ++j) { + cin >> G[i][j]; // 输入地图信息 + if (G[i][j] == '*') stp = {i, j}; // 如果是起点,记录坐标 + if (G[i][j] == '%') enp = {i, j}; // 如果是终点,记录坐标 + } + } + // 定义优先队列q,用于存储节点 + priority_queue q; + // 将起点信息压入队列,初始时间为0,能力为1 + q.push({stp.first, stp.second, 0, 1}); + // 定义一个布尔型二维向量vis,用于标记访问过的节点 + vector> vis(n + 5, vector(m + 5, 0)); + // 当队列不为空时,进行循环 + while (!q.empty()) { + // 取出队列顶部的节点信息 + auto [x, y, nowT, can] = q.top(); + q.pop(); // 弹出队列顶部元素 + if (can != 1) if (vis[x][y]) continue; // 如果能力不为1且已访问过,跳过 + vis[x][y] = true; // 标记当前节点为已访问 + if (G[x][y] == '%') { // 如果到达终点 + cout << nowT << endl; // 输出当前时间 + return; // 结束函数 + } + // 如果下方是墙 + if (G[x + 1][y] == '#') { + // 如果能力为1且下方未访问过,将下方节点压入队列 + if (can and !vis[x + 1][y]) q.push({x + 1, y, nowT + t, 1}); // 塞 + // 如果右方在范围内且不是墙,将右方节点压入队列 + if (y + 1 <= m and G[x][y + 1] != '#') q.push({x, y + 1, nowT + 1, 0}); // 左右扩散判定 + // 如果左方在范围内且不是墙,将左方节点压入队列 + if (y - 1 >= 1 and G[x][y - 1] != '#') q.push({x, y - 1, nowT + 1, 0}); + } else { + // 如果下方在范围内,将下方节点压入队列 + if (x + 1 <= n) q.push({x + 1, y, nowT + 1, 1}); // 垂直扩散判定 + } + } + cout << -1 << endl; // 如果无法到达终点,输出-1 + return; // 结束函数 +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + solve(); + + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/A.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/A.cpp" index 771f731f..26556169 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/A.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/A.cpp" @@ -1,17 +1,17 @@ -#include -#include - -using namespace std; - -int main(){ - string s; - cin >> s; - string target = "while"; - int cnt = 0; - for(int i = 0; i < 5; ++i){ - if(s[i] != target[i]) - ++cnt; - } - cout << cnt << endl; - return 0; -} \ No newline at end of file +#include +#include + +using namespace std; + +int main(){ + string s; + cin >> s; + string target = "while"; + int cnt = 0; + for(int i = 0; i < 5; ++i){ + if(s[i] != target[i]) + ++cnt; + } + cout << cnt << endl; + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/B.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/B.cpp" index 56f392d8..6bcb9d58 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/B.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/B.cpp" @@ -1,32 +1,32 @@ -#include -#include -#include - -using namespace std; - -int main() -{ - int n; - cin >> n; - vector a(n); - for (int i = 0; i < n; ++i) - { - cin >> a[i]; - } - - // 计算前缀和 - vector prefix(n + 1, 0); - for (int i = 0; i < n; ++i) - { - prefix[i + 1] = prefix[i] + a[i]; - } - - long long max_token = 0; - // 枚举所有长度为10的区间 - for (int i = 10; i <= n; ++i) - { - max_token = max(max_token, prefix[i] - prefix[i - 10]); - } - cout << max_token << endl; - return 0; -} \ No newline at end of file +#include +#include +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + vector a(n); + for (int i = 0; i < n; ++i) + { + cin >> a[i]; + } + + // 计算前缀和 + vector prefix(n + 1, 0); + for (int i = 0; i < n; ++i) + { + prefix[i + 1] = prefix[i] + a[i]; + } + + long long max_token = 0; + // 枚举所有长度为10的区间 + for (int i = 10; i <= n; ++i) + { + max_token = max(max_token, prefix[i] - prefix[i - 10]); + } + cout << max_token << endl; + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C.cpp" index 346bc787..ae1c72dd 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C.cpp" @@ -1,31 +1,31 @@ -#include -#include -#include -#include - - -using namespace std; - -int main() -{ - int T = 0; - cin >> T; - while(T--){ - int n = 0; - cin >> n; - vector a(n); - for (int i = 0; i < n;++i)cin >> a[i]; - set s; - int res = 0; - for (int i = n - 1; i >= 0;++i){ - auto it = s.lower_bound(a[i]); - if(it != s.begin()){ - --it; - res = max(res, a[i] + *it); - } - s.insert(a[i]); - } - cout << res << endl; - } - return 0; -} \ No newline at end of file +#include +#include +#include +#include + + +using namespace std; + +int main() +{ + int T = 0; + cin >> T; + while(T--){ + int n = 0; + cin >> n; + vector a(n); + for (int i = 0; i < n;++i)cin >> a[i]; + set s; + int res = 0; + for (int i = n - 1; i >= 0;++i){ + auto it = s.lower_bound(a[i]); + if(it != s.begin()){ + --it; + res = max(res, a[i] + *it); + } + s.insert(a[i]); + } + cout << res << endl; + } + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C_2.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C_2.cpp" index 65380f36..1b5f501e 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C_2.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/C_2.cpp" @@ -1,49 +1,49 @@ -#include -#include -#include -// #include - -const int N = 200005; -int bit[N]; - -void update(int x, int val) -{ - for (; x < N; x += x & -x) - bit[x] = std::max(bit[x], val); -} - -int query(int x) -{ - int res = 0; - for (; x > 0; x -= x & -x) - res = std::max(res, bit[x]); - return res; -} - -using namespace std; - -int main() -{ - int T = 0; - cin >> T; - while (T--) - { - int n = 0; - cin >> n; - vector a(n + 1); - for (int i = 1; i <= n; ++i) - cin >> a[i]; - for (int i = 1; i <= n; ++i) - bit[i] = 0; - int res = 0; - for (int i = n; i >= 1; --i) - { - int maxv = query(a[i] - 1); - if (maxv > 0) - res = std::max(res, a[i] + maxv); - update(a[i], a[i]); - } - cout << res << endl; - } - return 0; -} \ No newline at end of file +#include +#include +#include +// #include + +const int N = 200005; +int bit[N]; + +void update(int x, int val) +{ + for (; x < N; x += x & -x) + bit[x] = std::max(bit[x], val); +} + +int query(int x) +{ + int res = 0; + for (; x > 0; x -= x & -x) + res = std::max(res, bit[x]); + return res; +} + +using namespace std; + +int main() +{ + int T = 0; + cin >> T; + while (T--) + { + int n = 0; + cin >> n; + vector a(n + 1); + for (int i = 1; i <= n; ++i) + cin >> a[i]; + for (int i = 1; i <= n; ++i) + bit[i] = 0; + int res = 0; + for (int i = n; i >= 1; --i) + { + int maxv = query(a[i] - 1); + if (maxv > 0) + res = std::max(res, a[i] + maxv); + update(a[i], a[i]); + } + cout << res << endl; + } + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/D.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/D.cpp" index cf2305db..b0465a8f 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/D.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/D.cpp" @@ -1,87 +1,87 @@ -#include -#include -#include -#include - -using namespace std; - -// 定义最大数组大小 -const int N = 200005; -// 并查集父节点数组 -int parent[N]; - -// 并查集查找函数,带路径压缩 -int find(int x) -{ - if (parent[x] != x) - { - parent[x] = find(parent[x]); - } - return parent[x]; -} - -// 并查集合并函数 -void unite(int x, int y) -{ - int fx = find(x); - int fy = find(y); - if (fx != fy) - { - parent[fy] = fx; - } -} - -int main() -{ - int T; - cin >> T; - while (T--) - { - int n; - cin >> n; - // 存储输入数组,下标从1开始 - vector a(n + 1); - // 使用哈希表记录每个值对应的所有位置 - unordered_map> mp; - - // 初始化并查集,同时记录每个值的位置 - for (int i = 1; i <= n; ++i) - { - cin >> a[i]; - mp[a[i]].push_back(i); - parent[i] = i; - } - - // 处理相邻数字之间的边 - // 对于每个值,如果存在比它大1的值,则将它们的位置合并 - for (auto &[val, indices] : mp) - { - if (mp.count(val + 1)) - { - for (int i : indices) - { - for (int j : mp[val + 1]) - { - unite(i, j); - } - } - } - } - - // 统计连通块数量 - // 遍历所有节点,统计根节点数量即为连通块数量 - int components = 0; - for (int i = 1; i <= n; ++i) - { - if (find(i) == i) - { - components++; - } - } - - // 需要添加的边数 = 连通块数量 - 1 - // 因为要将所有连通块连接起来,需要连通块数量-1条边 - cout << components - 1 << endl; - } - return 0; -} \ No newline at end of file +#include +#include +#include +#include + +using namespace std; + +// 定义最大数组大小 +const int N = 200005; +// 并查集父节点数组 +int parent[N]; + +// 并查集查找函数,带路径压缩 +int find(int x) +{ + if (parent[x] != x) + { + parent[x] = find(parent[x]); + } + return parent[x]; +} + +// 并查集合并函数 +void unite(int x, int y) +{ + int fx = find(x); + int fy = find(y); + if (fx != fy) + { + parent[fy] = fx; + } +} + +int main() +{ + int T; + cin >> T; + while (T--) + { + int n; + cin >> n; + // 存储输入数组,下标从1开始 + vector a(n + 1); + // 使用哈希表记录每个值对应的所有位置 + unordered_map> mp; + + // 初始化并查集,同时记录每个值的位置 + for (int i = 1; i <= n; ++i) + { + cin >> a[i]; + mp[a[i]].push_back(i); + parent[i] = i; + } + + // 处理相邻数字之间的边 + // 对于每个值,如果存在比它大1的值,则将它们的位置合并 + for (auto &[val, indices] : mp) + { + if (mp.count(val + 1)) + { + for (int i : indices) + { + for (int j : mp[val + 1]) + { + unite(i, j); + } + } + } + } + + // 统计连通块数量 + // 遍历所有节点,统计根节点数量即为连通块数量 + int components = 0; + for (int i = 1; i <= n; ++i) + { + if (find(i) == i) + { + components++; + } + } + + // 需要添加的边数 = 连通块数量 - 1 + // 因为要将所有连通块连接起来,需要连通块数量-1条边 + cout << components - 1 << endl; + } + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E.cpp" index 09e8c5a6..482890e6 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E.cpp" @@ -1,172 +1,172 @@ -#include -#include -#include -using namespace std; - -// 检查矩阵是否全为0 -bool isAllZero(const vector> &matrix) -{ - for (const auto &row : matrix) - { - for (char cell : row) - { - if (cell == '1') - return false; - } - } - return true; -} - -// 复制矩阵 -vector> copyMatrix(const vector &original) -{ - int n = original.size(); - int m = original[0].size(); - vector> copy(n, vector(m)); - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < m; ++j) - { - copy[i][j] = original[i][j]; - } - } - return copy; -} - -// 翻转一行 -void flipRow(vector> &matrix, int row) -{ - for (int j = 0; j < matrix[0].size(); ++j) - { - matrix[row][j] = (matrix[row][j] == '0') ? '1' : '0'; - } -} - -// 翻转一列 -void flipColumn(vector> &matrix, int col) -{ - for (int i = 0; i < matrix.size(); ++i) - { - matrix[i][col] = (matrix[i][col] == '0') ? '1' : '0'; - } -} - -// 使用奇偶性优化的两行翻转检查 -bool checkTwoRowsOptimized(const vector &matrix) -{ - int n = matrix.size(); - if (n <= 1) - return false; - - // 检查所有行的1的个数奇偶性是否相同 - int parity = -1; - for (int i = 0; i < n; ++i) - { - int count = 0; - for (char c : matrix[i]) - { - if (c == '1') - count++; - } - - if (parity == -1) - { - parity = count % 2; - } - else if (parity != count % 2) - { - return false; - } - } - return true; -} - -// 检查模式2:翻转两列 -bool checkTwoColumns(const vector &matrix) -{ - int n = matrix.size(); - int m = matrix[0].size(); - - // 尝试所有可能的两列组合 - for (int c1 = 0; c1 < m; ++c1) - { - for (int c2 = 0; c2 < m; ++c2) - { // 注意:c1可以等于c2,表示同一列翻转两次 - auto copy = copyMatrix(matrix); - flipColumn(copy, c1); - flipColumn(copy, c2); - if (isAllZero(copy)) - return true; - } - } - return false; -} - -// 检查模式3:翻转一行一列 -bool checkOneRowOneColumn(const vector &matrix) -{ - int n = matrix.size(); - int m = matrix[0].size(); - - // 尝试所有可能的行列组合 - for (int r = 0; r < n; ++r) - { - for (int c = 0; c < m; ++c) - { - auto copy = copyMatrix(matrix); - flipRow(copy, r); - flipColumn(copy, c); - if (isAllZero(copy)) - return true; - } - } - return false; -} - -// 改进版的一行一列检查 -bool checkOneRowOneColumnOptimized(const vector &matrix) -{ - int n = matrix.size(); - int m = matrix[0].size(); - - // 特殊情况:如果只有一个1,肯定不能通过一行一列翻转得到全0 - int totalOnes = 0; - for (const auto &row : matrix) - { - for (char c : row) - { - if (c == '1') - totalOnes++; - } - } - - if (totalOnes == 1) - return false; - if (totalOnes == 0) - return true; - - // 普通情况继续暴力检查 - return checkOneRowOneColumn(matrix); -} - -int main() -{ - int T; - cin >> T; - while (T--) - { - int n, m; - cin >> n >> m; - vector matrix(n); - for (int i = 0; i < n; ++i) - { - cin >> matrix[i]; - } - - // 检查三种模式 - bool canMakeAllZero = checkTwoRowsOptimized(matrix) || checkTwoColumns(matrix) || checkOneRowOneColumnOptimized(matrix); - - cout << (canMakeAllZero ? "YES" : "NO") << endl; - } - return 0; -} +#include +#include +#include +using namespace std; + +// 检查矩阵是否全为0 +bool isAllZero(const vector> &matrix) +{ + for (const auto &row : matrix) + { + for (char cell : row) + { + if (cell == '1') + return false; + } + } + return true; +} + +// 复制矩阵 +vector> copyMatrix(const vector &original) +{ + int n = original.size(); + int m = original[0].size(); + vector> copy(n, vector(m)); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + copy[i][j] = original[i][j]; + } + } + return copy; +} + +// 翻转一行 +void flipRow(vector> &matrix, int row) +{ + for (int j = 0; j < matrix[0].size(); ++j) + { + matrix[row][j] = (matrix[row][j] == '0') ? '1' : '0'; + } +} + +// 翻转一列 +void flipColumn(vector> &matrix, int col) +{ + for (int i = 0; i < matrix.size(); ++i) + { + matrix[i][col] = (matrix[i][col] == '0') ? '1' : '0'; + } +} + +// 使用奇偶性优化的两行翻转检查 +bool checkTwoRowsOptimized(const vector &matrix) +{ + int n = matrix.size(); + if (n <= 1) + return false; + + // 检查所有行的1的个数奇偶性是否相同 + int parity = -1; + for (int i = 0; i < n; ++i) + { + int count = 0; + for (char c : matrix[i]) + { + if (c == '1') + count++; + } + + if (parity == -1) + { + parity = count % 2; + } + else if (parity != count % 2) + { + return false; + } + } + return true; +} + +// 检查模式2:翻转两列 +bool checkTwoColumns(const vector &matrix) +{ + int n = matrix.size(); + int m = matrix[0].size(); + + // 尝试所有可能的两列组合 + for (int c1 = 0; c1 < m; ++c1) + { + for (int c2 = 0; c2 < m; ++c2) + { // 注意:c1可以等于c2,表示同一列翻转两次 + auto copy = copyMatrix(matrix); + flipColumn(copy, c1); + flipColumn(copy, c2); + if (isAllZero(copy)) + return true; + } + } + return false; +} + +// 检查模式3:翻转一行一列 +bool checkOneRowOneColumn(const vector &matrix) +{ + int n = matrix.size(); + int m = matrix[0].size(); + + // 尝试所有可能的行列组合 + for (int r = 0; r < n; ++r) + { + for (int c = 0; c < m; ++c) + { + auto copy = copyMatrix(matrix); + flipRow(copy, r); + flipColumn(copy, c); + if (isAllZero(copy)) + return true; + } + } + return false; +} + +// 改进版的一行一列检查 +bool checkOneRowOneColumnOptimized(const vector &matrix) +{ + int n = matrix.size(); + int m = matrix[0].size(); + + // 特殊情况:如果只有一个1,肯定不能通过一行一列翻转得到全0 + int totalOnes = 0; + for (const auto &row : matrix) + { + for (char c : row) + { + if (c == '1') + totalOnes++; + } + } + + if (totalOnes == 1) + return false; + if (totalOnes == 0) + return true; + + // 普通情况继续暴力检查 + return checkOneRowOneColumn(matrix); +} + +int main() +{ + int T; + cin >> T; + while (T--) + { + int n, m; + cin >> n >> m; + vector matrix(n); + for (int i = 0; i < n; ++i) + { + cin >> matrix[i]; + } + + // 检查三种模式 + bool canMakeAllZero = checkTwoRowsOptimized(matrix) || checkTwoColumns(matrix) || checkOneRowOneColumnOptimized(matrix); + + cout << (canMakeAllZero ? "YES" : "NO") << endl; + } + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E_2.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E_2.cpp" index 8308e3fa..e3fb1d35 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E_2.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/E_2.cpp" @@ -1,157 +1,157 @@ -#include -#include -#include -using namespace std; - -int main() -{ - ios::sync_with_stdio(false); // 关闭 C/C++ 流同步,提升 cin/cout 性能 :contentReference[oaicite:1]{index=1} - cin.tie(nullptr); // 取消 cin 与 cout 绑定,避免不必要的刷新 :contentReference[oaicite:2]{index=2} - - int T; - cin >> T; - while (T--) - { - int n, m; - cin >> n >> m; - vector mat(n); - - for (int i = 0; i < n; i++) - { - cin >> mat[i]; - } - - // 计算每行和每列的1的个数 - vector row_ones(n, 0); - vector col_ones(m, 0); - - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { - if (mat[i][j] == '1') - { - row_ones[i]++; - col_ones[j]++; - } - } - } - - // 检查是否可以通过翻转两行得到全0矩阵 - bool two_rows_ok = false; - - // 两行全1,其余全0 => 翻转这两行可得全0 - vector full_rows; - for (int i = 0; i < n; i++) - { - if (row_ones[i] == m) - { - full_rows.push_back(i); - } - } - - if (full_rows.size() == 2) - { - bool others_zero = true; - for (int i = 0; i < n; i++) - { - if (i != full_rows[0] && i != full_rows[1] && row_ones[i] > 0) - { - others_zero = false; - break; - } - } - if (others_zero) - two_rows_ok = true; - } - - // 检查是否可以通过翻转两列得到全0矩阵 - bool two_cols_ok = false; - - // 两列全1,其余全0 => 翻转这两列可得全0 - vector full_cols; - for (int j = 0; j < m; j++) - { - if (col_ones[j] == n) - { - full_cols.push_back(j); - } - } - - if (full_cols.size() == 2) - { - bool others_zero = true; - for (int j = 0; j < m; j++) - { - if (j != full_cols[0] && j != full_cols[1] && col_ones[j] > 0) - { - others_zero = false; - break; - } - } - if (others_zero) - two_cols_ok = true; - } - - // 检查是否可以通过翻转一行一列得到全0矩阵 - bool row_col_ok = false; - - // 必要条件:找一个格子(r,c)是0,同时: - // 1. 第r行除(r,c)外都是1 - // 2. 第c列除(r,c)外都是1 - // 3. 其他位置都是0 - for (int r = 0; r < n && !row_col_ok; r++) - { - for (int c = 0; c < m && !row_col_ok; c++) - { - // 如果交点不是0,跳过 - if (mat[r][c] != '0') - continue; - - // 检查第r行是否除了(r,c)外都是1 - if (row_ones[r] != m - 1) - continue; - - // 检查第c列是否除了(r,c)外都是1 - if (col_ones[c] != n - 1) - continue; - - // 检查其他位置是否都是0 - bool other_positions_zero = true; - - for (int i = 0; i < n && other_positions_zero; i++) - { - for (int j = 0; j < m && other_positions_zero; j++) - { - // 跳过第r行和第c列 - if (i == r || j == c) - continue; - - if (mat[i][j] == '1') - { - other_positions_zero = false; - break; - } - } - } - - if (other_positions_zero) - { - row_col_ok = true; - } - } - } - - // 任一方式可行即可 - if (two_rows_ok || two_cols_ok || row_col_ok) - { - cout << "YES\n"; - } - else - { - cout << "NO\n"; - } - } - - return 0; -} +#include +#include +#include +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); // 关闭 C/C++ 流同步,提升 cin/cout 性能 :contentReference[oaicite:1]{index=1} + cin.tie(nullptr); // 取消 cin 与 cout 绑定,避免不必要的刷新 :contentReference[oaicite:2]{index=2} + + int T; + cin >> T; + while (T--) + { + int n, m; + cin >> n >> m; + vector mat(n); + + for (int i = 0; i < n; i++) + { + cin >> mat[i]; + } + + // 计算每行和每列的1的个数 + vector row_ones(n, 0); + vector col_ones(m, 0); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (mat[i][j] == '1') + { + row_ones[i]++; + col_ones[j]++; + } + } + } + + // 检查是否可以通过翻转两行得到全0矩阵 + bool two_rows_ok = false; + + // 两行全1,其余全0 => 翻转这两行可得全0 + vector full_rows; + for (int i = 0; i < n; i++) + { + if (row_ones[i] == m) + { + full_rows.push_back(i); + } + } + + if (full_rows.size() == 2) + { + bool others_zero = true; + for (int i = 0; i < n; i++) + { + if (i != full_rows[0] && i != full_rows[1] && row_ones[i] > 0) + { + others_zero = false; + break; + } + } + if (others_zero) + two_rows_ok = true; + } + + // 检查是否可以通过翻转两列得到全0矩阵 + bool two_cols_ok = false; + + // 两列全1,其余全0 => 翻转这两列可得全0 + vector full_cols; + for (int j = 0; j < m; j++) + { + if (col_ones[j] == n) + { + full_cols.push_back(j); + } + } + + if (full_cols.size() == 2) + { + bool others_zero = true; + for (int j = 0; j < m; j++) + { + if (j != full_cols[0] && j != full_cols[1] && col_ones[j] > 0) + { + others_zero = false; + break; + } + } + if (others_zero) + two_cols_ok = true; + } + + // 检查是否可以通过翻转一行一列得到全0矩阵 + bool row_col_ok = false; + + // 必要条件:找一个格子(r,c)是0,同时: + // 1. 第r行除(r,c)外都是1 + // 2. 第c列除(r,c)外都是1 + // 3. 其他位置都是0 + for (int r = 0; r < n && !row_col_ok; r++) + { + for (int c = 0; c < m && !row_col_ok; c++) + { + // 如果交点不是0,跳过 + if (mat[r][c] != '0') + continue; + + // 检查第r行是否除了(r,c)外都是1 + if (row_ones[r] != m - 1) + continue; + + // 检查第c列是否除了(r,c)外都是1 + if (col_ones[c] != n - 1) + continue; + + // 检查其他位置是否都是0 + bool other_positions_zero = true; + + for (int i = 0; i < n && other_positions_zero; i++) + { + for (int j = 0; j < m && other_positions_zero; j++) + { + // 跳过第r行和第c列 + if (i == r || j == c) + continue; + + if (mat[i][j] == '1') + { + other_positions_zero = false; + break; + } + } + } + + if (other_positions_zero) + { + row_col_ok = true; + } + } + } + + // 任一方式可行即可 + if (two_rows_ok || two_cols_ok || row_col_ok) + { + cout << "YES\n"; + } + else + { + cout << "NO\n"; + } + } + + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/F.cpp" "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/F.cpp" index 76dcebe8..ff34937a 100644 --- "a/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/F.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\345\221\250\350\265\23391/F.cpp" @@ -1,78 +1,78 @@ -#include -#include -using namespace std; - -const int MOD = 998244353; - -// 计算n!中2的次数 -int countPowerOf2(int n) -{ - int count = 0; - for (int i = 2; i <= n; i *= 2) - { - count += n / i; - } - return count; -} - -// 计算模逆元,用于分数取模 -int modInverse(int a, int m = MOD) -{ - int m0 = m, t, q; - int x0 = 0, x1 = 1; - if (m == 1) - return 0; - while (a > 1) - { - q = a / m; - t = m; - m = a % m; - a = t; - t = x0; - x0 = x1 - q * x0; - x1 = t; - } - if (x1 < 0) - x1 += m0; - return x1; -} - -int main() -{ - int T; - cin >> T; - vector results(T); - - for (int i = 0; i < T; i++) - { - int n; - cin >> n; - - if (n == 1) - { - // 1的因子只有1,概率为1 - results[i] = 1; - continue; - } - - // 计算n!中2的次数 - int powerOf2 = countPowerOf2(n); - - // 奇数因子的概率为1/(powerOf2+1) - int denominator = powerOf2 + 1; - int probability = (1LL * 1 * modInverse(denominator)) % MOD; - - results[i] = probability; - } - - // 输出结果 - for (int i = 0; i < T; i++) - { - cout << results[i]; - if (i < T - 1) - cout << " "; - } - cout << endl; - - return 0; -} +#include +#include +using namespace std; + +const int MOD = 998244353; + +// 计算n!中2的次数 +int countPowerOf2(int n) +{ + int count = 0; + for (int i = 2; i <= n; i *= 2) + { + count += n / i; + } + return count; +} + +// 计算模逆元,用于分数取模 +int modInverse(int a, int m = MOD) +{ + int m0 = m, t, q; + int x0 = 0, x1 = 1; + if (m == 1) + return 0; + while (a > 1) + { + q = a / m; + t = m; + m = a % m; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) + x1 += m0; + return x1; +} + +int main() +{ + int T; + cin >> T; + vector results(T); + + for (int i = 0; i < T; i++) + { + int n; + cin >> n; + + if (n == 1) + { + // 1的因子只有1,概率为1 + results[i] = 1; + continue; + } + + // 计算n!中2的次数 + int powerOf2 = countPowerOf2(n); + + // 奇数因子的概率为1/(powerOf2+1) + int denominator = powerOf2 + 1; + int probability = (1LL * 1 * modInverse(denominator)) % MOD; + + results[i] = probability; + } + + // 输出结果 + for (int i = 0; i < T; i++) + { + cout << results[i]; + if (i < T - 1) + cout << " "; + } + cout << endl; + + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/B.py" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/B.py" index ca60b406..c4121ec7 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/B.py" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/B.py" @@ -22,7 +22,7 @@ def solve(): print("NO\n") return print("YES\n") - + if __name__ == "__main__": sys.setrecursionlimit(10**7) t = 1 diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/C.py" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/C.py" index 7285598a..567512d8 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/C.py" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233 151/C.py" @@ -25,7 +25,7 @@ def solve(): input() print("Yes") return - + s.sort() mn,mx = s[0],s[-1] diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/A_\345\260\217s\347\232\204\347\255\276\345\210\260\351\242\230/1.cpp" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/A_\345\260\217s\347\232\204\347\255\276\345\210\260\351\242\230/1.cpp" index be3e32e4..1ce1094b 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/A_\345\260\217s\347\232\204\347\255\276\345\210\260\351\242\230/1.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/A_\345\260\217s\347\232\204\347\255\276\345\210\260\351\242\230/1.cpp" @@ -1,31 +1,31 @@ -#include -#include -#include -using namespace std; - -int main() -{ - int n; - cin >> n; - vector ids(n); - for (int i = 0; i < n; ++i) - { - cin >> ids[i]; - } - int max_pass = -1; - char ans = 'Z' + 1; - for (int i = 0; i < n; ++i) - { - string s; - cin >> s; - int div = s.find('/'); - int a = stoi(s.substr(0, div)); - if (a > max_pass || (a == max_pass && ids[i] < ans)) - { - max_pass = a; - ans = ids[i]; - } - } - cout << ans << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +using namespace std; + +int main() +{ + int n; + cin >> n; + vector ids(n); + for (int i = 0; i < n; ++i) + { + cin >> ids[i]; + } + int max_pass = -1; + char ans = 'Z' + 1; + for (int i = 0; i < n; ++i) + { + string s; + cin >> s; + int div = s.find('/'); + int a = stoi(s.substr(0, div)); + if (a > max_pass || (a == max_pass && ids[i] < ans)) + { + max_pass = a; + ans = ids[i]; + } + } + cout << ans << endl; + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/1.cpp" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/1.cpp" index d23349f9..d7582be6 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/1.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/1.cpp" @@ -1,46 +1,46 @@ -#include -#include -#include -using namespace std; - -int main() -{ - int n, m; - cin >> n >> m; - vector r(n), c(m); - for (int i = 0; i < n; ++i) - cin >> r[i]; - for (int i = 0; i < m; ++i) - cin >> c[i]; - sort(r.begin(), r.end(), greater()); - sort(c.begin(), c.end(), greater()); - - long long ans = 0; - - // 枚举操作次数,选择i个行操作,(n+m-i)个列操作 - for (int i = 0; i <= min(n, n + m); ++i) - { - int j = n + m - i; // 列覆盖数量 - if (j > m) - continue; // 列覆盖超过m个,不合法 - - // 找出i个最大的行和j个最大的列 - long long sum = 0; - - // 计算总和: 未被列覆盖的i个行的贡献 + j个列的总贡献 - 重叠区域的行贡献 - for (int k = 0; k < i; ++k) - { - sum += r[k] * (m - j); // 行未被列覆盖的部分 - } - - for (int k = 0; k < j; ++k) - { - sum += c[k] * n; // 列完全覆盖 - } - - ans = max(ans, sum); - } - - cout << ans << endl; - return 0; -} +#include +#include +#include +using namespace std; + +int main() +{ + int n, m; + cin >> n >> m; + vector r(n), c(m); + for (int i = 0; i < n; ++i) + cin >> r[i]; + for (int i = 0; i < m; ++i) + cin >> c[i]; + sort(r.begin(), r.end(), greater()); + sort(c.begin(), c.end(), greater()); + + long long ans = 0; + + // 枚举操作次数,选择i个行操作,(n+m-i)个列操作 + for (int i = 0; i <= min(n, n + m); ++i) + { + int j = n + m - i; // 列覆盖数量 + if (j > m) + continue; // 列覆盖超过m个,不合法 + + // 找出i个最大的行和j个最大的列 + long long sum = 0; + + // 计算总和: 未被列覆盖的i个行的贡献 + j个列的总贡献 - 重叠区域的行贡献 + for (int k = 0; k < i; ++k) + { + sum += r[k] * (m - j); // 行未被列覆盖的部分 + } + + for (int k = 0; k < j; ++k) + { + sum += c[k] * n; // 列完全覆盖 + } + + ans = max(ans, sum); + } + + cout << ans << endl; + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/2.cpp" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/2.cpp" index 774ea5fb..1d6f7e3d 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/2.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/B_\350\241\214\345\210\227\346\224\271\345\206\231/2.cpp" @@ -1,134 +1,134 @@ -#include -#include -#include -#include -using namespace std; - -const int MAXN = 1e5 + 5; -vector adj[MAXN]; // 邻接表表示树 -int dist[MAXN]; // 用于BFS计算距离 - -// 计算从start到所有其他点的距离 -void bfs(int start) -{ - fill(dist, dist + MAXN, -1); - queue q; - q.push(start); - dist[start] = 0; - - while (!q.empty()) - { - int curr = q.front(); - q.pop(); - - for (int next : adj[curr]) - { - if (dist[next] == -1) - { - dist[next] = dist[curr] + 1; - q.push(next); - } - } - } -} - -// 找出节点u到节点target的最短路径上的下一个节点 -int next_on_path(int u, int target) -{ - bfs(target); // 计算从target到所有点的距离 - - int min_dist = 1e9; - int next_node = -1; - - for (int v : adj[u]) - { - if (dist[v] < min_dist) - { - min_dist = dist[v]; - next_node = v; - } - } - - return next_node; -} - -int main() -{ - int n, k; - cin >> n >> k; - - // 读入树的边 - for (int i = 0; i < n - 1; ++i) - { - int u, v; - cin >> u >> v; - adj[u].push_back(v); - adj[v].push_back(u); - } - - // 模拟游戏过程 - int saki = k; // Saki的初始位置 - int miku = k; // Miku的初始位置 - int round = 0; - - while (true) - { - // 尝试所有Saki可能的移动方向 - bool can_survive = false; - int best_next_saki = -1; - int best_decoy = -1; - int best_next_miku = -1; - - for (int next_saki : adj[saki]) - { - // 尝试所有可能的替身位置 - for (int decoy : adj[next_saki]) - { - if (decoy == next_saki) - continue; // 替身不能和Saki在同一位置 - - // 计算Miku的下一步位置 - int next_miku; - if (miku == decoy) - { - next_miku = miku; // Miku停留不动 - } - else - { - next_miku = next_on_path(miku, decoy); - } - - // 如果Saki的新位置不与Miku的新位置重合,则可以继续游戏 - if (next_saki != next_miku) - { - can_survive = true; - best_next_saki = next_saki; - best_decoy = decoy; - best_next_miku = next_miku; - break; - } - } - if (can_survive) - break; - } - - if (!can_survive) - { - break; // 无法避免被Miku追上,游戏结束 - } - - // 执行最佳移动 - saki = best_next_saki; - miku = best_next_miku; - round++; - - // 如果Saki与Miku在同一位置,游戏结束 - if (saki == miku) - { - break; - } - } - - cout << round << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +#include +using namespace std; + +const int MAXN = 1e5 + 5; +vector adj[MAXN]; // 邻接表表示树 +int dist[MAXN]; // 用于BFS计算距离 + +// 计算从start到所有其他点的距离 +void bfs(int start) +{ + fill(dist, dist + MAXN, -1); + queue q; + q.push(start); + dist[start] = 0; + + while (!q.empty()) + { + int curr = q.front(); + q.pop(); + + for (int next : adj[curr]) + { + if (dist[next] == -1) + { + dist[next] = dist[curr] + 1; + q.push(next); + } + } + } +} + +// 找出节点u到节点target的最短路径上的下一个节点 +int next_on_path(int u, int target) +{ + bfs(target); // 计算从target到所有点的距离 + + int min_dist = 1e9; + int next_node = -1; + + for (int v : adj[u]) + { + if (dist[v] < min_dist) + { + min_dist = dist[v]; + next_node = v; + } + } + + return next_node; +} + +int main() +{ + int n, k; + cin >> n >> k; + + // 读入树的边 + for (int i = 0; i < n - 1; ++i) + { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + adj[v].push_back(u); + } + + // 模拟游戏过程 + int saki = k; // Saki的初始位置 + int miku = k; // Miku的初始位置 + int round = 0; + + while (true) + { + // 尝试所有Saki可能的移动方向 + bool can_survive = false; + int best_next_saki = -1; + int best_decoy = -1; + int best_next_miku = -1; + + for (int next_saki : adj[saki]) + { + // 尝试所有可能的替身位置 + for (int decoy : adj[next_saki]) + { + if (decoy == next_saki) + continue; // 替身不能和Saki在同一位置 + + // 计算Miku的下一步位置 + int next_miku; + if (miku == decoy) + { + next_miku = miku; // Miku停留不动 + } + else + { + next_miku = next_on_path(miku, decoy); + } + + // 如果Saki的新位置不与Miku的新位置重合,则可以继续游戏 + if (next_saki != next_miku) + { + can_survive = true; + best_next_saki = next_saki; + best_decoy = decoy; + best_next_miku = next_miku; + break; + } + } + if (can_survive) + break; + } + + if (!can_survive) + { + break; // 无法避免被Miku追上,游戏结束 + } + + // 执行最佳移动 + saki = best_next_saki; + miku = best_next_miku; + round++; + + // 如果Saki与Miku在同一位置,游戏结束 + if (saki == miku) + { + break; + } + } + + cout << round << endl; + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/C_\346\233\277\350\272\253/1.cpp" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/C_\346\233\277\350\272\253/1.cpp" index 64bd1fbe..dcfc44bf 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/C_\346\233\277\350\272\253/1.cpp" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233138/C_\346\233\277\350\272\253/1.cpp" @@ -1,148 +1,148 @@ -#include -#include -#include -#include -using namespace std; - -const int MAXN = 1e5 + 5; -vector adj[MAXN]; // 邻接表表示树 -int dist[MAXN]; // 用于BFS计算距离 - -// 计算从start到所有其他点的距离,并返回距离数组 -void bfs(int start) -{ - fill(dist, dist + MAXN, -1); - queue q; - q.push(start); - dist[start] = 0; - - while (!q.empty()) - { - int curr = q.front(); - q.pop(); - - for (int next : adj[curr]) - { - if (dist[next] == -1) - { - dist[next] = dist[curr] + 1; - q.push(next); - } - } - } -} - -// 找出节点u到节点v的最短路径上的下一个节点 -int next_on_path(int u, int v) -{ - if (u == v) - return u; // 已经到达目标 - - // 计算从目标v到所有点的距离 - bfs(v); - - // 选择u的邻居中距离v最近的 - int min_dist = INT_MAX; - int next_node = -1; - - for (int neighbor : adj[u]) - { - if (dist[neighbor] < min_dist) - { - min_dist = dist[neighbor]; - next_node = neighbor; - } - } - - return next_node; -} - -int main() -{ - ios::sync_with_stdio(false); - cin.tie(nullptr); - - int n, k; - cin >> n >> k; - - // 读入树的边 - for (int i = 0; i < n - 1; ++i) - { - int u, v; - cin >> u >> v; - adj[u].push_back(v); - adj[v].push_back(u); - } - - // 如果起始点只有一个邻居,那么Saki无法生存第一回合 - if (adj[k].size() <= 1) - { - cout << 0 << endl; - return 0; - } - - // 模拟游戏过程 - int saki = k; // Saki的初始位置 - int miku = k; // Miku的初始位置 - int rounds = 0; - - while (true) - { - // 尝试Saki的所有可能移动 - bool can_survive = false; - int best_next_saki = -1; - int best_next_miku = -1; - - for (int next_saki : adj[saki]) - { - // 对于每个可能的下一步位置,尝试所有可能的替身位置 - for (int decoy : adj[next_saki]) - { - if (decoy == next_saki) - continue; // 替身不能和Saki在同一位置 - - // 计算Miku的下一步位置 - int next_miku; - if (miku == decoy) - { - next_miku = miku; // Miku停留不动 - } - else - { - next_miku = next_on_path(miku, decoy); - } - - // 如果Saki的新位置不与Miku的新位置重合,则可以继续游戏 - if (next_saki != next_miku) - { - can_survive = true; - best_next_saki = next_saki; - best_next_miku = next_miku; - break; - } - } - if (can_survive) - break; - } - - // 如果无法找到安全的移动方式,下一回合将无法进行 - if (!can_survive) - { - break; - } - - // 执行最佳移动 - saki = best_next_saki; - miku = best_next_miku; - rounds++; // 成功完成一个回合 - - // 如果回合结束后Saki与Miku在同一位置,游戏结束 - if (saki == miku) - { - break; - } - } - - cout << rounds << endl; - return 0; -} \ No newline at end of file +#include +#include +#include +#include +using namespace std; + +const int MAXN = 1e5 + 5; +vector adj[MAXN]; // 邻接表表示树 +int dist[MAXN]; // 用于BFS计算距离 + +// 计算从start到所有其他点的距离,并返回距离数组 +void bfs(int start) +{ + fill(dist, dist + MAXN, -1); + queue q; + q.push(start); + dist[start] = 0; + + while (!q.empty()) + { + int curr = q.front(); + q.pop(); + + for (int next : adj[curr]) + { + if (dist[next] == -1) + { + dist[next] = dist[curr] + 1; + q.push(next); + } + } + } +} + +// 找出节点u到节点v的最短路径上的下一个节点 +int next_on_path(int u, int v) +{ + if (u == v) + return u; // 已经到达目标 + + // 计算从目标v到所有点的距离 + bfs(v); + + // 选择u的邻居中距离v最近的 + int min_dist = INT_MAX; + int next_node = -1; + + for (int neighbor : adj[u]) + { + if (dist[neighbor] < min_dist) + { + min_dist = dist[neighbor]; + next_node = neighbor; + } + } + + return next_node; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + // 读入树的边 + for (int i = 0; i < n - 1; ++i) + { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + adj[v].push_back(u); + } + + // 如果起始点只有一个邻居,那么Saki无法生存第一回合 + if (adj[k].size() <= 1) + { + cout << 0 << endl; + return 0; + } + + // 模拟游戏过程 + int saki = k; // Saki的初始位置 + int miku = k; // Miku的初始位置 + int rounds = 0; + + while (true) + { + // 尝试Saki的所有可能移动 + bool can_survive = false; + int best_next_saki = -1; + int best_next_miku = -1; + + for (int next_saki : adj[saki]) + { + // 对于每个可能的下一步位置,尝试所有可能的替身位置 + for (int decoy : adj[next_saki]) + { + if (decoy == next_saki) + continue; // 替身不能和Saki在同一位置 + + // 计算Miku的下一步位置 + int next_miku; + if (miku == decoy) + { + next_miku = miku; // Miku停留不动 + } + else + { + next_miku = next_on_path(miku, decoy); + } + + // 如果Saki的新位置不与Miku的新位置重合,则可以继续游戏 + if (next_saki != next_miku) + { + can_survive = true; + best_next_saki = next_saki; + best_next_miku = next_miku; + break; + } + } + if (can_survive) + break; + } + + // 如果无法找到安全的移动方式,下一回合将无法进行 + if (!can_survive) + { + break; + } + + // 执行最佳移动 + saki = best_next_saki; + miku = best_next_miku; + rounds++; // 成功完成一个回合 + + // 如果回合结束后Saki与Miku在同一位置,游戏结束 + if (saki == miku) + { + break; + } + } + + cout << rounds << endl; + return 0; +} diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/A.py" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/A.py" index 3940ffec..ef09dde2 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/A.py" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/A.py" @@ -19,11 +19,11 @@ def solve(): print(n-1) #total一定是奇数 else: print(total - n) - + if __name__ == "__main__": sys.setrecursionlimit(10**7) t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/B.py" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/B.py" index f90217e3..13c0930c 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/B.py" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/B.py" @@ -34,4 +34,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/C.py" "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/C.py" index 5d0dd448..08ec6d82 100644 --- "a/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/C.py" +++ "b/niuke/contest/\347\211\233\345\256\242\347\273\203\344\271\240\350\265\233146/C.py" @@ -19,7 +19,7 @@ def solve(): out.append("Yes" if s in traps else "No") print("\n".join(out)) return - + # 取出所有陷阱出现的时间 t_list = [t for t,_ in traps] @@ -48,7 +48,7 @@ def solve(): l = alpha if L < l: l = r = 0 - else: + else: if(L&1) == (alpha & 1): r = L else: @@ -70,7 +70,7 @@ def solve(): out.append("Yes") else: out.append("No") - + else: L_total = s_fixed_min R_total = s_fixed_max diff --git "a/niuke/daily_problem/25_10-15\351\255\224\345\257\274\345\270\210\346\231\250\346\213\245/1.cpp" "b/niuke/daily_problem/25_10-15\351\255\224\345\257\274\345\270\210\346\231\250\346\213\245/1.cpp" index 6c6a42bd..1f9750c3 100644 --- "a/niuke/daily_problem/25_10-15\351\255\224\345\257\274\345\270\210\346\231\250\346\213\245/1.cpp" +++ "b/niuke/daily_problem/25_10-15\351\255\224\345\257\274\345\270\210\346\231\250\346\213\245/1.cpp" @@ -1,20 +1,21 @@ -#include - -using namespace std; - -int main() { - int n, m; - cin >> n >> m; - vector a(n); - int damage = 2; - int damage2hero = 0; - for (int i = 0; i < n; ++i)cin >> a[i]; - while(m--){ - for (int i = 0; i < n; ++i) { - a[i] -= damage; - if (a[i] == 0)damage++; - } - damage2hero += damage; - } - cout << damage2hero << endl; -} +#include +#include + +using namespace std; + +int main() { + int n, m; + cin >> n >> m; + vector a(n); + int damage = 2; + int damage2hero = 0; + for (int i = 0; i < n; ++i)cin >> a[i]; + while(m--){ + for (int i = 0; i < n; ++i) { + a[i] -= damage; + if (a[i] == 0)damage++; + } + damage2hero += damage; + } + cout << damage2hero << endl; +} diff --git "a/niuke/daily_problem/25_10-17\346\234\200\345\260\217\345\276\252\347\216\257\350\212\202/1.cpp" "b/niuke/daily_problem/25_10-17\346\234\200\345\260\217\345\276\252\347\216\257\350\212\202/1.cpp" index 9cba0e2d..3e340f75 100644 --- "a/niuke/daily_problem/25_10-17\346\234\200\345\260\217\345\276\252\347\216\257\350\212\202/1.cpp" +++ "b/niuke/daily_problem/25_10-17\346\234\200\345\260\217\345\276\252\347\216\257\350\212\202/1.cpp" @@ -1,18 +1,18 @@ - -#include -using namespace std; - -typedef long long ll; - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - - string str; - cin >> str; - - set s(str.begin(), str.end()); - cout << s.size() << '\n'; - cout.flush(); - - return 0; -} \ No newline at end of file + +#include +using namespace std; + +typedef long long ll; + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + + string str; + cin >> str; + + set s(str.begin(), str.end()); + cout << s.size() << '\n'; + cout.flush(); + + return 0; +} diff --git "a/niuke/daily_problem/25_10-18\347\277\273\344\271\213/2.cpp" "b/niuke/daily_problem/25_10-18\347\277\273\344\271\213/2.cpp" index 5c9ff213..65b5fa83 100644 --- "a/niuke/daily_problem/25_10-18\347\277\273\344\271\213/2.cpp" +++ "b/niuke/daily_problem/25_10-18\347\277\273\344\271\213/2.cpp" @@ -1,39 +1,40 @@ - -#include -using namespace std; - -typedef long long ll; - -vector matrix; -unordered_map mp; - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - - int n, m; - cin >> n >> m; - - matrix.resize(n); - - cin.ignore(); - for (int i = 0; i < n; ++i)cin >> matrix[i]; - - for (int j = 0; j < m; ++j) { - string s = ""; - for (int i = 0; i < n; ++i) { - s += matrix[i][j]; - } - mp[s]++; - } - - int max_cnt = 0; - for (auto& [str, cnt] : mp) { - max_cnt = max(max_cnt, cnt); - } - - cout << max_cnt << '\n'; - - - - return 0; -} \ No newline at end of file +#include + +#include +using namespace std; + +typedef long long ll; + +vector matrix; +unordered_map mp; + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + + int n, m; + cin >> n >> m; + + matrix.resize(n); + + cin.ignore(); + for (int i = 0; i < n; ++i)cin >> matrix[i]; + + for (int j = 0; j < m; ++j) { + string s = ""; + for (int i = 0; i < n; ++i) { + s += matrix[i][j]; + } + mp[s]++; + } + + int max_cnt = 0; + for (auto& [str, cnt] : mp) { + max_cnt = max(max_cnt, cnt); + } + + cout << max_cnt << '\n'; + + + + return 0; +} diff --git "a/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/1.cpp" "b/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/1.cpp" index 3aaac212..978029f9 100644 --- "a/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/1.cpp" +++ "b/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/1.cpp" @@ -1,34 +1,35 @@ -#include -typedef long long ll; -using namespace std; - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - - int n; - cin >> n; - - vector a(n+1); - vector b(n + 1); - - for (int i = 1; i <= 2*n; ++i) { - int x; - cin >> x; - if (a[x] == 0)a[x] = i; - else b[x] = i; - } - - ll ans = 0; - for (int i = 1; i <= n; i++) { - ans += b[i] - a[i] - 1; - } - - sort(a.begin(), a.end()); - sort(b.begin(), b.end()); - if (a[n] > b[1])ans += 2 * (a[n] - b[1]); - - cout << ans << '\n'; - cout.flush(); - - return 0; -} +#include +#include +typedef long long ll; +using namespace std; + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + + int n; + cin >> n; + + vector a(n+1); + vector b(n + 1); + + for (int i = 1; i <= 2*n; ++i) { + int x; + cin >> x; + if (a[x] == 0)a[x] = i; + else b[x] = i; + } + + ll ans = 0; + for (int i = 1; i <= n; i++) { + ans += b[i] - a[i] - 1; + } + + sort(a.begin(), a.end()); + sort(b.begin(), b.end()); + if (a[n] > b[1])ans += 2 * (a[n] - b[1]); + + cout << ans << '\n'; + cout.flush(); + + return 0; +} diff --git "a/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/2.cpp" "b/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/2.cpp" index cc5e1649..3d3095a7 100644 --- "a/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/2.cpp" +++ "b/niuke/daily_problem/25_10-19Forsaken\347\273\231\345\255\246\347\224\237\345\210\206\347\273\204/2.cpp" @@ -1,27 +1,28 @@ -#include -#include -using namespace std; -#define ll long long -int main() -{ - int n; - cin>>n; - vector a(n+1); - vector b(n+1); - for(int i=1;i<=2*n;i++){ - int x; - cin>>x; - if(a[x]==0) a[x]=i; - else b[x]=i; - } - ll ans=0; - for(int i=1;i<=n;i++){ - ans+=b[i]-a[i]-1; - } - sort(a.begin(),a.end()); - sort(b.begin(),b.end()); - if(a[n]>b[1]) - ans+=2*(a[n]-b[1]); - cout< +#include +#include +using namespace std; +#define ll long long +int main() +{ + int n; + cin>>n; + vector a(n+1); + vector b(n+1); + for(int i=1;i<=2*n;i++){ + int x; + cin>>x; + if(a[x]==0) a[x]=i; + else b[x]=i; + } + ll ans=0; + for(int i=1;i<=n;i++){ + ans+=b[i]-a[i]-1; + } + sort(a.begin(),a.end()); + sort(b.begin(),b.end()); + if(a[n]>b[1]) + ans+=2*(a[n]-b[1]); + cout< -using namespace std; - -typedef long long ll; - -void flip(vector& arr, int k) { - reverse(arr.begin(), arr.begin() + k); -} - -int pancakeSort(vector& arr) { - int n = arr.size(); - int count = 0; - - for (int currSize = n; currSize > 1; currSize--) { - int maxidx = 0; - for (int i = 1; i < currSize; ++i) { - if (arr[i] > arr[maxidx])maxidx = i; - } - - if (maxidx == currSize - 1)continue; - - if (maxidx > 0) { - flip(arr, maxidx + 1); - count++; - } - - flip(arr, currSize); - count++; - } - - return count; - -} - -int main() { - ios::sync_with_stdio(false), cin.tie(0); - - int n; - cin >> n; - - vector redius(n); - for (int i = 0; i < n; i++)cin >> redius[i]; - - cout << pancakeSort(redius) << '\n'; - - return 0; -} \ No newline at end of file +#include + +#include +using namespace std; + +typedef long long ll; + +void flip(vector& arr, int k) { + reverse(arr.begin(), arr.begin() + k); +} + +int pancakeSort(vector& arr) { + int n = arr.size(); + int count = 0; + + for (int currSize = n; currSize > 1; currSize--) { + int maxidx = 0; + for (int i = 1; i < currSize; ++i) { + if (arr[i] > arr[maxidx])maxidx = i; + } + + if (maxidx == currSize - 1)continue; + + if (maxidx > 0) { + flip(arr, maxidx + 1); + count++; + } + + flip(arr, currSize); + count++; + } + + return count; + +} + +int main() { + ios::sync_with_stdio(false), cin.tie(0); + + int n; + cin >> n; + + vector redius(n); + for (int i = 0; i < n; i++)cin >> redius[i]; + + cout << pancakeSort(redius) << '\n'; + + return 0; +} diff --git "a/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/bfs.cpp" "b/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/bfs.cpp" index 4b664f43..f3905ed6 100644 --- "a/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/bfs.cpp" +++ "b/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/bfs.cpp" @@ -1,50 +1,50 @@ -#include -using namespace std; - -int a[1010]; - -int n; -int mi; -int check() { - int cnt = 0, i; - for (i = 2; i <= n + 1; i++) { - if (abs(a[i] - a[i - 1]) != 1)cnt++; - } - return cnt; -} -int dep; -void dfs(int p, int cnt) { - // cout< dep || mi)return; - if (check() == 0) { - mi = dep; - return; - } - // for(i=1;i<=n;++i)cout<mp; - int j = 1, i; - cin >> n; - for (i = 1; i <= n; i++)cin >> a[i], mp[a[i]] = 1; - for (auto i : mp)mp[i.first] = j++; - for (i = 1; i <= n; i++)a[i] = mp[a[i]]; - a[n + 1] = n + 1; - mi = 0; - dep = check(); - while (!mi)dfs(0, 0), dep++; - cout << mi; -} \ No newline at end of file +#include +using namespace std; + +int a[1010]; + +int n; +int mi; +int check() { + int cnt = 0, i; + for (i = 2; i <= n + 1; i++) { + if (abs(a[i] - a[i - 1]) != 1)cnt++; + } + return cnt; +} +int dep; +void dfs(int p, int cnt) { + // cout< dep || mi)return; + if (check() == 0) { + mi = dep; + return; + } + // for(i=1;i<=n;++i)cout<mp; + int j = 1, i; + cin >> n; + for (i = 1; i <= n; i++)cin >> a[i], mp[a[i]] = 1; + for (auto i : mp)mp[i.first] = j++; + for (i = 1; i <= n; i++)a[i] = mp[a[i]]; + a[n + 1] = n + 1; + mi = 0; + dep = check(); + while (!mi)dfs(0, 0), dep++; + cout << mi; +} diff --git "a/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/ida.cpp" "b/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/ida.cpp" index 7fc7000b..de3030b9 100644 --- "a/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/ida.cpp" +++ "b/niuke/daily_problem/25_10-21\351\223\201\347\233\230\346\225\264\347\220\206/others/ida.cpp" @@ -52,4 +52,4 @@ int main() { // while(!res)dfs(0,0),dep++; cout << mi; // cout< -using namespace std; - -typedef long long ll; - -int n; -vector cost; -vector> recipe; - -bool is_valid(int mask){ - for (int i = 0; i < n;++i){ - if(!(mask & (1<> n; - - cost.resize(n); - for (int i = 0; i < n;++i) - cin >> cost[i]; - - recipe.resize(n); - for (int i = 0; i < n;++i) - cin >> recipe[i].first >> recipe[i].second; - - ll min_cost = LLONG_MAX; - - // enumerate all the possible combinations of red potions - for (int mask = 0; mask < (1 << n);++mask){ - if(!is_valid(mask)){ - continue; - } - - ll total_cost = calc_cost(mask); - - min_cost = min(min_cost, total_cost); - } - - cout << min_cost << '\n'; - - return 0; -} \ No newline at end of file +#include +// brute solution,有问题 +#include +using namespace std; + +typedef long long ll; + +int n; +vector cost; +vector> recipe; + +bool is_valid(int mask){ + for (int i = 0; i < n;++i){ + if(!(mask & (1<> n; + + cost.resize(n); + for (int i = 0; i < n;++i) + cin >> cost[i]; + + recipe.resize(n); + for (int i = 0; i < n;++i) + cin >> recipe[i].first >> recipe[i].second; + + ll min_cost = LLONG_MAX; + + // enumerate all the possible combinations of red potions + for (int mask = 0; mask < (1 << n);++mask){ + if(!is_valid(mask)){ + continue; + } + + ll total_cost = calc_cost(mask); + + min_cost = min(min_cost, total_cost); + } + + cout << min_cost << '\n'; + + return 0; +} diff --git "a/niuke/daily_problem/25_10-25\345\260\217\350\213\257\347\232\204\346\225\260\345\255\227\345\220\210\345\271\266/1.cpp" "b/niuke/daily_problem/25_10-25\345\260\217\350\213\257\347\232\204\346\225\260\345\255\227\345\220\210\345\271\266/1.cpp" index f62cff68..6cdade53 100644 --- "a/niuke/daily_problem/25_10-25\345\260\217\350\213\257\347\232\204\346\225\260\345\255\227\345\220\210\345\271\266/1.cpp" +++ "b/niuke/daily_problem/25_10-25\345\260\217\350\213\257\347\232\204\346\225\260\345\255\227\345\220\210\345\271\266/1.cpp" @@ -1,30 +1,30 @@ -// 不妨将第i个数是否与i+1合并设为一种状态,很明显有n-1种状态 -// 每种状态都有合并和不合并两种情况,那么一共就有2的n-1次方种可能 -#include -using namespace std; -#define MOD 998244353 - -// 快速幂算法 -long long q(long long a,long long b) { - long long ans = 1; - while(b>0) { - if(b&1)ans = a*ans%MOD; //判断b是否为奇数 - b = b>>1; - a = a*a%MOD; - } - return ans; -} -int main() -{ - ios::sync_with_stdio(false); - cin.tie(nullptr); - - int t;cin >> t;long long n,x; - while(t--) { - cin >> n; - for(int i = 0;i> x; - } - cout << q(2,n-1) <<'\n'; - } -} +// 不妨将第i个数是否与i+1合并设为一种状态,很明显有n-1种状态 +// 每种状态都有合并和不合并两种情况,那么一共就有2的n-1次方种可能 +#include +using namespace std; +#define MOD 998244353 + +// 快速幂算法 +long long q(long long a,long long b) { + long long ans = 1; + while(b>0) { + if(b&1)ans = a*ans%MOD; //判断b是否为奇数 + b = b>>1; + a = a*a%MOD; + } + return ans; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t;cin >> t;long long n,x; + while(t--) { + cin >> n; + for(int i = 0;i> x; + } + cout << q(2,n-1) <<'\n'; + } +} diff --git "a/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/1.cpp" "b/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/1.cpp" index a2690ec8..1eb044ab 100644 --- "a/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/1.cpp" +++ "b/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/1.cpp" @@ -1,84 +1,85 @@ -#include -using namespace std; - -int main() -{ - ios::sync_with_stdio(false); - cin.tie(0); - - int n; - cin >> n; - - vector degree(n + 1, 0); - - // 读入边并统计度数 - for (int i = 0; i < n - 1; i++) - { - int u, v; - cin >> u >> v; - degree[u]++; - degree[v]++; - } - - // 找出最大度数和次大度数 - int max_deg = 0, second_max_deg = 0; - int max_count = 0; // 有多少个节点的度数等于max_deg - - for (int i = 1; i <= n; i++) - { - if (degree[i] > max_deg) - { - second_max_deg = max_deg; - max_deg = degree[i]; - max_count = 1; - } - else if (degree[i] == max_deg) - { - max_count++; - } - else if (degree[i] > second_max_deg) - { - second_max_deg = degree[i]; - } - } - - // 计算每个节点作为根时的k值 - int min_k = INT_MAX; - int best_root = 1; - - for (int r = 1; r <= n; r++) - { - int k_r; - - if (degree[r] < max_deg) - { - // r不是最大度数节点 - k_r = max(degree[r], max_deg - 1); - } - else - { - // degree[r] == max_deg - if (max_count == 1) - { - // r是唯一的最大度数节点 - k_r = max(max_deg, second_max_deg - 1); - } - else - { - // 有多个最大度数节点 - k_r = max_deg; - } - } - - // 更新最优答案 - if (k_r < min_k || (k_r == min_k && r < best_root)) - { - min_k = k_r; - best_root = r; - } - } - - cout << min_k << " " << best_root << endl; - - return 0; -} +#include +#include +using namespace std; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(0); + + int n; + cin >> n; + + vector degree(n + 1, 0); + + // 读入边并统计度数 + for (int i = 0; i < n - 1; i++) + { + int u, v; + cin >> u >> v; + degree[u]++; + degree[v]++; + } + + // 找出最大度数和次大度数 + int max_deg = 0, second_max_deg = 0; + int max_count = 0; // 有多少个节点的度数等于max_deg + + for (int i = 1; i <= n; i++) + { + if (degree[i] > max_deg) + { + second_max_deg = max_deg; + max_deg = degree[i]; + max_count = 1; + } + else if (degree[i] == max_deg) + { + max_count++; + } + else if (degree[i] > second_max_deg) + { + second_max_deg = degree[i]; + } + } + + // 计算每个节点作为根时的k值 + int min_k = INT_MAX; + int best_root = 1; + + for (int r = 1; r <= n; r++) + { + int k_r; + + if (degree[r] < max_deg) + { + // r不是最大度数节点 + k_r = max(degree[r], max_deg - 1); + } + else + { + // degree[r] == max_deg + if (max_count == 1) + { + // r是唯一的最大度数节点 + k_r = max(max_deg, second_max_deg - 1); + } + else + { + // 有多个最大度数节点 + k_r = max_deg; + } + } + + // 更新最优答案 + if (k_r < min_k || (k_r == min_k && r < best_root)) + { + min_k = k_r; + best_root = r; + } + } + + cout << min_k << " " << best_root << endl; + + return 0; +} diff --git "a/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/2.cpp" "b/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/2.cpp" index 206dcf67..4d29710b 100644 --- "a/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/2.cpp" +++ "b/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/2.cpp" @@ -1,58 +1,59 @@ -#include -using namespace std; - -#define ll long long - -int main() -{ - ios::sync_with_stdio(false); - cin.tie(0); - - int n; - cin >> n; - vector degree(n + 1, 0); - - for (int i = 0; i < n - 1; ++i) - { - int u, v; - cin >> u >> v; - degree[u]++; - degree[v]++; - } - - int max_deg = 0,second_max_deg = 0; - int max_count = 0; - - for (int i = 1; i <= n;++i){ - if(degree[i] > max_deg){ - second_max_deg = max_deg; - max_deg = degree[i]; - max_count = 1; - }else if(degree[i] == max_deg) - max_count++; - else if(degree[i] > second_max_deg) - second_max_deg = degree[i]; - } - - int min_k = INT_MAX; - int best_root = 1; - - for (int r = 1;r<=n;r++){ - int k_r; - if(degree[r] < max_deg){ - k_r = max(degree[r],max_deg - 1); - }else{ - if(max_count == 1) - k_r = max(max_deg,second_max_deg - 1); - else - k_r = max_deg; - } - - if(k_r < min_k || (k_r == min_k && r < best_root)){ - min_k = k_r; - best_root = r; - } - } - cout << min_k << " " << best_root << endl; - return 0; -} \ No newline at end of file +#include +#include +using namespace std; + +#define ll long long + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(0); + + int n; + cin >> n; + vector degree(n + 1, 0); + + for (int i = 0; i < n - 1; ++i) + { + int u, v; + cin >> u >> v; + degree[u]++; + degree[v]++; + } + + int max_deg = 0,second_max_deg = 0; + int max_count = 0; + + for (int i = 1; i <= n;++i){ + if(degree[i] > max_deg){ + second_max_deg = max_deg; + max_deg = degree[i]; + max_count = 1; + }else if(degree[i] == max_deg) + max_count++; + else if(degree[i] > second_max_deg) + second_max_deg = degree[i]; + } + + int min_k = INT_MAX; + int best_root = 1; + + for (int r = 1;r<=n;r++){ + int k_r; + if(degree[r] < max_deg){ + k_r = max(degree[r],max_deg - 1); + }else{ + if(max_count == 1) + k_r = max(max_deg,second_max_deg - 1); + else + k_r = max_deg; + } + + if(k_r < min_k || (k_r == min_k && r < best_root)){ + min_k = k_r; + best_root = r; + } + } + cout << min_k << " " << best_root << endl; + return 0; +} diff --git "a/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/3.cpp" "b/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/3.cpp" index a8d6f3af..d6ad164f 100644 --- "a/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/3.cpp" +++ "b/niuke/daily_problem/25_10-26\346\231\272\344\271\203\347\232\204\342\200\234K\342\200\235\345\217\211\346\240\221/3.cpp" @@ -1,34 +1,34 @@ -#include -using namespace std; -const int maxn = 1e5 + 10; -int edge[maxn]; - -int main() -{ - int n; - cin >> n; - int ui, vi; - for (int i = 1; i <= n - 1; i++) - { - cin >> ui >> vi; - edge[ui]++; - edge[vi]++; - } - int ans = INT_MIN; - for (int i = 1; i <= n; i++) - { - ans = max(ans, edge[i]); - } - int number = 1; - while (edge[number] == ans) - { - number++; - } - - if (n <= 2) - cout << "1 1"; - else - cout << ans - 1 << ' ' << number; - - return 0; -} +#include +using namespace std; +const int maxn = 1e5 + 10; +int edge[maxn]; + +int main() +{ + int n; + cin >> n; + int ui, vi; + for (int i = 1; i <= n - 1; i++) + { + cin >> ui >> vi; + edge[ui]++; + edge[vi]++; + } + int ans = INT_MIN; + for (int i = 1; i <= n; i++) + { + ans = max(ans, edge[i]); + } + int number = 1; + while (edge[number] == ans) + { + number++; + } + + if (n <= 2) + cout << "1 1"; + else + cout << ans - 1 << ' ' << number; + + return 0; +} diff --git "a/niuke/daily_problem/25_10-27\346\212\242\347\272\242\345\214\205/1.cpp" "b/niuke/daily_problem/25_10-27\346\212\242\347\272\242\345\214\205/1.cpp" index 99c7739a..15c5643e 100644 --- "a/niuke/daily_problem/25_10-27\346\212\242\347\272\242\345\214\205/1.cpp" +++ "b/niuke/daily_problem/25_10-27\346\212\242\347\272\242\345\214\205/1.cpp" @@ -1,45 +1,45 @@ -#include -using namespace std; - -typedef long long ll; - -const ll mod = 1e9 + 7; - -// 我们每次到下一个人的时候,对应的期望都会变成原来的一半,所以对应的结果是w/2^k mod 1e9+7 -// 快速幂 -// 计算 base^exp % mod -ll power(ll base, ll exp) -{ - ll res = 1; - base %= mod; - while (exp > 0) - { - if (exp & 1) - res = res * base % mod; - base = base * base % mod; - exp >>= 1; - } - return res; -} - -int main() -{ - ios::sync_with_stdio(false); - cin.tie(0); - - ll w, n, k; - cin >> w >> n >> k; - - // 计算 2 在模 mod 意义下的乘法逆元,逆元可以类比线性代数中求逆矩阵 - ll inv2 = power(2, mod - 2); - - // 计算 (2^k) 在模 mod 意义下的乘法逆元,即 (2 的模逆元)^k - ll inv2_k = power(inv2, k); - - // 期望 = (W * (2^k 的模逆元)) % mod - ll ans = (w * inv2_k) % mod; - - cout << ans << endl; - - return 0; -} +#include +using namespace std; + +typedef long long ll; + +const ll mod = 1e9 + 7; + +// 我们每次到下一个人的时候,对应的期望都会变成原来的一半,所以对应的结果是w/2^k mod 1e9+7 +// 快速幂 +// 计算 base^exp % mod +ll power(ll base, ll exp) +{ + ll res = 1; + base %= mod; + while (exp > 0) + { + if (exp & 1) + res = res * base % mod; + base = base * base % mod; + exp >>= 1; + } + return res; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(0); + + ll w, n, k; + cin >> w >> n >> k; + + // 计算 2 在模 mod 意义下的乘法逆元,逆元可以类比线性代数中求逆矩阵 + ll inv2 = power(2, mod - 2); + + // 计算 (2^k) 在模 mod 意义下的乘法逆元,即 (2 的模逆元)^k + ll inv2_k = power(inv2, k); + + // 期望 = (W * (2^k 的模逆元)) % mod + ll ans = (w * inv2_k) % mod; + + cout << ans << endl; + + return 0; +} diff --git "a/niuke/daily_problem/25_10-28\351\200\201\345\210\206\351\242\230/1.cpp" "b/niuke/daily_problem/25_10-28\351\200\201\345\210\206\351\242\230/1.cpp" index 820a177d..6a5b4517 100644 --- "a/niuke/daily_problem/25_10-28\351\200\201\345\210\206\351\242\230/1.cpp" +++ "b/niuke/daily_problem/25_10-28\351\200\201\345\210\206\351\242\230/1.cpp" @@ -35,4 +35,4 @@ int main() cout << f(n) << endl; return 0; >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1 copy.cpp" "b/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1 copy.cpp" index 7c47a3cd..92814490 100644 --- "a/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1 copy.cpp" +++ "b/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1 copy.cpp" @@ -1,3 +1,4 @@ +#include <<<<<<< HEAD #include using namespace std; diff --git "a/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1.cpp" "b/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1.cpp" index 79110732..5edcfd2f 100644 --- "a/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1.cpp" +++ "b/niuke/daily_problem/25_10-29\345\260\217\350\213\257\347\232\204IDE\346\213\254\345\217\267\351\227\256\351\242\230\357\274\210easy\357\274\211/1.cpp" @@ -33,4 +33,4 @@ int main() return 0; >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_10-30\346\225\260\345\210\227\345\220\216\347\274\200\346\236\201\345\244\247\344\275\215\347\275\256\347\273\237\350\256\241/1.cpp" "b/niuke/daily_problem/25_10-30\346\225\260\345\210\227\345\220\216\347\274\200\346\236\201\345\244\247\344\275\215\347\275\256\347\273\237\350\256\241/1.cpp" index c84b62e9..713f07a2 100644 --- "a/niuke/daily_problem/25_10-30\346\225\260\345\210\227\345\220\216\347\274\200\346\236\201\345\244\247\344\275\215\347\275\256\347\273\237\350\256\241/1.cpp" +++ "b/niuke/daily_problem/25_10-30\346\225\260\345\210\227\345\220\216\347\274\200\346\236\201\345\244\247\344\275\215\347\275\256\347\273\237\350\256\241/1.cpp" @@ -135,4 +135,4 @@ int main() return 0; >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_10-31\347\211\233\347\211\233\347\232\204\345\212\240\346\263\225/1.cpp" "b/niuke/daily_problem/25_10-31\347\211\233\347\211\233\347\232\204\345\212\240\346\263\225/1.cpp" index c99a8f71..ce57e675 100644 --- "a/niuke/daily_problem/25_10-31\347\211\233\347\211\233\347\232\204\345\212\240\346\263\225/1.cpp" +++ "b/niuke/daily_problem/25_10-31\347\211\233\347\211\233\347\232\204\345\212\240\346\263\225/1.cpp" @@ -15,13 +15,13 @@ int main() bool ok = false; // 标记是否已经开始输出非零数字(用于跳过前导零) std::string ans = ""; // 存储结果字符串 - + // 逐位相加(不考虑进位) for (int i = 0; i < n; i += 1) { int x = a[i] - '0', y = b[i] - '0'; // 将字符转换为数字 int c = (x + y) % 10; // 相加后对10取模(不考虑进位) - + // 如果当前位不为0,或者已经开始输出非零数字 if (c || ok) { @@ -56,13 +56,13 @@ int main() bool ok = false; // 标记是否已经开始输出非零数字(用于跳过前导零) std::string ans = ""; // 存储结果字符串 - + // 逐位相加(不考虑进位) for (int i = 0; i < n; i += 1) { int x = a[i] - '0', y = b[i] - '0'; // 将字符转换为数字 int c = (x + y) % 10; // 相加后对10取模(不考虑进位) - + // 如果当前位不为0,或者已经开始输出非零数字 if (c || ok) { diff --git "a/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.cpp" "b/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.cpp" index cc043d80..b632656a 100644 --- "a/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.cpp" +++ "b/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.cpp" @@ -1,3 +1,4 @@ +#include // 哈希表加排序,输出频次从高到低,频次相同的按照字典序升序进行排序 #include using namespace std; @@ -57,7 +58,7 @@ int main() { cout << keyword.getWord() << '\n'; } - + return 0; } diff --git "a/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.py" "b/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.py" index 66f60cbb..90308058 100644 --- "a/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.py" +++ "b/niuke/daily_problem/25_11-10\345\260\217\347\272\242\344\271\246\346\216\250\350\215\220\347\263\273\347\273\237/1.py" @@ -19,4 +19,4 @@ keywords.sort(key = lambda x:(-x[1],x[0])) for word,count in keywords: - print(word) \ No newline at end of file + print(word) diff --git "a/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1.cpp" "b/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1.cpp" index 0fbcf7f0..cd5a3a70 100644 --- "a/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1.cpp" +++ "b/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; @@ -48,4 +49,4 @@ int main() cout << win << ' ' << lose << ' ' << draw << '\n'; return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1_1.cpp" "b/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1_1.cpp" index 359357e7..81d90ab7 100644 --- "a/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1_1.cpp" +++ "b/niuke/daily_problem/25_11-11GCPC\346\200\273\345\206\263\350\265\233/1_1.cpp" @@ -1,3 +1,4 @@ +#include #include #include using namespace std; diff --git "a/niuke/daily_problem/25_11-12\347\211\233\347\211\233\347\232\204\346\236\204\351\200\240/1.cpp" "b/niuke/daily_problem/25_11-12\347\211\233\347\211\233\347\232\204\346\236\204\351\200\240/1.cpp" index 5a643d5f..432e34f2 100644 --- "a/niuke/daily_problem/25_11-12\347\211\233\347\211\233\347\232\204\346\236\204\351\200\240/1.cpp" +++ "b/niuke/daily_problem/25_11-12\347\211\233\347\211\233\347\232\204\346\236\204\351\200\240/1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/niuke/daily_problem/25_11-13\345\260\217\345\277\203\347\201\253\347\203\233\347\232\204\346\255\252/2.cpp" "b/niuke/daily_problem/25_11-13\345\260\217\345\277\203\347\201\253\347\203\233\347\232\204\346\255\252/2.cpp" index 071ee461..79726350 100644 --- "a/niuke/daily_problem/25_11-13\345\260\217\345\277\203\347\201\253\347\203\233\347\232\204\346\255\252/2.cpp" +++ "b/niuke/daily_problem/25_11-13\345\260\217\345\277\203\347\201\253\347\203\233\347\232\204\346\255\252/2.cpp" @@ -120,4 +120,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/1.cpp" "b/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/1.cpp" index 59a0a4fd..74cdab0a 100644 --- "a/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/1.cpp" +++ "b/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/1.cpp" @@ -1,3 +1,4 @@ +#include // 最小生成树的问题,我们先使用prim算法 // 实际上,这个题更像是TSP问题,使用状态压缩+记忆化搜索 diff --git "a/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2.cpp" "b/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2.cpp" index 8fddb779..47fb92bf 100644 --- "a/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2.cpp" +++ "b/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; @@ -66,4 +67,4 @@ int main() cout << "The shortest path has length " << answer << "\n"; } return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2_2.cpp" "b/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2_2.cpp" index 100fe69e..8a61267b 100644 --- "a/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2_2.cpp" +++ "b/niuke/daily_problem/25_11-17\346\224\266\351\233\206\347\272\270\347\211\207/2_2.cpp" @@ -34,4 +34,4 @@ int main() } while (next_permutation(p + 1, p + n + 1)); // 枚举1~n的全排列 printf("The shortest path has length %d\n", ans); } -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.cpp" "b/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.cpp" index c5d78cfa..59f2cbdc 100644 --- "a/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.cpp" +++ "b/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.py" "b/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.py" index 311a8bbd..5238463c 100644 --- "a/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.py" +++ "b/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1.py" @@ -37,14 +37,14 @@ def precompute_factorials(): def solve(): global n,bit - + precompute_factorials() first_line = input().strip() if not first_line: return - + n_str,q_str = first_line.split() n,q = int(n_str),int(q_str) @@ -77,7 +77,7 @@ def solve(): bit = [0]*(n+1) for i in range(1,n+1): update(i,1) - + rank = 0 for i in range(n): rank += query(p[i]-1)*fact[n-1-i] @@ -88,4 +88,4 @@ def solve(): if __name__ == "__main__": - solve() \ No newline at end of file + solve() diff --git "a/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1_1.py" "b/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1_1.py" index 58b557d7..c81ac93e 100644 --- "a/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1_1.py" +++ "b/niuke/daily_problem/25_11-18\345\245\266\347\211\233\346\216\222\346\216\222\347\253\231/1_1.py" @@ -21,7 +21,7 @@ def find_kth(k): pos = 0 current_sum = 0 log_n = n.bit_length() - 1 - + for i in range(log_n, -1, -1): step = 1 << i if pos + step <= n and current_sum + bit[pos + step] < k: @@ -37,15 +37,15 @@ def precompute_factorials(): def main(): global n, bit input = sys.stdin.readline - + precompute_factorials() - + first_line = input().strip() if not first_line: return n_str, q_str = first_line.split() n, q = int(n_str), int(q_str) - + for _ in range(q): type_line = input().strip() data_line = input().strip() @@ -57,17 +57,17 @@ def main(): bit = [0] * (n + 1) for i in range(1, n + 1): update(i, 1) - + p = [] for i in range(n): divisor = fact[n - 1 - i] quotient = k // divisor k %= divisor - + num = find_kth(quotient + 1) p.append(num) update(num, -1) - + print(*p) else: # type_line == 'Q' diff --git "a/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1.cpp" "b/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1.cpp" index a706462d..a6942133 100644 --- "a/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1.cpp" +++ "b/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -171,4 +172,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1_1.cpp" "b/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1_1.cpp" index 207fda9d..57060f6e 100644 --- "a/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1_1.cpp" +++ "b/niuke/daily_problem/25_11-19\346\227\205\346\270\270/1_1.cpp" @@ -1,3 +1,4 @@ +#include // FZANOTFOUND #include using namespace std; diff --git "a/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1.cpp" "b/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1.cpp" index 52d448bf..e478c032 100644 --- "a/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1.cpp" +++ "b/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -61,13 +62,13 @@ il void solve(){ int main() { fastio - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1_1.cpp" "b/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1_1.cpp" index 425cec2c..c3e87a12 100644 --- "a/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1_1.cpp" +++ "b/niuke/daily_problem/25_11-20\346\235\245\347\202\271gcd/1_1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; using ll = long long; diff --git "a/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.cpp" "b/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.cpp" index f58ba079..08c49bbe 100644 --- "a/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.cpp" +++ "b/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.cpp" @@ -79,4 +79,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.py" "b/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.py" index 2488a824..64b87405 100644 --- "a/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.py" +++ "b/niuke/daily_problem/25_11-21\345\260\217\350\213\257\347\232\204\350\256\241\347\256\227\345\274\217/1.py" @@ -23,4 +23,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/daily_problem/25_11-22\345\260\217A\345\274\271\345\220\211\344\273\226/1.cpp" "b/niuke/daily_problem/25_11-22\345\260\217A\345\274\271\345\220\211\344\273\226/1.cpp" index a8e5763e..add452f3 100644 --- "a/niuke/daily_problem/25_11-22\345\260\217A\345\274\271\345\220\211\344\273\226/1.cpp" +++ "b/niuke/daily_problem/25_11-22\345\260\217A\345\274\271\345\220\211\344\273\226/1.cpp" @@ -47,13 +47,13 @@ il void solve(){ int main() { fastio - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-24\350\203\275\351\207\217\350\276\220\345\260\204/1.cpp" "b/niuke/daily_problem/25_11-24\350\203\275\351\207\217\350\276\220\345\260\204/1.cpp" index a44f247c..144b8187 100644 --- "a/niuke/daily_problem/25_11-24\350\203\275\351\207\217\350\276\220\345\260\204/1.cpp" +++ "b/niuke/daily_problem/25_11-24\350\203\275\351\207\217\350\276\220\345\260\204/1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -64,13 +65,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/1.cpp" "b/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/1.cpp" index 7864e6b0..262f5123 100644 --- "a/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/1.cpp" +++ "b/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/1.cpp" @@ -17,22 +17,22 @@ const double PI = 3.1415926; il void solve(){ int a,b,x,y; cin >> a >> b >> x >> y; - + int ans = 1e9; // 枚举使用烈焰风暴的次数k int max_k = max((a + y - 1) / y, (b + y - 1) / y) + 1; - + for(int k = 0; k <= max_k; k++){ // 计算使用k次烈焰风暴后的剩余血量 int rem_a = max(0, a - k * y); int rem_b = max(0, b - k * y); - + // 如果两只都死了 if(rem_a == 0 && rem_b == 0){ ans = min(ans, k); continue; } - + // 计算需要额外火球术的次数 int fireball_needed = 0; if(rem_a > 0 && rem_b > 0){ @@ -45,23 +45,23 @@ il void solve(){ // 只有第二只活着 fireball_needed = (rem_b + x - 1) / x; } - + ans = min(ans, k + fireball_needed); } - + cout << ans << endl; } int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/2.cpp" "b/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/2.cpp" index 2f888ff9..7c86edf1 100644 --- "a/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/2.cpp" +++ "b/niuke/daily_problem/25_11-26\345\260\217\347\272\242\346\235\200\346\200\252/2.cpp" @@ -34,13 +34,13 @@ il void solve(){ int main() { fastio - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_11-27\345\260\217sun\347\232\204\345\201\207\346\234\237/1.cpp" "b/niuke/daily_problem/25_11-27\345\260\217sun\347\232\204\345\201\207\346\234\237/1.cpp" index cb9e5b5f..69a1bcd2 100644 --- "a/niuke/daily_problem/25_11-27\345\260\217sun\347\232\204\345\201\207\346\234\237/1.cpp" +++ "b/niuke/daily_problem/25_11-27\345\260\217sun\347\232\204\345\201\207\346\234\237/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,7 +13,7 @@ typedef long long ll; void solve(){ int n, m; cin >> n >> m; - + vector> segs; for(int i = 0; i < m; ++i) { int l, r; diff --git "a/niuke/daily_problem/25_11-28\350\277\267\351\200\224\344\271\213\345\256\266\347\232\204\345\244\247\350\264\244\350\200\205/1.cpp" "b/niuke/daily_problem/25_11-28\350\277\267\351\200\224\344\271\213\345\256\266\347\232\204\345\244\247\350\264\244\350\200\205/1.cpp" index d177332e..e9463854 100644 --- "a/niuke/daily_problem/25_11-28\350\277\267\351\200\224\344\271\213\345\256\266\347\232\204\345\244\247\350\264\244\350\200\205/1.cpp" +++ "b/niuke/daily_problem/25_11-28\350\277\267\351\200\224\344\271\213\345\256\266\347\232\204\345\244\247\350\264\244\350\200\205/1.cpp" @@ -25,13 +25,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_11-29\345\260\217\345\244\251\347\232\204 Minecraft/1.cpp" "b/niuke/daily_problem/25_11-29\345\260\217\345\244\251\347\232\204 Minecraft/1.cpp" index 30b5b7be..d1ab99bd 100644 --- "a/niuke/daily_problem/25_11-29\345\260\217\345\244\251\347\232\204 Minecraft/1.cpp" +++ "b/niuke/daily_problem/25_11-29\345\260\217\345\244\251\347\232\204 Minecraft/1.cpp" @@ -19,7 +19,7 @@ void solve() cout << fixed << setprecision(10) << 0.0 << endl; return; } - + double p_a = a / 16.0; double p_b = b / 16.0; double p_c = c / 16.0; diff --git "a/niuke/daily_problem/25_11-2\350\200\225\347\247\215\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" "b/niuke/daily_problem/25_11-2\350\200\225\347\247\215\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" index ed12cd82..961cd8a7 100644 --- "a/niuke/daily_problem/25_11-2\350\200\225\347\247\215\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" +++ "b/niuke/daily_problem/25_11-2\350\200\225\347\247\215\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" @@ -1,52 +1,52 @@ -#include -using namespace std; - -// 题意(简述): -// 给定 n 颗种子的初始等级 a[i]。每轮操作: -// 1) 等级 a[i] 变为 ceil(a[i] / 3)(用 (a[i] + 2) / 3 实现向上取整) -// 2) 对应种子数量 s[i] 翻倍 -// 问:在所有轮次(包括初始时刻)中,等级恰为 x 的种子数量之最大值。 - -int n; // 种子数量 -int a[100010]; // a[i]:第 i 颗种子的当前等级 -int s[100010]; // s[i]:第 i 颗种子对应的数量(每轮翻倍),初始化为 1 -int x; // 目标等级 -int main() -{ - cin >> n; // 读入种子数量 - for (int i = 1; i <= n; ++i) - { - cin >> a[i]; - s[i] = 1; // 初始每颗种子数量为 1 - } - cin >> x; // 目标等级 - long long int ans = 0; // 历史最大“等级为 x 的种子数量” - for (int i = 1; i <= n; ++i) - { // 统计初始时刻等级为 x 的种子数量 - if (a[i] == x) - { - ans += s[i]; - } - } - int t; // 标记:本轮结束后是否仍存在等级 > x 的种子(决定是否继续模拟) - do - { // 模拟一轮种植(等级衰减且数量翻倍) - t = 0; - long long res = 0; // 记录本轮“等级恰为 x”的种子数量总和 - for (int i = 1; i <= n; ++i) - { - a[i] = (a[i] + 2) / 3; // 等级变为 ceil(a[i]/3) - s[i] *= 2; // 数量翻倍 - if (a[i] > x) - { - t = 1; // 仍有等级大于 x,需要继续下一轮 - } - if (a[i] == x) - { - res += s[i]; // 累计本轮等级为 x 的数量 - } - } - ans = max(res, ans); // 维护历史最大值 - } while (t); - cout << ans << endl; // 输出答案 -} \ No newline at end of file +#include +using namespace std; + +// 题意(简述): +// 给定 n 颗种子的初始等级 a[i]。每轮操作: +// 1) 等级 a[i] 变为 ceil(a[i] / 3)(用 (a[i] + 2) / 3 实现向上取整) +// 2) 对应种子数量 s[i] 翻倍 +// 问:在所有轮次(包括初始时刻)中,等级恰为 x 的种子数量之最大值。 + +int n; // 种子数量 +int a[100010]; // a[i]:第 i 颗种子的当前等级 +int s[100010]; // s[i]:第 i 颗种子对应的数量(每轮翻倍),初始化为 1 +int x; // 目标等级 +int main() +{ + cin >> n; // 读入种子数量 + for (int i = 1; i <= n; ++i) + { + cin >> a[i]; + s[i] = 1; // 初始每颗种子数量为 1 + } + cin >> x; // 目标等级 + long long int ans = 0; // 历史最大“等级为 x 的种子数量” + for (int i = 1; i <= n; ++i) + { // 统计初始时刻等级为 x 的种子数量 + if (a[i] == x) + { + ans += s[i]; + } + } + int t; // 标记:本轮结束后是否仍存在等级 > x 的种子(决定是否继续模拟) + do + { // 模拟一轮种植(等级衰减且数量翻倍) + t = 0; + long long res = 0; // 记录本轮“等级恰为 x”的种子数量总和 + for (int i = 1; i <= n; ++i) + { + a[i] = (a[i] + 2) / 3; // 等级变为 ceil(a[i]/3) + s[i] *= 2; // 数量翻倍 + if (a[i] > x) + { + t = 1; // 仍有等级大于 x,需要继续下一轮 + } + if (a[i] == x) + { + res += s[i]; // 累计本轮等级为 x 的数量 + } + } + ans = max(res, ans); // 维护历史最大值 + } while (t); + cout << ans << endl; // 输出答案 +} diff --git "a/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/1.cpp" "b/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/1.cpp" index 4c0f3369..a8df1100 100644 --- "a/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/1.cpp" +++ "b/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/1.cpp" @@ -74,13 +74,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/2.cpp" "b/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/2.cpp" index 0dbcf95c..d56cedb8 100644 --- "a/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/2.cpp" +++ "b/niuke/daily_problem/25_11-30\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\344\277\256\346\224\271\357\274\210\344\270\200\357\274\211/2.cpp" @@ -2,7 +2,7 @@ #include #include #include -#define ll long long +#define ll long long using namespace std; // 使用lower_bound会先找到比自己小的第一个元素的 @@ -43,7 +43,7 @@ int main() { vp.insert(temp); res = 2+b-a; } - else + else { auto it = vp.lower_bound(temp); if (it == vp.begin()) @@ -63,7 +63,7 @@ int main() { res = max(b-a+2,res); } } - else + else { auto it1 = it; it--; diff --git "a/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/1.cpp" "b/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/1.cpp" index 61ab338c..adde7ca4 100644 --- "a/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/1.cpp" +++ "b/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/1.cpp" @@ -1,3 +1,4 @@ +#include <<<<<<< HEAD #include using namespace std; @@ -5,11 +6,11 @@ using namespace std; /** * 题目思路:挡住洪水问题 * 核心算法:DFS/BFS 边界淹没法 - * + * * 问题描述:给定一个 n×m 的网格,'0' 表示空地,'*' 表示墙 * 洪水从边界开始蔓延,遇到墙会被挡住 * 求最少需要在多少个空地上建墙,才能阻止洪水蔓延到整个区域 - * + * * 解题思路: * 1. 从四条边界开始,对所有能被洪水淹没的 '0' 进行标记(变成 '*') * 2. 剩余未被标记的 '0' 就是需要建墙的位置(因为它们无法被边界洪水直接淹没) @@ -21,7 +22,7 @@ using namespace std; * @param grid 网格地图 * @param x 当前位置的行坐标 * @param y 当前位置的列坐标 - * + * * 函数逻辑: * - 边界条件:越界或遇到墙('*')则返回 * - 递归处理:将当前 '0' 标记为 '*'(表示被洪水淹没),然后向上下左右四个方向继续蔓延 @@ -30,16 +31,16 @@ void bfs(vector& grid, int x, int y) { int n = grid.size(); // 网格行数 int m = grid[0].size(); // 网格列数 - + // 边界检查:越界或遇到墙则停止蔓延 if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == '*') { return; } - + // 将当前空地标记为墙(表示被洪水淹没) grid[x][y] = '*'; - + // 向四个方向递归蔓延(上、左、下、右) bfs(grid, x-1, y); // 上 bfs(grid, x, y-1); // 左 @@ -51,7 +52,7 @@ int main() { // 读取网格的行数 n 和列数 m int n, m; cin >> n >> m; - + // 创建 n×m 的字符串数组,存储网格地图 vector grid(n); for (int i = 0; i < n; ++i) @@ -88,7 +89,7 @@ int main() { bfs(grid, n-1, j); } } - + // ========== 第二步:统计内部剩余的空地数量 ========== // 经过边界淹没后,内部剩余的 '0' 就是需要建墙的位置 int ans = 0; @@ -102,7 +103,7 @@ int main() { } } } - + // 输出答案 cout << ans; } @@ -113,11 +114,11 @@ using namespace std; /** * 题目思路:挡住洪水问题 * 核心算法:DFS/BFS 边界淹没法 - * + * * 问题描述:给定一个 n×m 的网格,'0' 表示空地,'*' 表示墙 * 洪水从边界开始蔓延,遇到墙会被挡住 * 求最少需要在多少个空地上建墙,才能阻止洪水蔓延到整个区域 - * + * * 解题思路: * 1. 从四条边界开始,对所有能被洪水淹没的 '0' 进行标记(变成 '*') * 2. 剩余未被标记的 '0' 就是需要建墙的位置(因为它们无法被边界洪水直接淹没) @@ -129,7 +130,7 @@ using namespace std; * @param grid 网格地图 * @param x 当前位置的行坐标 * @param y 当前位置的列坐标 - * + * * 函数逻辑: * - 边界条件:越界或遇到墙('*')则返回 * - 递归处理:将当前 '0' 标记为 '*'(表示被洪水淹没),然后向上下左右四个方向继续蔓延 @@ -138,16 +139,16 @@ void bfs(vector& grid, int x, int y) { int n = grid.size(); // 网格行数 int m = grid[0].size(); // 网格列数 - + // 边界检查:越界或遇到墙则停止蔓延 if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == '*') { return; } - + // 将当前空地标记为墙(表示被洪水淹没) grid[x][y] = '*'; - + // 向四个方向递归蔓延(上、左、下、右) bfs(grid, x-1, y); // 上 bfs(grid, x, y-1); // 左 @@ -159,7 +160,7 @@ int main() { // 读取网格的行数 n 和列数 m int n, m; cin >> n >> m; - + // 创建 n×m 的字符串数组,存储网格地图 vector grid(n); for (int i = 0; i < n; ++i) @@ -196,7 +197,7 @@ int main() { bfs(grid, n-1, j); } } - + // ========== 第二步:统计内部剩余的空地数量 ========== // 经过边界淹没后,内部剩余的 '0' 就是需要建墙的位置 int ans = 0; @@ -210,7 +211,7 @@ int main() { } } } - + // 输出答案 cout << ans; } diff --git "a/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/2.cpp" "b/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/2.cpp" index f21a8966..29bf5578 100644 --- "a/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/2.cpp" +++ "b/niuke/daily_problem/25_11-3\346\214\241\344\275\217\346\264\252\346\260\264/2.cpp" @@ -14,13 +14,13 @@ int main() bool ok = false; // 标记是否已经开始输出非零数字(用于跳过前导零) std::string ans = ""; // 存储结果字符串 - + // 逐位相加(不考虑进位) for (int i = 0; i < n; i += 1) { int x = a[i] - '0', y = b[i] - '0'; // 将字符转换为数字 int c = (x + y) % 10; // 相加后对10取模(不考虑进位) - + // 如果当前位不为0,或者已经开始输出非零数字 if (c || ok) { diff --git "a/niuke/daily_problem/25_11-4\347\273\204\351\230\237/1.cpp" "b/niuke/daily_problem/25_11-4\347\273\204\351\230\237/1.cpp" index cd418d7d..4d229ce1 100644 --- "a/niuke/daily_problem/25_11-4\347\273\204\351\230\237/1.cpp" +++ "b/niuke/daily_problem/25_11-4\347\273\204\351\230\237/1.cpp" @@ -16,14 +16,14 @@ int main() while(T--){ int n, k, i, a[MAXN], l, r, longest = 0; cin >> n >> k; // n: 人数, k: 能力值差的最大值 - + // 读入每个人的能力值 for (int i = 0; i < n;++i) cin >> a[i]; - + // 对能力值进行排序,方便使用双指针 sort(a, a + n); - + // 双指针/滑动窗口:找满足条件的最长区间 // l 是左指针,r 是右指针 for (l = 0, r = 0; l < n; l++){ @@ -32,12 +32,12 @@ int main() // 因为数组已排序,所以 a[r] - a[l] 就是区间的最大差值 while(r < n && a[r] - a[l] <= k) r++; - + // 更新最长区间的长度 // r - l 是以 l 为左端点时,满足条件的最长区间长度 longest = max(longest, r - l); } - + // 输出最长区间的长度,即最多可以组队的人数 cout << longest << '\n'; } @@ -61,14 +61,14 @@ int main() while(T--){ int n, k, i, a[MAXN], l, r, longest = 0; cin >> n >> k; // n: 人数, k: 能力值差的最大值 - + // 读入每个人的能力值 for (int i = 0; i < n;++i) cin >> a[i]; - + // 对能力值进行排序,方便使用双指针 sort(a, a + n); - + // 双指针/滑动窗口:找满足条件的最长区间 // l 是左指针,r 是右指针 for (l = 0, r = 0; l < n; l++){ @@ -77,16 +77,16 @@ int main() // 因为数组已排序,所以 a[r] - a[l] 就是区间的最大差值 while(r < n && a[r] - a[l] <= k) r++; - + // 更新最长区间的长度 // r - l 是以 l 为左端点时,满足条件的最长区间长度 longest = max(longest, r - l); } - + // 输出最长区间的长度,即最多可以组队的人数 cout << longest << '\n'; } return 0; >>>>>>> 6948676579abca6ba4f3824e53edf9bbb7e1b238 -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/1.cpp" "b/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/1.cpp" index 3cd5fa5e..fbb8b13e 100644 --- "a/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/1.cpp" +++ "b/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/1.cpp" @@ -85,4 +85,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/2.cpp" "b/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/2.cpp" index a5b66558..e3ce5c2c 100644 --- "a/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/2.cpp" +++ "b/niuke/daily_problem/25_11-5\345\233\236\346\226\207\346\227\245\346\234\237/2.cpp" @@ -10,7 +10,7 @@ int main() for (int j = 1; j <= up[i]; j++) { - // 回文日期 = (日期倒置)(月份倒置)(月份)(日期) + // 回文日期 = (日期倒置)(月份倒置)(月份)(日期) // = D₂D₁M₂M₁ + M₁M₂ + D₁D₂ = YYYY + MM + DD int y = j % 10 * 1e7 // 日期个位 → 年份千万位 + j / 10 * 1e6 // 日期十位 → 年份百万位 diff --git "a/niuke/daily_problem/25_11-6\343\200\220\346\250\241\346\235\277\343\200\221\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214/1.cpp" "b/niuke/daily_problem/25_11-6\343\200\220\346\250\241\346\235\277\343\200\221\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214/1.cpp" index 505b3ee5..a028de0d 100644 --- "a/niuke/daily_problem/25_11-6\343\200\220\346\250\241\346\235\277\343\200\221\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214/1.cpp" +++ "b/niuke/daily_problem/25_11-6\343\200\220\346\250\241\346\235\277\343\200\221\344\272\214\347\273\264\345\211\215\347\274\200\345\222\214/1.cpp" @@ -23,7 +23,7 @@ int main() { { scanf("%d", &tmp); mat[i][j] = mat[i-1][j]+mat[i][j-1]-mat[i-1][j-1]+tmp; - + } } int x1, y1, x2, y2; diff --git "a/niuke/daily_problem/25_12-10\345\244\247\346\222\222\345\270\201/1.cpp" "b/niuke/daily_problem/25_12-10\345\244\247\346\222\222\345\270\201/1.cpp" index 3ee62a8f..e277ba35 100644 --- "a/niuke/daily_problem/25_12-10\345\244\247\346\222\222\345\270\201/1.cpp" +++ "b/niuke/daily_problem/25_12-10\345\244\247\346\222\222\345\270\201/1.cpp" @@ -27,13 +27,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-12\345\260\217\347\272\242\347\232\204\345\257\206\347\240\201\344\277\256\346\224\271/1.cpp" "b/niuke/daily_problem/25_12-12\345\260\217\347\272\242\347\232\204\345\257\206\347\240\201\344\277\256\346\224\271/1.cpp" index f133c6d5..4017dd1a 100644 --- "a/niuke/daily_problem/25_12-12\345\260\217\347\272\242\347\232\204\345\257\206\347\240\201\344\277\256\346\224\271/1.cpp" +++ "b/niuke/daily_problem/25_12-12\345\260\217\347\272\242\347\232\204\345\257\206\347\240\201\344\277\256\346\224\271/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include @@ -57,19 +58,19 @@ il void solve(){ ans += i[j] * 65; } cout << ans << endl; - + } int main() { fastio; - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/1.cpp" "b/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/1.cpp" index 8684f5ed..d3a15576 100644 --- "a/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/1.cpp" +++ "b/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/1.cpp" @@ -56,7 +56,7 @@ il void solve() { for (int i = 0; i < n; ++i) { int x; cin >> x; - cout << ans[x - 1] << endl; + cout << ans[x - 1] << endl; } } diff --git "a/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/2.cpp" "b/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/2.cpp" index ca81f3cc..3b7c7767 100644 --- "a/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/2.cpp" +++ "b/niuke/daily_problem/25_12-13\346\231\272\344\271\203\347\232\204\346\225\260\345\255\227/2.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' diff --git "a/niuke/daily_problem/25_12-15\346\234\210\346\234\210\346\237\245\345\215\216\345\215\216\347\232\204\346\211\213\346\234\272/1.cpp" "b/niuke/daily_problem/25_12-15\346\234\210\346\234\210\346\237\245\345\215\216\345\215\216\347\232\204\346\211\213\346\234\272/1.cpp" index e93448a4..9087693c 100644 --- "a/niuke/daily_problem/25_12-15\346\234\210\346\234\210\346\237\245\345\215\216\345\215\216\347\232\204\346\211\213\346\234\272/1.cpp" +++ "b/niuke/daily_problem/25_12-15\346\234\210\346\234\210\346\237\245\345\215\216\345\215\216\347\232\204\346\211\213\346\234\272/1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' @@ -57,6 +58,6 @@ int main() { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-1\346\236\204\351\200\240\346\225\260\347\213\254/1.cpp" "b/niuke/daily_problem/25_12-1\346\236\204\351\200\240\346\225\260\347\213\254/1.cpp" index 27178ec6..0f2ec38f 100644 --- "a/niuke/daily_problem/25_12-1\346\236\204\351\200\240\346\225\260\347\213\254/1.cpp" +++ "b/niuke/daily_problem/25_12-1\346\236\204\351\200\240\346\225\260\347\213\254/1.cpp" @@ -38,13 +38,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-2\345\260\217\347\272\242\347\232\204 gcd/1.cpp" "b/niuke/daily_problem/25_12-2\345\260\217\347\272\242\347\232\204 gcd/1.cpp" index 9b372064..4e4d8b44 100644 --- "a/niuke/daily_problem/25_12-2\345\260\217\347\272\242\347\232\204 gcd/1.cpp" +++ "b/niuke/daily_problem/25_12-2\345\260\217\347\272\242\347\232\204 gcd/1.cpp" @@ -24,13 +24,13 @@ il void solve(){ ll b; cin >> str; cin >> b; - + ll ans = 0; for(char c : str) { ans = (ans * 10 + (c - '0')) % b; } - + ll res = gcd(ans, b); cout << res << endl; } @@ -38,13 +38,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-3\346\270\270\346\270\270\347\232\204\345\255\227\347\254\246\351\207\215\346\216\222/1.cpp" "b/niuke/daily_problem/25_12-3\346\270\270\346\270\270\347\232\204\345\255\227\347\254\246\351\207\215\346\216\222/1.cpp" index 2b0ac44a..0cd32903 100644 --- "a/niuke/daily_problem/25_12-3\346\270\270\346\270\270\347\232\204\345\255\227\347\254\246\351\207\215\346\216\222/1.cpp" +++ "b/niuke/daily_problem/25_12-3\346\270\270\346\270\270\347\232\204\345\255\227\347\254\246\351\207\215\346\216\222/1.cpp" @@ -39,13 +39,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/1.cpp" "b/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/1.cpp" index 76cfcec8..774b31e1 100644 --- "a/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/1.cpp" +++ "b/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/1.cpp" @@ -9,8 +9,8 @@ using namespace std; typedef long long ll; const ll N = 10000006; -int primes[N],j,s[N+1]; -bool isPrime[N+1]; +int primes[N],j,s[N+1]; +bool isPrime[N+1]; void eulerSieve() { @@ -21,7 +21,7 @@ void eulerSieve() primes[++j] = i; s[i]++; } - for(ll q = 1;q<=j && primes[q] * i <= N;++q) + for(ll q = 1;q<=j && primes[q] * i <= N;++q) { isPrime[primes[q]*i] = 1; if(!(i%primes[q])) @@ -47,15 +47,15 @@ il void solve(){ int main() { fastio - + eulerSieve(); // 调用筛法初始化 - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/2.cpp" "b/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/2.cpp" index d4bbfc1a..606b407d 100644 --- "a/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/2.cpp" +++ "b/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/2.cpp" @@ -1,3 +1,4 @@ +#include #include #include #define il inline @@ -12,7 +13,7 @@ typedef long long ll; const ll N = 10000006; vector primes; vector s(N+1); -vector isPrime(N+1,true); +vector isPrime(N+1,true); void eulerSieve() { @@ -25,7 +26,7 @@ void eulerSieve() primes.push_back(i); s[i]++; } - for(ll q = 0;q> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/prime_sieve_examples.cpp" "b/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/prime_sieve_examples.cpp" index bda52329..3d6e2c60 100644 --- "a/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/prime_sieve_examples.cpp" +++ "b/niuke/daily_problem/25_12-4\350\264\250\346\225\260\347\273\237\350\256\241/prime_sieve_examples.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include @@ -31,13 +32,13 @@ const int MAXN = 10000006; vector eratosthenesSieve(int n) { vector isPrime(n + 1, true); vector primes; - + isPrime[0] = isPrime[1] = false; // 0和1不是质数 - + for (int i = 2; i <= n; i++) { if (isPrime[i]) { primes.push_back(i); // i是质数,加入结果 - + // 标记i的所有倍数为合数 // 优化:从i*i开始,因为i*(i-1)之前的已经被更小的质数标记过了 for (int j = i * i; j <= n; j += i) { @@ -45,7 +46,7 @@ vector eratosthenesSieve(int n) { } } } - + return primes; } @@ -53,7 +54,7 @@ vector eratosthenesSieve(int n) { void eratosthenesSieveMark(vector& isPrime, int n) { isPrime.assign(n + 1, true); isPrime[0] = isPrime[1] = false; - + for (int i = 2; i * i <= n; i++) { // 只需遍历到sqrt(n) if (isPrime[i]) { for (int j = i * i; j <= n; j += i) { @@ -75,19 +76,19 @@ void eratosthenesSieveMark(vector& isPrime, int n) { vector eulerSieve(int n) { vector isPrime(n + 1, true); vector primes; - + isPrime[0] = isPrime[1] = false; - + for (int i = 2; i <= n; i++) { // 如果i是质数,加入质数列表 if (isPrime[i]) { primes.push_back(i); } - + // 用已知的质数去筛 i * primes[j] for (int j = 0; j < primes.size() && primes[j] * i <= n; j++) { isPrime[primes[j] * i] = false; // 标记为合数 - + // 关键优化:当i能被primes[j]整除时停止 // 这样可以保证每个合数只被它的最小质因数筛一次 // 例如:i=4, primes[j]=2时,4*2=8被筛掉,此时停止 @@ -97,7 +98,7 @@ vector eulerSieve(int n) { } } } - + return primes; } @@ -106,12 +107,12 @@ void eulerSieveMark(vector& isPrime, vector& primes, int n) { isPrime.assign(n + 1, true); isPrime[0] = isPrime[1] = false; primes.clear(); - + for (int i = 2; i <= n; i++) { if (isPrime[i]) { primes.push_back(i); } - + for (int j = 0; j < primes.size() && primes[j] * i <= n; j++) { isPrime[primes[j] * i] = false; if (i % primes[j] == 0) { @@ -124,7 +125,7 @@ void eulerSieveMark(vector& isPrime, vector& primes, int n) { // ==================== 使用示例 ==================== int main() { int n = 100; - + cout << "=== 埃拉托斯特尼筛法 ===" << endl; vector primes1 = eratosthenesSieve(n); cout << "质数数量: " << primes1.size() << endl; @@ -133,7 +134,7 @@ int main() { cout << primes1[i] << " "; } cout << endl; - + cout << "\n=== 欧拉筛(线性筛)===" << endl; vector primes2 = eulerSieve(n); cout << "质数数量: " << primes2.size() << endl; @@ -142,7 +143,7 @@ int main() { cout << primes2[i] << " "; } cout << endl; - + // 验证两种方法结果一致 cout << "\n=== 验证结果一致性 ===" << endl; if (primes1 == primes2) { @@ -150,6 +151,6 @@ int main() { } else { cout << "✗ 结果不一致" << endl; } - + return 0; } diff --git "a/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/1.cpp" "b/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/1.cpp" index 7069474f..426e6937 100644 --- "a/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/1.cpp" +++ "b/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/1.cpp" @@ -6,18 +6,18 @@ using namespace std; // 棋谱长度最大值是 10^5 + 10 -const int MAX_N = 100000 + 10; +const int MAX_N = 100000 + 10; // dp[i]: 以原棋谱中第 i 步棋结尾的最长合法子序列长度 // 注意:索引从 1 开始 -int dp[MAX_N]; +int dp[MAX_N]; // 存储所有的棋步。pair 存储 (颜色 c, 劫争编号 a) // moves[i] 对应原棋谱中的第 i 步 -pair moves[MAX_N]; +pair moves[MAX_N]; void solve_on2() { - int n; + int n; if (!(cin >> n)) return; // 读取所有棋步 @@ -37,8 +37,8 @@ void solve_on2() { int a_i = moves[i].second; // 当前劫争编号 // 1. 任何一步棋本身至少构成一个长度为 1 的序列 - int max_prefix_len = 0; - + int max_prefix_len = 0; + // **内层循环:寻找最优前驱 s_j** // 遍历 i 之前的所有棋步 s_j (j 从 1 到 i-1) for (int j = 1; j < i; ++j) { @@ -46,12 +46,12 @@ void solve_on2() { int a_j = moves[j].second; // 前驱劫争编号 // 检查 s_j 是否是 s_i 的合法前驱 - + // 规则 1: 颜色必须不同 - bool color_diff = (c_j != c_i); - + bool color_diff = (c_j != c_i); + // 规则 2: 劫争必须不同 - bool action_diff = (a_j != a_i); + bool action_diff = (a_j != a_i); if (color_diff && action_diff) { // 如果 s_j 是合法前驱,则可以从 dp[j] 转移过来 @@ -59,7 +59,7 @@ void solve_on2() { max_prefix_len = max(max_prefix_len, dp[j]); } } - + // **状态转移方程:dp[i] = 1 + max(所有合法 dp[j])** // 最终的 dp[i] = 最长前缀长度 + 1 (当前步) dp[i] = max_prefix_len + 1; @@ -67,7 +67,7 @@ void solve_on2() { // 更新全局最长序列长度 overall_max_len = max(overall_max_len, dp[i]); } - + // 输出最终结果 cout << overall_max_len << "\n"; } diff --git "a/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/2.cpp" "b/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/2.cpp" index ade94dd0..9b759de9 100644 --- "a/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/2.cpp" +++ "b/niuke/daily_problem/25_12-5\347\263\237\347\263\225\347\232\204\346\211\223\350\260\261\345\221\230/2.cpp" @@ -10,23 +10,23 @@ # include # include # include -# include +# include # include # include -# include - +# include + // 宏定义 # define eps 1e-9 # define fi first # define se second # define ll long long # define int ll -// cout< PII; +typedef pair PII; // 常量定义 const int mod=1e9+7; @@ -35,7 +35,7 @@ const int Time=86400; const int X=131; const int inf=0x3f3f3f3f; const double PI = 1e-4; -double pai = 3.14159265358979323846; +double pai = 3.14159265358979323846; // 全局变量 int T,n; // T: 测试用例数量, n: 每个测试用例的元素个数 @@ -46,11 +46,11 @@ int pre[2][15]; // pre[j][k]: 颜色为j、数值为k的最后一 /** * 解题思路:动态规划 * 问题:找到最长的序列,使得相邻两个元素颜色不同且数值不同 - * + * * 状态定义: * - dp[i]: 以第i个位置结尾的最长合法序列长度 * - pre[j][k]: 颜色为j、数值为k的最后一个位置索引 - * + * * 转移方程: * 对于当前位置(c, a),找到所有颜色不同(j != c)且数值不同(k != a)的前驱位置 * dp[i] = max(dp[pre[j][k]] + 1),其中 j != c 且 k != a @@ -58,16 +58,16 @@ int pre[2][15]; // pre[j][k]: 颜色为j、数值为k的最后一 void solve(){ int ans = 0; // 记录全局最长序列长度 cin >> n; // 输入元素个数 - + // 初始化dp数组和pre数组 memset(dp,0,sizeof dp); memset(pre,0,sizeof pre); - + // 遍历每个输入的元素 for(int i = 1 ; i <= n ; i ++ ){ int c, a; // c: 颜色(0或1), a: 数值(1-10) cin >> c >> a; - + // 遍历所有可能的颜色 for(int j = 0 ; j < 2 ; j ++ ) if(j != c){ // 颜色必须不同 @@ -80,14 +80,14 @@ void solve(){ dp[i] = max(dp[i], dp[pre[j][k]] + 1); } } - + // 更新pre数组:记录颜色为c、数值为a的最后一个位置 pre[c][a] = i; - + // 更新全局答案 ans = max(ans, dp[i]); } - + // 输出最长合法序列长度 cout<> T; - + // 处理每组测试用例 while(T--){ solve(); diff --git "a/niuke/daily_problem/25_12-6\346\270\270\346\270\270\347\232\204\346\225\260\345\200\274\350\267\235\347\246\273/1.cpp" "b/niuke/daily_problem/25_12-6\346\270\270\346\270\270\347\232\204\346\225\260\345\200\274\350\267\235\347\246\273/1.cpp" index db6ea98f..e0820546 100644 --- "a/niuke/daily_problem/25_12-6\346\270\270\346\270\270\347\232\204\346\225\260\345\200\274\350\267\235\347\246\273/1.cpp" +++ "b/niuke/daily_problem/25_12-6\346\270\270\346\270\270\347\232\204\346\225\260\345\200\274\350\267\235\347\246\273/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #define il inline @@ -80,13 +81,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-7\345\257\222\346\275\256/1.cpp" "b/niuke/daily_problem/25_12-7\345\257\222\346\275\256/1.cpp" index 56867ba0..6e80eef1 100644 --- "a/niuke/daily_problem/25_12-7\345\257\222\346\275\256/1.cpp" +++ "b/niuke/daily_problem/25_12-7\345\257\222\346\275\256/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include @@ -78,13 +79,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-7\345\257\222\346\275\256/2.cpp" "b/niuke/daily_problem/25_12-7\345\257\222\346\275\256/2.cpp" index 1e506af3..c56c9794 100644 --- "a/niuke/daily_problem/25_12-7\345\257\222\346\275\256/2.cpp" +++ "b/niuke/daily_problem/25_12-7\345\257\222\346\275\256/2.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; using ll = long long; diff --git "a/niuke/daily_problem/25_12-8\350\210\236\350\220\214\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" "b/niuke/daily_problem/25_12-8\350\210\236\350\220\214\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" index 6bbbceb9..fc1bd4b2 100644 --- "a/niuke/daily_problem/25_12-8\350\210\236\350\220\214\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" +++ "b/niuke/daily_problem/25_12-8\350\210\236\350\220\214\346\227\266\351\227\264\345\210\260\357\274\201/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include @@ -48,13 +49,13 @@ il void solve(){ int main() { fastio - + int t = 1; while(t--) // cin >> t; { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/25_12-9\346\262\241\346\234\211\344\270\212\345\217\270\347\232\204\350\210\236\344\274\232/1.cpp" "b/niuke/daily_problem/25_12-9\346\262\241\346\234\211\344\270\212\345\217\270\347\232\204\350\210\236\344\274\232/1.cpp" index c02a9cdc..8f4191aa 100644 --- "a/niuke/daily_problem/25_12-9\346\262\241\346\234\211\344\270\212\345\217\270\347\232\204\350\210\236\344\274\232/1.cpp" +++ "b/niuke/daily_problem/25_12-9\346\262\241\346\234\211\344\270\212\345\217\270\347\232\204\350\210\236\344\274\232/1.cpp" @@ -1,3 +1,4 @@ +#include #include #include #include @@ -70,19 +71,19 @@ il void solve(){ dp.assign(n+1,vector(2,0)); dfs(root,-1); cout << max(dp[root][0],dp[root][1]) << endl; - + } int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/1.cpp b/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/1.cpp index b5f9cab3..7beac186 100644 --- a/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/1.cpp +++ b/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/1.cpp @@ -17,7 +17,7 @@ using namespace std; void solve() { int n,m; - cin >> n >> m; + cin >> n >> m; long long l = 0,r = 2e5; long long r1 = 2e5; while(l<=r){ diff --git a/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/2.cpp b/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/2.cpp index fd6e589a..6b5b4dbc 100644 --- a/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/2.cpp +++ b/niuke/daily_problem/26-3-11Tokitsukaze and Colorful Chessboard/2.cpp @@ -18,7 +18,7 @@ int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); - + int T = 3; cin >> T; while(T--){ diff --git "a/niuke/daily_problem/26-3-17\345\210\267\351\242\230\347\273\237\350\256\241/1.cpp" "b/niuke/daily_problem/26-3-17\345\210\267\351\242\230\347\273\237\350\256\241/1.cpp" index 9f1dc4f4..e6728df9 100644 --- "a/niuke/daily_problem/26-3-17\345\210\267\351\242\230\347\273\237\350\256\241/1.cpp" +++ "b/niuke/daily_problem/26-3-17\345\210\267\351\242\230\347\273\237\350\256\241/1.cpp" @@ -32,13 +32,13 @@ il void solve(){ int main() { fastio; - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/26-3-8\350\277\236\345\210\206\346\225\260/2.cpp" "b/niuke/daily_problem/26-3-8\350\277\236\345\210\206\346\225\260/2.cpp" index 35615a51..09808645 100644 --- "a/niuke/daily_problem/26-3-8\350\277\236\345\210\206\346\225\260/2.cpp" +++ "b/niuke/daily_problem/26-3-8\350\277\236\345\210\206\346\225\260/2.cpp" @@ -43,6 +43,6 @@ int main(){ murname(); cout << '\n'; } - + return 0; } diff --git a/niuke/daily_problem/26_01-14many sum/1.cpp b/niuke/daily_problem/26_01-14many sum/1.cpp index e5aac56f..a5d853da 100644 --- a/niuke/daily_problem/26_01-14many sum/1.cpp +++ b/niuke/daily_problem/26_01-14many sum/1.cpp @@ -1,3 +1,4 @@ +#include #include using namespace std; using ll = long long; diff --git "a/niuke/daily_problem/26_01-15\345\260\217A\345\217\226\347\237\263\345\255\220/1.cpp" "b/niuke/daily_problem/26_01-15\345\260\217A\345\217\226\347\237\263\345\255\220/1.cpp" index 97b1b952..2e971dd8 100644 --- "a/niuke/daily_problem/26_01-15\345\260\217A\345\217\226\347\237\263\345\255\220/1.cpp" +++ "b/niuke/daily_problem/26_01-15\345\260\217A\345\217\226\347\237\263\345\255\220/1.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; void solve() { diff --git "a/niuke/daily_problem/26_01-17\346\234\211\350\266\243\347\232\204\345\214\272\351\227\264/2.cpp" "b/niuke/daily_problem/26_01-17\346\234\211\350\266\243\347\232\204\345\214\272\351\227\264/2.cpp" index c8df6250..3a65c8db 100644 --- "a/niuke/daily_problem/26_01-17\346\234\211\350\266\243\347\232\204\345\214\272\351\227\264/2.cpp" +++ "b/niuke/daily_problem/26_01-17\346\234\211\350\266\243\347\232\204\345\214\272\351\227\264/2.cpp" @@ -1,3 +1,4 @@ +#include #include using namespace std; diff --git "a/niuke/daily_problem/26_01-29\350\256\241\346\225\260/1.cpp" "b/niuke/daily_problem/26_01-29\350\256\241\346\225\260/1.cpp" index 24b49c60..9d8865bb 100644 --- "a/niuke/daily_problem/26_01-29\350\256\241\346\225\260/1.cpp" +++ "b/niuke/daily_problem/26_01-29\350\256\241\346\225\260/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-01-29 21:04:00 diff --git "a/niuke/daily_problem/26_01-30\345\260\217\347\272\242\347\232\204gcd/1.cpp" "b/niuke/daily_problem/26_01-30\345\260\217\347\272\242\347\232\204gcd/1.cpp" index fd22aa66..27ba76a1 100644 --- "a/niuke/daily_problem/26_01-30\345\260\217\347\272\242\347\232\204gcd/1.cpp" +++ "b/niuke/daily_problem/26_01-30\345\260\217\347\272\242\347\232\204gcd/1.cpp" @@ -35,13 +35,13 @@ il void solve(){ int main() { fastio; - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/26_01_10-\347\211\233\347\211\233\345\226\234\346\254\242\345\255\227\347\254\246\344\270\262/1.cpp" "b/niuke/daily_problem/26_01_10-\347\211\233\347\211\233\345\226\234\346\254\242\345\255\227\347\254\246\344\270\262/1.cpp" index da436044..fbacc0b8 100644 --- "a/niuke/daily_problem/26_01_10-\347\211\233\347\211\233\345\226\234\346\254\242\345\255\227\347\254\246\344\270\262/1.cpp" +++ "b/niuke/daily_problem/26_01_10-\347\211\233\347\211\233\345\226\234\346\254\242\345\255\227\347\254\246\344\270\262/1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' @@ -46,13 +47,13 @@ il void solve(){ int main() { fastio; - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/26_2-1\350\272\262\350\227\217/1.cpp" "b/niuke/daily_problem/26_2-1\350\272\262\350\227\217/1.cpp" index ffd3d929..e994ee66 100644 --- "a/niuke/daily_problem/26_2-1\350\272\262\350\227\217/1.cpp" +++ "b/niuke/daily_problem/26_2-1\350\272\262\350\227\217/1.cpp" @@ -62,13 +62,13 @@ il void solve(){ int main() { fastio; - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/daily_problem/26_2-2\345\260\217\347\276\216\347\232\204\346\225\260\347\273\204\346\223\215\344\275\234/1.cpp" "b/niuke/daily_problem/26_2-2\345\260\217\347\276\216\347\232\204\346\225\260\347\273\204\346\223\215\344\275\234/1.cpp" index 5da4d1f5..ba2cea1a 100644 --- "a/niuke/daily_problem/26_2-2\345\260\217\347\276\216\347\232\204\346\225\260\347\273\204\346\223\215\344\275\234/1.cpp" +++ "b/niuke/daily_problem/26_2-2\345\260\217\347\276\216\347\232\204\346\225\260\347\273\204\346\223\215\344\275\234/1.cpp" @@ -1,3 +1,4 @@ +#include #include #define endl '\n' using namespace std; @@ -12,7 +13,7 @@ int main() { cin >> n; ll sum = 0; - vector nums(n); + vector nums(n); for (int i = 0; i < n; ++i) { cin >> nums[i]; sum += nums[i]; @@ -38,7 +39,7 @@ int main() { ll L1 = 0; for (ll v : nums) L1 += llabs(v - avg); - cout << (L1 / 2) << endl; + cout << (L1 / 2) << endl; } else { ll q = avg; // floor(sum/n) ll res = min(calc(q), calc(q + 1)); diff --git "a/niuke/daily_problem/26_2-8\345\260\217d\345\222\214\350\266\205\347\272\247\346\263\241\346\263\241\345\240\202/1.cpp" "b/niuke/daily_problem/26_2-8\345\260\217d\345\222\214\350\266\205\347\272\247\346\263\241\346\263\241\345\240\202/1.cpp" index c3300069..2619cd2e 100644 --- "a/niuke/daily_problem/26_2-8\345\260\217d\345\222\214\350\266\205\347\272\247\346\263\241\346\263\241\345\240\202/1.cpp" +++ "b/niuke/daily_problem/26_2-8\345\260\217d\345\222\214\350\266\205\347\272\247\346\263\241\346\263\241\345\240\202/1.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline #define endl '\n' diff --git a/niuke/practice/noob/noob1/noob1.cpp b/niuke/practice/noob/noob1/noob1.cpp index 14cd0ebe..faeea1c0 100644 --- a/niuke/practice/noob/noob1/noob1.cpp +++ b/niuke/practice/noob/noob1/noob1.cpp @@ -10,4 +10,4 @@ int main() { cout << "Hello Nowcoder!\n"; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob10/noob10.cpp b/niuke/practice/noob/noob10/noob10.cpp index bad6bf0b..f99a9d37 100644 --- a/niuke/practice/noob/noob10/noob10.cpp +++ b/niuke/practice/noob/noob10/noob10.cpp @@ -18,4 +18,4 @@ int main() { } cout.flush(); return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob11/noob11.cpp b/niuke/practice/noob/noob11/noob11.cpp index 10d67f40..31713357 100644 --- a/niuke/practice/noob/noob11/noob11.cpp +++ b/niuke/practice/noob/noob11/noob11.cpp @@ -13,4 +13,4 @@ int main() { cout << floor(sqrt(a)); cout.flush(); return 0; -}c \ No newline at end of file +}c diff --git a/niuke/practice/noob/noob12/noob12.cpp b/niuke/practice/noob/noob12/noob12.cpp index 11a170b1..f7bdf61e 100644 --- a/niuke/practice/noob/noob12/noob12.cpp +++ b/niuke/practice/noob/noob12/noob12.cpp @@ -14,4 +14,4 @@ int main() { } while (n /= 10); return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob13/noob13.cpp b/niuke/practice/noob/noob13/noob13.cpp index 66848843..223b19cf 100644 --- a/niuke/practice/noob/noob13/noob13.cpp +++ b/niuke/practice/noob/noob13/noob13.cpp @@ -13,4 +13,4 @@ int main() { return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob14/noob14.cpp b/niuke/practice/noob/noob14/noob14.cpp index 14da10c9..84757db6 100644 --- a/niuke/practice/noob/noob14/noob14.cpp +++ b/niuke/practice/noob/noob14/noob14.cpp @@ -32,4 +32,4 @@ int main() { cout << fixed << setprecision(10) << fabs(p1.get_euclideanDistance(p2) - p1.get_manhattanDistance(p2)); return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob15/noob15.cpp b/niuke/practice/noob/noob15/noob15.cpp index 144aad98..499e8953 100644 --- a/niuke/practice/noob/noob15/noob15.cpp +++ b/niuke/practice/noob/noob15/noob15.cpp @@ -6,7 +6,7 @@ typedef long long ll; int main() { ios::sync_with_stdio(false), cin.tie(0); - + int n,ans = 0; cin >> n; @@ -17,4 +17,4 @@ int main() { cout << ans; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob16/noob16.cpp b/niuke/practice/noob/noob16/noob16.cpp index 5b5147c2..808a7eb4 100644 --- a/niuke/practice/noob/noob16/noob16.cpp +++ b/niuke/practice/noob/noob16/noob16.cpp @@ -6,7 +6,7 @@ typedef long long ll; int main() { ios::sync_with_stdio(false), cin.tie(0); - + int seconds,hours,minutes; cin >> seconds; @@ -21,4 +21,4 @@ int main() { return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob18/noob18.cpp b/niuke/practice/noob/noob18/noob18.cpp index 58994478..ac548154 100644 --- a/niuke/practice/noob/noob18/noob18.cpp +++ b/niuke/practice/noob/noob18/noob18.cpp @@ -16,4 +16,4 @@ int main() { cout << v << '\n'; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob19/noob19.cpp b/niuke/practice/noob/noob19/noob19.cpp index 75861b5f..ab165f7a 100644 --- a/niuke/practice/noob/noob19/noob19.cpp +++ b/niuke/practice/noob/noob19/noob19.cpp @@ -14,4 +14,4 @@ int main() { cout << s << '\n'; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob2/noob2.cpp b/niuke/practice/noob/noob2/noob2.cpp index fe2448e5..aae283f1 100644 --- a/niuke/practice/noob/noob2/noob2.cpp +++ b/niuke/practice/noob/noob2/noob2.cpp @@ -12,4 +12,4 @@ int main() { cout << n;c return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob20/noob20.cpp b/niuke/practice/noob/noob20/noob20.cpp index b4af14c3..7124fd60 100644 --- a/niuke/practice/noob/noob20/noob20.cpp +++ b/niuke/practice/noob/noob20/noob20.cpp @@ -17,4 +17,4 @@ int main() { cout << get_sum(n) << '\n'; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob21/noob21.cpp b/niuke/practice/noob/noob21/noob21.cpp index dda034f1..997a2c28 100644 --- a/niuke/practice/noob/noob21/noob21.cpp +++ b/niuke/practice/noob/noob21/noob21.cpp @@ -12,4 +12,4 @@ int main() { cout << d % 7 + 1; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob22/noob22.cpp b/niuke/practice/noob/noob22/noob22.cpp index 2d6d222d..9087f4c2 100644 --- a/niuke/practice/noob/noob22/noob22.cpp +++ b/niuke/practice/noob/noob22/noob22.cpp @@ -18,4 +18,4 @@ int main() { else cout << "no\n"; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob23/noob23.cpp b/niuke/practice/noob/noob23/noob23.cpp index 3e2cb346..75e21db2 100644 --- a/niuke/practice/noob/noob23/noob23.cpp +++ b/niuke/practice/noob/noob23/noob23.cpp @@ -14,4 +14,4 @@ int main() { else cout << '>'; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob24/noob24.cpp b/niuke/practice/noob/noob24/noob24.cpp index d228bc32..57d8b755 100644 --- a/niuke/practice/noob/noob24/noob24.cpp +++ b/niuke/practice/noob/noob24/noob24.cpp @@ -17,4 +17,4 @@ int main() { cout << collatz_func(n) << '\n'; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob25/noob25.cpp b/niuke/practice/noob/noob25/noob25.cpp index 974b0073..b730109a 100644 --- a/niuke/practice/noob/noob25/noob25.cpp +++ b/niuke/practice/noob/noob25/noob25.cpp @@ -18,4 +18,4 @@ int main() { else cout << "no\n"; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob27/noob27.cpp b/niuke/practice/noob/noob27/noob27.cpp index df4c609e..300c6344 100644 --- a/niuke/practice/noob/noob27/noob27.cpp +++ b/niuke/practice/noob/noob27/noob27.cpp @@ -21,4 +21,4 @@ int main() { cout << "The minimum number is : " << min << '\n'; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob28/noob28.cpp b/niuke/practice/noob/noob28/noob28.cpp index cfda3d4e..85ec0848 100644 --- a/niuke/practice/noob/noob28/noob28.cpp +++ b/niuke/practice/noob/noob28/noob28.cpp @@ -18,4 +18,4 @@ int main() { return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob29/noob29.cpp b/niuke/practice/noob/noob29/noob29.cpp index c67d2649..d35921b9 100644 --- a/niuke/practice/noob/noob29/noob29.cpp +++ b/niuke/practice/noob/noob29/noob29.cpp @@ -18,4 +18,4 @@ int main() { } return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob3/noob3.cpp b/niuke/practice/noob/noob3/noob3.cpp index 0b6db062..2fd0f679 100644 --- a/niuke/practice/noob/noob3/noob3.cpp +++ b/niuke/practice/noob/noob3/noob3.cpp @@ -9,7 +9,7 @@ int main() { double n; cin >> n; - cout << n; + cout << n; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob30/noob30.cpp b/niuke/practice/noob/noob30/noob30.cpp index cab17c03..fa6a3a00 100644 --- a/niuke/practice/noob/noob30/noob30.cpp +++ b/niuke/practice/noob/noob30/noob30.cpp @@ -12,8 +12,8 @@ int main() { cin >> a >> b; if (a == 0 && b == 0)break; cout << a + b << '\n'; - + } while (1); return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob31/noob31.cpp b/niuke/practice/noob/noob31/noob31.cpp index 6dc40bab..06b032f7 100644 --- a/niuke/practice/noob/noob31/noob31.cpp +++ b/niuke/practice/noob/noob31/noob31.cpp @@ -26,4 +26,4 @@ int main() { } return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob32/noob32.cpp b/niuke/practice/noob/noob32/noob32.cpp index 0ef2efe9..7f1433b1 100644 --- a/niuke/practice/noob/noob32/noob32.cpp +++ b/niuke/practice/noob/noob32/noob32.cpp @@ -18,4 +18,4 @@ int main() { cout << sum << '\n'; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob33/noob33.cpp b/niuke/practice/noob/noob33/noob33.cpp index 35dd0cd0..d9b79e9d 100644 --- a/niuke/practice/noob/noob33/noob33.cpp +++ b/niuke/practice/noob/noob33/noob33.cpp @@ -23,7 +23,7 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + solve(); return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob34/noob34.cpp b/niuke/practice/noob/noob34/noob34.cpp index 5413961e..88dcda18 100644 --- a/niuke/practice/noob/noob34/noob34.cpp +++ b/niuke/practice/noob/noob34/noob34.cpp @@ -26,8 +26,8 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + solve(); return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob35/noob35.cpp b/niuke/practice/noob/noob35/noob35.cpp index bd723f6a..a642bcd1 100644 --- a/niuke/practice/noob/noob35/noob35.cpp +++ b/niuke/practice/noob/noob35/noob35.cpp @@ -33,4 +33,4 @@ int main() solve(); return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob36/noob36.cpp b/niuke/practice/noob/noob36/noob36.cpp index c9ad5f7a..399b76e8 100644 --- a/niuke/practice/noob/noob36/noob36.cpp +++ b/niuke/practice/noob/noob36/noob36.cpp @@ -25,13 +25,13 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob37/noob37.cpp b/niuke/practice/noob/noob37/noob37.cpp index 372361e3..e7243ee3 100644 --- a/niuke/practice/noob/noob37/noob37.cpp +++ b/niuke/practice/noob/noob37/noob37.cpp @@ -29,13 +29,13 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob38/noob38.cpp b/niuke/practice/noob/noob38/noob38.cpp index a4ea5d8f..c687f51d 100644 --- a/niuke/practice/noob/noob38/noob38.cpp +++ b/niuke/practice/noob/noob38/noob38.cpp @@ -30,13 +30,13 @@ il void solve(){ int main() { ios::sync_with_stdio(false),cin.tie(nullptr); - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob38/noob38.py b/niuke/practice/noob/noob38/noob38.py index 7ef77eaa..9065f3a6 100644 --- a/niuke/practice/noob/noob38/noob38.py +++ b/niuke/practice/noob/noob38/noob38.py @@ -4,4 +4,4 @@ if i%4 == 0 or '4' in str(i): continue else: - print(i) \ No newline at end of file + print(i) diff --git a/niuke/practice/noob/noob39/noob39.py b/niuke/practice/noob/noob39/noob39.py index dd925a44..0341e064 100644 --- a/niuke/practice/noob/noob39/noob39.py +++ b/niuke/practice/noob/noob39/noob39.py @@ -18,4 +18,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git a/niuke/practice/noob/noob4/noob4.cpp b/niuke/practice/noob/noob4/noob4.cpp index 4b936aa3..3332b622 100644 --- a/niuke/practice/noob/noob4/noob4.cpp +++ b/niuke/practice/noob/noob4/noob4.cpp @@ -12,4 +12,4 @@ int main() { cout << n; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob40/noob40.py b/niuke/practice/noob/noob40/noob40.py index f8b6197d..9096f3fb 100644 --- a/niuke/practice/noob/noob40/noob40.py +++ b/niuke/practice/noob/noob40/noob40.py @@ -19,4 +19,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/practice/noob/noob41/noob41\347\245\236\347\247\230\347\237\263\345\203\217\347\232\204\351\225\234\345\203\217\345\272\217\345\210\227.py" "b/niuke/practice/noob/noob41/noob41\347\245\236\347\247\230\347\237\263\345\203\217\347\232\204\351\225\234\345\203\217\345\272\217\345\210\227.py" index b8fbd306..07925abe 100644 --- "a/niuke/practice/noob/noob41/noob41\347\245\236\347\247\230\347\237\263\345\203\217\347\232\204\351\225\234\345\203\217\345\272\217\345\210\227.py" +++ "b/niuke/practice/noob/noob41/noob41\347\245\236\347\247\230\347\237\263\345\203\217\347\232\204\351\225\234\345\203\217\345\272\217\345\210\227.py" @@ -21,4 +21,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/practice/noob/noob43/noob43\347\211\233\347\211\233\347\232\204\346\225\260\345\255\246\344\275\234\344\270\232.cpp" "b/niuke/practice/noob/noob43/noob43\347\211\233\347\211\233\347\232\204\346\225\260\345\255\246\344\275\234\344\270\232.cpp" index 4ebbfc2b..8f06f1c5 100644 --- "a/niuke/practice/noob/noob43/noob43\347\211\233\347\211\233\347\232\204\346\225\260\345\255\246\344\275\234\344\270\232.cpp" +++ "b/niuke/practice/noob/noob43/noob43\347\211\233\347\211\233\347\232\204\346\225\260\345\255\246\344\275\234\344\270\232.cpp" @@ -1,3 +1,4 @@ +#include #include #define il inline using namespace std; @@ -42,13 +43,13 @@ il void solve(){ int main() { fastio - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; -} \ No newline at end of file +} diff --git "a/niuke/practice/noob/noob44/noob44\346\225\260\347\273\204\350\256\241\346\225\260\347\273\264\346\212\244.py" "b/niuke/practice/noob/noob44/noob44\346\225\260\347\273\204\350\256\241\346\225\260\347\273\264\346\212\244.py" index 50772ed3..d1d1b65f 100644 --- "a/niuke/practice/noob/noob44/noob44\346\225\260\347\273\204\350\256\241\346\225\260\347\273\264\346\212\244.py" +++ "b/niuke/practice/noob/noob44/noob44\346\225\260\347\273\204\350\256\241\346\225\260\347\273\264\346\212\244.py" @@ -23,4 +23,4 @@ def solve(): t = 1 t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/practice/noob/noob45/noob45\350\256\260\346\225\260\351\227\256\351\242\230.py" "b/niuke/practice/noob/noob45/noob45\350\256\260\346\225\260\351\227\256\351\242\230.py" index 9ab2dbf6..d5db36bd 100644 --- "a/niuke/practice/noob/noob45/noob45\350\256\260\346\225\260\351\227\256\351\242\230.py" +++ "b/niuke/practice/noob/noob45/noob45\350\256\260\346\225\260\351\227\256\351\242\230.py" @@ -11,12 +11,12 @@ def solve(): cnt += s1.count(str(x)) print(cnt) - - + + if __name__ == "__main__": sys.setrecursionlimit(10**7) t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/practice/noob/noob46/noob46\347\272\246\347\221\237\345\244\253\347\216\257.py" "b/niuke/practice/noob/noob46/noob46\347\272\246\347\221\237\345\244\253\347\216\257.py" index 6f8bccb0..d1b48b4b 100644 --- "a/niuke/practice/noob/noob46/noob46\347\272\246\347\221\237\345\244\253\347\216\257.py" +++ "b/niuke/practice/noob/noob46/noob46\347\272\246\347\221\237\345\244\253\347\216\257.py" @@ -18,4 +18,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/practice/noob/noob47/noob47\346\240\241\351\227\250\345\244\226\347\232\204\346\240\221.py" "b/niuke/practice/noob/noob47/noob47\346\240\241\351\227\250\345\244\226\347\232\204\346\240\221.py" index 2fb9e2cc..ff94ec9b 100644 --- "a/niuke/practice/noob/noob47/noob47\346\240\241\351\227\250\345\244\226\347\232\204\346\240\221.py" +++ "b/niuke/practice/noob/noob47/noob47\346\240\241\351\227\250\345\244\226\347\232\204\346\240\221.py" @@ -7,20 +7,20 @@ class Interval: start: int end: int - + def length(self): return self.end - self.start + 1 - + def intersects(self, other): return self.end >= other.start and self.start <= other.end - + def merge(self, other): return Interval(min(self.start, other.start), max(self.end, other.end)) def solve(): l, m = map(int, input().split()) - + # 读取所有区间 intervals = [] for _ in range(m): @@ -28,10 +28,10 @@ def solve(): # s = math.ceil(s) 如果s和e是小数的话 # e = math.floor(e) intervals.append(Interval(s, e)) - + # 按起始位置排序 intervals.sort(key=lambda x: x.start) - + # 合并重叠区间 merged = [intervals[0]] for i in range(1, len(intervals)): @@ -41,12 +41,12 @@ def solve(): else: # 无重叠,添加新区间 merged.append(intervals[i]) - + # 计算被移除的树数 removed = 0 for interval in merged: removed += interval.length() - + # 剩余的树数 tree_remain = l + 1 - removed print(tree_remain) @@ -65,15 +65,15 @@ def solve(): # def solve(): # l, m = map(int, input().split()) - + # # 标记数组,记录每棵树是否被移除 # removed = [False] * (l + 1) - + # for _ in range(m): # s, e = map(int, input().split()) # for i in range(s, e + 1): # removed[i] = True - + # # 统计剩余的树 # tree_remain = sum(1 for x in removed if not x) # print(tree_remain) @@ -82,4 +82,4 @@ def solve(): # if __name__ == "__main__": # t = 1 # for _ in range(t): -# solve() \ No newline at end of file +# solve() diff --git "a/niuke/practice/noob/noob48/noob48\345\215\225\347\273\204_\344\272\214\347\273\264\346\225\260\347\273\204.py" "b/niuke/practice/noob/noob48/noob48\345\215\225\347\273\204_\344\272\214\347\273\264\346\225\260\347\273\204.py" index 42067b79..6f632bb2 100644 --- "a/niuke/practice/noob/noob48/noob48\345\215\225\347\273\204_\344\272\214\347\273\264\346\225\260\347\273\204.py" +++ "b/niuke/practice/noob/noob48/noob48\345\215\225\347\273\204_\344\272\214\347\273\264\346\225\260\347\273\204.py" @@ -16,4 +16,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git "a/niuke/practice/noob/noob49/noob49\344\270\212\344\270\211\350\247\222\347\237\251\351\230\265\345\210\244\345\256\232.py" "b/niuke/practice/noob/noob49/noob49\344\270\212\344\270\211\350\247\222\347\237\251\351\230\265\345\210\244\345\256\232.py" index 84470554..b6c27986 100644 --- "a/niuke/practice/noob/noob49/noob49\344\270\212\344\270\211\350\247\222\347\237\251\351\230\265\345\210\244\345\256\232.py" +++ "b/niuke/practice/noob/noob49/noob49\344\270\212\344\270\211\350\247\222\347\237\251\351\230\265\345\210\244\345\256\232.py" @@ -23,4 +23,4 @@ def solve(): t = 1 # t = int(input()) for _ in range(t): - solve() \ No newline at end of file + solve() diff --git a/niuke/practice/noob/noob5/noob5.cpp b/niuke/practice/noob/noob5/noob5.cpp index af9b34e5..e07338fb 100644 --- a/niuke/practice/noob/noob5/noob5.cpp +++ b/niuke/practice/noob/noob5/noob5.cpp @@ -8,12 +8,12 @@ // ios::sync_with_stdio(false), cin.tie(0); // // int a; -// ll b; +// ll b; // double c; char d; // string e; -// +// // cin >> a >> b >> c >> d >> e; -// cout << a << '\n' << b << '\n' << fixed << +// cout << a << '\n' << b << '\n' << fixed << // setprecision(1) << c << '\n' << d << '\n' << e << '\n'; // cout.flush(); // @@ -39,4 +39,4 @@ int main() { cout.unsetf(ios::fixed); cout.flush(); -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob53/1.cpp b/niuke/practice/noob/noob53/1.cpp index 173efb7a..80bca124 100644 --- a/niuke/practice/noob/noob53/1.cpp +++ b/niuke/practice/noob/noob53/1.cpp @@ -17,7 +17,7 @@ int main() { break; } - else if(c == '5') + else if(c == '5') { c = '*'; } diff --git a/niuke/practice/noob/noob54/1.cpp b/niuke/practice/noob/noob54/1.cpp index fd840606..e724adee 100644 --- a/niuke/practice/noob/noob54/1.cpp +++ b/niuke/practice/noob/noob54/1.cpp @@ -10,7 +10,7 @@ using namespace std; rules = { "elephant" : 'tiger', - + } int main() { diff --git a/niuke/practice/noob/noob55/1.cpp b/niuke/practice/noob/noob55/1.cpp index 6bcf26ee..9dab18f6 100644 --- a/niuke/practice/noob/noob55/1.cpp +++ b/niuke/practice/noob/noob55/1.cpp @@ -39,13 +39,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/niuke/practice/noob/noob56/1.cpp b/niuke/practice/noob/noob56/1.cpp index 377837c5..192fa984 100644 --- a/niuke/practice/noob/noob56/1.cpp +++ b/niuke/practice/noob/noob56/1.cpp @@ -48,13 +48,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/niuke/practice/noob/noob57/1.cpp b/niuke/practice/noob/noob57/1.cpp index fe7ebc37..2d3879e5 100644 --- a/niuke/practice/noob/noob57/1.cpp +++ b/niuke/practice/noob/noob57/1.cpp @@ -32,13 +32,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/niuke/practice/noob/noob58/1.cpp b/niuke/practice/noob/noob58/1.cpp index 29f692a7..6170a704 100644 --- a/niuke/practice/noob/noob58/1.cpp +++ b/niuke/practice/noob/noob58/1.cpp @@ -30,13 +30,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/niuke/practice/noob/noob59/1.cpp b/niuke/practice/noob/noob59/1.cpp index c859dc23..d74406f5 100644 --- a/niuke/practice/noob/noob59/1.cpp +++ b/niuke/practice/noob/noob59/1.cpp @@ -30,13 +30,13 @@ il void solve(){ int main() { fastio - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/niuke/practice/noob/noob6/noob6.cpp b/niuke/practice/noob/noob6/noob6.cpp index 15c19479..04f3b29f 100644 --- a/niuke/practice/noob/noob6/noob6.cpp +++ b/niuke/practice/noob/noob6/noob6.cpp @@ -12,4 +12,4 @@ int main() { cout << a + b; return 0; -} \ No newline at end of file +} diff --git a/niuke/practice/noob/noob60/1.cpp b/niuke/practice/noob/noob60/1.cpp index 6db199b2..d86a80e6 100644 --- a/niuke/practice/noob/noob60/1.cpp +++ b/niuke/practice/noob/noob60/1.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -48,19 +49,19 @@ il void solve(){ default: cout << 'C' << '\n'; break; - } + } } int main() { fastio - + int t = 1; cin >> t; while(t--) { solve(); } - + return 0; } diff --git a/niuke/practice/noob/noob61/1.cpp b/niuke/practice/noob/noob61/1.cpp index f61fb611..94342725 100644 --- a/niuke/practice/noob/noob61/1.cpp +++ b/niuke/practice/noob/noob61/1.cpp @@ -37,7 +37,7 @@ il void solve(){ int main() { fastio; - + cin >> n >> m; cin >> s; while(m--) @@ -45,6 +45,6 @@ int main() solve(); } cout << s << "\n"; - + return 0; } diff --git "a/niuke/practice/noob/noob62/noob62 a \345\212\240 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211.cpp" "b/niuke/practice/noob/noob62/noob62 a \345\212\240 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211.cpp" index aee660a3..788d0d16 100644 --- "a/niuke/practice/noob/noob62/noob62 a \345\212\240 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211.cpp" +++ "b/niuke/practice/noob/noob62/noob62 a \345\212\240 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211.cpp" @@ -4,8 +4,8 @@ class Solution { * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 实现求两个参数的和 - * @param Integer1 int整型 - * @param Integer2 int整型 + * @param Integer1 int整型 + * @param Integer2 int整型 * @return int整型 */ int addTwoInteger(int Integer1, int Integer2) { diff --git "a/niuke/practice/noob/noob63 a \344\271\230 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211/1.cpp" "b/niuke/practice/noob/noob63 a \344\271\230 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211/1.cpp" index 4e3dc6af..7bbfee58 100644 --- "a/niuke/practice/noob/noob63 a \344\271\230 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211/1.cpp" +++ "b/niuke/practice/noob/noob63 a \344\271\230 b \351\227\256\351\242\230\357\274\210\345\207\275\346\225\260\357\274\211/1.cpp" @@ -4,8 +4,8 @@ class Solution { * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 计算两个参数的乘积 - * @param Number1 int整型 - * @param Number2 int整型 + * @param Number1 int整型 + * @param Number2 int整型 * @return long长整型 */ long long aTimesB(int Number1, int Number2) { diff --git "a/niuke/practice/noob/noob67 \346\261\202\351\230\266\344\271\230/1.cpp" "b/niuke/practice/noob/noob67 \346\261\202\351\230\266\344\271\230/1.cpp" index b58607e1..54ecc2f4 100644 --- "a/niuke/practice/noob/noob67 \346\261\202\351\230\266\344\271\230/1.cpp" +++ "b/niuke/practice/noob/noob67 \346\261\202\351\230\266\344\271\230/1.cpp" @@ -4,7 +4,7 @@ class Solution { * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 计算 n 的阶乘 - * @param n int整型 + * @param n int整型 * @return int整型 */ int factorialOfN(int n) { diff --git "a/niuke/practice/noob/noob69 \346\234\200\345\216\211\345\256\263\347\232\204\345\255\246\347\224\237/1.cpp" "b/niuke/practice/noob/noob69 \346\234\200\345\216\211\345\256\263\347\232\204\345\255\246\347\224\237/1.cpp" index c5f846cd..39f9bec8 100644 --- "a/niuke/practice/noob/noob69 \346\234\200\345\216\211\345\256\263\347\232\204\345\255\246\347\224\237/1.cpp" +++ "b/niuke/practice/noob/noob69 \346\234\200\345\216\211\345\256\263\347\232\204\345\255\246\347\224\237/1.cpp" @@ -57,14 +57,14 @@ il void solve() { int main() { fastio; - + int t = 1; cin >> t; - + while (t--) { solve(); } - + if (!pq.empty()) { Student top_student = pq.top(); cout << top_student.name << " " << top_student.scores["chinese"] << " " << top_student.scores["math"] << " " diff --git a/niuke/practice/noob/noob7/noob7.cpp b/niuke/practice/noob/noob7/noob7.cpp index f52adbc8..f9d60c02 100644 --- a/niuke/practice/noob/noob7/noob7.cpp +++ b/niuke/practice/noob/noob7/noob7.cpp @@ -14,4 +14,4 @@ int main() { cout.unsetf(ios::fixed); cout.flush(); return 0; -} \ No newline at end of file +} diff --git "a/niuke/practice/noob/noob72 \347\202\271\345\210\260\347\233\264\347\272\277\350\267\235\347\246\273/1.cpp" "b/niuke/practice/noob/noob72 \347\202\271\345\210\260\347\233\264\347\272\277\350\267\235\347\246\273/1.cpp" index 058d7ff2..b9ea9f4b 100644 --- "a/niuke/practice/noob/noob72 \347\202\271\345\210\260\347\233\264\347\272\277\350\267\235\347\246\273/1.cpp" +++ "b/niuke/practice/noob/noob72 \347\202\271\345\210\260\347\233\264\347\272\277\350\267\235\347\246\273/1.cpp" @@ -23,13 +23,13 @@ double getDistance(point P, line L) { // 这个方法适用于所有情况,包括垂直线和水平线 double dx = L.point_B.x - L.point_A.x; double dy = L.point_B.y - L.point_A.y; - + // 计算叉积的绝对值: |(B-A) × (A-P)| double crossProduct = fabs(dx * (L.point_A.y - P.y) - (L.point_A.x - P.x) * dy); - + // 计算线段长度: |B-A| double lineLength = sqrt(dx * dx + dy * dy); - + // 距离 = 叉积 / 线段长度 double distance = crossProduct / lineLength; return distance; diff --git "a/niuke/practice/noob/noob73 \344\270\211\350\247\222\345\275\242\351\235\242\347\247\257/3.py" "b/niuke/practice/noob/noob73 \344\270\211\350\247\222\345\275\242\351\235\242\347\247\257/3.py" index 6eb74229..2d4108d9 100644 --- "a/niuke/practice/noob/noob73 \344\270\211\350\247\222\345\275\242\351\235\242\347\247\257/3.py" +++ "b/niuke/practice/noob/noob73 \344\270\211\350\247\222\345\275\242\351\235\242\347\247\257/3.py" @@ -34,13 +34,13 @@ def get_area(T): def main(): x, y = map(int, input().split()) a = Point(x, y) - + x, y = map(int, input().split()) b = Point(x, y) - + x, y = map(int, input().split()) c = Point(x, y) - + T = Triangle(a, b, c) print("{:.2f}".format(get_area(T))) diff --git "a/niuke/practice/noob/noob77 \346\261\202\345\263\260\350\260\267\347\202\271\346\225\260/1.cpp" "b/niuke/practice/noob/noob77 \346\261\202\345\263\260\350\260\267\347\202\271\346\225\260/1.cpp" index 4c01e38a..434794e1 100644 --- "a/niuke/practice/noob/noob77 \346\261\202\345\263\260\350\260\267\347\202\271\346\225\260/1.cpp" +++ "b/niuke/practice/noob/noob77 \346\261\202\345\263\260\350\260\267\347\202\271\346\225\260/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-08 10:35:53 diff --git "a/niuke/practice/noob/noob78 \345\220\221\351\207\217\347\202\271\344\271\230/1.cpp" "b/niuke/practice/noob/noob78 \345\220\221\351\207\217\347\202\271\344\271\230/1.cpp" index ec2ec8c9..18f11eb7 100644 --- "a/niuke/practice/noob/noob78 \345\220\221\351\207\217\347\202\271\344\271\230/1.cpp" +++ "b/niuke/practice/noob/noob78 \345\220\221\351\207\217\347\202\271\344\271\230/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-18 14:32:54 diff --git "a/niuke/practice/noob/noob79 \345\220\221\351\207\217\345\217\211\344\271\230/1.cpp" "b/niuke/practice/noob/noob79 \345\220\221\351\207\217\345\217\211\344\271\230/1.cpp" index 795d24fd..ac334223 100644 --- "a/niuke/practice/noob/noob79 \345\220\221\351\207\217\345\217\211\344\271\230/1.cpp" +++ "b/niuke/practice/noob/noob79 \345\220\221\351\207\217\345\217\211\344\271\230/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: /** diff --git a/niuke/practice/noob/noob8/noob8.cpp b/niuke/practice/noob/noob8/noob8.cpp index b159a5e5..5e5a243a 100644 --- a/niuke/practice/noob/noob8/noob8.cpp +++ b/niuke/practice/noob/noob8/noob8.cpp @@ -15,4 +15,4 @@ int main() { //cout.unsetf(ios::fixed); cout.flush(); return 0; -} \ No newline at end of file +} diff --git "a/niuke/practice/noob/noob80 \346\227\272\344\273\224\345\223\245\345\223\245\346\214\244\345\234\260\351\223\201/1.cpp" "b/niuke/practice/noob/noob80 \346\227\272\344\273\224\345\223\245\345\223\245\346\214\244\345\234\260\351\223\201/1.cpp" index 88d1b911..3d4e4f37 100644 --- "a/niuke/practice/noob/noob80 \346\227\272\344\273\224\345\223\245\345\223\245\346\214\244\345\234\260\351\223\201/1.cpp" +++ "b/niuke/practice/noob/noob80 \346\227\272\344\273\224\345\223\245\345\223\245\346\214\244\345\234\260\351\223\201/1.cpp" @@ -1,3 +1,4 @@ +#include /* * @Author: tkzzzzzz6 * @Date: 2026-02-18 15:24:15 diff --git "a/niuke/practice/noob/noob82 \346\227\272\344\273\224\345\223\245\345\223\245\350\275\254\345\234\210\345\234\210/1.cpp" "b/niuke/practice/noob/noob82 \346\227\272\344\273\224\345\223\245\345\223\245\350\275\254\345\234\210\345\234\210/1.cpp" index 2c204102..2d06194f 100644 --- "a/niuke/practice/noob/noob82 \346\227\272\344\273\224\345\223\245\345\223\245\350\275\254\345\234\210\345\234\210/1.cpp" +++ "b/niuke/practice/noob/noob82 \346\227\272\344\273\224\345\223\245\345\223\245\350\275\254\345\234\210\345\234\210/1.cpp" @@ -1,3 +1,4 @@ +#include class Solution { public: /** diff --git "a/niuke/practice/noob/noob88 \347\211\233\347\211\233\344\270\216\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/1.cpp" "b/niuke/practice/noob/noob88 \347\211\233\347\211\233\344\270\216\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/1.cpp" index 4415059f..bdedee33 100644 --- "a/niuke/practice/noob/noob88 \347\211\233\347\211\233\344\270\216\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/1.cpp" +++ "b/niuke/practice/noob/noob88 \347\211\233\347\211\233\344\270\216\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/1.cpp" @@ -10,7 +10,7 @@ class Solution { * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 给定一个后缀表达式,返回它的结果 - * @param str string字符串 + * @param str string字符串 * @return long长整型 */ long long legalExp(string str) { @@ -19,7 +19,7 @@ class Solution { for(int i = 0;i> q; - + while(q--){ stack st; int n; diff --git "a/niuke/practice/noob/noob95 \351\230\237\345\210\227\346\266\210\346\225\260/1.cpp" "b/niuke/practice/noob/noob95 \351\230\237\345\210\227\346\266\210\346\225\260/1.cpp" index d6a2fc75..05d35bde 100644 --- "a/niuke/practice/noob/noob95 \351\230\237\345\210\227\346\266\210\346\225\260/1.cpp" +++ "b/niuke/practice/noob/noob95 \351\230\237\345\210\227\346\266\210\346\225\260/1.cpp" @@ -27,6 +27,6 @@ class Solution { ans += min(tickets[k] - 1,tickets[i]); } } - return ans; + return ans; } }; diff --git "a/niuke/practice/techtree/ch1/\345\260\217\351\243\236\346\234\272/3.cpp" "b/niuke/practice/techtree/ch1/\345\260\217\351\243\236\346\234\272/3.cpp" index 36384aed..11a727a8 100644 --- "a/niuke/practice/techtree/ch1/\345\260\217\351\243\236\346\234\272/3.cpp" +++ "b/niuke/practice/techtree/ch1/\345\260\217\351\243\236\346\234\272/3.cpp" @@ -1,3 +1,4 @@ +#include #include #include using namespace std; @@ -31,4 +32,4 @@ int main() } return 0; -} \ No newline at end of file +} diff --git "a/niuke/practice/techtree/ch1/\347\211\233\347\211\233\347\232\204\347\251\272\346\240\274\345\210\206\351\232\224/1.cpp" "b/niuke/practice/techtree/ch1/\347\211\233\347\211\233\347\232\204\347\251\272\346\240\274\345\210\206\351\232\224/1.cpp" index 8bf6aacb..4054172c 100644 --- "a/niuke/practice/techtree/ch1/\347\211\233\347\211\233\347\232\204\347\251\272\346\240\274\345\210\206\351\232\224/1.cpp" +++ "b/niuke/practice/techtree/ch1/\347\211\233\347\211\233\347\232\204\347\251\272\346\240\274\345\210\206\351\232\224/1.cpp" @@ -27,13 +27,13 @@ il void solve(){ int main() { fastio; - + int t = 1; // cin >> t; while(t--) { solve(); } - + return 0; } diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP16.\345\217\221\351\200\201offer.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP16.\345\217\221\351\200\201offer.py" index 769b8dfb..58bd17fc 100644 --- "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP16.\345\217\221\351\200\201offer.py" +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP16.\345\217\221\351\200\201offer.py" @@ -33,7 +33,7 @@ def solve(): for name in offer_list: printSentence2(name) - + if __name__ == "__main__": sys.setrecursionlimit(10**7) diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP23.\345\210\240\351\231\244\345\245\275\345\217\213.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP23.\345\210\240\351\231\244\345\245\275\345\217\213.py" index f6f3b3a9..41c4d946 100644 --- "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP23.\345\210\240\351\231\244\345\245\275\345\217\213.py" +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP23.\345\210\240\351\231\244\345\245\275\345\217\213.py" @@ -13,7 +13,7 @@ def solve(): friends = input().split() remove_friends_name = input() - + if remove_friends_name in friends: friends.remove(remove_friends_name) print(friends) diff --git a/note/vector/c1.cpp b/note/vector/c1.cpp index c30d15f1..df696fab 100644 --- a/note/vector/c1.cpp +++ b/note/vector/c1.cpp @@ -1,36 +1,36 @@ -#include -#include - -using namespace std; -/* - *动态数组 - *人员管理 - */ - -int main() -{ - vector vec1; - vector vec2(3); - vector vec3(3, "ABC"); - vector vec4(vec3); - for (int i = 0; i < 6; i++) - { - vec1.push_back(i); - } - vec2.push_back(1); - vector vec5(vec1.begin(), vec1.end()); - - // vec1.clear(); - int max = vec1.max_size(); - cout << "max is " << max << endl; - if (vec5.empty()) - cout << "vector is empty" << endl; - else - { - cout << "vec5 = " << endl; - for (int i = 0; i < vec5.size(); i++) - { - cout << vec5[i] << endl; - } - } -} \ No newline at end of file +#include +#include + +using namespace std; +/* + *动态数组 + *人员管理 + */ + +int main() +{ + vector vec1; + vector vec2(3); + vector vec3(3, "ABC"); + vector vec4(vec3); + for (int i = 0; i < 6; i++) + { + vec1.push_back(i); + } + vec2.push_back(1); + vector vec5(vec1.begin(), vec1.end()); + + // vec1.clear(); + int max = vec1.max_size(); + cout << "max is " << max << endl; + if (vec5.empty()) + cout << "vector is empty" << endl; + else + { + cout << "vec5 = " << endl; + for (int i = 0; i < vec5.size(); i++) + { + cout << vec5[i] << endl; + } + } +} diff --git a/online_judge/10487-Closest Sums.cpp b/online_judge/10487-Closest Sums.cpp index cad9e8f7..9024fced 100644 --- a/online_judge/10487-Closest Sums.cpp +++ b/online_judge/10487-Closest Sums.cpp @@ -1,24 +1,25 @@ -#include - -using namespace std; - -int main(){ - - int n; - cin >> n; - vector a(n); - for (auto &i : a) - cin >> i; - int N = 10, x = 6; - // lower_bound将返回a中第一个大于等于x的元素的地址,计算出的i为其下标 - int i = lower_bound(a, a + N, x) - a; - // 在以下两种情况下,a[i] (a中第一个大于等于x的元素) 即为答案: - // 1. a中最小的元素都大于等于x; - // 2. a中存在大于等于x的元素,且第一个大于等于x的元素 (a[i]) - // 相比于第一个小于x的元素 (a[i - 1]) 更接近x; - // 否则,a[i - 1] (a中第一个小于x的元素) 即为答案 - if (i == 0 || (i < N && a[i] - x < x - a[i - 1])) - cout << a[i]; - else - cout << a[i - 1]; -} +#include +#include + +using namespace std; + +int main(){ + + int n; + cin >> n; + vector a(n); + for (auto &i : a) + cin >> i; + int N = 10, x = 6; + // lower_bound将返回a中第一个大于等于x的元素的地址,计算出的i为其下标 + int i = lower_bound(a, a + N, x) - a; + // 在以下两种情况下,a[i] (a中第一个大于等于x的元素) 即为答案: + // 1. a中最小的元素都大于等于x; + // 2. a中存在大于等于x的元素,且第一个大于等于x的元素 (a[i]) + // 相比于第一个小于x的元素 (a[i - 1]) 更接近x; + // 否则,a[i - 1] (a中第一个小于x的元素) 即为答案 + if (i == 0 || (i < N && a[i] - x < x - a[i - 1])) + cout << a[i]; + else + cout << a[i - 1]; +} diff --git a/open_judge/2811/1.cpp b/open_judge/2811/1.cpp index 79680644..b0b973cf 100644 --- a/open_judge/2811/1.cpp +++ b/open_judge/2811/1.cpp @@ -1,67 +1,67 @@ -#include -#include -#include - -using namespace std; - -int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0}; -int g[5][6], backup[5][6], res[5][6]; - -void turn(int a,int b){ - res[a][b] = 1; - for (int i = 0;i < 5;++i){ - int x = a + dx[i]; - int y = b + dy[i]; - if(x < 0 || y < 0 || x >= 5 || y >= 6) - continue; - g[x][y] ^= 1; - } - -} - -int main() -{ - for(int i = 0; i < 5; i++){ - for(int j = 0; j < 6; j++){ - cin >> g[i][j]; - } - } - - memcpy(backup, g, sizeof g); - for (int op = 0; op < (1<<6);++op){ - memcpy(g, backup, sizeof backup); - memset(res, 0, sizeof res); - - for (int j = 0;j < 6;++j){ - if(op >> j & 1) - turn(0,j); - } - - for (int i = 0;i < 4;++i){ - for (int j = 0; j < 6;++j){ - if(g[i][j] == 1) - turn(i+1,j); - } - } - - bool dark = true; - for (int i = 0;i < 6;++i){ - if(g[4][i] == 1){ - dark = false; - break; - } - } - - if(dark){ - for(int i = 0;i < 5;++i){ - for(int j = 0;j < 6;++j){ - cout << res[i][j] << " "; - } - cout << endl; - } - break; - } - - } - return 0; -} +#include +#include +#include + +using namespace std; + +int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0}; +int g[5][6], backup[5][6], res[5][6]; + +void turn(int a,int b){ + res[a][b] = 1; + for (int i = 0;i < 5;++i){ + int x = a + dx[i]; + int y = b + dy[i]; + if(x < 0 || y < 0 || x >= 5 || y >= 6) + continue; + g[x][y] ^= 1; + } + +} + +int main() +{ + for(int i = 0; i < 5; i++){ + for(int j = 0; j < 6; j++){ + cin >> g[i][j]; + } + } + + memcpy(backup, g, sizeof g); + for (int op = 0; op < (1<<6);++op){ + memcpy(g, backup, sizeof backup); + memset(res, 0, sizeof res); + + for (int j = 0;j < 6;++j){ + if(op >> j & 1) + turn(0,j); + } + + for (int i = 0;i < 4;++i){ + for (int j = 0; j < 6;++j){ + if(g[i][j] == 1) + turn(i+1,j); + } + } + + bool dark = true; + for (int i = 0;i < 6;++i){ + if(g[4][i] == 1){ + dark = false; + break; + } + } + + if(dark){ + for(int i = 0;i < 5;++i){ + for(int j = 0;j < 6;++j){ + cout << res[i][j] << " "; + } + cout << endl; + } + break; + } + + } + return 0; +} diff --git a/open_judge/2811/2.cpp b/open_judge/2811/2.cpp index 5822f8b6..d2e77841 100644 --- a/open_judge/2811/2.cpp +++ b/open_judge/2811/2.cpp @@ -1,89 +1,89 @@ -#include -#include -#include -using namespace std; - -// g数组存储灯的状态,backup为备份数组,res存储操作结果(按下哪些开关) -int g[5][6], backup[5][6], res[5][6]; -// dx和dy数组用于表示自身及四个方向(上、右、下、左、自身)的坐标偏移 -int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0}; - -// 按下(a,b)位置的开关 -void turn(int a, int b) -{ - res[a][b] = 1; // 记录在(a,b)位置按下了开关 - for (int i = 0; i < 5; ++i) - { - int x = a + dx[i]; - int y = b + dy[i]; - if (x < 0 || y < 0 || x >= 5 || y >= 6) - continue; // 如果坐标超出边界则跳过 - g[x][y] ^= 1; // 使用异或运算切换灯的状态(0变1,1变0) - } - - return; -} - -int main() -{ - int n; - cin >> n; // 输入谜题数量 - for (int p = 1; p <= n; ++p) - { // 处理每个谜题 - - // 输入灯的初始状态 - for (int i = 0; i < 5; ++i) - for (int j = 0; j < 6; ++j) - { - cin >> g[i][j]; - } - memcpy(backup, g, sizeof g); // 备份初始状态 - - // 枚举第一行所有可能的按法(2^6=64种可能) - for (int op = 0; op < (1 << 6); ++op) - { - // 恢复初始状态 - memcpy(g, backup, sizeof backup); - memset(res, 0, sizeof res); // 清空操作记录 - - // 处理第一行的按法 - for (int j = 0; j < 6; ++j) - { - if (op >> j & 1) // 检查第j位是否为1(是否按下第j个开关) - turn(0, j); // 按下第一行第j列的开关 - } - - // 处理第2-5行:如果上一行有亮着的灯,则必须按下当前行对应位置的开关 - for (int i = 0; i < 4; ++i) - for (int j = 0; j < 6; ++j) - { - if (g[i][j] == 1) // 如果当前位置的灯亮着 - turn(i + 1, j); // 按下下一行对应位置的开关 - } - - // 检查最后一行是否全部熄灭 - bool dark = true; - for (int i = 0; i < 6; ++i) - if (g[4][i] == 1) // 如果最后一行有灯亮着 - { - dark = false; // 标记为未解决 - break; - } - - // 如果找到解决方案(最后一行全部熄灭) - if (dark) - { - cout << "PUZZLE #" << p << endl; // 输出谜题编号 - // 输出解决方案(哪些开关需要按下) - for (int i = 0; i < 5; ++i) - { - for (int j = 0; j < 6; ++j) - cout << res[i][j] << ' '; - cout << endl; - } - break; // 找到解决方案后跳出循环,处理下一个谜题 - } - } - } - return 0; -} \ No newline at end of file +#include +#include +#include +using namespace std; + +// g数组存储灯的状态,backup为备份数组,res存储操作结果(按下哪些开关) +int g[5][6], backup[5][6], res[5][6]; +// dx和dy数组用于表示自身及四个方向(上、右、下、左、自身)的坐标偏移 +int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0}; + +// 按下(a,b)位置的开关 +void turn(int a, int b) +{ + res[a][b] = 1; // 记录在(a,b)位置按下了开关 + for (int i = 0; i < 5; ++i) + { + int x = a + dx[i]; + int y = b + dy[i]; + if (x < 0 || y < 0 || x >= 5 || y >= 6) + continue; // 如果坐标超出边界则跳过 + g[x][y] ^= 1; // 使用异或运算切换灯的状态(0变1,1变0) + } + + return; +} + +int main() +{ + int n; + cin >> n; // 输入谜题数量 + for (int p = 1; p <= n; ++p) + { // 处理每个谜题 + + // 输入灯的初始状态 + for (int i = 0; i < 5; ++i) + for (int j = 0; j < 6; ++j) + { + cin >> g[i][j]; + } + memcpy(backup, g, sizeof g); // 备份初始状态 + + // 枚举第一行所有可能的按法(2^6=64种可能) + for (int op = 0; op < (1 << 6); ++op) + { + // 恢复初始状态 + memcpy(g, backup, sizeof backup); + memset(res, 0, sizeof res); // 清空操作记录 + + // 处理第一行的按法 + for (int j = 0; j < 6; ++j) + { + if (op >> j & 1) // 检查第j位是否为1(是否按下第j个开关) + turn(0, j); // 按下第一行第j列的开关 + } + + // 处理第2-5行:如果上一行有亮着的灯,则必须按下当前行对应位置的开关 + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 6; ++j) + { + if (g[i][j] == 1) // 如果当前位置的灯亮着 + turn(i + 1, j); // 按下下一行对应位置的开关 + } + + // 检查最后一行是否全部熄灭 + bool dark = true; + for (int i = 0; i < 6; ++i) + if (g[4][i] == 1) // 如果最后一行有灯亮着 + { + dark = false; // 标记为未解决 + break; + } + + // 如果找到解决方案(最后一行全部熄灭) + if (dark) + { + cout << "PUZZLE #" << p << endl; // 输出谜题编号 + // 输出解决方案(哪些开关需要按下) + for (int i = 0; i < 5; ++i) + { + for (int j = 0; j < 6; ++j) + cout << res[i][j] << ' '; + cout << endl; + } + break; // 找到解决方案后跳出循环,处理下一个谜题 + } + } + } + return 0; +} diff --git a/open_judge/2811/3.cpp b/open_judge/2811/3.cpp index 30bd34ee..3e3236bd 100644 --- a/open_judge/2811/3.cpp +++ b/open_judge/2811/3.cpp @@ -1,16 +1,16 @@ -#include -constexpr int MAXN = 100000; // 此处 MAXN 是数组内元素的界 - -int solve(int n, int a[]) -{ - bool met[MAXN * 2 + 1]; // 创建一个能装下 [-MAXN, MAXN] 的桶 - memset(met, 0, sizeof(met)); - int ans = 0; - for (int i = 0; i < n; ++i) - { - if (met[MAXN - a[i]]) - ++ans; // 如果桶内有想要的元素,答案加一 - met[MAXN + a[i]] = true; // 无论如何,都要把当前元素放进桶里 - } - return ans; -} \ No newline at end of file +#include +constexpr int MAXN = 100000; // 此处 MAXN 是数组内元素的界 + +int solve(int n, int a[]) +{ + bool met[MAXN * 2 + 1]; // 创建一个能装下 [-MAXN, MAXN] 的桶 + memset(met, 0, sizeof(met)); + int ans = 0; + for (int i = 0; i < n; ++i) + { + if (met[MAXN - a[i]]) + ++ans; // 如果桶内有想要的元素,答案加一 + met[MAXN + a[i]] = true; // 无论如何,都要把当前元素放进桶里 + } + return ans; +} diff --git a/open_judge/2811/4.cpp b/open_judge/2811/4.cpp index 7db45a9a..b1e4bcdc 100644 --- a/open_judge/2811/4.cpp +++ b/open_judge/2811/4.cpp @@ -1,19 +1,19 @@ -#include -#include -#include - -using namespace std; - -int main(){ - int n = 100; - int u = 3; - int d = 2; - int i = 0; - int cnt = 0; - while(i >= n){ - i += u; - cnt++; - i -= d; - } - cout << cnt << endl; -} +#include +#include +#include + +using namespace std; + +int main(){ + int n = 100; + int u = 3; + int d = 2; + int i = 0; + int cnt = 0; + while(i >= n){ + i += u; + cnt++; + i -= d; + } + cout << cnt << endl; +} diff --git a/open_judge/3750/1.cpp b/open_judge/3750/1.cpp index f38bc059..0b24dbd0 100644 --- a/open_judge/3750/1.cpp +++ b/open_judge/3750/1.cpp @@ -1,842 +1,842 @@ -#include -#include -#include -#include -#include -#include - -using namespace std; - -enum WarriorType -{ - DRAGON, - NINJA, - ICEMAN, - LION, - WOLF -}; -enum Color -{ - RED, - BLUE -}; - -// 前向声明 -class Headquarters; -class City; - -class Warrior -{ -private: - int id; - WarriorType type; - int health; - int attack; - Color color; - int position; // 0 表示在红色司令部,n+1 表示在蓝色司令部,1~n 表示在城市中 - Headquarters *headquarters; - int killCount; // 记录该武士杀死敌人的次数 - -public: - Warrior(int id, WarriorType type, int health, int attack, Color color, Headquarters *headquarters) - : id(id), type(type), health(health), attack(attack), color(color), headquarters(headquarters), killCount(0) - { - position = 0; // 临时初始值,会在Headquarters中设置 - } - - virtual ~Warrior() {} - - int getId() const { return id; } - WarriorType getType() const { return type; } - int getHealth() const { return health; } - int getAttack() const { return attack; } - Color getColor() const { return color; } - int getPosition() const { return position; } - void setPosition(int pos) { position = pos; } - int getKillCount() const { return killCount; } - - void increaseHealth(int amount) { health += amount; } - void decreaseHealth(int amount) { health = max(0, health - amount); } - void increaseAttack(int amount) { attack += amount; } - - bool isAlive() const { return health > 0; } - - virtual void march() - { - if (color == RED) - { - position++; - } - else - { - position--; - } - } - - virtual void attack(Warrior *enemy) - { - enemy->beAttacked(attack); - if (!enemy->isAlive()) - { - killCount++; - } - } - - virtual void beAttacked(int damage) - { - health -= damage; - } - - virtual void fightBack(Warrior *enemy) - { - int backDamage = attack / 2; - enemy->beAttacked(backDamage); - } - - // 返回武士类型的字符串 - string getTypeStr() const - { - switch (type) - { - case DRAGON: - return "dragon"; - case NINJA: - return "ninja"; - case ICEMAN: - return "iceman"; - case LION: - return "lion"; - case WOLF: - return "wolf"; - default: - return "unknown"; - } - } - - // 返回武士所属的颜色字符串 - string getColorStr() const - { - return (color == RED) ? "red" : "blue"; - } - - // 由子类实现的特殊行为 - virtual void specialAction() {} - virtual void afterKill(Warrior *enemy) {} -}; - -// Dragon特有行为 -class Dragon : public Warrior -{ -public: - Dragon(int id, int health, int attack, Color color, Headquarters *headquarters) - : Warrior(id, DRAGON, health, attack, color, headquarters) {} - - // Dragon在主动进攻后会欢呼 - void specialAction() override - { - printf("%03d:40 %s dragon %d yelled in city %d\n", - getPosition() / 60, getColorStr().c_str(), getId(), getPosition()); - } -}; - -// Ninja特有行为 -class Ninja : public Warrior -{ -public: - Ninja(int id, int health, int attack, Color color, Headquarters *headquarters) - : Warrior(id, NINJA, health, attack, color, headquarters) {} - - // Ninja不会反击 - void fightBack(Warrior *enemy) override {} -}; - -// Iceman特有行为 -class Iceman : public Warrior -{ -private: - int stepCount; - -public: - Iceman(int id, int health, int attack, Color color, Headquarters *headquarters) - : Warrior(id, ICEMAN, health, attack, color, headquarters), stepCount(0) {} - - // Iceman每前进两步,生命值减少9,攻击力增加20 - void march() override - { - Warrior::march(); - stepCount++; - if (stepCount % 2 == 0) - { - if (getHealth() <= 9) - { - // 生命值不会低于1 - decreaseHealth(getHealth() - 1); - } - else - { - decreaseHealth(9); - } - increaseAttack(20); - } - } -}; - -// Lion特有行为 -class Lion : public Warrior -{ -public: - Lion(int id, int health, int attack, Color color, Headquarters *headquarters) - : Warrior(id, LION, health, attack, color, headquarters) {} - - // Lion死亡时,生命值转移给对手 - void beAttacked(int damage) override - { - int before = getHealth(); - Warrior::beAttacked(damage); - if (!isAlive()) - { - // 转移生命值的逻辑在战斗处理中实现 - } - } -}; - -// Wolf特有行为 -class Wolf : public Warrior -{ -public: - Wolf(int id, int health, int attack, Color color, Headquarters *headquarters) - : Warrior(id, WOLF, health, attack, color, headquarters) {} - - // Wolf在杀死敌人次数为偶数时,生命值和攻击力翻倍 - void afterKill(Warrior *enemy) override - { - if (getKillCount() % 2 == 0 && getKillCount() > 0) - { - increaseHealth(getHealth()); - increaseAttack(getAttack()); - } - } -}; - -class Headquarters -{ -private: - Color color; - int elements; // 生命元 - int totalWarriors; - vector warriors; - vector warriorSequence; // 制造武士的顺序 - int currentSequenceIndex; - int cityCount; // 城市数量,用于确定蓝色司令部位置 - - // 各类武士的初始属性 - vector initialHealth; - vector initialAttack; - -public: - Headquarters(Color color, int initialElements, int cityCount, - vector initHealth, vector initAttack) - : color(color), elements(initialElements), totalWarriors(0), - currentSequenceIndex(0), cityCount(cityCount), - initialHealth(initHealth), initialAttack(initAttack) - { - - // 设置武士制造顺序 - if (color == RED) - { - warriorSequence = {ICEMAN, LION, WOLF, NINJA, DRAGON}; - } - else - { - warriorSequence = {LION, DRAGON, NINJA, ICEMAN, WOLF}; - } - } - - Color getColor() const { return color; } - int getElements() const { return elements; } - - bool canMakeWarrior() const - { - WarriorType nextType = warriorSequence[currentSequenceIndex]; - return elements >= initialHealth[nextType]; - } - - // 制造下一个武士 - Warrior *produceWarrior() - { - if (!canMakeWarrior()) - { - return nullptr; - } - - WarriorType type = warriorSequence[currentSequenceIndex]; - int warriorId = ++totalWarriors; - int health = initialHealth[type]; - int attack = initialAttack[type]; - - // 减少生命元 - elements -= health; - - // 更新下一个要制造的武士类型 - currentSequenceIndex = (currentSequenceIndex + 1) % warriorSequence.size(); - - Warrior *warrior = nullptr; - switch (type) - { - case DRAGON: - warrior = new Dragon(warriorId, health, attack, color, this); - break; - case NINJA: - warrior = new Ninja(warriorId, health, attack, color, this); - break; - case ICEMAN: - warrior = new Iceman(warriorId, health, attack, color, this); - break; - case LION: - warrior = new Lion(warriorId, health, attack, color, this); - break; - case WOLF: - warrior = new Wolf(warriorId, health, attack, color, this); - break; - } - - // 设置初始位置 - if (color == RED) - { - warrior->setPosition(0); - } - else - { - warrior->setPosition(cityCount + 1); - } - - warriors.push_back(warrior); - return warrior; - } - - void addElements(int count) - { - elements += count; - } - - void reportElements() - { - cout << elements << " elements in " << (color == RED ? "red" : "blue") << " headquarter" << endl; - } - - void decreaseElements(int amount) - { - elements -= amount; - } -}; - -class City -{ -private: - int id; - int elements; - Color flag; // RED, BLUE或-1表示无旗帜 - bool hasFlag; - Color lastWinner; // 记录上一次胜利方 - int consecutiveWins; // 记录连续胜利次数 - -public: - City(int id) : id(id), elements(0), hasFlag(false), consecutiveWins(0) {} - - int getId() const { return id; } - int getElements() const { return elements; } - - void produceElements() - { - elements += 10; - } - - void takeAllElements() - { - elements = 0; - } - - void battle(Warrior *red, Warrior *blue, int hour) - { - // 战斗逻辑将在Game类中实现 - } - - Color getFlag() const { return flag; } - bool hasFlag() const { return hasFlag; } - - void updateFlag(Color winner, int hour) - { - if (lastWinner == winner) - { - consecutiveWins++; - if (consecutiveWins == 2 && (!hasFlag || flag != winner)) - { - hasFlag = true; - flag = winner; - // 打印旗帜升起事件 - printf("%03d:40 %s flag raised in city %d\n", - hour, winner == RED ? "red" : "blue", id); - } - } - else - { - lastWinner = winner; - consecutiveWins = 1; - } - } -}; - -class Game -{ -private: - int M, N, T; // M: 初始生命元, N: 城市数量, T: 结束时间(分钟) - vector initialHealth; - vector initialAttack; - Headquarters *redHQ; - Headquarters *blueHQ; - vector cities; - map> warriorsInCity; // 每个位置的武士列表 - int redHQEnemyCount = 0; // 红方司令部敌人数量 - int blueHQEnemyCount = 0; // 蓝方司令部敌人数量 - int currentTime; // 当前时间(分钟) - bool gameOver; - -public: - Game(int M, int N, int T, vector health, vector attack) - : M(M), N(N), T(T), initialHealth(health), initialAttack(attack), - currentTime(0), gameOver(false) - { - - // 创建总部 - redHQ = new Headquarters(RED, M, N, initialHealth, initialAttack); - blueHQ = new Headquarters(BLUE, M, N, initialHealth, initialAttack); - - // 创建城市 - for (int i = 1; i <= N; i++) - { - cities.push_back(new City(i)); - } - } - - ~Game() - { - delete redHQ; - delete blueHQ; - for (auto city : cities) - { - delete city; - } - for (auto warrior : warriorsInCity) - { - for (Warrior *w : warrior.second) - { - delete w; - } - } - } - - void run() - { - while (currentTime <= T && !gameOver) - { - // 处理当前时间的事件 - int hour = currentTime / 60; - int minute = currentTime % 60; - - switch (minute) - { - case 0: // 整点:武士诞生 - produceWarriors(); - break; - case 10: // 10分:武士前进 - warriorsAdvance(); - break; - case 20: // 20分:城市产生生命元 - citiesProduceElements(); - break; - case 30: // 30分:武士获取生命元 - warriorsTakeElements(); - break; - case 40: // 40分:战斗 - battles(); - break; - case 50: // 50分:总部报告生命元数量 - reportElements(); - break; - } - - currentTime++; - } - } - -private: - void produceWarriors() - { - int hour = currentTime / 60; - - // 红方司令部生产武士 - Warrior *redWarrior = redHQ->produceWarrior(); - if (redWarrior) - { - printf("%03d:00 red %s %d born\n", - hour, redWarrior->getTypeStr().c_str(), redWarrior->getId()); - addWarriorToCity(redWarrior, 0); - } - - // 蓝方司令部生产武士 - Warrior *blueWarrior = blueHQ->produceWarrior(); - if (blueWarrior) - { - printf("%03d:00 blue %s %d born\n", - hour, blueWarrior->getTypeStr().c_str(), blueWarrior->getId()); - addWarriorToCity(blueWarrior, N + 1); - } - } - - void warriorsAdvance() - { - int hour = currentTime / 60; - map> newPositions; - - // 处理所有位置的武士 - for (int pos = 0; pos <= N + 1; pos++) - { - if (!warriorsInCity[pos].empty()) - { - for (Warrior *warrior : warriorsInCity[pos]) - { - int oldPos = warrior->getPosition(); - - // 武士前进 - warrior->march(); - int newPos = warrior->getPosition(); - - // 如果到达敌方司令部 - if ((warrior->getColor() == RED && newPos == N + 1) || - (warrior->getColor() == BLUE && newPos == 0)) - { - printf("%03d:10 %s %s %d reached %s headquarter with %d elements and force %d\n", - hour, warrior->getColorStr().c_str(), warrior->getTypeStr().c_str(), - warrior->getId(), - warrior->getColor() == RED ? "blue" : "red", - warrior->getHealth(), warrior->getAttack()); - - // 增加敌方司令部的敌人计数 - if (warrior->getColor() == RED) - { - blueHQEnemyCount++; - if (blueHQEnemyCount == 2) - { - printf("%03d:10 blue headquarter was taken\n", hour); - gameOver = true; - } - } - else - { - redHQEnemyCount++; - if (redHQEnemyCount == 2) - { - printf("%03d:10 red headquarter was taken\n", hour); - gameOver = true; - } - } - } - // 如果在城市间移动 - else if (oldPos != newPos) - { - printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n", - hour, warrior->getColorStr().c_str(), warrior->getTypeStr().c_str(), - warrior->getId(), newPos, - warrior->getHealth(), warrior->getAttack()); - } - - // 将武士添加到新位置列表 - newPositions[newPos].push_back(warrior); - } - } - } - - // 更新位置列表 - warriorsInCity = newPositions; - } - - void citiesProduceElements() - { - for (auto city : cities) - { - city->produceElements(); - } - } - - void warriorsTakeElements() - { - int hour = currentTime / 60; - - // 遍历所有城市 - for (int i = 1; i <= N; i++) - { - City *city = cities[i - 1]; - vector &warriorsHere = warriorsInCity[i]; - - // 如果城市只有一个武士,他获取所有生命元 - if (warriorsHere.size() == 1) - { - Warrior *warrior = warriorsHere[0]; - int cityElements = city->getElements(); - if (cityElements > 0) - { - // 武士获取生命元并传送到总部 - if (warrior->getColor() == RED) - { - redHQ->addElements(cityElements); - } - else - { - blueHQ->addElements(cityElements); - } - - printf("%03d:30 %s %s %d earned %d elements for his headquarter\n", - hour, warrior->getColorStr().c_str(), warrior->getTypeStr().c_str(), - warrior->getId(), cityElements); - - // 城市生命元清零 - city->takeAllElements(); - } - } - } - } - - void battles() - { - int hour = currentTime / 60; - - // 在每个城市进行战斗 - for (int i = 1; i <= N; i++) - { - City *city = cities[i - 1]; - vector &warriorsHere = warriorsInCity[i]; - - // 如果城市有两个武士 - if (warriorsHere.size() == 2) - { - Warrior *redWarrior = nullptr; - Warrior *blueWarrior = nullptr; - - // 确定红蓝双方武士 - for (Warrior *warrior : warriorsHere) - { - if (warrior->getColor() == RED) - { - redWarrior = warrior; - } - else - { - blueWarrior = warrior; - } - } - - if (!redWarrior || !blueWarrior) - continue; // 安全检查 - - // 确定谁先攻击 - bool redFirst = false; - if (city->hasFlag()) - { - redFirst = (city->getFlag() == RED); - } - else - { - redFirst = (i % 2 == 1); // 奇数城市红方先攻 - } - - Warrior *attacker = redFirst ? redWarrior : blueWarrior; - Warrior *defender = redFirst ? blueWarrior : redWarrior; - - // 记录战前生命值 - int attackerHealthBefore = attacker->getHealth(); - int defenderHealthBefore = defender->getHealth(); - - // 打印攻击信息 - printf("%03d:40 %s %s %d attacked %s %s %d in city %d with %d elements and force %d\n", - hour, attacker->getColorStr().c_str(), attacker->getTypeStr().c_str(), - attacker->getId(), defender->getColorStr().c_str(), defender->getTypeStr().c_str(), - defender->getId(), i, attacker->getHealth(), attacker->getAttack()); - - // 进行攻击 - attacker->attack(defender); - - // 如果防守方死亡 - if (!defender->isAlive()) - { - printf("%03d:40 %s %s %d was killed in city %d\n", - hour, defender->getColorStr().c_str(), defender->getTypeStr().c_str(), - defender->getId(), i); - - // 如果是lion死亡,转移生命值给攻击方 - if (defender->getType() == LION) - { - attacker->increaseHealth(defenderHealthBefore); - } - - // wolf杀死敌人的特殊处理 - attacker->afterKill(defender); - - // 攻击方总部奖励8个生命元 - if (attacker->getColor() == RED) - { - if (redHQ->getElements() >= 8) - { - redHQ->decreaseElements(8); - attacker->increaseHealth(8); - } - } - else - { - if (blueHQ->getElements() >= 8) - { - blueHQ->decreaseElements(8); - attacker->increaseHealth(8); - } - } - - // 攻击方获取城市所有生命元 - int cityElements = city->getElements(); - if (cityElements > 0) - { - if (attacker->getColor() == RED) - { - redHQ->addElements(cityElements); - } - else - { - blueHQ->addElements(cityElements); - } - city->takeAllElements(); - } - - // 更新城市旗帜 - city->updateFlag(attacker->getColor(), hour); - - // 如果是dragon,则欢呼 - if (attacker->getType() == DRAGON) - { - attacker->specialAction(); - } - } - // 如果防守方没死,可能会反击 - else - { - // 如果不是ninja,就会反击 - if (defender->getType() != NINJA) - { - printf("%03d:40 %s %s %d fought back against %s %s %d in city %d\n", - hour, defender->getColorStr().c_str(), defender->getTypeStr().c_str(), - defender->getId(), attacker->getColorStr().c_str(), attacker->getTypeStr().c_str(), - attacker->getId(), i); - - defender->fightBack(attacker); - - // 如果攻击方被反击死亡 - if (!attacker->isAlive()) - { - printf("%03d:40 %s %s %d was killed in city %d\n", - hour, attacker->getColorStr().c_str(), attacker->getTypeStr().c_str(), - attacker->getId(), i); - - // 如果是lion死亡,转移生命值 - if (attacker->getType() == LION) - { - defender->increaseHealth(attackerHealthBefore); - } - - // 更新城市旗帜 - city->updateFlag(defender->getColor(), hour); - } - // 如果都没死,是平局 - else - { - // 平局时城市不会改变旗帜,生命元也不会被拿走 - } - } - // ninja不反击 - else - { - // 如果是dragon,则欢呼 - if (attacker->getType() == DRAGON) - { - attacker->specialAction(); - } - } - } - - // 移除死亡的武士 - vector survivors; - for (Warrior *warrior : warriorsHere) - { - if (warrior->isAlive()) - { - survivors.push_back(warrior); - } - else - { - // 从warriors全局列表中移除 - auto it = find(warriorsInCity[i].begin(), warriorsInCity[i].end(), warrior); - if (it != warriorsInCity[i].end()) - { - warriorsInCity[i].erase(it); - } - delete warrior; - } - } - warriorsInCity[i] = survivors; - } - } - } - - void reportElements() - { - int hour = currentTime / 60; - printf("%03d:50 ", hour); - redHQ->reportElements(); - printf("%03d:50 ", hour); - blueHQ->reportElements(); - } - - // 辅助函数:将武士添加到城市列表 - void addWarriorToCity(Warrior *warrior, int position) - { - warriorsInCity[position].push_back(warrior); - } -}; - -int main() -{ - int caseCount; - cin >> caseCount; - - for (int caseNum = 1; caseNum <= caseCount; caseNum++) - { - int M, N, T; - cin >> M >> N >> T; - - vector initialHealth(5); - vector initialAttack(5); - - for (int i = 0; i < 5; i++) - { - cin >> initialHealth[i]; - } - - for (int i = 0; i < 5; i++) - { - cin >> initialAttack[i]; - } - - cout << "Case:" << caseNum << endl; - - Game game(M, N, T, initialHealth, initialAttack); - game.run(); - } - - return 0; -} +#include +#include +#include +#include +#include +#include + +using namespace std; + +enum WarriorType +{ + DRAGON, + NINJA, + ICEMAN, + LION, + WOLF +}; +enum Color +{ + RED, + BLUE +}; + +// 前向声明 +class Headquarters; +class City; + +class Warrior +{ +private: + int id; + WarriorType type; + int health; + int attack; + Color color; + int position; // 0 表示在红色司令部,n+1 表示在蓝色司令部,1~n 表示在城市中 + Headquarters *headquarters; + int killCount; // 记录该武士杀死敌人的次数 + +public: + Warrior(int id, WarriorType type, int health, int attack, Color color, Headquarters *headquarters) + : id(id), type(type), health(health), attack(attack), color(color), headquarters(headquarters), killCount(0) + { + position = 0; // 临时初始值,会在Headquarters中设置 + } + + virtual ~Warrior() {} + + int getId() const { return id; } + WarriorType getType() const { return type; } + int getHealth() const { return health; } + int getAttack() const { return attack; } + Color getColor() const { return color; } + int getPosition() const { return position; } + void setPosition(int pos) { position = pos; } + int getKillCount() const { return killCount; } + + void increaseHealth(int amount) { health += amount; } + void decreaseHealth(int amount) { health = max(0, health - amount); } + void increaseAttack(int amount) { attack += amount; } + + bool isAlive() const { return health > 0; } + + virtual void march() + { + if (color == RED) + { + position++; + } + else + { + position--; + } + } + + virtual void attack(Warrior *enemy) + { + enemy->beAttacked(attack); + if (!enemy->isAlive()) + { + killCount++; + } + } + + virtual void beAttacked(int damage) + { + health -= damage; + } + + virtual void fightBack(Warrior *enemy) + { + int backDamage = attack / 2; + enemy->beAttacked(backDamage); + } + + // 返回武士类型的字符串 + string getTypeStr() const + { + switch (type) + { + case DRAGON: + return "dragon"; + case NINJA: + return "ninja"; + case ICEMAN: + return "iceman"; + case LION: + return "lion"; + case WOLF: + return "wolf"; + default: + return "unknown"; + } + } + + // 返回武士所属的颜色字符串 + string getColorStr() const + { + return (color == RED) ? "red" : "blue"; + } + + // 由子类实现的特殊行为 + virtual void specialAction() {} + virtual void afterKill(Warrior *enemy) {} +}; + +// Dragon特有行为 +class Dragon : public Warrior +{ +public: + Dragon(int id, int health, int attack, Color color, Headquarters *headquarters) + : Warrior(id, DRAGON, health, attack, color, headquarters) {} + + // Dragon在主动进攻后会欢呼 + void specialAction() override + { + printf("%03d:40 %s dragon %d yelled in city %d\n", + getPosition() / 60, getColorStr().c_str(), getId(), getPosition()); + } +}; + +// Ninja特有行为 +class Ninja : public Warrior +{ +public: + Ninja(int id, int health, int attack, Color color, Headquarters *headquarters) + : Warrior(id, NINJA, health, attack, color, headquarters) {} + + // Ninja不会反击 + void fightBack(Warrior *enemy) override {} +}; + +// Iceman特有行为 +class Iceman : public Warrior +{ +private: + int stepCount; + +public: + Iceman(int id, int health, int attack, Color color, Headquarters *headquarters) + : Warrior(id, ICEMAN, health, attack, color, headquarters), stepCount(0) {} + + // Iceman每前进两步,生命值减少9,攻击力增加20 + void march() override + { + Warrior::march(); + stepCount++; + if (stepCount % 2 == 0) + { + if (getHealth() <= 9) + { + // 生命值不会低于1 + decreaseHealth(getHealth() - 1); + } + else + { + decreaseHealth(9); + } + increaseAttack(20); + } + } +}; + +// Lion特有行为 +class Lion : public Warrior +{ +public: + Lion(int id, int health, int attack, Color color, Headquarters *headquarters) + : Warrior(id, LION, health, attack, color, headquarters) {} + + // Lion死亡时,生命值转移给对手 + void beAttacked(int damage) override + { + int before = getHealth(); + Warrior::beAttacked(damage); + if (!isAlive()) + { + // 转移生命值的逻辑在战斗处理中实现 + } + } +}; + +// Wolf特有行为 +class Wolf : public Warrior +{ +public: + Wolf(int id, int health, int attack, Color color, Headquarters *headquarters) + : Warrior(id, WOLF, health, attack, color, headquarters) {} + + // Wolf在杀死敌人次数为偶数时,生命值和攻击力翻倍 + void afterKill(Warrior *enemy) override + { + if (getKillCount() % 2 == 0 && getKillCount() > 0) + { + increaseHealth(getHealth()); + increaseAttack(getAttack()); + } + } +}; + +class Headquarters +{ +private: + Color color; + int elements; // 生命元 + int totalWarriors; + vector warriors; + vector warriorSequence; // 制造武士的顺序 + int currentSequenceIndex; + int cityCount; // 城市数量,用于确定蓝色司令部位置 + + // 各类武士的初始属性 + vector initialHealth; + vector initialAttack; + +public: + Headquarters(Color color, int initialElements, int cityCount, + vector initHealth, vector initAttack) + : color(color), elements(initialElements), totalWarriors(0), + currentSequenceIndex(0), cityCount(cityCount), + initialHealth(initHealth), initialAttack(initAttack) + { + + // 设置武士制造顺序 + if (color == RED) + { + warriorSequence = {ICEMAN, LION, WOLF, NINJA, DRAGON}; + } + else + { + warriorSequence = {LION, DRAGON, NINJA, ICEMAN, WOLF}; + } + } + + Color getColor() const { return color; } + int getElements() const { return elements; } + + bool canMakeWarrior() const + { + WarriorType nextType = warriorSequence[currentSequenceIndex]; + return elements >= initialHealth[nextType]; + } + + // 制造下一个武士 + Warrior *produceWarrior() + { + if (!canMakeWarrior()) + { + return nullptr; + } + + WarriorType type = warriorSequence[currentSequenceIndex]; + int warriorId = ++totalWarriors; + int health = initialHealth[type]; + int attack = initialAttack[type]; + + // 减少生命元 + elements -= health; + + // 更新下一个要制造的武士类型 + currentSequenceIndex = (currentSequenceIndex + 1) % warriorSequence.size(); + + Warrior *warrior = nullptr; + switch (type) + { + case DRAGON: + warrior = new Dragon(warriorId, health, attack, color, this); + break; + case NINJA: + warrior = new Ninja(warriorId, health, attack, color, this); + break; + case ICEMAN: + warrior = new Iceman(warriorId, health, attack, color, this); + break; + case LION: + warrior = new Lion(warriorId, health, attack, color, this); + break; + case WOLF: + warrior = new Wolf(warriorId, health, attack, color, this); + break; + } + + // 设置初始位置 + if (color == RED) + { + warrior->setPosition(0); + } + else + { + warrior->setPosition(cityCount + 1); + } + + warriors.push_back(warrior); + return warrior; + } + + void addElements(int count) + { + elements += count; + } + + void reportElements() + { + cout << elements << " elements in " << (color == RED ? "red" : "blue") << " headquarter" << endl; + } + + void decreaseElements(int amount) + { + elements -= amount; + } +}; + +class City +{ +private: + int id; + int elements; + Color flag; // RED, BLUE或-1表示无旗帜 + bool hasFlag; + Color lastWinner; // 记录上一次胜利方 + int consecutiveWins; // 记录连续胜利次数 + +public: + City(int id) : id(id), elements(0), hasFlag(false), consecutiveWins(0) {} + + int getId() const { return id; } + int getElements() const { return elements; } + + void produceElements() + { + elements += 10; + } + + void takeAllElements() + { + elements = 0; + } + + void battle(Warrior *red, Warrior *blue, int hour) + { + // 战斗逻辑将在Game类中实现 + } + + Color getFlag() const { return flag; } + bool hasFlag() const { return hasFlag; } + + void updateFlag(Color winner, int hour) + { + if (lastWinner == winner) + { + consecutiveWins++; + if (consecutiveWins == 2 && (!hasFlag || flag != winner)) + { + hasFlag = true; + flag = winner; + // 打印旗帜升起事件 + printf("%03d:40 %s flag raised in city %d\n", + hour, winner == RED ? "red" : "blue", id); + } + } + else + { + lastWinner = winner; + consecutiveWins = 1; + } + } +}; + +class Game +{ +private: + int M, N, T; // M: 初始生命元, N: 城市数量, T: 结束时间(分钟) + vector initialHealth; + vector initialAttack; + Headquarters *redHQ; + Headquarters *blueHQ; + vector cities; + map> warriorsInCity; // 每个位置的武士列表 + int redHQEnemyCount = 0; // 红方司令部敌人数量 + int blueHQEnemyCount = 0; // 蓝方司令部敌人数量 + int currentTime; // 当前时间(分钟) + bool gameOver; + +public: + Game(int M, int N, int T, vector health, vector attack) + : M(M), N(N), T(T), initialHealth(health), initialAttack(attack), + currentTime(0), gameOver(false) + { + + // 创建总部 + redHQ = new Headquarters(RED, M, N, initialHealth, initialAttack); + blueHQ = new Headquarters(BLUE, M, N, initialHealth, initialAttack); + + // 创建城市 + for (int i = 1; i <= N; i++) + { + cities.push_back(new City(i)); + } + } + + ~Game() + { + delete redHQ; + delete blueHQ; + for (auto city : cities) + { + delete city; + } + for (auto warrior : warriorsInCity) + { + for (Warrior *w : warrior.second) + { + delete w; + } + } + } + + void run() + { + while (currentTime <= T && !gameOver) + { + // 处理当前时间的事件 + int hour = currentTime / 60; + int minute = currentTime % 60; + + switch (minute) + { + case 0: // 整点:武士诞生 + produceWarriors(); + break; + case 10: // 10分:武士前进 + warriorsAdvance(); + break; + case 20: // 20分:城市产生生命元 + citiesProduceElements(); + break; + case 30: // 30分:武士获取生命元 + warriorsTakeElements(); + break; + case 40: // 40分:战斗 + battles(); + break; + case 50: // 50分:总部报告生命元数量 + reportElements(); + break; + } + + currentTime++; + } + } + +private: + void produceWarriors() + { + int hour = currentTime / 60; + + // 红方司令部生产武士 + Warrior *redWarrior = redHQ->produceWarrior(); + if (redWarrior) + { + printf("%03d:00 red %s %d born\n", + hour, redWarrior->getTypeStr().c_str(), redWarrior->getId()); + addWarriorToCity(redWarrior, 0); + } + + // 蓝方司令部生产武士 + Warrior *blueWarrior = blueHQ->produceWarrior(); + if (blueWarrior) + { + printf("%03d:00 blue %s %d born\n", + hour, blueWarrior->getTypeStr().c_str(), blueWarrior->getId()); + addWarriorToCity(blueWarrior, N + 1); + } + } + + void warriorsAdvance() + { + int hour = currentTime / 60; + map> newPositions; + + // 处理所有位置的武士 + for (int pos = 0; pos <= N + 1; pos++) + { + if (!warriorsInCity[pos].empty()) + { + for (Warrior *warrior : warriorsInCity[pos]) + { + int oldPos = warrior->getPosition(); + + // 武士前进 + warrior->march(); + int newPos = warrior->getPosition(); + + // 如果到达敌方司令部 + if ((warrior->getColor() == RED && newPos == N + 1) || + (warrior->getColor() == BLUE && newPos == 0)) + { + printf("%03d:10 %s %s %d reached %s headquarter with %d elements and force %d\n", + hour, warrior->getColorStr().c_str(), warrior->getTypeStr().c_str(), + warrior->getId(), + warrior->getColor() == RED ? "blue" : "red", + warrior->getHealth(), warrior->getAttack()); + + // 增加敌方司令部的敌人计数 + if (warrior->getColor() == RED) + { + blueHQEnemyCount++; + if (blueHQEnemyCount == 2) + { + printf("%03d:10 blue headquarter was taken\n", hour); + gameOver = true; + } + } + else + { + redHQEnemyCount++; + if (redHQEnemyCount == 2) + { + printf("%03d:10 red headquarter was taken\n", hour); + gameOver = true; + } + } + } + // 如果在城市间移动 + else if (oldPos != newPos) + { + printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n", + hour, warrior->getColorStr().c_str(), warrior->getTypeStr().c_str(), + warrior->getId(), newPos, + warrior->getHealth(), warrior->getAttack()); + } + + // 将武士添加到新位置列表 + newPositions[newPos].push_back(warrior); + } + } + } + + // 更新位置列表 + warriorsInCity = newPositions; + } + + void citiesProduceElements() + { + for (auto city : cities) + { + city->produceElements(); + } + } + + void warriorsTakeElements() + { + int hour = currentTime / 60; + + // 遍历所有城市 + for (int i = 1; i <= N; i++) + { + City *city = cities[i - 1]; + vector &warriorsHere = warriorsInCity[i]; + + // 如果城市只有一个武士,他获取所有生命元 + if (warriorsHere.size() == 1) + { + Warrior *warrior = warriorsHere[0]; + int cityElements = city->getElements(); + if (cityElements > 0) + { + // 武士获取生命元并传送到总部 + if (warrior->getColor() == RED) + { + redHQ->addElements(cityElements); + } + else + { + blueHQ->addElements(cityElements); + } + + printf("%03d:30 %s %s %d earned %d elements for his headquarter\n", + hour, warrior->getColorStr().c_str(), warrior->getTypeStr().c_str(), + warrior->getId(), cityElements); + + // 城市生命元清零 + city->takeAllElements(); + } + } + } + } + + void battles() + { + int hour = currentTime / 60; + + // 在每个城市进行战斗 + for (int i = 1; i <= N; i++) + { + City *city = cities[i - 1]; + vector &warriorsHere = warriorsInCity[i]; + + // 如果城市有两个武士 + if (warriorsHere.size() == 2) + { + Warrior *redWarrior = nullptr; + Warrior *blueWarrior = nullptr; + + // 确定红蓝双方武士 + for (Warrior *warrior : warriorsHere) + { + if (warrior->getColor() == RED) + { + redWarrior = warrior; + } + else + { + blueWarrior = warrior; + } + } + + if (!redWarrior || !blueWarrior) + continue; // 安全检查 + + // 确定谁先攻击 + bool redFirst = false; + if (city->hasFlag()) + { + redFirst = (city->getFlag() == RED); + } + else + { + redFirst = (i % 2 == 1); // 奇数城市红方先攻 + } + + Warrior *attacker = redFirst ? redWarrior : blueWarrior; + Warrior *defender = redFirst ? blueWarrior : redWarrior; + + // 记录战前生命值 + int attackerHealthBefore = attacker->getHealth(); + int defenderHealthBefore = defender->getHealth(); + + // 打印攻击信息 + printf("%03d:40 %s %s %d attacked %s %s %d in city %d with %d elements and force %d\n", + hour, attacker->getColorStr().c_str(), attacker->getTypeStr().c_str(), + attacker->getId(), defender->getColorStr().c_str(), defender->getTypeStr().c_str(), + defender->getId(), i, attacker->getHealth(), attacker->getAttack()); + + // 进行攻击 + attacker->attack(defender); + + // 如果防守方死亡 + if (!defender->isAlive()) + { + printf("%03d:40 %s %s %d was killed in city %d\n", + hour, defender->getColorStr().c_str(), defender->getTypeStr().c_str(), + defender->getId(), i); + + // 如果是lion死亡,转移生命值给攻击方 + if (defender->getType() == LION) + { + attacker->increaseHealth(defenderHealthBefore); + } + + // wolf杀死敌人的特殊处理 + attacker->afterKill(defender); + + // 攻击方总部奖励8个生命元 + if (attacker->getColor() == RED) + { + if (redHQ->getElements() >= 8) + { + redHQ->decreaseElements(8); + attacker->increaseHealth(8); + } + } + else + { + if (blueHQ->getElements() >= 8) + { + blueHQ->decreaseElements(8); + attacker->increaseHealth(8); + } + } + + // 攻击方获取城市所有生命元 + int cityElements = city->getElements(); + if (cityElements > 0) + { + if (attacker->getColor() == RED) + { + redHQ->addElements(cityElements); + } + else + { + blueHQ->addElements(cityElements); + } + city->takeAllElements(); + } + + // 更新城市旗帜 + city->updateFlag(attacker->getColor(), hour); + + // 如果是dragon,则欢呼 + if (attacker->getType() == DRAGON) + { + attacker->specialAction(); + } + } + // 如果防守方没死,可能会反击 + else + { + // 如果不是ninja,就会反击 + if (defender->getType() != NINJA) + { + printf("%03d:40 %s %s %d fought back against %s %s %d in city %d\n", + hour, defender->getColorStr().c_str(), defender->getTypeStr().c_str(), + defender->getId(), attacker->getColorStr().c_str(), attacker->getTypeStr().c_str(), + attacker->getId(), i); + + defender->fightBack(attacker); + + // 如果攻击方被反击死亡 + if (!attacker->isAlive()) + { + printf("%03d:40 %s %s %d was killed in city %d\n", + hour, attacker->getColorStr().c_str(), attacker->getTypeStr().c_str(), + attacker->getId(), i); + + // 如果是lion死亡,转移生命值 + if (attacker->getType() == LION) + { + defender->increaseHealth(attackerHealthBefore); + } + + // 更新城市旗帜 + city->updateFlag(defender->getColor(), hour); + } + // 如果都没死,是平局 + else + { + // 平局时城市不会改变旗帜,生命元也不会被拿走 + } + } + // ninja不反击 + else + { + // 如果是dragon,则欢呼 + if (attacker->getType() == DRAGON) + { + attacker->specialAction(); + } + } + } + + // 移除死亡的武士 + vector survivors; + for (Warrior *warrior : warriorsHere) + { + if (warrior->isAlive()) + { + survivors.push_back(warrior); + } + else + { + // 从warriors全局列表中移除 + auto it = find(warriorsInCity[i].begin(), warriorsInCity[i].end(), warrior); + if (it != warriorsInCity[i].end()) + { + warriorsInCity[i].erase(it); + } + delete warrior; + } + } + warriorsInCity[i] = survivors; + } + } + } + + void reportElements() + { + int hour = currentTime / 60; + printf("%03d:50 ", hour); + redHQ->reportElements(); + printf("%03d:50 ", hour); + blueHQ->reportElements(); + } + + // 辅助函数:将武士添加到城市列表 + void addWarriorToCity(Warrior *warrior, int position) + { + warriorsInCity[position].push_back(warrior); + } +}; + +int main() +{ + int caseCount; + cin >> caseCount; + + for (int caseNum = 1; caseNum <= caseCount; caseNum++) + { + int M, N, T; + cin >> M >> N >> T; + + vector initialHealth(5); + vector initialAttack(5); + + for (int i = 0; i < 5; i++) + { + cin >> initialHealth[i]; + } + + for (int i = 0; i < 5; i++) + { + cin >> initialAttack[i]; + } + + cout << "Case:" << caseNum << endl; + + Game game(M, N, T, initialHealth, initialAttack); + game.run(); + } + + return 0; +} diff --git a/open_judge/NOIP2014/1.cpp b/open_judge/NOIP2014/1.cpp index 99e336d0..d35e167d 100644 --- a/open_judge/NOIP2014/1.cpp +++ b/open_judge/NOIP2014/1.cpp @@ -1,56 +1,56 @@ -#include -#include - -using namespace std; - -// 初始化平0,输-1,赢1 -int R[5][5] = { - 0, -1, 1, 1, -1, - 1, 0, -1, 1, -1, - -1, 1, 0, -1, 1, - -1, -1, 1, 0, 1, - 1, 1, -1, -1, 0}; - -int main() -{ - int N, NA, NB; - cin >> N >> NA >> NB; - vector A(NA), B(NB); - for (int i = 0; i < NA; i++) - { - cin >> A[i]; - } - for (int i = 0; i < NB; i++) - { - cin >> B[i]; - } - - int scoreA = 0, scoreB = 0; - int i = 0, j = 0; - while (N--) - { - if (i == NA) - { - i = 0; - } - if (j == NB) - { - j = 0; - } - int result = R[A[i]][B[j]]; - if (result == 1) - { - scoreA++; - } - else if (result == -1) - { - scoreB++; - } - i++; - j++; - } - - cout << scoreA << " " << scoreB << endl; - - return 0; -} +#include +#include + +using namespace std; + +// 初始化平0,输-1,赢1 +int R[5][5] = { + 0, -1, 1, 1, -1, + 1, 0, -1, 1, -1, + -1, 1, 0, -1, 1, + -1, -1, 1, 0, 1, + 1, 1, -1, -1, 0}; + +int main() +{ + int N, NA, NB; + cin >> N >> NA >> NB; + vector A(NA), B(NB); + for (int i = 0; i < NA; i++) + { + cin >> A[i]; + } + for (int i = 0; i < NB; i++) + { + cin >> B[i]; + } + + int scoreA = 0, scoreB = 0; + int i = 0, j = 0; + while (N--) + { + if (i == NA) + { + i = 0; + } + if (j == NB) + { + j = 0; + } + int result = R[A[i]][B[j]]; + if (result == 1) + { + scoreA++; + } + else if (result == -1) + { + scoreB++; + } + i++; + j++; + } + + cout << scoreA << " " << scoreB << endl; + + return 0; +} diff --git a/tools/blog_generator.py b/tools/blog_generator.py index 5200fa27..802b4fab 100644 --- a/tools/blog_generator.py +++ b/tools/blog_generator.py @@ -17,11 +17,11 @@ class BlogGenerator: """博客生成器类""" - + def __init__(self, template_dir: str = None): """ 初始化博客生成器 - + Args: template_dir: 模板文件所在目录 """ @@ -30,14 +30,14 @@ def __init__(self, template_dir: str = None): self.template_dir = Path(__file__).parent.parent / "note" / "刷题笔记模板" else: self.template_dir = Path(template_dir) - + def detect_language(self, file_path: str) -> str: """ 根据文件扩展名检测编程语言 - + Args: file_path: 代码文件路径 - + Returns: 语言名称 """ @@ -53,14 +53,14 @@ def detect_language(self, file_path: str) -> str: '.rs': 'Rust', } return language_map.get(ext, 'Unknown') - + def read_code_file(self, file_path: str) -> str: """ 读取代码文件内容 - + Args: file_path: 代码文件路径 - + Returns: 代码内容 """ @@ -70,14 +70,14 @@ def read_code_file(self, file_path: str) -> str: except Exception as e: print(f"❌ 读取代码文件失败: {e}") return "" - + def extract_problem_info_from_path(self, file_path: str) -> Dict[str, str]: """ 从文件路径中提取题目信息 - + Args: file_path: 代码文件路径 - + Returns: 题目信息字典 """ @@ -88,7 +88,7 @@ def extract_problem_info_from_path(self, file_path: str) -> Dict[str, str]: 'problem_name': '', 'difficulty': 'Medium' } - + # 判断是 LeetCode 还是牛客 if 'leetcode' in str(file_path).lower(): info['platform'] = 'LeetCode' @@ -112,17 +112,17 @@ def extract_problem_info_from_path(self, file_path: str) -> Dict[str, str]: problem_name = re.sub(r'^\d+_\d+-\d+', '', part) info['problem_name'] = problem_name break - + return info - + def format_code_block(self, code: str, language: str) -> str: """ 格式化代码块 - + Args: code: 代码内容 language: 编程语言 - + Returns: 格式化后的代码块 """ @@ -138,15 +138,15 @@ def format_code_block(self, code: str, language: str) -> str: } lang_tag = lang_map.get(language, 'text') return f"```{lang_tag}\n{code.strip()}\n```" - + def analyze_algorithm(self, code: str, language: str) -> Dict[str, str]: """ 分析算法的时间和空间复杂度 - + Args: code: 代码内容 language: 编程语言 - + Returns: 包含复杂度分析的字典 """ @@ -157,57 +157,57 @@ def analyze_algorithm(self, code: str, language: str) -> Dict[str, str]: 'algorithm_type': '待分析', 'key_techniques': [] } - + code_lower = code.lower() - + # 检测常见算法模式 if 'sort' in code_lower or 'sorted' in code_lower: analysis['time_complexity'] = 'O(n log n)' analysis['key_techniques'].append('排序') - + if 'dp' in code_lower or 'memo' in code_lower: analysis['algorithm_type'] = '动态规划' analysis['key_techniques'].append('动态规划') - + if 'bfs' in code_lower or 'queue' in code_lower: analysis['algorithm_type'] = '广度优先搜索' analysis['key_techniques'].append('BFS') - + if 'dfs' in code_lower or 'recursion' in code_lower: analysis['algorithm_type'] = '深度优先搜索' analysis['key_techniques'].append('DFS/递归') - + if 'binary' in code_lower or 'bisect' in code_lower: analysis['time_complexity'] = 'O(log n)' analysis['key_techniques'].append('二分查找') - + if 'unionfind' in code_lower or 'union' in code_lower or 'find' in code_lower: analysis['algorithm_type'] = '并查集' analysis['key_techniques'].append('并查集') - + if 'sliding' in code_lower or ('left' in code_lower and 'right' in code_lower): analysis['algorithm_type'] = '滑动窗口' analysis['key_techniques'].append('滑动窗口') - + # 检测嵌套循环 for_count = code.count('for') while_count = code.count('while') loop_count = for_count + while_count - + if loop_count >= 3: analysis['time_complexity'] = 'O(n³) 或更高' elif loop_count == 2: analysis['time_complexity'] = 'O(n²)' - + # 检测空间复杂度 if 'vector' in code_lower or 'array' in code_lower or 'list' in code_lower: if '[][]' in code or 'vector str: """ 生成 LeetCode 博客文章 - + Args: code_file: 代码文件路径 problem_url: 题目链接 @@ -231,7 +231,7 @@ def generate_leetcode_blog( description: 题目描述 approach: 解题思路 output_file: 输出文件路径 - + Returns: 生成的博客内容 """ @@ -239,23 +239,23 @@ def generate_leetcode_blog( code = self.read_code_file(code_file) if not code: return "" - + # 检测语言 language = self.detect_language(code_file) - + # 如果没有提供题目信息,尝试从路径中提取 if not problem_number or not problem_name: info = self.extract_problem_info_from_path(code_file) problem_number = problem_number or info['problem_number'] problem_name = problem_name or info['problem_name'] difficulty = difficulty or info['difficulty'] - + # 分析算法 analysis = self.analyze_algorithm(code, language) - + # 生成日期 today = datetime.now().strftime('%Y-%m-%d') - + # 生成博客内容 blog_content = f"""--- title: {today}-LeetCode刷题笔记-{problem_number}-{problem_name} @@ -314,7 +314,7 @@ def generate_leetcode_blog( --- """ - + # 如果指定了输出文件,写入文件 if output_file: try: @@ -323,9 +323,9 @@ def generate_leetcode_blog( print(f"✅ LeetCode 博客已生成: {output_file}") except Exception as e: print(f"❌ 写入文件失败: {e}") - + return blog_content - + def generate_niuke_blog( self, code_file: str, @@ -341,7 +341,7 @@ def generate_niuke_blog( ) -> str: """ 生成牛客博客文章 - + Args: code_file: 代码文件路径 problem_url: 题目链接 @@ -353,7 +353,7 @@ def generate_niuke_blog( examples: 示例列表 approach: 解题思路 output_file: 输出文件路径 - + Returns: 生成的博客内容 """ @@ -361,21 +361,21 @@ def generate_niuke_blog( code = self.read_code_file(code_file) if not code: return "" - + # 检测语言 language = self.detect_language(code_file) - + # 如果没有提供题目信息,尝试从路径中提取 if not problem_name: info = self.extract_problem_info_from_path(code_file) problem_name = problem_name or info['problem_name'] - + # 分析算法 analysis = self.analyze_algorithm(code, language) - + # 生成日期 today = datetime.now().strftime('%Y-%m-%d') - + # 处理示例 examples_text = "" if examples: @@ -387,7 +387,7 @@ def generate_niuke_blog( examples_text += f"说明:{example['explanation']}\n" else: examples_text = "\n## 示例1\n```\n输入:\n待补充\n\n输出:\n待补充\n```\n" - + # 生成博客内容 blog_content = f"""--- title: {today}-牛客刷题笔记-{problem_name} @@ -463,7 +463,7 @@ def generate_niuke_blog( --- """ - + # 如果指定了输出文件,写入文件 if output_file: try: @@ -472,31 +472,31 @@ def generate_niuke_blog( print(f"✅ 牛客博客已生成: {output_file}") except Exception as e: print(f"❌ 写入文件失败: {e}") - + return blog_content - + def interactive_generate(self): """交互式生成博客""" print("=" * 60) print("📝 算法学习博客生成器") print("=" * 60) print() - + # 选择平台 print("请选择题目平台:") print("1. LeetCode") print("2. 牛客") platform_choice = input("请输入选项 (1/2): ").strip() - + # 输入代码文件路径 code_file = input("\n请输入代码文件路径: ").strip() if not os.path.exists(code_file): print(f"❌ 文件不存在: {code_file}") return - + # 输入题目信息 problem_url = input("请输入题目链接: ").strip() - + if platform_choice == "1": # LeetCode problem_number = input("请输入题目编号: ").strip() @@ -504,11 +504,11 @@ def interactive_generate(self): difficulty = input("请输入难度 (Easy/Medium/Hard): ").strip() or "Medium" description = input("请输入题目描述 (回车跳过): ").strip() approach = input("请输入解题思路 (回车跳过): ").strip() - + # 生成输出文件名 today = datetime.now().strftime('%Y-%m-%d') output_file = f"{today}-LeetCode刷题笔记-{problem_number}-{problem_name}.md" - + # 生成博客 self.generate_leetcode_blog( code_file=code_file, @@ -520,18 +520,18 @@ def interactive_generate(self): approach=approach, output_file=output_file ) - + elif platform_choice == "2": # 牛客 problem_name = input("请输入题目名称: ").strip() difficulty = input("请输入难度 (简单/中等/困难): ").strip() or "中等" description = input("请输入题目描述 (回车跳过): ").strip() approach = input("请输入解题思路 (回车跳过): ").strip() - + # 生成输出文件名 today = datetime.now().strftime('%Y-%m-%d') output_file = f"{today}-牛客刷题笔记-{problem_name}.md" - + # 生成博客 self.generate_niuke_blog( code_file=code_file, @@ -545,7 +545,7 @@ def interactive_generate(self): else: print("❌ 无效的选项") return - + print("\n✨ 博客生成完成!") @@ -556,18 +556,18 @@ def main(): formatter_class=argparse.RawDescriptionHelpFormatter, epilog=""" 示例用法: - + 交互式模式: python blog_generator.py - + LeetCode 模式: python blog_generator.py --platform leetcode --code solution.py --url https://leetcode.cn/problems/xxx --number 1437 --name "题目名称" - + 牛客模式: python blog_generator.py --platform niuke --code solution.cpp --url https://www.nowcoder.com/xxx --name "题目名称" """ ) - + parser.add_argument('--platform', choices=['leetcode', 'niuke'], help='题目平台') parser.add_argument('--code', help='代码文件路径') parser.add_argument('--url', help='题目链接') @@ -577,11 +577,11 @@ def main(): parser.add_argument('--desc', help='题目描述') parser.add_argument('--approach', help='解题思路') parser.add_argument('--output', help='输出文件路径') - + args = parser.parse_args() - + generator = BlogGenerator() - + # 如果提供了命令行参数,使用命令行模式 if args.platform and args.code: if args.platform == 'leetcode': diff --git a/tools/organize_noob.py b/tools/organize_noob.py index 38efa1d0..e90a542d 100644 --- a/tools/organize_noob.py +++ b/tools/organize_noob.py @@ -5,7 +5,7 @@ def organize_files(): # Define the target directory relative to the project root base_dir = os.path.join("niuke", "practice", "noob") - + # Check if directory exists if not os.path.exists(base_dir): print(f"Error: Directory {base_dir} does not exist.") @@ -24,7 +24,7 @@ def organize_files(): for i in range(1, 131): folder_name = f"noob{i}" folder_path = os.path.join(base_dir, folder_name) - + # Create the folder if it doesn't exist if not os.path.exists(folder_path): try: @@ -33,17 +33,17 @@ def organize_files(): except Exception as e: print(f"Error creating directory {folder_name}: {e}") continue - + # Define regex pattern to match files for this number # Pattern: starts with "noob" + i + (end of string OR non-digit char) # This ensures "noob1" matches "noob1.cpp" but not "noob10.cpp" pattern = re.compile(f"^noob{i}(\\D.*)?$") - + for filename in files: if pattern.match(filename): src_path = os.path.join(base_dir, filename) dst_path = os.path.join(folder_path, filename) - + try: shutil.move(src_path, dst_path) print(f"Moved {filename} -> {folder_name}/") From c3ed709591c8124adc033c9542533be1b39bcca8 Mon Sep 17 00:00:00 2001 From: astrbot-bot Date: Sun, 26 Apr 2026 23:01:15 +0800 Subject: [PATCH 2/2] fix: add missing vector headers for 4 C++ files --- ...4\347\232\204\350\267\257\345\276\204.cpp" | 41 +++++++ ...0\347\232\204\345\244\215\345\210\273.cpp" | 56 ++++++++++ ...4\345\220\221\351\223\276\350\241\250.cpp" | 58 ++++++++++ ...6\344\272\214\345\217\211\346\240\221.cpp" | 62 +++++++++++ ...0\345\255\227\346\216\222\345\210\227.cpp" | 35 ++++++ ...2\347\232\204\346\225\260\345\255\227.cpp" | 25 +++++ ...\347\232\204k\344\270\252\346\225\260.cpp" | 32 ++++++ ...4\344\270\255\344\275\215\346\225\260.cpp" | 36 +++++++ ...4\346\234\200\345\244\247\345\222\214.cpp" | 24 +++++ ...0\347\232\204\346\254\241\346\225\260.cpp" | 39 +++++++ .../613.\351\235\242\347\247\257.cpp" | 26 +++++ ....\346\234\200\345\244\247\345\200\274.cpp" | 30 ++++++ .../615.\346\262\271\350\200\227.cpp" | 23 ++++ ...4\347\232\204\350\267\235\347\246\273.cpp" | 30 ++++++ acwing/test.go | 5 + "acwing/\346\200\273\347\273\223/1.md" | 1 + leetcode/problems/.lcpr_data/bricks.json | 7 +- .../102.binary-tree-level-order-traversal.cpp | 82 ++++++++++++++ .../144.binary-tree-preorder-traversal-2.cpp | 84 +++++++++++++++ .../145.binary-tree-postorder-traversal-2.cpp | 101 ++++++++++++++++++ leetcode/problems/226.invert-binary-tree.cpp | 93 ++++++++++++++++ .../94.binary-tree-inorder-traversal-2.cpp | 80 ++++++++++++++ ...15\347\232\204\351\225\277\345\272\246.py" | 13 +++ ...72\347\216\260\346\254\241\346\225\260.py" | 32 ++++++ ...04\346\234\213\345\217\213\344\273\254.py" | 32 ++++++ ...15\347\232\204\351\225\277\345\272\246.py" | 31 ++++++ ...72\357\274\210\344\272\214\357\274\211.py" | 34 ++++++ ...72\357\274\210\344\270\211\357\274\211.py" | 30 ++++++ ...15\345\244\215\350\276\223\345\207\272.py" | 32 ++++++ ...\345\220\215\345\211\21510\344\275\215.py" | 32 ++++++ ...54\347\232\204\345\226\234\345\245\275.py" | 38 +++++++ ...06\347\240\201\346\270\270\346\210\217.py" | 36 +++++++ ...64\346\225\260\346\225\260\345\255\227.py" | 15 +++ ...72\357\274\210\344\270\200\357\274\211.py" | 31 ++++++ ...17\346\225\260\350\276\223\345\207\272.py" | 26 +++++ ...60\345\214\226\346\225\264\346\225\260.py" | 24 ++++- ...40\345\260\217\346\225\260\347\202\271.py" | 25 +++++ 37 files changed, 1395 insertions(+), 6 deletions(-) create mode 100644 "acwing/problems/47.\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.cpp" create mode 100644 "acwing/problems/48.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\273.cpp" create mode 100644 "acwing/problems/49.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250.cpp" create mode 100644 "acwing/problems/50.\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221.cpp" create mode 100644 "acwing/problems/51.\346\225\260\345\255\227\346\216\222\345\210\227.cpp" create mode 100644 "acwing/problems/52.\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227.cpp" create mode 100644 "acwing/problems/53.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" create mode 100644 "acwing/problems/54.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260.cpp" create mode 100644 "acwing/problems/55.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214.cpp" create mode 100644 "acwing/problems/56.\344\273\2161\345\210\260n\346\225\264\346\225\260\344\270\2551\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" create mode 100644 "acwing/problems/613.\351\235\242\347\247\257.cpp" create mode 100644 "acwing/problems/614.\346\234\200\345\244\247\345\200\274.cpp" create mode 100644 "acwing/problems/615.\346\262\271\350\200\227.cpp" create mode 100644 "acwing/problems/616.\344\270\244\347\202\271\351\227\264\347\232\204\350\267\235\347\246\273.cpp" create mode 100644 acwing/test.go create mode 100644 "acwing/\346\200\273\347\273\223/1.md" create mode 100644 leetcode/problems/102.binary-tree-level-order-traversal.cpp create mode 100644 leetcode/problems/144.binary-tree-preorder-traversal-2.cpp create mode 100644 leetcode/problems/145.binary-tree-postorder-traversal-2.cpp create mode 100644 leetcode/problems/226.invert-binary-tree.cpp create mode 100644 leetcode/problems/94.binary-tree-inorder-traversal-2.cpp create mode 100644 "niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ1.\345\255\227\347\254\246\344\270\262\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" create mode 100644 "niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ2.\350\256\241\347\256\227\346\237\220\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP10.\347\211\233\347\211\233\346\234\200\345\245\275\347\232\204\346\234\213\345\217\213\344\273\254.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP11.\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP12.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\272\214\357\274\211.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP13.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\211\357\274\211.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP14.\344\270\215\347\224\250\345\276\252\347\216\257\350\257\255\345\217\245\347\232\204\351\207\215\345\244\215\350\276\223\345\207\272.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP15.\346\210\252\345\217\226\347\224\250\346\210\267\345\220\215\345\211\21510\344\275\215.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP27.\346\234\213\345\217\213\344\273\254\347\232\204\345\226\234\345\245\275.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP28.\345\257\206\347\240\201\346\270\270\346\210\217.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP5.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\200\357\274\211.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP6.\347\211\233\347\211\233\347\232\204\345\260\217\346\225\260\350\276\223\345\207\272.py" create mode 100644 "niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP8.\344\270\272\346\225\264\346\225\260\345\242\236\345\212\240\345\260\217\346\225\260\347\202\271.py" diff --git "a/acwing/problems/47.\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.cpp" "b/acwing/problems/47.\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.cpp" new file mode 100644 index 00000000..a60e2f3a --- /dev/null +++ "b/acwing/problems/47.\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.cpp" @@ -0,0 +1,41 @@ +#include +/* + * @acwing app=acwing.cn id=45 lang=C++ + * + * 47. 二叉树中和为某一值的路径 + */ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + vector> res; + vector tracker; + vector> findPath(TreeNode *root, int sum) { + dfs(root, sum); + return res; + } + + void dfs(TreeNode *root, int sum) { + if (!root) + return; + sum -= root->val; + tracker.push_back(root->val); + if (!root->right && !root->left && !sum) { + res.push_back(tracker); + } + dfs(root->left, sum); + dfs(root->right, sum); + tracker.pop_back(); + } +}; +// @acwing code end diff --git "a/acwing/problems/48.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\273.cpp" "b/acwing/problems/48.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\273.cpp" new file mode 100644 index 00000000..9805a104 --- /dev/null +++ "b/acwing/problems/48.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\273.cpp" @@ -0,0 +1,56 @@ +/* + * @acwing app=acwing.cn id=89 lang=C++ + * + * 48. 复杂链表的复刻 + */ + +// @acwing code start + +/** + * Definition for singly-linked list with a random pointer. + * struct ListNode { + * int val; + * ListNode *next, *random; + * ListNode(int x) : val(x), next(NULL), random(NULL) {} + * }; + */ +class Solution { + public: + ListNode *copyRandomList(ListNode *head) { + if (!head) + return head; + + // 1.在每个原节点后面复制新节点 + auto p = head; + while (p) { + auto np = new ListNode(p->val); + np->next = p->next; + p->next = np; + p = np->next; + } + + // 2.设置新节点的 random 指针 + p = head; + while (p) { + if (p->random) { + p->next->random = p->random->next; + } + p = p->next->next; + } + + // 3.拆分链表 + auto dummy = new ListNode(-1); + auto cur = dummy; + p = head; + while (p) { + cur->next = p->next; + cur = cur->next; + p->next = p->next->next; + p = p->next; + } + + return dummy->next; + } +}; + +// @acwing code end diff --git "a/acwing/problems/49.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250.cpp" "b/acwing/problems/49.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250.cpp" new file mode 100644 index 00000000..4d9dfe1f --- /dev/null +++ "b/acwing/problems/49.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250.cpp" @@ -0,0 +1,58 @@ +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-25 11:33:20 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-25 11:36:00 + */ +/* + * @acwing app=acwing.cn id=87 lang=C++ + * + * 49. 二叉搜索树与双向链表 + */ + +// @acwing code start + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +#include +using namespace std; +class Solution { + public: + TreeNode *convert(TreeNode *root) { + if (!root) + return nullptr; + auto res = dfs(root); + return res.first; + } + + pair dfs(TreeNode *root) { + if (!root->left && !root->right) + return {root, root}; + if (root->left && root->right) { + auto lslide = dfs(root->left), rslide = dfs(root->right); + lslide.second->right = root, root->left = lslide.second; + rslide.first->left = root, root->right = rslide.first; + return {lslide.first, rslide.second}; + } + if (root->left) { + auto lslide = dfs(root->left); + lslide.second->right = root, root->left = lslide.second; + return {lslide.first, root}; + } + if (root->right) { + auto rslide = dfs(root->right); + rslide.first->left = root, root->right = rslide.first; + return {root, rslide.second}; + } + + return {nullptr, nullptr}; + } +}; +// @acwing code end diff --git "a/acwing/problems/50.\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221.cpp" "b/acwing/problems/50.\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221.cpp" new file mode 100644 index 00000000..dd4a6aff --- /dev/null +++ "b/acwing/problems/50.\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221.cpp" @@ -0,0 +1,62 @@ +/* + * @acwing app=acwing.cn id=46 lang=C++ + * + * 50. 序列化二叉树 + */ + +// @acwing code start + +#include +#include + +using namespace std; + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { + public: + // Encodes a tree to a single string. + string serialize(TreeNode *root) { + string s; + dfs_s(root, s); + return s; + } + + void dfs_s(TreeNode *root, string &s) { + if (!root) { + s += "n "; + return; + } else { + s += to_string(root->val) + ' '; + } + dfs_s(root->left, s); + dfs_s(root->right, s); + } + + // Decodes your encoded data to tree. + TreeNode *deserialize(string data) { + stringstream ss(data); + return dfs_d(ss); + } + + TreeNode *dfs_d(stringstream &ss) { + string s; + ss >> s; + if (s == "n") + return nullptr; + + auto p = new TreeNode(stoi(s)); + p->left = dfs_d(ss); + p->right = dfs_d(ss); + + return p; + } +}; +// @acwing code end diff --git "a/acwing/problems/51.\346\225\260\345\255\227\346\216\222\345\210\227.cpp" "b/acwing/problems/51.\346\225\260\345\255\227\346\216\222\345\210\227.cpp" new file mode 100644 index 00000000..e6ad659a --- /dev/null +++ "b/acwing/problems/51.\346\225\260\345\255\227\346\216\222\345\210\227.cpp" @@ -0,0 +1,35 @@ +/* + * @acwing app=acwing.cn id=47 lang=C++ + * + * 51. 数字排列 + */ + +// @acwing code start + +#include +class Solution { +public: + vector> res; + vector path; + vector> permutation(vector &nums) { + path.resize(nums.size()); + sort(nums.begin(), nums.end()); + dfs(nums, 0, 0, 0); + return res; + } + void dfs(vector &nums, int u, int start, int state) { + if (u == nums.size()) { + res.push_back(path); + return; + } + if (!u || nums[u] != nums[u - 1]) + start = 0; + for (int i = start; i < nums.size(); ++i) { + if (!(state >> i & 1)) { + path[i] = nums[u]; + dfs(nums, u + 1, i + 1, state + (1 << i)); + } + } + } +}; +// @acwing code end diff --git "a/acwing/problems/52.\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227.cpp" "b/acwing/problems/52.\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227.cpp" new file mode 100644 index 00000000..68a91a16 --- /dev/null +++ "b/acwing/problems/52.\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227.cpp" @@ -0,0 +1,25 @@ +#include +/* + * @acwing app=acwing.cn id=48 lang=C++ + * + * 52. 数组中出现次数超过一半的数字 + */ + +// @acwing code start + +class Solution { +public: + int moreThanHalfNum_Solution(vector &nums) { + int cnt = 0, val = -1; + for (int i = 0; i < nums.size(); ++i) { + if (cnt <= 0) + val = nums[i]; + if (nums[i] == val) + cnt++; + else + cnt--; + } + return val; + } +}; +// @acwing code end diff --git "a/acwing/problems/53.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" "b/acwing/problems/53.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" new file mode 100644 index 00000000..c50c64a4 --- /dev/null +++ "b/acwing/problems/53.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" @@ -0,0 +1,32 @@ +/* + * @acwing app=acwing.cn id=49 lang=C++ + * + * 53. 最小的k个数 + */ + +// @acwing code start + +#include +#include +#include +class Solution { +public: + vector getLeastNumbers_Solution(vector input, int k) { + std::priority_queue heap; + std::vector res; + + for (auto x : input) { + heap.push(x); + if (heap.size() > k) + heap.pop(); + } + + while (!heap.empty()) { + res.push_back(heap.top()); + heap.pop(); + } + std::reverse(res.begin(), res.end()); + return res; + } +}; +// @acwing code endnt diff --git "a/acwing/problems/54.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260.cpp" "b/acwing/problems/54.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260.cpp" new file mode 100644 index 00000000..b23ef4fb --- /dev/null +++ "b/acwing/problems/54.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260.cpp" @@ -0,0 +1,36 @@ +/* + * @acwing app=acwing.cn id=88 lang=C++ + * + * 54. 数据流中的中位数 + */ + +// @acwing code start + +#include +#include +class Solution { + public: + priority_queue max_heap; // 大顶堆,存储较小的一半 + priority_queue, std::greater> min_heap; // 小顶堆,存储较大的一半 + void insert(int num) { + max_heap.push(num); + if (min_heap.size() && max_heap.top() > min_heap.top()) { + auto maxv = max_heap.top(), minv = min_heap.top(); + max_heap.pop(), min_heap.pop(); + max_heap.push(minv), min_heap.push(maxv); + } + if (max_heap.size() > min_heap.size() + 1) { + min_heap.push(max_heap.top()); + max_heap.pop(); + } + } + + double getMedian() { + if (max_heap.size() + min_heap.size() & 1) { + return max_heap.top(); + } else { + return (max_heap.top() + min_heap.top()) / 2.0; + } + } +}; +// @acwing code end diff --git "a/acwing/problems/55.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214.cpp" "b/acwing/problems/55.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214.cpp" new file mode 100644 index 00000000..cc1792c9 --- /dev/null +++ "b/acwing/problems/55.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214.cpp" @@ -0,0 +1,24 @@ +/* + * @acwing app=acwing.cn id=50 lang=C++ + * + * 55. 连续子数组的最大和 + */ + +// @acwing code start + +#include +#include +class Solution { + public: + int maxSubArray(vector &nums) { + int res = INT_MIN, s = 0; + for (auto x : nums) { + if (s < 0) + s = 0; + s += x; + res = max(res, s); + } + return res; + } +}; +// @acwing code end diff --git "a/acwing/problems/56.\344\273\2161\345\210\260n\346\225\264\346\225\260\344\270\2551\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" "b/acwing/problems/56.\344\273\2161\345\210\260n\346\225\264\346\225\260\344\270\2551\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" new file mode 100644 index 00000000..07e0cdc4 --- /dev/null +++ "b/acwing/problems/56.\344\273\2161\345\210\260n\346\225\264\346\225\260\344\270\2551\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" @@ -0,0 +1,39 @@ +#include +/* + * @acwing app=acwing.cn id=51 lang=C++ + * + * 56. 从1到n整数中1出现的次数 + */ + +// @acwing code start + +class Solution { + public: + int numberOf1Between1AndN_Solution(int n) { + if (!n) + return 0; + int res = 0; + vector digits; + while (n) + digits.push_back(n % 10), n /= 10; + + for (int i = 0; i < digits.size(); ++i) { + int left = 0, right = 0, t = 1; + for (int j = digits.size() - 1; j > i; --j) + left = left * 10 + digits[j]; + for (int j = i - 1; j >= 0; --j) + right = right * 10 + digits[j], t *= 10; + res += left * t; + if (digits[i] == 0) + ; + else if (digits[i] == 1) { + res += right + 1; + } else if (digits[i] > 1) { + res += t; + } + } + + return res; + } +}; +// @acwing code end diff --git "a/acwing/problems/613.\351\235\242\347\247\257.cpp" "b/acwing/problems/613.\351\235\242\347\247\257.cpp" new file mode 100644 index 00000000..7a0b5cb7 --- /dev/null +++ "b/acwing/problems/613.\351\235\242\347\247\257.cpp" @@ -0,0 +1,26 @@ +/* +* @acwing app=acwing.cn id=615 lang=C++ +* +* 613. 面积 +*/ + +// @acwing code start + +#include +#include + +using namespace std; + +int main(){ + double a,b,c;cin >> a >> b >> c; + const double PI = 3.14159; + cout << fixed << setprecision(3); + cout << "TRIANGULO: " << 1 / 2.0 * a * c << endl; + cout << "CIRCULO: " << PI * c * c << endl; + cout << "TRAPEZIO: " << 1 / 2.0 * (a + b) * c << endl; + cout << "QUADRADO: " << b * b << endl; + cout << "RETANGULO: " << a * b << endl; + return 0; +} + +// @acwing code end diff --git "a/acwing/problems/614.\346\234\200\345\244\247\345\200\274.cpp" "b/acwing/problems/614.\346\234\200\345\244\247\345\200\274.cpp" new file mode 100644 index 00000000..3b26403a --- /dev/null +++ "b/acwing/problems/614.\346\234\200\345\244\247\345\200\274.cpp" @@ -0,0 +1,30 @@ +/* +* @acwing app=acwing.cn id=616 lang=C++ +* +* 614. 最大值 +*/ + +// @acwing code start + +#include +#include + +using namespace std; + +int intMax(int a,int b){ + return ((a+b)+abs(a-b))/2; +} + +int intMin(int a,int b){ + return ((a+b)-abs(a-b))/2; +} + +int main(){ + int a,b,c; + cin >> a >> b >> c; + cout << intMax(intMax(a,b),c) << " eh o maior" << endl; + return 0; +} + + +// @acwing code end diff --git "a/acwing/problems/615.\346\262\271\350\200\227.cpp" "b/acwing/problems/615.\346\262\271\350\200\227.cpp" new file mode 100644 index 00000000..19e47323 --- /dev/null +++ "b/acwing/problems/615.\346\262\271\350\200\227.cpp" @@ -0,0 +1,23 @@ +/* +* @acwing app=acwing.cn id=617 lang=C++ +* +* 615. 油耗 +*/ + +// @acwing code start + +#include +#include + +using namespace std; + +int main(){ + int x;cin >> x; + double y;cin >> y; + + cout << fixed << setprecision(3) << x / y << " km/l" << endl; + + return 0; +} + +// @acwing code end diff --git "a/acwing/problems/616.\344\270\244\347\202\271\351\227\264\347\232\204\350\267\235\347\246\273.cpp" "b/acwing/problems/616.\344\270\244\347\202\271\351\227\264\347\232\204\350\267\235\347\246\273.cpp" new file mode 100644 index 00000000..0e145b79 --- /dev/null +++ "b/acwing/problems/616.\344\270\244\347\202\271\351\227\264\347\232\204\350\267\235\347\246\273.cpp" @@ -0,0 +1,30 @@ +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-25 12:01:40 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-25 12:08:09 + */ +/* + * @acwing app=acwing.cn id=618 lang=C++ + * + * 616. 两点间的距离 + */ + +// @acwing code start +#include +#include +#include +#include + +using namespace std; + +int main() { + pair a, b; + cin >> a.first >> a.second; + cin >> b.first >> b.second; + cout << fixed << setprecision(4) + << sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)) << endl; + return 0; +} + +// @acwing code end diff --git a/acwing/test.go b/acwing/test.go new file mode 100644 index 00000000..398e1212 --- /dev/null +++ b/acwing/test.go @@ -0,0 +1,5 @@ +import fmt + +int main(){ + return 0; +} diff --git "a/acwing/\346\200\273\347\273\223/1.md" "b/acwing/\346\200\273\347\273\223/1.md" new file mode 100644 index 00000000..5255be46 --- /dev/null +++ "b/acwing/\346\200\273\347\273\223/1.md" @@ -0,0 +1 @@ +递归中可以将需要传入或者传出的参数放在函数的参数列表中 diff --git a/leetcode/problems/.lcpr_data/bricks.json b/leetcode/problems/.lcpr_data/bricks.json index e4891713..19452760 100644 --- a/leetcode/problems/.lcpr_data/bricks.json +++ b/leetcode/problems/.lcpr_data/bricks.json @@ -60,7 +60,8 @@ "144": { "submit_time": [ 1776787803, - 1776788099 + 1776788099, + 1776846127 ], "type": 4, "review_day": [ @@ -74,7 +75,9 @@ }, "145": { "submit_time": [ - 1776788192 + 1776788192, + 1776846338, + 1776846527 ], "type": 4, "review_day": [ diff --git a/leetcode/problems/102.binary-tree-level-order-traversal.cpp b/leetcode/problems/102.binary-tree-level-order-traversal.cpp new file mode 100644 index 00000000..59f03dd0 --- /dev/null +++ b/leetcode/problems/102.binary-tree-level-order-traversal.cpp @@ -0,0 +1,82 @@ +/* + * @lc app=leetcode.cn id=102 lang=cpp + * @lcpr version=30204 + * + * [102] 二叉树的层序遍历 + */ + +// @lcpr-template-start +using namespace std; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// @lcpr-template-end +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), + * right(right) {} + * }; + */ +class Solution { +public: + vector> levelOrder(TreeNode *root) { + vector> res; + if (!root) + return res; + + queue q; + q.push(root); + + while (!q.empty()) { + int size = q.size(); + vector line; + while (size--) { + auto t = q.front(); + line.push_back(t->val); + q.pop(); + if (t->left) + q.push(t->left); + if (t->right) + q.push(t->right); + } + res.push_back(line); + line.clear(); + } + return res; + } +}; +// @lc code=end + +/* +// @lcpr case=start +// [3,9,20,null,null,15,7]\n +// @lcpr case=end + +// @lcpr case=start +// [1]\n +// @lcpr case=end + +// @lcpr case=start +// []\n +// @lcpr case=end + + */ diff --git a/leetcode/problems/144.binary-tree-preorder-traversal-2.cpp b/leetcode/problems/144.binary-tree-preorder-traversal-2.cpp new file mode 100644 index 00000000..bfb4f647 --- /dev/null +++ b/leetcode/problems/144.binary-tree-preorder-traversal-2.cpp @@ -0,0 +1,84 @@ +/* + * @lc app=leetcode.cn id=144 lang=cpp + * @lcpr version=30204 + * + * [144] 二叉树的前序遍历 + */ + +// @lcpr-template-start +using namespace std; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// @lcpr-template-end +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + public: + vector preorderTraversal(TreeNode *root) { + vector res; + if (!root) + return res; + stack st; + + st.push(root); + + while (st.size()) { + auto t = st.top(); + st.pop(); + + if (!t) { + continue; + } else { + res.push_back(t->val); + } + + st.push(t->right); + st.push(t->left); + } + + return res; + } +}; +// @lc code=end + +/* +// @lcpr case=start +// [1,null,2,3]\n +// @lcpr case=end + +// @lcpr case=start +// [1,2,3,4,5,null,8,null,null,6,7,9]\n +// @lcpr case=end + +// @lcpr case=start +// []\n +// @lcpr case=end + +// @lcpr case=start +// [1]\n +// @lcpr case=end + + */ diff --git a/leetcode/problems/145.binary-tree-postorder-traversal-2.cpp b/leetcode/problems/145.binary-tree-postorder-traversal-2.cpp new file mode 100644 index 00000000..4a232ddd --- /dev/null +++ b/leetcode/problems/145.binary-tree-postorder-traversal-2.cpp @@ -0,0 +1,101 @@ +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-22 00:15:14 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-22 16:27:58 + */ +/* + * @lc app=leetcode.cn id=145 lang=cpp + * @lcpr version=30204 + * + * [145] 二叉树的后序遍历 + */ + +// @lcpr-template-start +using namespace std; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// @lcpr-template-end +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + public: + vector res; + + void reverse_array(vector &res) { + int l = 0, r = res.size() - 1; + while (l < r) { + swap(res[l], res[r]); + ++l; + --r; + } + } + + vector postorderTraversal(TreeNode *root) { + if (!root) + return res; + + stack st; + st.push(root); + + while (!st.empty()) { + auto t = st.top(); + st.pop(); + + if (!t) { + continue; + } else { + res.push_back(t->val); + } + + st.push(t->left); + st.push(t->right); + } + + reverse_array(res); + return res; + } +}; +// @lc code=end + +/* +// @lcpr case=start +// [1,null,2,3]\n +// @lcpr case=end + +// @lcpr case=start +// [1,2,3,4,5,null,8,null,null,6,7,9]\n +// @lcpr case=end + +// @lcpr case=start +// []\n +// @lcpr case=end + +// @lcpr case=start +// [1]\n +// @lcpr case=end + + */ diff --git a/leetcode/problems/226.invert-binary-tree.cpp b/leetcode/problems/226.invert-binary-tree.cpp new file mode 100644 index 00000000..25bc7801 --- /dev/null +++ b/leetcode/problems/226.invert-binary-tree.cpp @@ -0,0 +1,93 @@ +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-23 15:04:03 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-23 15:44:44 + */ +/* + * @lc app=leetcode.cn id=226 lang=cpp + * @lcpr version=30204 + * + * [226] 翻转二叉树 + */ + +// @lcpr-template-start +using namespace std; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// @lcpr-template-end +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), + * right(right) {} + * }; + */ +class Solution { + public: + TreeNode *invertTree(TreeNode *root) { + if (!root) + return root; + + // 前序遍历 + // swap(root->left, root->right); + // invertTree(root->left); + // invertTree(root->right); + // 后序遍历 + // invertTree(root->left); + // invertTree(root->right); + // swap(root->left, root->right); + // // 中序遍历 + // invertTree(root->left); + // swap(root->left, root->right); + // invertTree(root->left); + // // 层次遍历 + queue q; + q.push(root); + while (!q.empty()) { + auto t = q.front(); + q.pop(); + swap(t->left, t->right); + if (t->left) + q.push(t->left); + if (t->right) + q.push(t->right); + } + return root; + } +}; +// @lc code=end + +/* +// @lcpr case=start +// [4,2,7,1,3,6,9]\n +// @lcpr case=end + +// @lcpr case=start +// [2,1,3]\n +// @lcpr case=end + +// @lcpr case=start +// []\n +// @lcpr case=end + + */ diff --git a/leetcode/problems/94.binary-tree-inorder-traversal-2.cpp b/leetcode/problems/94.binary-tree-inorder-traversal-2.cpp new file mode 100644 index 00000000..68b485b7 --- /dev/null +++ b/leetcode/problems/94.binary-tree-inorder-traversal-2.cpp @@ -0,0 +1,80 @@ +/* + * @Author: tkzzzzzz6 + * @Date: 2026-04-22 00:17:58 + * @LastEditors: tkzzzzzz6 + * @LastEditTime: 2026-04-23 14:21:14 + */ +/* + * @lc app=leetcode.cn id=94 lang=cpp + * @lcpr version=30204 + * + * [94] 二叉树的中序遍历 + */ + +// @lcpr-template-start +using namespace std; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// @lcpr-template-end +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + public: + vector inorderTraversal(TreeNode *root) { + vector res; + auto cur = root; + stack st; + + while (cur || !st.empty()) { + if (cur) { + st.push(cur); + cur = cur->left; + } else { + cur = st.top(); + st.pop(); + res.push_back(cur->val); + cur = cur->right; + } + } + return res; + } +}; +// @lc code=end + +/* +// @lcpr case=start +// [1,null,2,3]\n +// @lcpr case=end + +// @lcpr case=start +// []\n +// @lcpr case=end + +// @lcpr case=start +// [1]\n +// @lcpr case=end + + */ diff --git "a/niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ1.\345\255\227\347\254\246\344\270\262\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" "b/niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ1.\345\255\227\347\254\246\344\270\262\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" new file mode 100644 index 00000000..8a533a9f --- /dev/null +++ "b/niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ1.\345\255\227\347\254\246\344\270\262\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" @@ -0,0 +1,13 @@ +# @nc app=nowcoder id=8c949ea5f36f422594b306a2300315da topic=37 question=21224 lang=Python3 +# 2026-04-26 19:40:38 +# https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da?tpId=37&tqId=21224 +# [HJ1] 字符串最后一个单词的长度 + +# @nc code=start + + +a = input().split() +print(len(a[-1])) + + +# @nc code=end diff --git "a/niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ2.\350\256\241\347\256\227\346\237\220\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260.py" "b/niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ2.\350\256\241\347\256\227\346\237\220\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260.py" new file mode 100644 index 00000000..27f6a26c --- /dev/null +++ "b/niuke/problems/\345\215\216\344\270\272\346\234\272\350\257\225/HJ2.\350\256\241\347\256\227\346\237\220\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260.py" @@ -0,0 +1,32 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-26 19:45:14 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-26 20:10:59 +''' +# @nc app=nowcoder id=a35ce98431874e3a820dbe4b2d0508b1 topic=37 question=21225 lang=Python3 +# 2026-04-26 19:45:14 +# https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1?tpId=37&tqId=21225 +# [HJ2] 计算某字符出现次数 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + s = input().lower() + c = input().lower() + cnt = {} + print(s.count(c)) + +if __name__ == "__main__": + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP10.\347\211\233\347\211\233\346\234\200\345\245\275\347\232\204\346\234\213\345\217\213\344\273\254.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP10.\347\211\233\347\211\233\346\234\200\345\245\275\347\232\204\346\234\213\345\217\213\344\273\254.py" new file mode 100644 index 00000000..ddfd2a56 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP10.\347\211\233\347\211\233\346\234\200\345\245\275\347\232\204\346\234\213\345\217\213\344\273\254.py" @@ -0,0 +1,32 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-23 07:51:35 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 07:51:42 +''' +# @nc app=nowcoder id=ff823846af304a9a8cbbdfe2c9e8874c topic=314 question=10055850 lang=Python3 +# 2026-04-23 07:51:35 +# https://www.nowcoder.com/practice/ff823846af304a9a8cbbdfe2c9e8874c?tpId=314&tqId=10055850 +# [NP10] 牛牛最好的朋友们 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + name1 = input() + name2 = input() + print(name1 + name2) + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP11.\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP11.\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" new file mode 100644 index 00000000..d0b90b16 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP11.\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" @@ -0,0 +1,31 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-23 07:52:15 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 07:52:24 +''' +# @nc app=nowcoder id=3236759a8bcd43819cfd2b7eb4a9eebb topic=314 question=10055851 lang=Python3 +# 2026-04-23 07:52:15 +# https://www.nowcoder.com/practice/3236759a8bcd43819cfd2b7eb4a9eebb?tpId=314&tqId=10055851 +# [NP11] 单词的长度 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + word = input() + print(len(word)) + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP12.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\272\214\357\274\211.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP12.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\272\214\357\274\211.py" new file mode 100644 index 00000000..0b004709 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP12.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\272\214\357\274\211.py" @@ -0,0 +1,34 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-23 07:53:36 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 07:53:49 +''' +# @nc app=nowcoder id=3bd78a6de34a447fa49571a03f164fd3 topic=314 question=2365106 lang=Python3 +# 2026-04-23 07:53:36 +# https://www.nowcoder.com/practice/3bd78a6de34a447fa49571a03f164fd3?tpId=314&tqId=2365106 +# [NP12] 格式化输出(二) + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + name = input() + + print(name.lower()) + print(name.upper()) + # print(name.capitalize()) + print(name.title()) +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP13.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\211\357\274\211.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP13.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\211\357\274\211.py" new file mode 100644 index 00000000..58257d08 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP13.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\211\357\274\211.py" @@ -0,0 +1,30 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-23 07:58:34 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 07:58:45 +''' +# @nc app=nowcoder id=cc6cb2b4aac446da902356fec043f5c1 topic=314 question=2365278 lang=Python3 +# 2026-04-23 07:58:34 +# https://www.nowcoder.com/practice/cc6cb2b4aac446da902356fec043f5c1?tpId=314&tqId=2365278 +# [NP13] 格式化输出(三) + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect + +def solve(): + name = input().lstrip().rstrip() + print(name) + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP14.\344\270\215\347\224\250\345\276\252\347\216\257\350\257\255\345\217\245\347\232\204\351\207\215\345\244\215\350\276\223\345\207\272.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP14.\344\270\215\347\224\250\345\276\252\347\216\257\350\257\255\345\217\245\347\232\204\351\207\215\345\244\215\350\276\223\345\207\272.py" new file mode 100644 index 00000000..482746c4 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP14.\344\270\215\347\224\250\345\276\252\347\216\257\350\257\255\345\217\245\347\232\204\351\207\215\345\244\215\350\276\223\345\207\272.py" @@ -0,0 +1,32 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-23 07:59:22 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 07:59:28 +''' +# @nc app=nowcoder id=9baca399143e418cb471159d3b96743e topic=314 question=10055852 lang=Python3 +# 2026-04-23 07:59:22 +# https://www.nowcoder.com/practice/9baca399143e418cb471159d3b96743e?tpId=314&tqId=10055852 +# [NP14] 不用循环语句的重复输出 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + name = input() + for _ in range(100): + print(name,end='') + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP15.\346\210\252\345\217\226\347\224\250\346\210\267\345\220\215\345\211\21510\344\275\215.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP15.\346\210\252\345\217\226\347\224\250\346\210\267\345\220\215\345\211\21510\344\275\215.py" new file mode 100644 index 00000000..b472bd3a --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP15.\346\210\252\345\217\226\347\224\250\346\210\267\345\220\215\345\211\21510\344\275\215.py" @@ -0,0 +1,32 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-23 08:00:15 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 08:00:22 +''' +# @nc app=nowcoder id=0d3cd14ca4bc4e67ace6399a041e961e topic=314 question=10055853 lang=Python3 +# 2026-04-23 08:00:15 +# https://www.nowcoder.com/practice/0d3cd14ca4bc4e67ace6399a041e961e?tpId=314&tqId=10055853 +# [NP15] 截取用户名前10位 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + name = input() + if len(name) > 10: + name = name[:10] + print(name) +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP27.\346\234\213\345\217\213\344\273\254\347\232\204\345\226\234\345\245\275.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP27.\346\234\213\345\217\213\344\273\254\347\232\204\345\226\234\345\245\275.py" new file mode 100644 index 00000000..43cff5cb --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP27.\346\234\213\345\217\213\344\273\254\347\232\204\345\226\234\345\245\275.py" @@ -0,0 +1,38 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-26 20:13:26 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-26 20:15:05 +''' +# @nc app=nowcoder id=c62ef46892ef4c478802df983f8624b7 topic=314 question=2549745 lang=Python3 +# 2026-04-26 20:13:26 +# https://www.nowcoder.com/practice/c62ef46892ef4c478802df983f8624b7?tpId=314&tqId=2549745 +# [NP27] 朋友们的喜好 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + res = [] + name = ['Niumei', 'YOLO', 'Niu Ke Le', 'Mona'] + food = ['pizza', 'fish', 'potato', 'beef'] + nums = [3, 6, 0, 3] + + res.append(name) + res.append(food) + res.append(nums) + + print(res) + +if __name__ == "__main__": + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP28.\345\257\206\347\240\201\346\270\270\346\210\217.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP28.\345\257\206\347\240\201\346\270\270\346\210\217.py" new file mode 100644 index 00000000..fdebd274 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP28.\345\257\206\347\240\201\346\270\270\346\210\217.py" @@ -0,0 +1,36 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-26 20:15:54 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-26 20:16:05 +''' +# @nc app=nowcoder id=36641ab168664384aff798ba7ce34bc1 topic=314 question=2583410 lang=Python3 +# 2026-04-26 20:15:54 +# https://www.nowcoder.com/practice/36641ab168664384aff798ba7ce34bc1?tpId=314&tqId=2583410 +# [NP28] 密码游戏 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + s = input().strip() + a = [str((int(c) + 3)%9) for c in s] + a[0],a[2] = a[2],a[0] + a[3],a[1] = a[1],a[3] + + print("".join(a)) + + + +if __name__ == "__main__": + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP4.\350\257\273\345\205\245\346\225\264\346\225\260\346\225\260\345\255\227.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP4.\350\257\273\345\205\245\346\225\264\346\225\260\346\225\260\345\255\227.py" index fd9adaf8..9460f287 100644 --- "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP4.\350\257\273\345\205\245\346\225\264\346\225\260\346\225\260\345\255\227.py" +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP4.\350\257\273\345\205\245\346\225\264\346\225\260\346\225\260\345\255\227.py" @@ -10,7 +10,22 @@ # [NP4] 读入整数数字 # @nc code=start +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() +def solve(): + num = int(input()) + print(num) + print(type(num)) + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() # @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP5.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\200\357\274\211.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP5.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\200\357\274\211.py" new file mode 100644 index 00000000..8e4d2fa5 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP5.\346\240\274\345\274\217\345\214\226\350\276\223\345\207\272\357\274\210\344\270\200\357\274\211.py" @@ -0,0 +1,31 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-23 07:44:15 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 07:45:32 +''' +# @nc app=nowcoder id=fe82dc48f4f04b2397a85fb1bdbe34a2 topic=314 question=2365083 lang=Python3 +# 2026-04-23 07:44:15 +# https://www.nowcoder.com/practice/fe82dc48f4f04b2397a85fb1bdbe34a2?tpId=314&tqId=2365083 +# [NP5] 格式化输出(一) + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + name = input() + print(f"I am {name} and I am studying Python in Nowcoder!") + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP6.\347\211\233\347\211\233\347\232\204\345\260\217\346\225\260\350\276\223\345\207\272.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP6.\347\211\233\347\211\233\347\232\204\345\260\217\346\225\260\350\276\223\345\207\272.py" new file mode 100644 index 00000000..fb3276dd --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP6.\347\211\233\347\211\233\347\232\204\345\260\217\346\225\260\350\276\223\345\207\272.py" @@ -0,0 +1,26 @@ +# @nc app=nowcoder id=0454d1bd28cd499ba06afdabe0fb9caf topic=314 question=10055839 lang=Python3 +# 2026-04-23 07:45:56 +# https://www.nowcoder.com/practice/0454d1bd28cd499ba06afdabe0fb9caf?tpId=314&tqId=10055839 +# [NP6] 牛牛的小数输出 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + f = float(input()) + # print(f"{f:.2f}") + print("{:.2f}".format(f)) + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP7.\345\260\217\346\225\260\345\214\226\346\225\264\346\225\260.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP7.\345\260\217\346\225\260\345\214\226\346\225\264\346\225\260.py" index 14511800..163e1bf2 100644 --- "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP7.\345\260\217\346\225\260\345\214\226\346\225\264\346\225\260.py" +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP7.\345\260\217\346\225\260\345\214\226\346\225\264\346\225\260.py" @@ -1,3 +1,9 @@ +''' +Author: tkzzzzzz6 +Date: 2026-04-22 22:02:53 +LastEditors: tkzzzzzz6 +LastEditTime: 2026-04-23 07:48:25 +''' # @nc app=nowcoder id=e302a62c1fbd4bbf84793e8c52ce4705 topic=314 question=10055829 lang=Python3 # 2026-04-22 22:02:53 # https://www.nowcoder.com/practice/e302a62c1fbd4bbf84793e8c52ce4705?tpId=314&tqId=10055829 @@ -5,11 +11,21 @@ # @nc code=start -import sys +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() -for line in sys.stdin: - a = line.split() - print(int(a[0]) + int(a[1])) +def solve(): + x = float(input()) + print(int(x)) + +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() # @nc code=end diff --git "a/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP8.\344\270\272\346\225\264\346\225\260\345\242\236\345\212\240\345\260\217\346\225\260\347\202\271.py" "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP8.\344\270\272\346\225\264\346\225\260\345\242\236\345\212\240\345\260\217\346\225\260\347\202\271.py" new file mode 100644 index 00000000..6d64af59 --- /dev/null +++ "b/niuke/problems/\347\211\233\345\256\242\351\242\230\351\234\270-python\345\205\245\351\227\250\345\210\260\345\256\236\350\267\265\345\233\233\345\215\201\346\213\233/NP8.\344\270\272\346\225\264\346\225\260\345\242\236\345\212\240\345\260\217\346\225\260\347\202\271.py" @@ -0,0 +1,25 @@ +# @nc app=nowcoder id=ca4dd83cd8fb49c2a6d2b7cd182e3de7 topic=314 question=10055835 lang=Python3 +# 2026-04-23 07:49:46 +# https://www.nowcoder.com/practice/ca4dd83cd8fb49c2a6d2b7cd182e3de7?tpId=314&tqId=10055835 +# [NP8] 为整数增加小数点 + +# @nc code=start + +import sys, math +from collections import deque, defaultdict, Counter +import heapq, bisect +input = lambda: sys.stdin.readline().rstrip() + +def solve(): + x = float(int(input())) + print(x) + print(type(x)) +if __name__ == "__main__": + sys.setrecursionlimit(10**7) + t = 1 + # t = int(input()) + for _ in range(t): + solve() + + +# @nc code=end