├── .gitignore ├── 2017年期中考试题 ├── 1-date.cpp ├── 2-hospital.cpp ├── 3-disappeared.cpp ├── 4-doc&nurse.cpp ├── 5-flatten.cpp ├── 6-max_rec.cpp ├── 7-bacteria.cpp ├── README.md └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5-1.PNG │ ├── 5-2.PNG │ ├── 6.PNG │ └── 7.PNG ├── 2017年期末考试题 ├── README.md └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5-1.PNG │ ├── 5-2.PNG │ ├── 6.PNG │ ├── 7.PNG │ └── 8.PNG ├── 2018年期中考试题 ├── 1-text.cpp ├── 2-phote.cpp ├── 3-jump.cpp ├── 4-agct.cpp ├── 5-fishing.cpp ├── 6-house.cpp ├── 7-car.cpp ├── 8-AI.cpp ├── 9-priority_q.cpp ├── README.md └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3-1.PNG │ ├── 3-2.PNG │ ├── 4.PNG │ ├── 5.PNG │ ├── 6.PNG │ ├── 7-1.PNG │ ├── 7-2.PNG │ ├── 8.PNG │ ├── 9-1.PNG │ └── 9-2.PNG ├── 2018年期末考试题 └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5-1.PNG │ ├── 5-2.PNG │ ├── 6.PNG │ ├── 7.PNG │ ├── 8.PNG │ ├── 9-1.PNG │ └── 9-2.PNG ├── 2019-2020期末考试 └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4-1.PNG │ ├── 4-2.PNG │ ├── 5.PNG │ ├── 6.PNG │ ├── 7.PNG │ ├── 8-1.PNG │ ├── 8-2.PNG │ └── 9.PNG ├── 2019年期中考试题 ├── 1-Frac.cpp ├── 2-Count.cpp ├── 3-MilkTea.cpp ├── 4-Encryp.cpp ├── 5-Binary.cpp ├── 6-Coins.cpp ├── 7-N-gram.cpp ├── 8-Cyclic.cpp ├── 9-Influ.cpp ├── README.md └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5.PNG │ ├── 6.PNG │ ├── 7.PNG │ ├── 8.PNG │ └── 9.PNG ├── 2019年期末考试考前模拟 ├── 1-good.cpp ├── 7-treesum.cpp ├── 8-post.cpp ├── 9-angrybirds.cpp ├── README.md └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5.PNG │ ├── 6.PNG │ ├── 7-1.PNG │ ├── 7-2.PNG │ ├── 8.PNG │ └── 9.PNG ├── README.md ├── 初步编程练习 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp ├── 5.cpp └── README.md ├── 抄程序练习 ├── 1-BubbleSort.cpp ├── 2-FracAdd.cpp ├── 3-BigIntAdd.cpp └── README.md ├── 期中考试考前模拟 ├── 1-AmicNum.cpp ├── 2-Intersec.cpp ├── 3-Rotate.cpp ├── 4-Bacteria.cpp ├── 5-Influ.cpp ├── 6-Bracket.cpp ├── 7-Quad.cpp ├── 8-MagicSquare.cpp ├── 9-Calendar.cpp ├── README.md └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5.PNG │ ├── 6.PNG │ ├── 7.PNG │ ├── 8.PNG │ └── 9.PNG ├── 第一阶段 ├── 第一阶段编程练习1 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ ├── 4.cpp │ ├── 5.cpp │ ├── 6.cpp │ └── README.md ├── 第一阶段编程练习2 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ ├── 4.cpp │ ├── 5.cpp │ ├── 6.cpp │ └── README.md ├── 第一阶段编程练习3 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ ├── 4.cpp │ ├── 5.cpp │ └── README.md └── 第一阶段编程练习4 │ ├── 1-prime.cpp │ ├── 2-newton.cpp │ ├── 3-quad.cpp │ ├── 4-number7.cpp │ ├── 5-equiv.cpp │ ├── README.md │ └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ └── 5.PNG ├── 第三阶段 ├── 第三阶段编程练习1 │ ├── 1-BinaryComp.cpp │ ├── 2-Stone.cpp │ ├── 3-LCM.cpp │ ├── 4-Palindrome.cpp │ ├── 5-487-3279.cpp │ ├── 6-Calendar.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ ├── 5-1.PNG │ │ ├── 5-2.PNG │ │ ├── 6-1.PNG │ │ └── 6-2.PNG ├── 第三阶段编程练习2 │ ├── 1-Tooth.cpp │ ├── 10-SetProd.cpp │ ├── 2-Equiv.cpp │ ├── 3-Bacteria.cpp │ ├── 4-Monkey.cpp │ ├── 5-Queue.cpp │ ├── 6-SimilarString.cpp │ ├── 7-P-Encoding.cpp │ ├── 8-BinaryTree.cpp │ ├── 9-Apple.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 10.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ ├── 5.PNG │ │ ├── 6.PNG │ │ ├── 7.PNG │ │ ├── 8.PNG │ │ └── 9.PNG ├── 第三阶段编程练习3 │ ├── 1-PrefixExp.cpp │ ├── 10-Match.cpp │ ├── 2-BalanceMat.cpp │ ├── 3-KBaseSeq.cpp │ ├── 4-Factor.cpp │ ├── 5-Coin.cpp │ ├── 6-SimpleShort.cpp │ ├── 7-BoolExp.cpp │ ├── 8-Cal.cpp │ ├── 8-Cal2.cpp │ ├── 9-Permutation.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 10.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ ├── 5.PNG │ │ ├── 6.PNG │ │ ├── 7.PNG │ │ ├── 8.PNG │ │ └── 9.PNG └── 第三阶段编程练习4 │ ├── 1-PrimeFac.cpp │ ├── 10-TSP.cpp │ ├── 2-Element.cpp │ ├── 3-Maze.cpp │ ├── 4-Fruit.cpp │ ├── 5-Pattern.cpp │ ├── 6-Red&Black.cpp │ ├── 7-Rational.cpp │ ├── 8-BalanceMat.cpp │ ├── 9-Queue.cpp │ ├── README.md │ └── pics │ ├── 1.PNG │ ├── 10.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5.PNG │ ├── 6-1.PNG │ ├── 6-2.PNG │ ├── 7.PNG │ ├── 8.PNG │ └── 9.PNG ├── 第二阶段 ├── 第二阶段编程练习1 │ ├── 1-BigSmallCase.cpp │ ├── 2-Indep3.cpp │ ├── 3-BaseCalc.cpp │ ├── 4-Area.cpp │ ├── 5-BinaryAdd.cpp │ ├── 6-Count.cpp │ ├── 7-DiffNumber.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ ├── 5.PNG │ │ ├── 6.PNG │ │ └── 7.PNG ├── 第二阶段编程练习2 │ ├── 1-doctor.cpp │ ├── 2-greatk.cpp │ ├── 3-specialnumber.cpp │ ├── 4-dif_sum.cpp │ ├── 5-primepair.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ └── 5.PNG ├── 第二阶段编程练习3 │ ├── 1-e.cpp │ ├── 2-invoice.cpp │ ├── 3-lake.cpp │ ├── 4-car.cpp │ ├── 5-jumpwater.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ └── 5.PNG ├── 第二阶段编程练习4 │ ├── 1-dif_sum.cpp │ ├── 2-mat_add.cpp │ ├── 3-snake_fill.cpp │ ├── 4-frog.cpp │ ├── 5-doctor.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ └── 5.PNG ├── 第二阶段编程练习5 │ ├── 1-reverse_word.cpp │ ├── 2-sort_char.cpp │ ├── 3-grades.cpp │ ├── 4-palindrom.cpp │ ├── 5-count_words.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ ├── 5-1.PNG │ │ └── 5-2.PNG └── 第二阶段编程练习6 │ ├── 1-Doctor.cpp │ ├── 2-Battery.cpp │ ├── 3-Date.cpp │ ├── 4-Prime.cpp │ ├── 5-Short.cpp │ ├── 6-MaxDis.cpp │ ├── README.md │ └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5.PNG │ └── 6.PNG ├── 第四阶段 ├── 第四阶段编程练习1 │ ├── 1-sort3.cpp │ ├── 10-road.cpp │ ├── 2-matmul.cpp │ ├── 3-las.cpp │ ├── 4-basek.cpp │ ├── 5-ngram.cpp │ ├── 6-peanut.cpp │ ├── 7-cheatcheck.cpp │ ├── 8-wpassword.cpp │ ├── 9-maxprod.cpp │ ├── README.md │ └── pics │ │ ├── 1.PNG │ │ ├── 10.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ ├── 5.PNG │ │ ├── 6-1.PNG │ │ ├── 6-2.PNG │ │ ├── 7.PNG │ │ ├── 8-1.PNG │ │ ├── 8-2.PNG │ │ └── 9.PNG ├── 第四阶段编程练习2 │ └── pics │ │ ├── 1.PNG │ │ ├── 2-1.PNG │ │ ├── 2-2.PNG │ │ ├── 3.PNG │ │ ├── 4.PNG │ │ ├── 5-1.PNG │ │ ├── 5-2.PNG │ │ ├── 6.PNG │ │ └── 7.PNG ├── 第四阶段编程练习3 │ └── pics │ │ ├── 1.PNG │ │ ├── 2.PNG │ │ ├── 3.PNG │ │ ├── 4-1.PNG │ │ ├── 4-2.PNG │ │ └── 5.PNG └── 第四阶段编程练习4 │ └── pics │ ├── 1.PNG │ ├── 2.PNG │ ├── 3.PNG │ ├── 4.PNG │ ├── 5-1.PNG │ └── 5-2.PNG └── 编程基础练习 ├── 1-Leap.cpp ├── 10-TypeSetting.cpp ├── 2-ReverseWord.cpp ├── 3-Merge.cpp ├── 4-BaseCal.cpp ├── 5-AmicNum.cpp ├── 6-Doctor.cpp ├── 7-Activity.cpp ├── 8-CountWord.cpp ├── 9-Substr.cpp ├── README.md └── pics ├── 1.PNG ├── 10.PNG ├── 2.PNG ├── 3.PNG ├── 4.PNG ├── 5.PNG ├── 6.PNG ├── 7.PNG ├── 8-1.PNG ├── 8-2.PNG └── 9.PNG /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /2017年期中考试题/1-date.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n; 10 | cin >> n; 11 | cout << ((n == 1 || n == 3 || n == 5) ? "NO" : "YES") << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /2017年期中考试题/2-hospital.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct People { 6 | int id; 7 | int key; 8 | friend bool operator < (const People &a, const People &b) { 9 | return a.key > b.key || a.key == b.key && a.id < b.id; 10 | } 11 | } peo[10000]; 12 | 13 | int main() 14 | { 15 | int t, n, k; 16 | cin >> t; 17 | while (t--) { 18 | cin >> n >> k; 19 | for (int i = 0; i < n; ++i) { 20 | peo[i].id = i + 1; 21 | cin >> peo[i].key; 22 | } 23 | sort(peo, peo + n); 24 | k = min(k, n); 25 | cout << k << endl; 26 | for (int i = 0; i < k; ++i) { 27 | if (i == k - 1) 28 | cout << peo[i].id << endl; 29 | else 30 | cout << peo[i].id << ' '; 31 | } 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /2017年期中考试题/3-disappeared.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n, tmp; 10 | bool seen[110] = {false}; 11 | cin >> n; 12 | for (int i = 0; i < n; ++i) { 13 | cin >> tmp; 14 | seen[tmp] = true; 15 | } 16 | for (int i = 1; i <= n; ++i) 17 | if (!seen[i]) 18 | cout << i << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /2017年期中考试题/4-doc&nurse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool related4(int n) 8 | { 9 | if (n % 4 == 0) 10 | return true; 11 | while (n) { 12 | if (n % 10 == 4) 13 | return true; 14 | n /= 10; 15 | } 16 | return false; 17 | } 18 | 19 | int main() 20 | { 21 | int n, res = 0, num = 1, k = 0; 22 | cin >> n; 23 | while (k < 10) { 24 | while (related4(num)) 25 | num++; 26 | res += num; 27 | num++, k++; 28 | } 29 | cout << res << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /2017年期中考试题/5-flatten.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int m, n, nums[100][100]; 10 | cin >> m >> n; 11 | for (int i = 0; i < m; ++i) 12 | for (int j = 0; j < n; ++j) 13 | cin >> nums[i][j]; 14 | for (int k = 0; k < m + n - 1; ++k) { 15 | int i = 0, j = k; 16 | if (j >= n) { 17 | j = n - 1; 18 | i = k - j; 19 | } 20 | while (i < m && j >= 0) { 21 | cout << nums[i][j] << endl; 22 | i++, j--; 23 | } 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /2017年期中考试题/6-max_rec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int num[20000]; 8 | 9 | int main() 10 | { 11 | int n, res = 0; 12 | cin >> n; 13 | for (int i = 0; i < n; ++i) 14 | cin >> num[i]; 15 | for (int i = 0; i < n; ++i) { 16 | int minh = num[i]; 17 | for (int j = i; j < n; ++j) { 18 | minh = min(minh, num[j]); 19 | res = max(res, minh * (j - i + 1)); 20 | } 21 | } 22 | cout << res << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /2017年期中考试题/7-bacteria.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Group { 9 | double grow; 10 | int id; 11 | } g[100]; 12 | 13 | bool cmp(const Group &a, const Group &b) 14 | { 15 | return a.grow < b.grow || a.grow == b.grow && a.id < b.id; 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | double a, b; 22 | cin >> n; 23 | for (int i = 0; i < n; ++i) { 24 | cin >> g[i].id; 25 | cin >> a >> b; 26 | g[i].grow = b / a; 27 | } 28 | sort(g, g + n, cmp); 29 | double ave = (g[0].grow + g[n - 1].grow) / 2; 30 | int split = 0; 31 | while (split < n && g[split].grow < ave) 32 | split++; 33 | cout << n - split << endl; 34 | for (int i = split; i < n; ++i) 35 | cout << g[i].id << endl; 36 | cout << split << endl; 37 | for (int i = 0; i < split; ++i) 38 | cout << g[i].id << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /2017年期中考试题/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/1.PNG -------------------------------------------------------------------------------- /2017年期中考试题/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/2.PNG -------------------------------------------------------------------------------- /2017年期中考试题/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/3.PNG -------------------------------------------------------------------------------- /2017年期中考试题/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/4.PNG -------------------------------------------------------------------------------- /2017年期中考试题/pics/5-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/5-1.PNG -------------------------------------------------------------------------------- /2017年期中考试题/pics/5-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/5-2.PNG -------------------------------------------------------------------------------- /2017年期中考试题/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/6.PNG -------------------------------------------------------------------------------- /2017年期中考试题/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期中考试题/pics/7.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/1.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/2.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/3.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/4.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/5-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/5-1.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/5-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/5-2.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/6.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/7.PNG -------------------------------------------------------------------------------- /2017年期末考试题/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2017年期末考试题/pics/8.PNG -------------------------------------------------------------------------------- /2018年期中考试题/1-text.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int n, tmp; 12 | double res = 0; 13 | cin >> n; 14 | while (n--) { 15 | cin >> tmp; 16 | res += (tmp + 69) / 70; 17 | } 18 | printf("%.1f\n", res / 10); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /2018年期中考试题/2-phote.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int n; 12 | int nums[100]; 13 | while (cin >> n && n != 0) { 14 | for (int i = 0; i < n; ++i) 15 | cin >> nums[i]; 16 | sort(nums, nums + n); 17 | cout << nums[n / 2] << endl; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /2018年期中考试题/3-jump.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int nums[30], n; 7 | bool visited[30] = {false}; 8 | nums[0] = 1; 9 | cin >> n; 10 | for (int i = 1; i <= n; ++i) 11 | cin >> nums[i]; 12 | int res = 0, idx = 0; 13 | while (true) { 14 | if (idx > n) 15 | break; 16 | if (idx < 0) 17 | idx = 0; 18 | if (visited[idx]) { 19 | idx += 1; 20 | res += 2; 21 | } else if (nums[idx] == 0) { 22 | visited[idx] = true; 23 | res += 2; 24 | idx += 1; 25 | } else { 26 | res += 1; 27 | visited[idx] = true; 28 | idx += nums[idx]; 29 | } 30 | } 31 | cout << res << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /2018年期中考试题/4-agct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char s1[256], s2[256]; 6 | 7 | int main() 8 | { 9 | int n; 10 | cin >> n; 11 | while (n--) { 12 | cin >> s1; 13 | int l = strlen(s1); 14 | for (int i = 0; i < l; ++i) { 15 | if (s1[i] == 'A') 16 | s2[i] = 'T'; 17 | else if (s1[i] == 'T') 18 | s2[i] = 'A'; 19 | else if (s1[i] == 'C') 20 | s2[i] = 'G'; 21 | else 22 | s2[i] = 'C'; 23 | } 24 | s2[l] = '\0'; 25 | cout << s2 << endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /2018年期中考试题/5-fishing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int year, month, day; 7 | int ms[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 8 | cin >> year >> month >> day; 9 | int d = 0; 10 | for (int i = 1990; i < year; ++i) 11 | if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) 12 | d += 366; 13 | else 14 | d += 365; 15 | for (int i = 1; i < month; ++i) 16 | d += ms[i]; 17 | if (month > 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) 18 | d += 1; 19 | d += day - 1; 20 | d %= 5; 21 | cout << (d < 3 ? "fishing" : "sleeping") << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /2018年期中考试题/6-house.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool judge(int mat[][20], int i, int j, int k, int l) 5 | { 6 | for (int x = i; x < j; ++x) 7 | for (int y = k; y < l; ++y) 8 | if (mat[x][y] != 0) 9 | return false; 10 | return true; 11 | } 12 | 13 | int main() 14 | { 15 | int m, n, mat[20][20]; 16 | cin >> m >> n; 17 | for (int i = 0; i < m; ++i) 18 | for (int j = 0; j < n; ++j) 19 | cin >> mat[i][j]; 20 | int res = 0; 21 | for (int i = 0; i < m; ++i) 22 | for (int j = i + 1; j <= m; ++j) 23 | for (int k = 0; k < n; ++k) 24 | for (int l = k + 1; l <= n; ++l) 25 | if (judge(mat, i, j, k, l)) 26 | res = max(res, (j - i) * (l - k)); 27 | cout << res << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /2018年期中考试题/7-car.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool leap(int year) 6 | { 7 | return year % 4 == 0 && year % 100 != 0 8 | || year % 400 == 0; 9 | } 10 | 11 | int weekday(int year, int month, int day) 12 | { 13 | int start = 5; // 0-6 for 周一-日 14 | for (int i = 2000; i < year; ++i) 15 | start += leap(i) ? 366 : 365; 16 | int months[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 17 | if (leap(year)) 18 | months[2] = 29; 19 | for (int i = 1; i < month; ++i) 20 | start += months[i]; 21 | start += day - 1; 22 | return start % 7; 23 | } 24 | 25 | bool restrict(int DAY, char end) 26 | { 27 | return DAY == 0 && (end == '1' || end == '6') 28 | || DAY == 1 && (end == '2' || end == '7') 29 | || DAY == 2 && (end == '3' || end == '8') 30 | || DAY == 3 && (end == '4' || end == '9') 31 | || DAY == 4 && (end == '5' || end == '0' || isalpha(end)); 32 | } 33 | 34 | int main() 35 | { 36 | int n; 37 | cin >> n; 38 | while (n--) { 39 | int year, month, day; 40 | char tmp; 41 | char id[10]; 42 | cin >> year >> tmp >> month >> tmp >> day; 43 | cin >> id; 44 | int DAY = weekday(year, month, day); 45 | cout << (restrict(DAY, id[5]) ? "yes" : "no") << endl; 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /2018年期中考试题/8-AI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool getpattern(char s[], int l, const char pattern[], double *res) 7 | { 8 | char *p = strstr(s, pattern); 9 | if (p == NULL) 10 | return false; 11 | p += strlen(pattern); 12 | char c; 13 | sscanf(p, "%lf%c", res, &c); 14 | switch (c) 15 | { 16 | case 'm': 17 | *res /= 1000; 18 | break; 19 | case 'k': 20 | *res *= 1000; 21 | break; 22 | case 'M': 23 | *res *= 1e6; 24 | break; 25 | } 26 | return true; 27 | } 28 | 29 | int main() 30 | { 31 | char s[1010]; 32 | int n; 33 | cin >> n; 34 | cin.get(); 35 | double P, I, U; 36 | bool gotIUP[3] = { false }; 37 | for (int i = 0; i < n; ++i) { 38 | cin.getline(s, 1000); 39 | printf("Problem #%d\n", i + 1); 40 | int l = strlen(s); 41 | gotIUP[0] = getpattern(s, l, "I=", &I); 42 | gotIUP[1] = getpattern(s, l, "U=", &U); 43 | gotIUP[2] = getpattern(s, l, "P=", &P); 44 | int idx = 0; 45 | for (int i = 0; i < 3; ++i) 46 | if (!gotIUP[i]) 47 | idx = i; 48 | switch (idx) 49 | { 50 | case 0: 51 | I = P / U; 52 | printf("I=%.2lfA\n", I); 53 | break; 54 | case 1: 55 | U = P / I; 56 | printf("U=%.2lfV\n", U); 57 | break; 58 | case 2: 59 | P = U * I; 60 | printf("P=%.2lfW\n", P); 61 | break; 62 | default: 63 | printf("???\n"); 64 | break; 65 | } 66 | } 67 | return 0; 68 | } -------------------------------------------------------------------------------- /2018年期中考试题/9-priority_q.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int array[200000]; // 本题不要求掌握,有兴趣的同学可以自学堆 6 | int asize = 0; 7 | 8 | void up(int array[], int size, int idx) 9 | { 10 | while (idx > 0) { 11 | int parent = (idx - 1) / 2; 12 | if (array[parent] > array[idx]) { 13 | swap(array[parent], array[idx]); 14 | idx = parent; 15 | } else { 16 | break; 17 | } 18 | } 19 | } 20 | 21 | void down(int array[], int size, int idx) 22 | { 23 | int leftidx = idx * 2 + 1; 24 | int rightidx = idx * 2 + 2; 25 | while (leftidx < size) { 26 | int minidx = rightidx; 27 | if (rightidx >= size || array[leftidx] < array[rightidx]) 28 | minidx = leftidx; 29 | if (array[minidx] < array[idx]) { 30 | swap(array[minidx], array[idx]); 31 | idx = minidx; 32 | leftidx = idx * 2 + 1; 33 | rightidx = idx * 2 + 2; 34 | } else { 35 | break; 36 | } 37 | } 38 | } 39 | 40 | int extract(int array[], int &size) 41 | { 42 | swap(array[0], array[size - 1]); 43 | size--; 44 | down(array, size, 0); 45 | return array[size]; 46 | } 47 | 48 | void insert(int array[], int &size, int val) 49 | { 50 | array[size++] = val; 51 | up(array, size, size - 1); 52 | return; 53 | } 54 | 55 | 56 | void heapify(int array[], int size) 57 | { 58 | for (int i = (size - 1) / 2; i >= 0; --i) 59 | down(array, size, i); 60 | return; 61 | } 62 | 63 | int main() 64 | { 65 | cin >> asize; 66 | for (int i = 0; i < asize; ++i) 67 | cin >> array[i]; 68 | heapify(array, asize); 69 | int k, val; 70 | char ope[100]; 71 | cin >> k; 72 | while (k--) { 73 | cin >> ope; 74 | if (ope[0] == 'A') { 75 | cin >> val; 76 | insert(array, asize, val); 77 | } else { 78 | if (asize == 0) 79 | cout << "NULL" << endl; 80 | else 81 | cout << extract(array, asize) << endl; 82 | } 83 | } 84 | return 0; 85 | } -------------------------------------------------------------------------------- /2018年期中考试题/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/1.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/2.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/3-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/3-1.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/3-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/3-2.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/4.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/5.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/6.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/7-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/7-1.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/7-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/7-2.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/8.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/9-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/9-1.PNG -------------------------------------------------------------------------------- /2018年期中考试题/pics/9-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期中考试题/pics/9-2.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/1.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/2.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/3.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/4.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/5-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/5-1.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/5-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/5-2.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/6.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/7.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/8.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/9-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/9-1.PNG -------------------------------------------------------------------------------- /2018年期末考试题/pics/9-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2018年期末考试题/pics/9-2.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/1.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/2.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/3.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/4-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/4-1.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/4-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/4-2.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/5.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/6.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/7.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/8-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/8-1.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/8-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/8-2.PNG -------------------------------------------------------------------------------- /2019-2020期末考试/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019-2020期末考试/pics/9.PNG -------------------------------------------------------------------------------- /2019年期中考试题/1-Frac.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } 7 | 8 | int main() 9 | { 10 | int n; 11 | int nums[200]; 12 | cin >> n; 13 | for (int i = 0; i < n; ++i) 14 | cin >> nums[i]; 15 | sort(nums, nums + n); 16 | bool flag = false; 17 | for (int i = 0; i < n - 1; ++i) { 18 | if (nums[i] == 0) 19 | continue; 20 | for (int j = i + 1; j < n; ++j) { 21 | if (gcd(nums[i], nums[j]) == 1) { 22 | if (!flag) { 23 | flag = true; 24 | printf("%d/%d", nums[i], nums[j]); 25 | } else { 26 | printf(",%d/%d", nums[i], nums[j]); 27 | } 28 | } 29 | } 30 | } 31 | if (!flag) 32 | cout << "NO"; 33 | cout << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /2019年期中考试题/2-Count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int upper, x; 9 | cin >> upper >> x; 10 | int cnt[10] = { 0 }; 11 | for (int i = 1; i <= upper; ++i) { 12 | int tmp = i; 13 | while (tmp) { 14 | cnt[tmp % 10]++; 15 | tmp /= 10; 16 | } 17 | } 18 | cout << cnt[x] << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /2019年期中考试题/3-MilkTea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int cnt = 0; 9 | for (int i = 1; i * i <= 150; ++i) { 10 | if (cnt > 0) 11 | cout << ' '; 12 | cout << 'N' << i * i; 13 | cnt++; 14 | } 15 | cout << endl << cnt << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /2019年期中考试题/4-Encryp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int m, n; 9 | char s[110], fill[220]; 10 | char matrix[11][11]; 11 | bool visited[11][11]; 12 | cin >> m >> n; 13 | cin.get(); 14 | cin.getline(s, 102); 15 | int l = strlen(s); 16 | int idx = 0; 17 | for (int i = 0; i < l; ++i) { 18 | int num = s[i] == ' ' ? 0 : s[i] - 'A' + 1; 19 | fill[idx++] = num / 10 + '0'; 20 | fill[idx++] = num % 10 + '0'; 21 | } 22 | for (int i = l * 2; i < m * n; ++i) 23 | fill[idx++] = '0'; 24 | fill[idx] = '\0'; 25 | memset(visited, false, sizeof(visited)); 26 | memset(matrix, 0, sizeof(matrix)); 27 | int dx[] = { 0, 1, 0, -1 }; 28 | int dy[] = { 1, 0, -1, 0 }; 29 | int dir = 0, i = 0, j = 0, cnt = 0; 30 | while (cnt < m * n) { 31 | matrix[i][j] = fill[cnt++]; 32 | visited[i][j] = true; 33 | int ii = i + dx[dir], jj = j + dy[dir]; 34 | if (ii >= m || ii < 0 || jj >= n || jj < 0 || visited[ii][jj]) 35 | dir = (dir + 1) % 4; 36 | i = i + dx[dir], j = j + dy[dir]; 37 | } 38 | for (int i = 0; i < m; ++i) 39 | cout << matrix[i]; 40 | cout << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /2019年期中考试题/5-Binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | int nums1[200], nums2[200]; 10 | int idxs1[200], idxs2[200]; 11 | int idx1 = 0, idx2 = 0; 12 | cin >> n; 13 | for (int i = 0; i < n; ++i) 14 | cin >> nums1[i]; 15 | for (int i = 0; i < n; ++i) 16 | cin >> nums2[i]; 17 | for (int i = 0; i < n; ++i) 18 | if (nums1[i] == 1) 19 | idxs1[idx1++] = i; 20 | for (int i = 0; i < n; ++i) 21 | if (nums2[i] == 1) 22 | idxs2[idx2++] = i; 23 | if (idx1 != idx2) 24 | cout << -1 << endl; 25 | else { 26 | int res = 0; 27 | for (int i = 0; i < idx1; ++i) 28 | res += abs(idxs1[i] - idxs2[i]); 29 | cout << res << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /2019年期中考试题/6-Coins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int nums[100010], presum[100010]; 7 | 8 | int main() 9 | { 10 | int n, k; 11 | presum[0] = 0; 12 | cin >> n >> k; 13 | for (int i = 1; i <= n; ++i) { 14 | cin >> nums[i]; 15 | presum[i] = nums[i] + presum[i - 1]; 16 | } 17 | int minn = presum[n]; 18 | for (int i = n - k; i <= n; ++i) 19 | minn = min(minn, presum[i] - presum[i - n + k]); 20 | cout << presum[n] - minn << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /2019年期中考试题/7-N-gram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char s[510]; 7 | char words[500][10]; 8 | int words_idx; 9 | char tmp[10]; 10 | int cnts[500]; 11 | 12 | int main() 13 | { 14 | int k; 15 | cin >> k >> s; 16 | int l = strlen(s); 17 | for (int i = 0; i < l - k + 1; ++i) { 18 | char saved = s[i + k]; 19 | s[i + k] = '\0'; 20 | strcpy(tmp, s + i); 21 | s[i + k] = saved; 22 | bool flag = false; 23 | for (int i = 0; i < words_idx; ++i) { 24 | if (strcmp(words[i], tmp) == 0) { 25 | cnts[i]++; 26 | flag = true; 27 | break; 28 | } 29 | } 30 | if (!flag) 31 | strcpy(words[words_idx++], tmp); 32 | } 33 | int maxn = 0; 34 | for (int i = 0; i < words_idx; ++i) 35 | maxn = max(maxn, cnts[i]); 36 | if (maxn == 0) { 37 | cout << "NO" << endl; 38 | return 0; 39 | } 40 | cout << maxn + 1 << endl; 41 | for (int i = 0; i < words_idx; ++i) 42 | if (cnts[i] == maxn) 43 | cout << words[i] << endl; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /2019年期中考试题/8-Cyclic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char s1[200], s2[200]; 7 | 8 | bool iscycl(char s1[], char s2[]) 9 | { 10 | int l1 = strlen(s1); 11 | int l2 = strlen(s2); 12 | if (l1 != l2) 13 | return false; 14 | for (int i = 1; i < l1; ++i) { 15 | int cnt = 0; 16 | for (int j = 0; j < l1; ++j) 17 | if (s1[j] == s2[(j + i) % l1]) 18 | cnt += 1; 19 | if (cnt == l1) 20 | return true; 21 | } 22 | return false; 23 | } 24 | 25 | void add(char s1[], char s2[]) 26 | { 27 | int l1 = strlen(s1); 28 | int l2 = strlen(s2); // in fact, l1 equals with l20 29 | reverse(s1, s1 + l1); 30 | reverse(s2, s2 + l2); 31 | for (int i = 0; i < l1; ++i) { 32 | s1[i + 1] += (s1[i] - '0' + s2[i] - '0') / 10; 33 | s1[i] = (s1[i] - '0' + s2[i] - '0') % 10 + '0'; 34 | } 35 | reverse(s1, s1 + l1); 36 | reverse(s2, s2 + l1); 37 | } 38 | 39 | int main() 40 | { 41 | while (cin >> s1) { 42 | strcpy(s2, s1); 43 | int l1 = strlen(s1); 44 | bool flag = true; 45 | for (int i = 0; i < l1 - 1; ++i) { 46 | add(s1, s2); 47 | if (s1[l1] != '\0' || !iscycl(s1, s2)) { // 如果发生了进位,不是循环数 48 | flag = false; 49 | break; 50 | } 51 | } 52 | cout << s2; 53 | cout << (flag ? " is cyclic" : " is not cyclic") << endl; 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /2019年期中考试题/9-Influ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | vector G[100010]; 9 | int waited[100010]; 10 | bool isvis[100010]; 11 | int n, m, u, v, s; 12 | int ans; 13 | queue que; 14 | 15 | int main(){ 16 | scanf("%d %d", &n, &m); 17 | for (int i = 0; i < m; i++) { 18 | scanf("%d %d", &u, &v); 19 | G[u].push_back(v); 20 | G[v].push_back(u); 21 | } 22 | scanf("%d", &s); 23 | memset(isvis, 0, sizeof(isvis)); 24 | ans = 0; 25 | que.push(s); 26 | isvis[s] = true; 27 | while (!que.empty()){ 28 | ++ans; 29 | int cnt = 0; 30 | while(!que.empty()){ 31 | u = que.front(); 32 | que.pop(); 33 | for(int i = 0; i < G[u].size(); i++){ 34 | v = G[u][i]; 35 | if(!isvis[v]){ 36 | isvis[v] = true; 37 | waited[cnt] = v; 38 | ++cnt; 39 | } 40 | } 41 | } 42 | for(int i = 0; i < cnt; i++) 43 | que.push(waited[i]); 44 | } 45 | printf("%d", ans); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /2019年期中考试题/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/1.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/2.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/3.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/4.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/5.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/6.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/7.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/8.PNG -------------------------------------------------------------------------------- /2019年期中考试题/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期中考试题/pics/9.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/1-good.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | int array[200]; 8 | cin >> T; 9 | while (T--) { 10 | int n, m, cnt = 0; 11 | cin >> n >> m; 12 | for (int i = 0; i < n; ++i) { 13 | int t = 0, tmp; 14 | for (int j = 0; j < m; ++j) { 15 | cin >> tmp; 16 | t += tmp; 17 | } 18 | if (t * 2 >= m) 19 | cnt += 1; 20 | } 21 | cout << cnt << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /2019年期末考试考前模拟/7-treesum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct { 6 | int l, r; 7 | int n; 8 | int val; 9 | } a[110]; 10 | 11 | void calval(int k) 12 | { 13 | if (a[k].l == 0 && a[k].r == 0) 14 | return; 15 | if (a[k].l != 0) { 16 | calval(a[k].l); 17 | a[k].val += a[a[k].l].val; 18 | } 19 | if (a[k].r != 0) { 20 | calval(a[k].r); 21 | a[k].val += a[a[k].r].val; 22 | } 23 | return; 24 | } 25 | 26 | int search(int k) 27 | { 28 | if (a[k].l == 0 && a[k].r == 0) 29 | return k; 30 | if (a[k].l == 0) 31 | return search(a[k].r); 32 | if (a[k].r == 0) 33 | return search(a[k].l); 34 | if (a[a[k].l].val < a[a[k].r].val) 35 | return search(a[k].r); 36 | return search(a[k].l); 37 | } 38 | 39 | int main() 40 | { 41 | int T, n; 42 | cin >> T; 43 | while (T--) { 44 | cin >> n; 45 | for (int i = 1; i <= n; ++i) { 46 | cin >> a[i].val; 47 | a[i].l = a[i].r = 0; 48 | a[i].n = i; 49 | } 50 | for (int i = 0; i < n - 1; ++i) { 51 | int y1, y2, type; 52 | cin >> y1 >> y2 >> type; 53 | if (type == 0) 54 | a[y1].l = y2; 55 | else 56 | a[y1].r = y2; 57 | } 58 | calval(1); 59 | cout << search(1) << endl; 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /2019年期末考试考前模拟/8-post.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Node { 9 | Node *left, *right; 10 | int l, r; 11 | int id; 12 | }; 13 | 14 | Node *build(int l, int r, int id = -1) 15 | { 16 | Node *root = new Node; 17 | root->left = root->right = NULL; 18 | root->l = l; 19 | root->r = r; 20 | root->id = id; 21 | if (root->l < root->r) { 22 | int mid = (l + r) / 2; 23 | root->left = build(l, mid, id); 24 | root->right = build(mid + 1, r, id); 25 | } 26 | return root; 27 | } 28 | 29 | void add(Node *root, int l, int r, int id) 30 | { 31 | if (root == NULL) 32 | return; 33 | if (l <= root->l && r >= root->r) { 34 | root->id = id; 35 | return; 36 | } 37 | if (l > root->r || r < root->l) 38 | return; 39 | if (root->id != -1) { 40 | root->left->id = root->id; 41 | root->right->id = root->id; 42 | } 43 | root->id = -1; 44 | add(root->left, l, r, id); 45 | add(root->right, l, r, id); 46 | return; 47 | } 48 | 49 | int ls[10000], rs[10000], lrs[20000]; 50 | set s; 51 | void cnt(Node *root) 52 | { 53 | if (root == NULL) 54 | return; 55 | if (root->id != -1) { 56 | if (s.find(root->id) == s.end()) 57 | s.insert(root->id); 58 | return; 59 | } 60 | cnt(root->left); 61 | cnt(root->right); 62 | return; 63 | } 64 | 65 | int main() 66 | { 67 | int k; 68 | cin >> k; 69 | while (k--) { 70 | int t; 71 | cin >> t; 72 | for (int i = 0; i < t; ++i) 73 | cin >> ls[i] >> rs[i]; 74 | memcpy(lrs, ls, t * sizeof(int)); 75 | memcpy(lrs + t, rs, t * sizeof(int)); 76 | sort(lrs, lrs + 2 * t); 77 | map m; 78 | for (int i = 0; i < 2 * t; ++i) 79 | m[lrs[i]] = i; 80 | Node *root = build(0, 2 * t - 1); 81 | for (int i = 0; i < t; ++i) 82 | add(root, m[ls[i]], m[rs[i]] - 1, i); 83 | s.clear(); 84 | cnt(root); 85 | cout << s.size() << endl; 86 | } 87 | return 0; 88 | } -------------------------------------------------------------------------------- /2019年期末考试考前模拟/9-angrybirds.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int K; 9 | cin >> K; 10 | for (int h = 0; h < K; ++h) { 11 | int t, n, tmp; 12 | double res = 0; 13 | cin >> n >> t; 14 | for (int i = 0; i < n; ++i) { 15 | cin >> tmp; 16 | double p = i * (n - i) + (n - i - 1) * (i + 1) + n; 17 | p = p / n / n; 18 | double p1 = (1 - pow(1 - 2 * p, 1.0 * t)) / 2; // 伯努利事件发生奇数次的概率,二项式定理 19 | double p2 = 1 - p1; 20 | if (tmp == 1) 21 | swap(p1, p2); 22 | res += p1; 23 | } 24 | printf("Case #%d: %.4lf\n", h + 1, res); 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/1.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/2.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/3.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/4.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/5.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/6.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/7-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/7-1.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/7-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/7-2.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/8.PNG -------------------------------------------------------------------------------- /2019年期末考试考前模拟/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/2019年期末考试考前模拟/pics/9.PNG -------------------------------------------------------------------------------- /初步编程练习/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | float a; 7 | /* Take multiple cases as input. */ 8 | while(scanf("%f", &a) != EOF) 9 | { 10 | /* Take the opposite if the number is negative. */ 11 | if (a < 0) 12 | a = -a; 13 | /* Special case: 0. */ 14 | if (a == 0) 15 | printf("0.00\n"); 16 | else 17 | printf("%.2f\n", a); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /初步编程练习/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | cin >> a >> b; 8 | cout << "The sum is " << a + b << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /初步编程练习/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int max, num; 7 | /* Take the larger one from the 8 | first two numbers. */ 9 | cin >> max >> num; 10 | if (num > max) 11 | max = num; 12 | cin >> num; 13 | /* Compare with the third number, 14 | and get the largest number. */ 15 | if (num > max) 16 | max = num; 17 | cout << max << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /初步编程练习/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | char ch; 8 | cin >> a >> b >> ch; 9 | switch(ch) 10 | { 11 | /* Addition. */ 12 | case '+': cout << a + b << endl; break; 13 | /* Subtraction. */ 14 | case '-': cout << a - b << endl; break; 15 | /* Multiplication. */ 16 | case '*': cout << a * b << endl; break; 17 | /* Division. */ 18 | case '/': 19 | /* The dividend is zero. */ 20 | if (b == 0) 21 | cout << "Divided by zero!" << endl; 22 | else 23 | cout << a / b << endl; 24 | break; 25 | /* Other invalid operations. */ 26 | default: cout << "Invalid operator!" << endl; break; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /初步编程练习/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b, c; 7 | cin >> a >> b >> c; 8 | /* The summation of any two sides of a 9 | triangle is greater than the third. */ 10 | if (a + b > c 11 | && a + c > b 12 | && b + c > a) 13 | cout << "yes" << endl; 14 | else 15 | cout << "no" << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /初步编程练习/README.md: -------------------------------------------------------------------------------- 1 | ### 两数之和 2 | 3 | ```cpp 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int a, b; 10 | cin >> a >> b; 11 | cout << "The sum is " << a + b << endl; 12 | return 0; 13 | } 14 | ``` 15 | 16 | ### 最大数输出 17 | 18 | ```cpp 19 | #include 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | int max, num; 25 | /* Take the larger one from the 26 | first two numbers. */ 27 | cin >> max >> num; 28 | if (num > max) 29 | max = num; 30 | cin >> num; 31 | /* Compare with the third number, 32 | and get the largest number. */ 33 | if (num > max) 34 | max = num; 35 | cout << max << endl; 36 | return 0; 37 | } 38 | ``` 39 | 40 | ### 简单计算器 41 | 42 | ```cpp 43 | #include 44 | using namespace std; 45 | 46 | int main() 47 | { 48 | int a, b; 49 | char ch; 50 | cin >> a >> b >> ch; 51 | switch(ch) 52 | { 53 | /* Addition. */ 54 | case '+': cout << a + b << endl; break; 55 | /* Subtraction. */ 56 | case '-': cout << a - b << endl; break; 57 | /* Multiplication. */ 58 | case '*': cout << a * b << endl; break; 59 | /* Division. */ 60 | case '/': 61 | /* The dividend is zero. */ 62 | if (b == 0) 63 | cout << "Divided by zero!" << endl; 64 | else 65 | cout << a / b << endl; 66 | break; 67 | /* Other invalid operations. */ 68 | default: cout << "Invalid operator!" << endl; break; 69 | } 70 | return 0; 71 | } 72 | ``` 73 | 74 | ### 三角形判断 75 | 76 | ```cpp 77 | #include 78 | using namespace std; 79 | 80 | int main() 81 | { 82 | int a, b, c; 83 | cin >> a >> b >> c; 84 | /* The summation of any two sides of a 85 | triangle is greater than the third. */ 86 | if (a + b > c 87 | && a + c > b 88 | && b + c > a) 89 | cout << "yes" << endl; 90 | else 91 | cout << "no" << endl; 92 | return 0; 93 | } 94 | ``` 95 | 96 | ### 输出绝对值 97 | 98 | ```cpp 99 | #include 100 | 101 | int main() 102 | { 103 | float a; 104 | /* Take multiple cases as input. */ 105 | while(scanf("%f", &a) != EOF) 106 | { 107 | /* Take the opposite if the number is negative. */ 108 | if (a < 0) 109 | a = -a; 110 | /* Special case: 0. */ 111 | if (a == 0) 112 | printf("0.00\n"); 113 | else 114 | printf("%.2f\n", a); 115 | } 116 | return 0; 117 | } 118 | ``` 119 | 120 | -------------------------------------------------------------------------------- /抄程序练习/1-BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, a[1000]; 6 | int i = 0, j = 0; 7 | cin >> n; 8 | for (i = 0; i < n; i++) 9 | cin >> a[i]; 10 | for (i = 0; i < n - 1; i++) 11 | for (j = 1; j < n - i; j++) { 12 | if (a[j - 1] > a[j]) { 13 | int temp = a[j]; 14 | a[j] = a[j - 1]; 15 | a[j - 1] = temp; 16 | } 17 | } 18 | for (i = 0; i < n; i++) { 19 | cout << a[i] << endl; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /抄程序练习/2-FracAdd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int sumn = 0, sumd = 1; 8 | while (n--) { 9 | int num, deno; 10 | char slash; 11 | cin >> num >> slash >> deno; 12 | sumn = sumn * deno + num * sumd; 13 | sumd = sumd * deno; 14 | } 15 | int a = sumd, b = sumn, c; 16 | while (a != 0) { 17 | c = a; 18 | a = b % a; 19 | b = c; 20 | } 21 | int gcd = b; 22 | sumd /= gcd; 23 | sumn /= gcd; 24 | if (sumd > 1) 25 | cout << sumn << '/' << sumd << endl; 26 | else 27 | cout << sumn << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /抄程序练习/3-BigIntAdd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char str1[201], str2[201]; 6 | int a1[200] = {0}, a2[201] = {0}; 7 | 8 | int main() { 9 | int i, n, len1, len2; 10 | cin >> n; 11 | while (n--) { 12 | memset(a1, 0, sizeof(a1)); 13 | memset(a2, 0, sizeof(a2)); 14 | cin >> str1 >> str2; 15 | len1 = strlen(str1); 16 | len2 = strlen(str2); 17 | for (i = 0; i < len1; i++) 18 | a1[i] = str1[len1 - 1 - i] - '0'; 19 | for (i = 0; i < len2; i++) 20 | a2[i] = str2[len2 - 1 - i] - '0'; 21 | for (i = 0; i < 200; i++) { 22 | a2[i] += a1[i]; 23 | if (a2[i] >= 10) { 24 | a2[i] -= 10; 25 | a2[i + 1]++; 26 | } 27 | } 28 | for (i = 200; i >= 0; i--) 29 | if (a2[i] != 0) 30 | break; 31 | if (i == -1) 32 | cout << 0; 33 | else 34 | for (; i >= 0; i--) 35 | cout << a2[i]; 36 | cout << endl; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /抄程序练习/README.md: -------------------------------------------------------------------------------- 1 | #### 冒泡排序 2 | 3 | ```c++ 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int n, a[1000]; 9 | int i = 0, j = 0; 10 | cin >> n; 11 | for (i = 0; i < n; i++) 12 | cin >> a[i]; 13 | for (i = 0; i < n - 1; i++) 14 | for (j = 1; j < n - i; j++) { 15 | if (a[j - 1] > a[j]) { 16 | int temp = a[j]; 17 | a[j] = a[j - 1]; 18 | a[j - 1] = temp; 19 | } 20 | } 21 | for (i = 0; i < n; i++) { 22 | cout << a[i] << endl; 23 | } 24 | return 0; 25 | } 26 | ``` 27 | 28 | #### 分数加法 29 | 30 | ```cpp 31 | #include 32 | using namespace std; 33 | 34 | int main() { 35 | int n; 36 | cin >> n; 37 | int sumn = 0, sumd = 1; 38 | while (n--) { 39 | int num, deno; 40 | char slash; 41 | cin >> num >> slash >> deno; 42 | sumn = sumn * deno + num * sumd; 43 | sumd = sumd * deno; 44 | } 45 | int a = sumd, b = sumn, c; 46 | while (a != 0) { 47 | c = a; 48 | a = b % a; 49 | b = c; 50 | } 51 | int gcd = b; 52 | sumd /= gcd; 53 | sumn /= gcd; 54 | if (sumd > 1) 55 | cout << sumn << '/' << sumd << endl; 56 | else 57 | cout << sumn << endl; 58 | return 0; 59 | } 60 | ``` 61 | 62 | #### 大整数加法 63 | 64 | ```cpp 65 | #include 66 | #include 67 | using namespace std; 68 | 69 | char str1[201], str2[201]; 70 | int a1[200] = {0}, a2[201] = {0}; 71 | 72 | int main() { 73 | int i, n, len1, len2; 74 | cin >> n; 75 | while (n--) { 76 | memset(a1, 0, sizeof(a1)); 77 | memset(a2, 0, sizeof(a2)); 78 | cin >> str1 >> str2; 79 | len1 = strlen(str1); 80 | len2 = strlen(str2); 81 | for (i = 0; i < len1; i++) 82 | a1[i] = str1[len1 - 1 - i] - '0'; 83 | for (i = 0; i < len2; i++) 84 | a2[i] = str2[len2 - 1 - i] - '0'; 85 | for (i = 0; i < 200; i++) { 86 | a2[i] += a1[i]; 87 | if (a2[i] >= 10) { 88 | a2[i] -= 10; 89 | a2[i + 1]++; 90 | } 91 | } 92 | for (i = 200; i >= 0; i--) 93 | if (a2[i] != 0) 94 | break; 95 | if (i == -1) 96 | cout << 0; 97 | else 98 | for (; i >= 0; i--) 99 | cout << a2[i]; 100 | cout << endl; 101 | } 102 | return 0; 103 | } 104 | ``` 105 | 106 | -------------------------------------------------------------------------------- /期中考试考前模拟/1-AmicNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int acc[100010]; 5 | 6 | int func(int num) 7 | { 8 | int res = 0; 9 | for (int i = 1; i * i <= num; ++i) { 10 | if (num % i == 0) { 11 | res += i; 12 | if (i != num / i && i != 1) 13 | res += num / i; 14 | } 15 | } 16 | return res; 17 | } 18 | 19 | int main() 20 | { 21 | int n; 22 | cin >> n; 23 | for (int i = 1; i <= n; ++i) 24 | acc[i] = func(i); 25 | for (int i = 1; i < n; ++i) 26 | if (i < acc[i] && acc[i] <= n && acc[acc[i]] == i) 27 | cout << i << ' ' << acc[i] << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /期中考试考前模拟/2-Intersec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | int num1[210], num2[210], num3[210]; 9 | int idx1 = 0, idx2 = 0, idx3 = 0; 10 | char c; 11 | while (true) { 12 | cin >> num1[idx1++]; 13 | cin.get(c); 14 | if (c == '\n') 15 | break; 16 | } 17 | while (true) { 18 | cin >> num2[idx2++]; 19 | cin.get(c); 20 | if (c == '\n') 21 | break; 22 | } 23 | sort(num1, num1 + idx1); 24 | sort(num2, num2 + idx2); 25 | for (int i = 0; i < idx1; ++i) 26 | for (int j = 0; j < idx2; ++j) 27 | if (num1[i] == num2[j]) 28 | num3[idx3++] = num1[i]; 29 | if (idx3 == 0) 30 | cout << "NULL" << endl; 31 | else { 32 | sort(num3, num3 + idx3); 33 | int *end = unique(num3, num3 + idx3); 34 | for (int *p = num3; p != end; ++p) { 35 | if (p + 1 == end) 36 | cout << *p << endl; 37 | else 38 | cout << *p << ','; 39 | } 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /期中考试考前模拟/3-Rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main() 8 | { 9 | int n, num[100][100], mem[100][100]; 10 | int arr[10000], idx = 0; 11 | int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0}, dir = 0; 12 | cin >> n; 13 | for (int i = 0; i < n; ++i) 14 | for (int j = 0; j < n; ++j) 15 | cin >> num[i][j]; 16 | memset(mem, 0, sizeof(mem)); 17 | int x = 0, y = 0, cnt = 0; 18 | while (cnt < n * n) { 19 | arr[idx++] = num[x][y]; 20 | mem[x][y] = 1; 21 | int xx = x + dx[dir]; 22 | int yy = y + dy[dir]; 23 | if (xx < 0 || xx >= n || yy < 0 || yy >= n || mem[xx][yy]) { 24 | dir = (dir + 1) % 4; 25 | xx = x + dx[dir]; 26 | yy = y + dy[dir]; 27 | } 28 | x = xx, y = yy; 29 | cnt++; 30 | } 31 | bool flag = false; 32 | int i = 0, j = n * n - 1; 33 | for (int t = 0; t < n * n / 2; ++t) { 34 | if (!flag) { 35 | cout << arr[t]; 36 | flag = true; 37 | } else { 38 | cout << ' ' << arr[t]; 39 | } 40 | cout << ' ' << arr[n * n - 1 - t]; 41 | } 42 | if (n % 2 == 1) { 43 | if (flag) 44 | cout << ' '; 45 | cout << arr[n * n / 2]; 46 | } 47 | cout << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /期中考试考前模拟/4-Bacteria.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int num1[9][9], num2[9][9]; 9 | int m, n; 10 | memset(num1, 0, sizeof(num1)); 11 | cin >> num1[4][4] >> n; 12 | while (n--) { 13 | memset(num2, 0, sizeof(num2)); 14 | for (int i = 0; i < 9; ++i) 15 | for (int j = 0; j < 9; ++j) 16 | if (num1[i][j] > 0) { 17 | for (int x = i - 1; x <= i + 1; ++x) 18 | for (int y = j - 1; y <= j + 1; ++y) 19 | num2[x][y] += num1[i][j]; 20 | } 21 | for (int i = 0; i < 9; ++i) 22 | for (int j = 0; j < 9; ++j) 23 | num1[i][j] += num2[i][j]; 24 | } 25 | for (int i = 0; i < 9; ++i) 26 | for (int j = 0; j < 9; ++j) 27 | if (j == 8) 28 | cout << num1[i][j] << endl; 29 | else 30 | cout << num1[i][j] << ' '; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /期中考试考前模拟/5-Influ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char grids[100][100]; 7 | int n, k; 8 | int dx[] = {1, 0, -1, 0}; 9 | int dy[] = {0, 1, 0, -1}; 10 | cin >> n; 11 | for (int i = 0; i < n; ++i) 12 | for (int j = 0; j < n; ++j) 13 | cin >> grids[i][j]; 14 | cin >> k; 15 | while (--k) { 16 | for (int i = 0; i < n; ++i) 17 | for (int j = 0; j < n; ++j) { 18 | if (grids[i][j] != '@') 19 | continue; 20 | for (int k = 0; k < 4; ++k) { 21 | int ii = i + dx[k]; 22 | int jj = j + dy[k]; 23 | if (ii < 0 || ii >= n || jj < 0 || jj >= n) 24 | continue; 25 | if (grids[ii][jj] == '.') 26 | grids[ii][jj] = '!'; 27 | } 28 | } 29 | for (int i = 0; i < n; ++i) 30 | for (int j = 0; j < n; ++j) 31 | if (grids[i][j] == '!') 32 | grids[i][j] = '@'; 33 | } 34 | int sum = 0; 35 | for (int i = 0; i < n; ++i) 36 | for (int j = 0; j < n; ++j) 37 | sum += grids[i][j] == '@'; 38 | cout << sum << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /期中考试考前模拟/6-Bracket.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int fake_stack[510]; 7 | int stack_idx = 0; 8 | 9 | void push(int val) { fake_stack[stack_idx++] = val; } 10 | int pop() { return fake_stack[--stack_idx]; } 11 | int top() { return fake_stack[stack_idx - 1]; } 12 | 13 | struct Pair { 14 | int start, end; 15 | } pairs[500]; 16 | int pair_idx = 0; 17 | 18 | bool cmp(const Pair &a, const Pair &b) 19 | { 20 | if (a.end - a.start < b.end - b.start) 21 | return true; 22 | if (a.end - a.start > b.end - b.start) 23 | return false; 24 | return a.start < b.start; 25 | } 26 | 27 | int main() 28 | { 29 | char s[510]; 30 | cin.get(s, 500); 31 | int l = strlen(s); 32 | for (int i = 0; i < l; ++i) { 33 | if (s[i] == '(') 34 | push(i); 35 | else if (s[i] == ')') { 36 | if (stack_idx == 0) { 37 | cout << "mismatch" << endl; 38 | goto end; 39 | } 40 | int start = pop(); 41 | pairs[pair_idx].start = start + 1; 42 | pairs[pair_idx].end = i + 1; 43 | pair_idx++; 44 | } 45 | } 46 | if (stack_idx > 0) { 47 | cout << "mismatch" << endl; 48 | goto end; 49 | } 50 | if (pair_idx == 0) { 51 | cout << "0,0" << endl; 52 | goto end; 53 | } 54 | sort(pairs, pairs + pair_idx, cmp); 55 | for (int i = 0; i < pair_idx; ++i) 56 | cout << pairs[i].start << ',' << pairs[i].end << endl; 57 | end: 58 | return 0; 59 | } -------------------------------------------------------------------------------- /期中考试考前模拟/7-Quad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | bool judge(int x[], int y[]) 6 | { 7 | for (int i = 0; i < 4; ++i) { 8 | int v1_x = x[(i + 1) % 4] - x[i]; // vector AB 9 | int v1_y = y[(i + 1) % 4] - y[i]; 10 | int v2_x = x[(i + 2) % 4] - x[(i + 1) % 4]; // vector BC 11 | int v2_y = y[(i + 2) % 4] - y[(i + 1) % 4]; 12 | int v3_x = x[(i + 3) % 4] - x[(i + 1) % 4]; // vector BD 13 | int v3_y = y[(i + 3) % 4] - y[(i + 1) % 4]; 14 | int prod1 = v1_x * v2_y - v1_y * v2_x; // AB x BC 15 | int prod2 = v1_x * v3_y - v1_y * v3_x; // AB x BD 16 | if (1.0 * prod1 * prod2 <= 0) // different sign 17 | return false; 18 | } 19 | return true; 20 | } 21 | 22 | int main() 23 | { 24 | int x[4], y[4]; 25 | while (cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3]) 26 | cout << (judge(x, y) ? "yes" : "no") << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /期中考试考前模拟/8-MagicSquare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int grids[30][30], n; 8 | bool visited[30][30]; 9 | memset(visited, 0, sizeof(visited)); 10 | cin >> n; 11 | int num = 1; 12 | int i = 0, j = n / 2; 13 | while (num <= n * n) { 14 | grids[i][j] = num++; 15 | visited[i][j] = true; 16 | if (i == 0 && j == n - 1 17 | || i - 1 >= 0 && j + 1 < n && visited[i - 1][j + 1]) { 18 | i = i + 1; 19 | } else if (i == 0) { 20 | i = n - 1; 21 | j++; 22 | } else if (j == n - 1) { 23 | j = 0; 24 | i--; 25 | } else { 26 | i--, j++; 27 | } 28 | } 29 | for (int i = 0; i < n; ++i) { 30 | cout << grids[i][0]; 31 | for (int j = 1; j < n; ++j) 32 | cout << ' ' << grids[i][j]; 33 | cout << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /期中考试考前模拟/9-Calendar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool leap(int year) 7 | { 8 | return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; 9 | } 10 | 11 | int main() 12 | { 13 | int months[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 14 | int year, month, d = 1; // 0-6 for Sun-Sat 15 | cin >> year >> month; 16 | for (int i = 1900; i < year; ++i) 17 | if (leap(i)) 18 | d += 366; 19 | else 20 | d += 365; 21 | if (leap(year)) 22 | months[2] = 29; 23 | for (int i = 1; i < month; ++i) 24 | d += months[i]; 25 | d %= 7; 26 | printf(" Sun Mon Tue Wed Thu Fri Sat\n"); 27 | for (int i = 0; i < d; ++i) 28 | printf(" "); 29 | for (int i = 1; i <= months[month]; ++i) { 30 | printf("%4d", i); 31 | if ((d + 1) % 7 == 0) 32 | printf("\n"); 33 | d += 1; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /期中考试考前模拟/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/1.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/2.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/3.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/4.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/5.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/6.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/7.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/8.PNG -------------------------------------------------------------------------------- /期中考试考前模拟/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/期中考试考前模拟/pics/9.PNG -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习1/1.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 小袁同学今年刚毕业,成为了一名光荣的程序员。他找了一份工作,年薪是X万元,并且公司保证每年给他固定加薪8%。 小袁同学很开心,他看上了一套房子,售价是M万元,于是下决心攒钱买下。假设房子的价格每年稳定上涨10%,再假设小袁不吃不喝,把所有的钱都存下来买房。 4 | 那么请你帮他算算,他多少年后可以买的起这套房子? 5 | 如果100年内(含100年) 都不可能的话,则输出“Forget it.” 6 | * 关于输入 7 | 一共有2行。 8 | 第一行是一个整数X,表示小袁同学的初始年薪; 9 | 第二行是一个整数M,表示房子的售价。 10 | * 关于输出 11 | 如果有解,输出一个整数N,表示第N年小袁可以攒够钱买房子; 12 | 如果解大于100或者无解,则输出“Forget it.” 13 | *****/ 14 | 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | double salary, saving = 0, price; 21 | int year = 1; 22 | cin >> salary >> price; 23 | do 24 | { 25 | /* Save some money. */ 26 | saving += salary; 27 | /* See if he can buy the house. */ 28 | if (saving >= price) 29 | { 30 | /* Buy the house and return. */ 31 | cout << year << endl; 32 | return 1; 33 | } 34 | /* One more year... */ 35 | /* Raise both salary and house price. */ 36 | salary *= 1.08; 37 | price *= 1.1; 38 | year++; 39 | } while (year <= 100); // 100 years later... 40 | cout << "Forget it." << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习1/2.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 北京地铁涨价了,现在的地铁票价计算方法如下: 4 | 6公里内 3元 (包括6公里) 5 | 6-12 公里 4元 (不包括6公里,包括12公里) 6 | 12-32 公里 每10公里加1元(不包括12公里,包括32公里,不满10公里按10公里算) 7 | 32公里以上 每20公里加1元(不包括32公里,不满20公里按20公里算) 8 | 比如,50公里时,需要 4+2+1=7元 9 | 10 | 同时,北京地铁也给出了优惠方案: 11 | 每个自然月内,使用交通卡满100元,则票价可打8折;满150元,则票价可打5折;满400元,则票价不打折。 12 | 13 | 现在,小明有一张公交卡,小明想知道自己现在坐地铁的票价是多少?请你写程序完成这个功能。 14 | * 关于输入 15 | 输入有两个整数,一个是小明现在这个月的累计消费,一个是小明本次乘坐的里程数 16 | * 关于输出 17 | 输出为本次乘坐的票价,保留两位小数 18 | *****/ 19 | 20 | #include 21 | #include 22 | using namespace std; 23 | 24 | int main() 25 | { 26 | int x, d; 27 | double p = 0; 28 | scanf("%d%d", &x, &d); 29 | /* Determine the original price. */ 30 | if (d <= 6) 31 | p = 3; 32 | else if (d <= 12) 33 | p = 4; 34 | else if (d <= 32) 35 | p = 4 + ceil(double(d-12)/10); 36 | else 37 | p = 4 + 2 + ceil(double(d-32)/20); 38 | /* Consider discounts. */ 39 | if (x < 100) 40 | ; 41 | else if (x < 150) 42 | p *= 0.8; 43 | else if (x < 400) 44 | p *= 0.5; 45 | else 46 | ; 47 | printf("%.2lf\n", p); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习1/3.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 有一个正方形,四个角的坐标(x,y)分别是(1,-1),(1,1),(-1,1),(-1,-1),x是横轴,y是纵轴。写一个程序,判断一个给定的点是否在这个正方形内。 4 | * 关于输入 5 | 输入坐标x,y 6 | * 关于输出 7 | 点在正方形内,输出yes;点在正方形外,输出no 8 | *****/ 9 | 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | double x, y; 16 | scanf("%lf,%lf", &x, &y); 17 | if (x < 1 && x > -1 && y < 1 && y > -1) 18 | printf("yes\n"); 19 | else 20 | printf("no\n"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习1/4.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 输入一个整数,判断它能否被3,5,7整除,并输出以下信息: 4 | (1)能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格); 5 | (2)能被其中两个数整除(输出两个数,小的在前,大的在后。例如:3 5或者 3 7或者5 7,中间用空格分隔) 6 | (3)能被其中一个数整除(输出这个除数) 7 | (4)不能被任何数整除;(输出小写字符n) 8 | * 关于输入 9 | 一个整数 10 | * 关于输出 11 | 实现题目的要求 12 | *****/ 13 | 14 | #include 15 | using namespace std; 16 | 17 | #define N_NUM 3 18 | 19 | int main() 20 | { 21 | int num, arr[N_NUM] = {3, 5, 7}; 22 | bool found = false; // Whether we have found a number. 23 | cin >> num; 24 | for (int i = 0; i < N_NUM; i++) 25 | if (num % arr[i] == 0) 26 | { 27 | if (found) // If we have already found a number, 28 | cout << " " << arr[i]; // use a space to seperate. 29 | else 30 | { 31 | cout << arr[i]; // Otherwise, this if the first number. 32 | found = true; // Do not use space, and set the "found" flag. 33 | } 34 | } 35 | if (!found) // Cannot find any number. 36 | cout << "n"; 37 | cout << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习1/5.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 北医三院近期开展了最受欢迎的医生评选活动,评选规则是:请患者及其家属给自己最喜欢的医生投票(每位参加评选的医生有一个相应的编号),票数最多的医生评选为年度最受欢迎医生,奖励年终休假一个月。 4 | * 关于输入 5 | 输入为两行,第一行为投票的总数n(0 < n <= 100),第二行为投票情况(医生编号均为整数,最大不超过15) 6 | * 关于输出 7 | 输出最受欢迎的医生的编号 8 | *****/ 9 | 10 | #include 11 | using namespace std; 12 | 13 | #define MAX_DOC 16 14 | 15 | int main() 16 | { 17 | int n, vote[MAX_DOC] = {0}, max = 0; 18 | cin >> n; 19 | for (int i = 0; i < n; i++) 20 | { 21 | int tmp; 22 | cin >> tmp; 23 | vote[tmp]++; // Vote for the corresponding dotor. 24 | if (vote[tmp] > max) // Update "max" immediately. 25 | max = vote[tmp]; 26 | } 27 | /* Find the most popular doctor. */ 28 | for (int i = 0; i < MAX_DOC; i++) 29 | if (vote[i] == max) 30 | { 31 | cout << i << endl; 32 | break; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习1/6.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 菲波那契数列是指这样的数列: 数列的第一个和第二个数都为1,接下来每个数都等于前面2个数之和。 4 | 给出一个正整数a,要求菲波那契数列中第a个数是多少。 5 | * 关于输入 6 | 第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a(1 <= a <= 20) 7 | * 关于输出 8 | n行,每行输出对应一个输入。输出应是一个正整数,为菲波那契数列中第a个数的大小 9 | *****/ 10 | 11 | #include 12 | using namespace std; 13 | 14 | #define N_FAB 30 15 | 16 | int main() 17 | { 18 | int fab[N_FAB] = {1,1}, n, a; 19 | /* Compute the fabonacci sequence at first. */ 20 | for (int i = 2; i < N_FAB; i++) 21 | fab[i] = fab[i-1] + fab[i-2]; 22 | cin >> n; 23 | for (int i = 0; i < n; i++) 24 | { 25 | /* Print each required number. */ 26 | cin >> a; 27 | cout << fab[a-1] < 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | int max, min, a; 16 | cin >> max >> min; 17 | if (max < min) 18 | { 19 | max = max ^ min; 20 | min = max ^ min; 21 | max = max ^ min; 22 | } 23 | cin >> a; 24 | if (max < a) 25 | { 26 | max = max ^ a; 27 | a = max ^ a; 28 | max = max ^ a; 29 | } 30 | if (min > a) 31 | { 32 | min = min ^ a; 33 | a = min ^ a; 34 | min = min ^ a; 35 | } 36 | cout << max - min << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习2/2.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 石头剪子布是一种很简单的游戏: 4 | 石头胜剪子 5 | 剪子胜布 6 | 布胜石头 7 | A和B猜拳,已知他们出拳的序列,编程求出谁胜谁负。 8 | * 关于输入 9 | 第一行是一个正整数n(n<200),表明A和B一共猜了n次拳。 10 | 接下来是n行,每行有两个数字,分别表示A和B在这次猜拳中出了什么。0表示石头,1表示剪刀,2表示布。 11 | * 关于输出 12 | 输出一行,"A"表示A胜,"B"表示B胜,"Tie"表示平局。 13 | *****/ 14 | 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | int n, a, b, win=0; 21 | cin >> n; 22 | for (int i = 0; i < n; i++) 23 | { 24 | cin >> a >> b; 25 | switch ((a-b+3)%3) 26 | { 27 | case 0: break; 28 | case 1: win++; break; 29 | case 2: win--; break; 30 | default: break; 31 | } 32 | } 33 | if (win < 0) 34 | cout << "A" << endl; 35 | else if (win > 0) 36 | cout << "B" << endl; 37 | else 38 | cout << "Tie" << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习2/3.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 输出一个整数数列中不与最大数相同的数字之和 4 | * 关于输入 5 | 输入分为两行: 6 | 第一行为N(N为接下来数字的个数,N<=100); 7 | 第二行为N个整数,以空格隔开; 8 | * 关于输出 9 | 输出为N个数中除去最大数其余数字之和。(注意,最大数可能出现多次) 10 | *****/ 11 | 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | int n, num, max = -1000000, sum = 0, cnt = 0; 18 | cin >> n; 19 | for (int i = 0; i < n; i++) 20 | { 21 | cin >> num; 22 | sum += num; 23 | if (max < num) 24 | { 25 | max = num; 26 | cnt = 1; 27 | } 28 | else if (max == num) 29 | cnt++; 30 | } 31 | cout << sum - max * cnt << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习2/4.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 给定一个数组,数组长度小于1000,统计前k大的数并且把这k个数从大到小输出。 4 | * 关于输入 5 | 先输入一个n表示数组的长度,然后连续地输入n个数。最后输入一个k。 6 | * 关于输出 7 | 换行输出前k大的数 8 | *****/ 9 | 10 | #include 11 | using namespace std; 12 | 13 | #define N 1010 14 | 15 | int arr[N], n, k; 16 | 17 | int main() 18 | { 19 | cin >> n; 20 | for (int i = 0; i < n; i++) 21 | cin >> arr[i]; 22 | for (int i = 0; i < n; i++) 23 | for (int j = 0; j < i; j++) 24 | if (arr[j] < arr[i]) 25 | { 26 | arr[j] = arr[j] ^ arr[i]; 27 | arr[i] = arr[j] ^ arr[i]; 28 | arr[j] = arr[j] ^ arr[i]; 29 | } 30 | cin >> k; 31 | for (int i = 0; i < k; i++) 32 | cout << arr[i] << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习2/5.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 从键盘输入一指定金额(以元为单位,如345),然后输出支付该金额的各种面额的人民币数量,显示100元,50元,20元,10元,5元,1元各多少张,要求尽量使用大面额的钞票。 4 | * 关于输入 5 | 一个小于1000的正整数。 6 | * 关于输出 7 | 输出分行,每行显示一个整数,从上到下分别表示100元,50元,20元,10元,5元,1元人民币的张数 8 | *****/ 9 | 10 | #include 11 | using namespace std; 12 | 13 | #define N_VALUE 6 14 | 15 | int value[N_VALUE] = {100, 50, 20, 10, 5, 1}; 16 | 17 | int main() 18 | { 19 | int money; 20 | cin >> money; 21 | for (int i = 0; i < N_VALUE; i++) 22 | { 23 | cout << money / value[i] << endl; 24 | money %= value[i]; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习2/6.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 小朋友玩跳绳比赛,要计算在一分钟内跳了多少下.假设每秒钟跳一下,如果中途失败了,则要花三秒钟后才能开始重跳.一般小朋友跳绳一分钟要跳坏好几次.现在给出小朋友每次跳坏时已经跳的总数,求该小朋友在一分钟内能跳多少下. 4 | * 关于输入 5 | 输入有一行 6 | 第一个整数是跳坏的次数m(m<20),其余m个整数是跳坏时累计跳了多少下. 7 | * 关于输出 8 | 输出有一行 9 | 输出小朋友头一分钟内跳了多少下. 10 | *****/ 11 | 12 | #include 13 | using namespace std; 14 | 15 | #define MINUTE 60 16 | 17 | int cnt[100] = {0}; // Count per second 18 | int curr = 0; // Current time (sec) 19 | 20 | int main() 21 | { 22 | int n, br; 23 | cin >> n; 24 | for (int i = 0; i < n; i++) 25 | { 26 | cin >> br; 27 | if (curr > MINUTE) // Already out of time. 28 | continue; // Ignore... 29 | /* Jump until reaching the break point. */ 30 | while(cnt[curr] < br) 31 | { 32 | cnt[curr+1] = cnt[curr] + 1; 33 | curr++; 34 | } 35 | /* Wait for 3 seconds. */ 36 | cnt[curr+1] = cnt[curr]; 37 | cnt[curr+2] = cnt[curr]; 38 | cnt[curr+3] = cnt[curr]; 39 | curr += 3; 40 | } 41 | /* Fill the minute. */ 42 | for (; curr <= MINUTE; curr++) 43 | cnt[curr+1] = cnt[curr] + 1; 44 | cout << cnt[MINUTE] << endl; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习3/1.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | A国出现了埃博拉疫情,第1天有N人被感染。每个病人在被感染的第2天开始感染其它健康人,每个病人每天可以感染X个健康人,病人在被感染的第Y天死去(死去的当天不再感染他人,也不计入当天存活人数)。求出第K天时,该国有多少被感染且活着的病人? 4 | * 关于输入 5 | 输入的第一行为3个整数,用空格分隔:N,第1天的感染人数,0 < N < 10;X,每人每天感染健康人的数量,0 < X < 10;Y,病人被感染到死去的天数,2 < Y < 10。 6 | 输入的第二行为1个整数K,即欲求感染人数的日期,0 < K < 10。 7 | * 关于输出 8 | 输出为1行,输出第K天时该国被感染且活着的病人的总数。 9 | *****/ 10 | 11 | #include 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | int sick[100] = {0}; 17 | int N, X, Y, K, cnt; 18 | cin >> N >> X >> Y >> K; 19 | sick[0] = N; 20 | cnt = N; 21 | for (int i = 1; i < K; i++) 22 | { 23 | if (i >= Y - 1) // People die... 24 | cnt -= sick[i-Y+1]; 25 | sick[i] = cnt * X; // People get sick... 26 | cnt += sick[i]; 27 | } 28 | cout << cnt << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习3/2.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 编写程序,读入若干个四位数(小于30),求出其中满足以下条件的数的个数: 4 | 5 | 个位数上的数字减去千位数上的数字,再减去百位数上的数字, 再减去十位数上的数字的结果大于零。 6 | * 关于输入 7 | 输入为两行,第一行为四位数的个数,第二行为要读入的四位数 8 | * 关于输出 9 | 输出为一行,即满足条件的四位数的个数 10 | *****/ 11 | 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | int n, num, cnt = 0; 18 | cin >> n; 19 | for (int i = 0; i < n; i++) 20 | { 21 | cin >> num; 22 | if ((num%10)-(num/1000)-(num/100%10)-(num/10%10)>0) 23 | cnt++; 24 | } 25 | cout << cnt << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习3/3.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 给出n的值, 计算公式 e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + … + 1/n! 4 | * 关于输入 5 | 只有一个输入,即n的值, n>=1, 且n为整数 6 | * 关于输出 7 | 按照上述公式求出的e的值(精确到小数点后6位) 8 | *****/ 9 | 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | int n; 16 | double val = 1., frac = 1.; 17 | scanf("%d", &n); 18 | for (int i = 1; i <= n; i++) 19 | { 20 | val += 1. / frac; 21 | frac *= (i + 1); 22 | } 23 | printf("%.6lf\n", val); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习3/4.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HAART),由美籍华裔科学家何大一于1996年提出,是通过三种或三种以上的抗病毒药物联合使用来治疗艾滋病。该疗法的应用可以减少单一用药产生的抗药性,最大限度地抑制病毒的复制,使被破坏的机体免疫功能部分甚至全部恢复,从而延缓病程进展,延长患者生命,提高生活质量。人们在鸡尾酒疗法的基础上又提出了很多种改进的疗法。为了验证这些治疗方法是否在疗效上比鸡尾酒疗法更好,可用通过临床对照实验的方式进行。假设鸡尾酒疗法的有效率为x,新疗法的有效率为y,如果y-x大于5%,则效果更好,如果x-y大于5%,则效果更差,否则称为效果差不多。下面给出n组临床对照实验,其中第一组采用鸡尾酒疗法,其他n-1组为各种不同的改进疗法。请写程序判定各种改进疗法效果如何。 4 | * 关于输入 5 | 第一行为整数n; 6 | 其余n行每行两个整数,第一个整数是临床实验的总病例数,第二个疗效有效的病例数。 7 | 这n行数据中,第一行为鸡尾酒疗法的数据,其余各行为各种改进疗法的数据。 8 | * 关于输出 9 | 有n-1行输出,分别表示对应改进疗法的效果: 10 | 如果效果更好,输出better;如果效果更差,输出worse;否则输出same 11 | *****/ 12 | 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | int n; 19 | double total, valid, cocktail, therapy; 20 | cin >> n; 21 | cin >> total >> valid; 22 | cocktail = valid / total; 23 | for (int i = 1; i < n; i++) 24 | { 25 | cin >> total >> valid; 26 | therapy = valid / total; 27 | if (therapy - cocktail > 0.05) 28 | cout << "better" << endl; 29 | else if (therapy - cocktail < -0.05) 30 | cout << "worse" << endl; 31 | else 32 | cout << "same" << endl; 33 | 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习3/5.cpp: -------------------------------------------------------------------------------- 1 | /***** 2 | * 描述 3 | 生日蛋糕上有三棵草莓,小F想把蛋糕切成形状完全相同的三块,并且每一块上都有一个草莓。你可以把草莓看成一个点,切块的时候不能切中草莓。请问能否切成满足相同的三块? 4 | 5 | * 关于输入 6 | 第一行包含一个整数t表示有t组测试数据。 7 | 每组测试数据一行,包含7个整数:r x1 y1 x2 y2 x3 y3,r表示蛋糕半径,(x1,y1)(x2,y2)(x3,y3)表示三个草莓的位置(以圆心为坐标轴原点)。 8 | * 关于输出 9 | 对每组数据输出一行。 10 | 如果能够切成全等的三块并且每块上有一个草莓,输出"Yes",否则输出"No" 11 | *****/ 12 | 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { int n; 19 | cin >> n; 20 | int r; 21 | double x1, y1, x2, y2, x3, y3; 22 | double r1, r2, r3, c1, c2, c3; 23 | double cos1, cos2, cos3; 24 | 25 | int counter=0; 26 | for(int i = 0; i < n; i++) 27 | { 28 | cin >> r >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; 29 | /* Compute cosine values... */ 30 | r1 = sqrt(pow(x1, 2)+pow(y1, 2)); 31 | r2 = sqrt(pow(x2, 2)+pow(y2, 2)); 32 | r3 = sqrt(pow(x3, 2)+pow(y3, 2)); 33 | c1 = sqrt(pow(x2-x3, 2)+pow(y2-y3, 2)); 34 | c2 = sqrt(pow(x3-x1, 2)+pow(y3-y1, 2)); 35 | c3 = sqrt(pow(x1-x2, 2)+pow(y1-y2, 2)); 36 | cos1 = (pow(r2, 2)+pow(r3, 2)-pow(c1, 2))/(2*r2*r3); 37 | cos2 = (pow(r3, 2)+pow(r1, 2)-pow(c2, 2))/(2*r3*r1); 38 | cos3 = (pow(r1, 2)+pow(r2, 2)-pow(c3, 2))/(2*r1*r2); 39 | /* If the strawberry is at the center... */ 40 | if ((x1 == 0 && y1 == 0) || (x2 == 0 && y2 == 0) 41 | || (x3 == 0 && y3 == 0)) 42 | cout << "No" << endl; 43 | else if (cos1>=-0.5 && cos2>=-0.5 && cos3>=-0.5) 44 | cout << "No" << endl; 45 | else 46 | cout << "Yes"<< endl; 47 | } 48 | return 0; 49 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/1-prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(int n) 5 | { 6 | if (n < 2) 7 | return false; 8 | for (int i = 2; i * i <= n; ++i) 9 | if (n % i == 0) 10 | return false; 11 | return true; 12 | } 13 | 14 | int main() 15 | { 16 | int m, k; 17 | cin >> m >> k; 18 | m += 1; 19 | while (k--) { 20 | while (!check(m)) 21 | m++; 22 | cout << m; 23 | if (k != 0) 24 | cout << " "; 25 | m++; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/2-newton.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | double n; 9 | while (cin >> n) { 10 | double root = 1.0, prev = 0; 11 | int counter = 0; 12 | while (abs(root - prev) > 1e-6) { 13 | prev = root; 14 | root = (root + n / root) / 2; 15 | counter++; 16 | } 17 | cout << counter << ' ' << setprecision(2) 18 | << setiosflags(ios::fixed) << root << endl; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/3-quad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | bool judge(int x[], int y[]) 6 | { 7 | for (int i = 0; i < 4; ++i) { 8 | int v1_x = x[(i + 1) % 4] - x[i]; // vector AB 9 | int v1_y = y[(i + 1) % 4] - y[i]; 10 | int v2_x = x[(i + 2) % 4] - x[(i + 1) % 4]; // vector BC 11 | int v2_y = y[(i + 2) % 4] - y[(i + 1) % 4]; 12 | int v3_x = x[(i + 3) % 4] - x[(i + 1) % 4]; // vector BD 13 | int v3_y = y[(i + 3) % 4] - y[(i + 1) % 4]; 14 | int prod1 = v1_x * v2_y - v1_y * v2_x; // AB x BC 15 | int prod2 = v1_x * v3_y - v1_y * v3_x; // AB x BD 16 | if (1.0 * prod1 * prod2 <= 0) // different sign 17 | return false; 18 | } 19 | return true; 20 | } 21 | 22 | int main() 23 | { 24 | int x[4], y[4]; 25 | while (cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3]) 26 | cout << (judge(x, y) ? "yes" : "no") << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/4-number7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(int n) 5 | { 6 | if (n % 7 == 0) 7 | return true; 8 | while (n) { 9 | if (n % 10 == 7) 10 | return true; 11 | n /= 10; 12 | } 13 | return false; 14 | } 15 | 16 | int main() 17 | { 18 | bool site[100] = { false }; 19 | int n, cnt = 0, idx = 0, num = 1; 20 | cin >> n; 21 | while (cnt < n) { 22 | if (!site[idx]) { 23 | if (check(num)) { 24 | cout << idx + 1 << endl; 25 | site[idx] = true; 26 | cnt += 1; 27 | } 28 | num += 1; 29 | } 30 | idx = (idx + 1) % n; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/5-equiv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int nums[40][10000]; 5 | int idxs[40]; 6 | 7 | int cal(int n) 8 | { 9 | int res = 0; 10 | while (n) { 11 | res += n % 10; 12 | n /= 10; 13 | } 14 | return res; 15 | } 16 | 17 | int main() 18 | { 19 | 20 | char c; 21 | int m, n, k; 22 | cin >> m >> c >> n >> c >> k; 23 | for (int i = m + 1; i < n; ++i) { 24 | int p = cal(i); 25 | if (p % k == 0) { 26 | nums[p / k][idxs[p / k]] = i; 27 | idxs[p / k]++; 28 | } 29 | } 30 | for (int i = 0; i < 40; ++i) { 31 | for (int j = 0; j < idxs[i]; ++j) { 32 | if (j > 0) 33 | cout << ','; 34 | cout << nums[i][j]; 35 | if (j == idxs[i] - 1) 36 | cout << endl; 37 | } 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ![](pics/1.PNG) 4 | 5 | ```cpp 6 | #include 7 | using namespace std; 8 | 9 | bool check(int n) 10 | { 11 | if (n < 2) 12 | return false; 13 | for (int i = 2; i * i <= n; ++i) 14 | if (n % i == 0) 15 | return false; 16 | return true; 17 | } 18 | 19 | int main() 20 | { 21 | int m, k; 22 | cin >> m >> k; 23 | m += 1; 24 | while (k--) { 25 | while (!check(m)) 26 | m++; 27 | cout << m; 28 | if (k != 0) 29 | cout << " "; 30 | m++; 31 | } 32 | return 0; 33 | } 34 | ``` 35 | 36 | 37 | 38 | 39 | 40 | ![](pics/2.PNG) 41 | 42 | ```cpp 43 | #include 44 | #include 45 | #include 46 | using namespace std; 47 | 48 | int main() 49 | { 50 | double n; 51 | while (cin >> n) { 52 | double root = 1.0, prev = 0; 53 | int counter = 0; 54 | while (abs(root - prev) > 1e-6) { 55 | prev = root; 56 | root = (root + n / root) / 2; 57 | counter++; 58 | } 59 | cout << counter << ' ' << setprecision(2) 60 | << setiosflags(ios::fixed) << root << endl; 61 | } 62 | return 0; 63 | } 64 | ``` 65 | 66 | 67 | 68 | 69 | 70 | ![](pics/3.PNG) 71 | 72 | ```cpp 73 | #include 74 | using namespace std; 75 | 76 | 77 | bool judge(int x[], int y[]) 78 | { 79 | for (int i = 0; i < 4; ++i) { 80 | int v1_x = x[(i + 1) % 4] - x[i]; // vector AB 81 | int v1_y = y[(i + 1) % 4] - y[i]; 82 | int v2_x = x[(i + 2) % 4] - x[(i + 1) % 4]; // vector BC 83 | int v2_y = y[(i + 2) % 4] - y[(i + 1) % 4]; 84 | int v3_x = x[(i + 3) % 4] - x[(i + 1) % 4]; // vector BD 85 | int v3_y = y[(i + 3) % 4] - y[(i + 1) % 4]; 86 | int prod1 = v1_x * v2_y - v1_y * v2_x; // AB x BC 87 | int prod2 = v1_x * v3_y - v1_y * v3_x; // AB x BD 88 | if (1.0 * prod1 * prod2 <= 0) // different sign 89 | return false; 90 | } 91 | return true; 92 | } 93 | 94 | int main() 95 | { 96 | int x[4], y[4]; 97 | while (cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3]) 98 | cout << (judge(x, y) ? "yes" : "no") << endl; 99 | return 0; 100 | } 101 | ``` 102 | 103 | 104 | 105 | 106 | 107 | ![](pics/4.PNG) 108 | 109 | ```cpp 110 | #include 111 | using namespace std; 112 | 113 | bool check(int n) 114 | { 115 | if (n % 7 == 0) 116 | return true; 117 | while (n) { 118 | if (n % 10 == 7) 119 | return true; 120 | n /= 10; 121 | } 122 | return false; 123 | } 124 | 125 | int main() 126 | { 127 | bool site[100] = { false }; 128 | int n, cnt = 0, idx = 0, num = 1; 129 | cin >> n; 130 | while (cnt < n) { 131 | if (!site[idx]) { 132 | if (check(num)) { 133 | cout << idx + 1 << endl; 134 | site[idx] = true; 135 | cnt += 1; 136 | } 137 | num += 1; 138 | } 139 | idx = (idx + 1) % n; 140 | } 141 | return 0; 142 | } 143 | ``` 144 | 145 | 146 | 147 | 148 | 149 | ![](pics/5.PNG) 150 | 151 | ```cpp 152 | #include 153 | using namespace std; 154 | 155 | int nums[40][10000]; 156 | int idxs[40]; 157 | 158 | int cal(int n) 159 | { 160 | int res = 0; 161 | while (n) { 162 | res += n % 10; 163 | n /= 10; 164 | } 165 | return res; 166 | } 167 | 168 | int main() 169 | { 170 | 171 | char c; 172 | int m, n, k; 173 | cin >> m >> c >> n >> c >> k; 174 | for (int i = m + 1; i < n; ++i) { 175 | int p = cal(i); 176 | if (p % k == 0) { 177 | nums[p / k][idxs[p / k]] = i; 178 | idxs[p / k]++; 179 | } 180 | } 181 | for (int i = 0; i < 40; ++i) { 182 | for (int j = 0; j < idxs[i]; ++j) { 183 | if (j > 0) 184 | cout << ','; 185 | cout << nums[i][j]; 186 | if (j == idxs[i] - 1) 187 | cout << endl; 188 | } 189 | } 190 | return 0; 191 | } 192 | ``` 193 | 194 | -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第一阶段/第一阶段编程练习4/pics/1.PNG -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第一阶段/第一阶段编程练习4/pics/2.PNG -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第一阶段/第一阶段编程练习4/pics/3.PNG -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第一阶段/第一阶段编程练习4/pics/4.PNG -------------------------------------------------------------------------------- /第一阶段/第一阶段编程练习4/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第一阶段/第一阶段编程练习4/pics/5.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/1-BinaryComp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | while (!isalpha(cin.peek())) { 10 | cin >> n; 11 | cin.get(); // remove '\n' 12 | char nums[40]; 13 | memset(nums, '0', sizeof(nums)); 14 | bool neg = n < 0; 15 | n = n < 0 ? -n : n; // assume n is not INT_MIN 16 | int idx = 1; // preserve nums[0] for '\0' 17 | while (n) { 18 | nums[idx++] = n % 2 + '0'; 19 | n /= 2; 20 | } 21 | if (neg) { 22 | for (int i = 1; i < 33; ++i) 23 | nums[i] = (1 - (nums[i] - '0')) + '0'; 24 | nums[1] += 1; 25 | for (int i = 1; i < 33; ++i) { 26 | nums[i + 1] += (nums[i] - '0') / 2; 27 | nums[i] = (nums[i] - '0') % 2 + '0'; 28 | } 29 | } 30 | nums[0] = '\0'; 31 | reverse(nums, nums + 33); 32 | cout << nums << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/2-Stone.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool win(int a, int b) 7 | { 8 | if (a == 0 || b == 0) 9 | return false; 10 | if (a < b) 11 | swap(a, b); 12 | if (a / b >= 2) 13 | return true; 14 | return !win(b, a - b); 15 | } 16 | 17 | int main() 18 | { 19 | int a, b; 20 | while (cin >> a >> b && a) 21 | cout << (win(a, b) ? "win" : "lose") << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/3-LCM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } 7 | 8 | int main() 9 | { 10 | int a, b; 11 | char c; 12 | cin >> a >> c >> b; 13 | cout << a * b / gcd(a, b) << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/4-Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool palind(int n) 7 | { 8 | char s1[10], s2[10]; 9 | int idx = 0; 10 | while (n) { 11 | s1[idx++] = n % 10 + '0'; 12 | n /= 10; 13 | } 14 | s1[idx] = '\0'; 15 | strcpy(s2, s1); 16 | reverse(s2, s2 + idx); 17 | return strcmp(s1, s2) == 0; 18 | } 19 | 20 | int main() 21 | { 22 | int n; 23 | cin >> n; 24 | cout << (palind(n) ? "yes" : "no") << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/5-487-3279.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int cnt[10000010]; 7 | 8 | int MAP(char c) 9 | { 10 | if (c <= '9' && c >= '0') 11 | return c - '0'; 12 | if (c >= 'A' && c <= 'P') 13 | return (c - 'A') / 3 + 2; 14 | if (c >= 'R' && c <= 'Y') 15 | return (c - 'Q') / 3 + 7; 16 | cerr << "Wrong Input" << endl; 17 | exit(1); 18 | } 19 | 20 | int main() 21 | { 22 | ios::sync_with_stdio(false); 23 | int k; 24 | cin >> k; 25 | char s[100]; 26 | memset(cnt, 0, sizeof(cnt)); 27 | while (k--) { 28 | cin >> s; 29 | int l = strlen(s); 30 | int num = 0; 31 | for (int i = 0; i < l; ++i) { 32 | if (s[i] == '-') 33 | continue; 34 | num = num * 10 + MAP(s[i]); 35 | } 36 | cnt[num]++; 37 | } 38 | int maxc = 0; 39 | for (int i = 0; i < 10000001; ++i) 40 | maxc = max(maxc, cnt[i]); 41 | if (maxc == 1) { 42 | cout << "No duplicates." << endl; 43 | return 0; 44 | } 45 | for (int i = 0; i < 10000001; ++i) { 46 | if (cnt[i] <= 1) 47 | continue; 48 | int tmp = i; 49 | for (int j = 0; j < 4; ++j) { 50 | s[j] = tmp % 10 + '0'; 51 | tmp /= 10; 52 | } 53 | s[4] = '-'; 54 | for (int j = 5; j < 8; ++j) { 55 | s[j] = tmp % 10 + '0'; 56 | tmp /= 10; 57 | } 58 | s[8] = '\0'; 59 | reverse(s, s + 8); 60 | cout << s << ' ' << cnt[i] << endl; 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/6-Calendar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const char tzol[20][10] = { "imix", "ik", "akbal", "kan", "chicchan", "cimi", 6 | "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", 7 | "ix", "mem", "cib", "caban", "eznab", "canac", "ahau" }; 8 | const char haab[20][10] = { "pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", 9 | "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", 10 | "muan", "pax", "koyab", "cumhu", "uayet" }; 11 | 12 | 13 | int main() 14 | { 15 | int k; 16 | cin >> k; 17 | cout << k << endl; 18 | while (k--) { 19 | int day, month, year; 20 | char c; 21 | char s[10]; 22 | cin >> day >> c >> s >> year; 23 | for (int i = 0; i < 19; ++i) 24 | if (strcmp(haab[i], s) == 0) 25 | month = i; 26 | int D = month * 20 + day; 27 | D += year * 365; 28 | year = D / 260; 29 | D %= 260; 30 | month = D % 13; 31 | D = D % 20; 32 | cout << month + 1 << " " << tzol[D] << ' ' << year << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/1.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/2.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/3.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/4.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/5-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/5-1.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/5-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/5-2.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/6-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/6-1.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习1/pics/6-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习1/pics/6-2.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/1-Tooth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int func(int n) 5 | { 6 | return n <= 1 ? 1 : func(n - 1) + func(n - 2); 7 | } 8 | 9 | int main() 10 | { 11 | int n; 12 | cin >> n; 13 | cout << func(n) << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/10-SetProd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool dfs(int nums[], int depth, int n, int prod, bool got) 7 | { // got means at least one element has been used 8 | if (depth == n) 9 | return got && prod == 1; 10 | if (dfs(nums, depth + 1, n, prod, got)) 11 | return true; 12 | if (prod == 0 && nums[depth] == 0) 13 | return true; 14 | if (nums[depth] != 0 && prod % nums[depth] == 0) 15 | return dfs(nums, depth + 1, n, prod / nums[depth], true); 16 | return false; 17 | } 18 | 19 | int main() 20 | { 21 | char useless[100]; 22 | int prod; 23 | while (cin >> prod) { 24 | int n, nums[20]; 25 | cin >> n; 26 | for (int i = 0; i < n; ++i) 27 | cin >> nums[i]; 28 | cout << (dfs(nums, 0, n, prod, false) ? "YES" : "NO") << endl; 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/2-Equiv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int nums[40][10000]; 5 | int idxs[40]; 6 | 7 | int cal(int n) 8 | { 9 | int res = 0; 10 | while (n) { 11 | res += n % 10; 12 | n /= 10; 13 | } 14 | return res; 15 | } 16 | 17 | int main() 18 | { 19 | 20 | char c; 21 | int m, n, k; 22 | cin >> m >> c >> n >> c >> k; 23 | for (int i = m + 1; i < n; ++i) { 24 | int p = cal(i); 25 | if (p % k == 0) { 26 | nums[p / k][idxs[p / k]] = i; 27 | idxs[p / k]++; 28 | } 29 | } 30 | for (int i = 0; i < 40; ++i) { 31 | for (int j = 0; j < idxs[i]; ++j) { 32 | if (j > 0) 33 | cout << ','; 34 | cout << nums[i][j]; 35 | if (j == idxs[i] - 1) 36 | cout << endl; 37 | } 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/3-Bacteria.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int nums[100][100]; 7 | bool visited[100][100]; 8 | 9 | void dfs(int i, int j, int m, int n) 10 | { 11 | if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || nums[i][j] == 0) 12 | return; 13 | visited[i][j] = true; 14 | int dx[] = { 0, 1, 0, -1 }; 15 | int dy[] = { 1, 0, -1, 0 }; 16 | for (int dir = 0; dir < 4; ++dir) 17 | dfs(i + dx[dir], j + dy[dir], m, n); 18 | return; 19 | } 20 | 21 | int main() 22 | { 23 | 24 | int m, n, res = 0; 25 | cin >> m >> n; 26 | for (int i = 0; i < m; ++i) 27 | for (int j = 0; j < n; ++j) 28 | cin >> nums[i][j]; 29 | for (int i = 0; i < m; ++i) 30 | for (int j = 0; j < n; ++j) { 31 | if (visited[i][j] || nums[i][j] == 0) 32 | continue; 33 | res += 1; 34 | dfs(i, j, m, n); 35 | } 36 | cout << res << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/4-Monkey.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool dfs(int x, int y, int m, int n, int nums[][50], bool visited[][50]) 6 | { 7 | if (x >= m || x < 0 || y >= n || y < 0 || visited[x][y] || nums[x][y] == 1) 8 | return false; 9 | if (nums[x][y] == 3) 10 | return true; 11 | visited[x][y] = true; 12 | int dx[] = { 0, 1, 0, -1 }; 13 | int dy[] = { 1, 0, -1, 0 }; 14 | for (int i = 0; i < 4; ++i) 15 | if (dfs(x + dx[i], y + dy[i], m, n, nums, visited)) 16 | return true; 17 | return false; 18 | } 19 | 20 | int main() 21 | { 22 | int m, n, nums[50][50], startx, starty; 23 | bool visited[50][50]; 24 | memset(visited, 0, sizeof(visited)); 25 | cin >> m >> n; 26 | for (int i = 0; i < m; ++i) 27 | for (int j = 0; j < n; ++j) { 28 | cin >> nums[i][j]; 29 | if (nums[i][j] == 2) 30 | startx = i, starty = j; 31 | } 32 | cout << (dfs(startx, starty, m, n, nums, visited) ? "Yes" : "No") << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/5-Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Pos { 7 | char c; 8 | int idx; 9 | Pos(char cc, int iidx) : c(cc), idx(iidx) {} 10 | }; 11 | 12 | int main() 13 | { 14 | stack st; 15 | char s[110]; 16 | cin >> s; 17 | int l = strlen(s); 18 | char boy = s[0]; 19 | for (int i = 0; i < l; ++i) { 20 | if (st.empty() || s[i] == boy) 21 | st.push(Pos(s[i], i)); 22 | else { 23 | cout << st.top().idx << ' ' << i << endl; 24 | st.pop(); 25 | } 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/6-SimilarString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char S[520], T[200][520]; 6 | int dp[520][520]; 7 | int dist[520]; 8 | 9 | int similar(char s[], char t[]) 10 | { 11 | int l1 = strlen(s), l2 = strlen(t); 12 | for (int i = 0; i < l1; ++i) 13 | dp[i][0] = i; 14 | for (int j = 0; j < l2; ++j) 15 | dp[0][j] = j; 16 | for (int i = 0; i < l1; ++i) 17 | for (int j = 0; j < l2; ++j) 18 | dp[i + 1][j + 1] = s[i] == t[j] ? dp[i][j] : min(dp[i + 1][j], dp[i][j + 1]) + 1; 19 | return dp[l1][l2]; 20 | } 21 | 22 | int main() 23 | { 24 | int n; 25 | cin >> S >> n; 26 | int mindist = 1000; 27 | for (int i = 0; i < n; ++i) { 28 | cin >> T[i]; 29 | dist[i] = similar(S, T[i]); 30 | mindist = min(mindist, dist[i]); 31 | } 32 | for (int i = 0; i < n; ++i) 33 | if (dist[i] == mindist) 34 | cout << T[i] << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/7-P-Encoding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char *fillint2char(int num, char *p) 7 | { 8 | int cnt = 0; 9 | while (num) { 10 | *p++ = num % 10 + '0'; 11 | num /= 10; 12 | cnt += 1; 13 | } 14 | reverse(p - cnt, p); 15 | *p = '\0'; 16 | return p; 17 | } 18 | 19 | void encode(char src[], char *tgt) 20 | { 21 | int idx = 0; 22 | int l1 = strlen(src); 23 | while (idx < l1) { 24 | int newidx = idx + 1; 25 | while (newidx < l1 && src[newidx] == src[newidx - 1]) 26 | newidx++; 27 | tgt = fillint2char(newidx - idx, tgt); 28 | *tgt++ = src[newidx - 1]; 29 | *tgt = '\0'; 30 | idx = newidx; 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | char s[1010], t[2010]; 37 | int n; 38 | cin >> n; 39 | while (n--) { 40 | cin >> s; 41 | encode(s, t); 42 | cout << t << endl; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/8-BinaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int parentlist1[20], idx1 = 0; 10 | int parentlist2[20], idx2 = 0; 11 | int a, b; 12 | cin >> a >> b; 13 | while (a) { 14 | parentlist1[idx1++] = a; 15 | a /= 2; 16 | } 17 | while (b) { 18 | parentlist2[idx2++] = b; 19 | b /= 2; 20 | } 21 | reverse(parentlist1, parentlist1 + idx1); 22 | reverse(parentlist2, parentlist2 + idx2); 23 | int i; 24 | for (i = 0; i < min(idx1, idx2); ++i) 25 | if (parentlist1[i] != parentlist2[i]) 26 | break; 27 | cout << parentlist1[i - 1] << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/9-Apple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int func(int apple, int plate) 6 | { 7 | if (apple == 0) 8 | return 1; 9 | if (plate == 0) 10 | return 0; 11 | if (plate > apple) 12 | return func(apple, apple); 13 | return func(apple - plate, plate) + func(apple, plate - 1); 14 | } 15 | 16 | int main() 17 | { 18 | int cases, apple, plate; 19 | cin >> cases; 20 | while (cases--) { 21 | cin >> apple >> plate; 22 | cout << func(apple, plate) << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/1.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/10.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/10.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/2.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/3.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/4.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/5.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/6.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/7.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/8.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习2/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习2/pics/9.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/1-PrefixExp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | double func() 7 | { 8 | char c[100]; 9 | cin >> c; 10 | switch (c[0]) { 11 | case '*': return func() * func(); 12 | case '+': return func() + func(); 13 | case '-': return func() - func(); 14 | case '/': return func() / func(); 15 | default: 16 | return atof(c); 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | printf("%f\n", func()); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/10-Match.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int mem[22][22]; 6 | 7 | bool match(char s1[], char s2[], int i, int l1, int j, int l2) 8 | { 9 | if (mem[i][j] != -1) 10 | return mem[i][j] == 1; 11 | if (i == l1) 12 | return mem[i][j] = j >= l2; 13 | if (s1[i] == '?') 14 | return mem[i][j] = match(s1, s2, i + 1, l1, j + 1, l2); 15 | if (s1[i] == '*') { 16 | for (int p = j; p <= l2; ++p) 17 | if (match(s1, s2, i + 1, l1, p, l2)) 18 | return mem[i][j] = 1; 19 | return mem[i][j] = 0; 20 | } 21 | return mem[i][j] = j < l2 && s2[j] == s1[i] && match(s1, s2, i + 1, l1, j + 1, l2); 22 | } 23 | 24 | int main() 25 | { 26 | char s1[22], s2[22]; 27 | memset(mem, -1, sizeof(mem)); 28 | cin >> s1 >> s2; 29 | cout << (match(s1, s2, 0, strlen(s1), 0, strlen(s2)) ? "matched" : "not matched") << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/2-BalanceMat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int res; 5 | 6 | int cal(int mat[][7], int n, int offset[]) 7 | { 8 | int ret = 0; 9 | for (int i = 0; i < n; ++i) { 10 | int tmp = 0; 11 | for (int j = 0; j < n; ++j) 12 | tmp += mat[j][(i + offset[j]) % n]; 13 | ret = max(ret, tmp); 14 | } 15 | return ret; 16 | } 17 | 18 | void dfs(int mat[][7], int depth, int n, int offset[]) 19 | { 20 | if (depth == n) { 21 | res = min(res, cal(mat, n, offset)); 22 | return; 23 | } 24 | for (int i = 0; i < n; ++i) { 25 | offset[depth] = i; 26 | dfs(mat, depth + 1, n, offset); 27 | } 28 | return; 29 | } 30 | 31 | int main() 32 | { 33 | int n, mat[7][7], offset[7]; 34 | while (cin >> n && n > 0) { 35 | res = 100000; 36 | for (int i = 0; i < n; ++i) 37 | for (int j = 0; j < n; ++j) 38 | cin >> mat[i][j]; 39 | dfs(mat, 0, n, offset); 40 | cout << res << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/3-KBaseSeq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void addone(char s[], int k) 7 | { 8 | int l = strlen(s); 9 | reverse(s, s + l); 10 | s[l] = '0'; 11 | s[0] += 1; 12 | for (int i = 0; i < l; ++i) { 13 | s[i + 1] += (s[i] - '0') / k; 14 | s[i] = (s[i] - '0') % k + '0'; 15 | } 16 | if (s[l] != '0') 17 | l++; 18 | s[l] = '\0'; 19 | reverse(s, s + l); 20 | return; 21 | } 22 | 23 | int main() 24 | { 25 | int k, m; 26 | char s[1010], c; 27 | cin >> k >> c >> m >> c >> s; 28 | for (int i = 0; i < m; ++i) { 29 | addone(s, k); 30 | cout << s; 31 | if (i != m - 1) 32 | cout << (i % 5 == 4 ? '\n' : ','); 33 | } 34 | cout << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/4-Factor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int func(int n, int start) 6 | { 7 | int res = 0; 8 | for (int i = start; i * i <= n; ++i) 9 | if (n % i == 0) 10 | res += func(n / i, i); 11 | return res + 1; 12 | } 13 | 14 | int main() 15 | { 16 | int k, n; 17 | cin >> k; 18 | while (k--) { 19 | cin >> n; 20 | cout << func(n, 2) << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/5-Coin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int num; 6 | int cnt[4]; 7 | int coin[4] = {5, 2, 1}; 8 | 9 | void money(int left, int k) 10 | { 11 | if (k == 3) { 12 | if (left == 0) 13 | printf("%03d%12d%12d%12d\n", ++num, cnt[2], cnt[1], cnt[0]);; 14 | return; 15 | } 16 | int prob = left / coin[k]; 17 | for (int i = 0; i <= prob; ++i) { 18 | cnt[k] = i; 19 | money(left - coin[k] * i, k + 1); 20 | } 21 | return; 22 | } 23 | 24 | int main() 25 | { 26 | int n; 27 | cin >> n; 28 | money(n, 0); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/6-SimpleShort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isShort(char s1[], char s2[]) 7 | { 8 | int l1 = strlen(s1); 9 | int l2 = strlen(s2); 10 | if (l1 >= l2) 11 | return false; 12 | int i = 0, j = 0; 13 | while (i < l1) { 14 | while (j < l2 && s2[j] != s1[i]) 15 | j++; 16 | if (j == l2) 17 | return false; 18 | i++, j++; 19 | } 20 | return true; 21 | } 22 | 23 | int main() 24 | { 25 | int n; 26 | cin >> n; 27 | cin.get(); 28 | char s1[32], s2[320]; 29 | while (n--) { 30 | cin.getline(s1, 31, ':'); 31 | cin.getline(s2, 310); 32 | cout << (isShort(s1, s2) ? "YES" : "NO") << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/7-BoolExp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct op { 8 | char c; 9 | int level; // 0-2 | & ! 10 | op(char _c, int _level) : c(_c), level(_level) {} 11 | }; 12 | 13 | void cal(stack &ops, stack &vals) 14 | { 15 | op cur = ops.top(); 16 | ops.pop(); 17 | switch (cur.c) 18 | { 19 | bool tmp; 20 | case '!': 21 | vals.top() = !vals.top(); 22 | break; 23 | case '|': 24 | tmp = vals.top(); 25 | vals.pop(); 26 | vals.top() |= tmp; 27 | break; 28 | case '&': 29 | tmp = vals.top(); 30 | vals.pop(); 31 | vals.top() &= tmp; 32 | break; 33 | default: 34 | cout << "????????" << endl; 35 | break; 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | char s[1010]; 42 | while (cin.getline(s, 1000)) { 43 | int l = strlen(s); 44 | stack ops; 45 | stack vals; 46 | for (int i = 0; i < l; ++i) { 47 | if (s[i] == ' ') 48 | continue; 49 | if (s[i] == '(') { 50 | ops.push(op('(', -1)); 51 | } else if (s[i] == ')') { 52 | while (ops.top().c != '(') 53 | cal(ops, vals); 54 | ops.pop(); 55 | } else if (s[i] == '|') { 56 | while (!ops.empty() && ops.top().level > 0) 57 | cal(ops, vals); 58 | ops.push(op(s[i], 0)); 59 | } else if (s[i] == '&') { 60 | while (!ops.empty() && ops.top().level > 1) 61 | cal(ops, vals); 62 | ops.push(op(s[i], 1)); 63 | } else if (s[i] == '!') { 64 | while (!ops.empty() && ops.top().level > 2) 65 | cal(ops, vals); 66 | ops.push(op(s[i], 2)); 67 | } else { 68 | vals.push(s[i] == 'V'); 69 | } 70 | } 71 | while (!ops.empty()) 72 | cal(ops, vals); 73 | cout << (vals.top() ? "V" : "F") << endl; 74 | } 75 | return 0; 76 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/8-Cal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct op { 8 | char c; 9 | int level; // 0-2 (+,-), (*,/), -+ 10 | op(char _c, int _level) : c(_c), level(_level) {} 11 | }; 12 | 13 | void cal(stack &ops, stack &vals) 14 | { 15 | op cur = ops.top(); 16 | ops.pop(); 17 | switch (cur.c) 18 | { 19 | int tmp; 20 | case '-': 21 | if (cur.level == 2) { 22 | vals.top() = -vals.top(); 23 | } else { 24 | tmp = vals.top(); 25 | vals.pop(); 26 | vals.top() -= tmp; 27 | } 28 | break; 29 | case '+': 30 | if (cur.level == 2) { 31 | 32 | } else { 33 | tmp = vals.top(); 34 | vals.pop(); 35 | vals.top() += tmp; 36 | } 37 | break; 38 | case '*': 39 | tmp = vals.top(); 40 | vals.pop(); 41 | vals.top() *= tmp; 42 | break; 43 | case '/': 44 | tmp = vals.top(); 45 | vals.pop(); 46 | vals.top() /= tmp; 47 | break; 48 | default: 49 | cout << "????????" << endl; 50 | break; 51 | } 52 | } 53 | 54 | bool lastOP; 55 | 56 | int main() 57 | { 58 | char s[1010]; 59 | while (cin.getline(s, 1000)) { 60 | int l = strlen(s); 61 | stack ops; 62 | stack vals; 63 | lastOP = true; 64 | for (int i = 0; i < l; ++i) { 65 | if (s[i] == ' ') 66 | continue; 67 | if (s[i] == '(') { 68 | ops.push(op('(', -1)); 69 | lastOP = true; 70 | } else if (s[i] == ')') { 71 | while (ops.top().c != '(') 72 | cal(ops, vals); 73 | ops.pop(); 74 | lastOP = false; 75 | } else if (s[i] == '+' || s[i] == '-') { 76 | if (lastOP) { 77 | ops.push(op(s[i], 2)); 78 | } else { 79 | while (!ops.empty() && ops.top().level >= 0) 80 | cal(ops, vals); 81 | ops.push(op(s[i], 0)); 82 | } 83 | lastOP = true; 84 | } else if (s[i] == '*' || s[i] == '/') { 85 | while (!ops.empty() && ops.top().level >= 1) 86 | cal(ops, vals); 87 | ops.push(op(s[i], 1)); 88 | lastOP = true; 89 | } else { 90 | int val = 0; 91 | while (s[i] <= '9' && s[i] >= '0') 92 | val = val * 10 + s[i++] - '0'; 93 | i -= 1; 94 | vals.push(val); 95 | lastOP = false; 96 | } 97 | } 98 | while (!ops.empty()) 99 | cal(ops, vals); 100 | cout << vals.top() << endl; 101 | } 102 | return 0; 103 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/8-Cal2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char a[100] = {0}; 7 | 8 | int matched(int l, int r) //前终止点,后开始点 9 | { 10 | int cnt = 1, ans = 0; 11 | for (int i = r - 1; i >= l; i--) { 12 | if (a[i] == ')') 13 | cnt++; 14 | else if (a[i] == '(') 15 | cnt--; 16 | if (cnt == 0) { 17 | ans = i; 18 | break; 19 | } 20 | } 21 | return ans; 22 | } 23 | 24 | int compute(int l, int r) 25 | { 26 | for (int i = r; i >= l; i--) { 27 | if (a[i] == ')') 28 | i = matched(l, i); 29 | if (a[i] == '+') 30 | return compute(i + 1, r) + compute(l, i - 1); 31 | if (a[i] == '-') 32 | return compute(l, i - 1) - compute(i + 1, r); 33 | } 34 | for (int i = r; i >= l; i--) { 35 | if (a[i] == ')') 36 | i = matched(l, i); 37 | if (a[i] == '*') 38 | return compute(i + 1, r) * compute(l, i - 1); 39 | if (a[i] == '/') 40 | return compute(l, i - 1) / compute(i + 1, r); 41 | } 42 | //到此处还没返回,说明没有运算符,只有括号和数字 43 | if (a[r] == ')') 44 | return compute(l + 1, r - 1); 45 | if (a[r] >= '0' && a[r] <= '9') { 46 | int ans = 0, j = 0; 47 | for (int i = r; i >= l; i--, j++) 48 | ans += pow(10.0, (double)j) * (a[i] - '0'); 49 | return ans; 50 | } 51 | return 0; 52 | } 53 | 54 | int main() 55 | { 56 | while (cin.getline(a, 100)) { 57 | int x = strlen(a); 58 | cout << compute(0, x - 1) << endl; 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/9-Permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void dfs(char s[], int depth, int n, int visited[], int idx[]) 6 | { 7 | if (depth == n) { 8 | for (int i = 0; i < n; ++i) 9 | cout << s[idx[i]]; 10 | cout << endl; 11 | return; 12 | } 13 | for (int i = 0; i < n; ++i) { 14 | if (!visited[i]) { 15 | visited[i] = true; 16 | idx[depth] = i; 17 | dfs(s, depth + 1, n, visited, idx); 18 | visited[i] = false; 19 | } 20 | } 21 | return; 22 | } 23 | 24 | int main() 25 | { 26 | char s[7]; 27 | int visited[6] = { 0 }, idx[6]; 28 | cin >> s; 29 | dfs(s, 0, strlen(s), visited, idx); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/1.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/10.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/10.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/2.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/3.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/4.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/5.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/6.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/7.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/8.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习3/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习3/pics/9.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/1-PrimeFac.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool out = false; 5 | 6 | void func(int n, int start) 7 | { 8 | for (int i = start; i * i <= n; ++i) { 9 | if (n % i == 0) { 10 | if (out) 11 | cout << '*'; 12 | out = true; 13 | cout << i; 14 | func(n / i, i); 15 | return; 16 | } 17 | } 18 | if (out) 19 | cout << '*'; 20 | out = true; 21 | cout << n ; 22 | return; 23 | } 24 | 25 | int main() 26 | { 27 | int n; 28 | cin >> n; 29 | cout << n << '='; 30 | func(n, 2); 31 | cout << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/10-TSP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int cost[15][15]; 6 | int mem[1 << 15][15]; 7 | bool visited[15]; 8 | int n; 9 | 10 | void dfs(int depth, int curpos, int curcost, int state) 11 | { 12 | if (depth == n - 1) { 13 | mem[(1 << n) - 1][0] = min((unsigned)mem[(1 << n) - 1][0], unsigned(curcost + cost[curpos][0])); 14 | return; 15 | } 16 | for (int i = 1; i < n; ++i) { 17 | if (visited[i]) 18 | continue; 19 | int nextstate = state | (1 << i); 20 | int nextcost = curcost + cost[curpos][i]; 21 | if (mem[nextstate][i] > 0 && mem[nextstate][i] <= nextcost) 22 | continue; 23 | mem[nextstate][i] = nextcost; 24 | visited[i] = true; 25 | dfs(depth + 1, i, nextcost, nextstate); 26 | visited[i] = false; 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | cin >> n; 33 | for (int i = 0; i < n; ++i) 34 | for (int j = 0; j < n; ++j) 35 | cin >> cost[i][j]; 36 | memset(mem, -1, sizeof(mem)); 37 | dfs(0, 0, 0, 0); 38 | cout << mem[(1 << n) - 1][0] << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/2-Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool judge(int k, int x) 5 | { 6 | if (x == k) 7 | return true; 8 | if (x < k) 9 | return false; 10 | if ((x - 1) % 2 == 0 && judge(k, (x - 1) / 2)) 11 | return true; 12 | if ((x - 1) % 3 == 0 && judge(k, (x - 1) / 3)) 13 | return true; 14 | return false; 15 | } 16 | 17 | int main() 18 | { 19 | int k, x; 20 | char c; 21 | cin >> k >> c >> x; 22 | cout << (judge(k, x) ? "YES" : "NO") << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/3-Maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int n; 6 | 7 | char m[100][100]; 8 | bool visited[100][100]; 9 | 10 | bool dfs(int sx, int sy, int ex, int ey) 11 | { 12 | if (sx >= n || sx < 0 || sy >= n || sy < 0) 13 | return false; 14 | if (visited[sx][sy] || m[sx][sy] == '#') 15 | return false; 16 | if (sx == ex && sy == ey) 17 | return true; 18 | visited[sx][sy] = true; 19 | int dx[] = { 0, 1, 0, - 1}; 20 | int dy[] = { 1, 0, -1, 0 }; 21 | for (int i = 0; i < 4; ++i) 22 | if (dfs(sx + dx[i], sy + dy[i], ex, ey)) 23 | return true; 24 | return false; 25 | } 26 | 27 | int main() 28 | { 29 | int k; 30 | cin >> k; 31 | while (k--) { 32 | memset(visited, 0, sizeof(visited)); 33 | cin >> n; 34 | for (int i = 0; i < n; ++i) 35 | for (int j = 0; j < n; ++j) 36 | cin >> m[i][j]; 37 | int sx, sy, ex, ey; 38 | cin >> sx >> sy >> ex >> ey; 39 | cout << (dfs(sx, sy, ex, ey) ? "YES" : "NO") << endl; 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/4-Fruit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char c[1010]; 8 | const char fruits[][15] = {"apples", "bananas", "peaches", "cherries", "pears", "oranges", "strawberries"}; 9 | const char fail[] = "You must not enjoy fruit."; 10 | const char replace[] = "Brussels sprouts"; 11 | while (cin.getline(c, 1002)) { 12 | bool got = false; 13 | int match = 0; 14 | int l = strlen(c); 15 | char *s; 16 | for (int j = 0; j < 7; ++j) { 17 | s = strstr(c, fruits[j]); 18 | if (s != NULL) { 19 | got = true; 20 | match = j; 21 | break; 22 | } 23 | } 24 | if (!got) { 25 | cout << fail << endl; 26 | } else { 27 | *s = '\0'; 28 | cout << c << replace << s + strlen(fruits[match]) << endl; 29 | } 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/5-Pattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char grids[200][200]; 7 | bool visited[200][200]; 8 | 9 | void dfs(int i, int j, int m, int n) 10 | { 11 | if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || grids[i][j] != '#') 12 | return; 13 | visited[i][j] = true; 14 | int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; 15 | int dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; 16 | for (int dir = 0; dir < 8; ++dir) 17 | dfs(i + dx[dir], j + dy[dir], m, n); 18 | return; 19 | } 20 | 21 | int main() 22 | { 23 | 24 | int m, n, res = 0; 25 | cin >> m >> n; 26 | for (int i = 0; i < m; ++i) 27 | for (int j = 0; j < n; ++j) 28 | cin >> grids[i][j]; 29 | for (int i = 0; i < m; ++i) 30 | for (int j = 0; j < n; ++j) { 31 | if (visited[i][j] || grids[i][j] != '#') 32 | continue; 33 | res += 1; 34 | dfs(i, j, m, n); 35 | } 36 | cout << res << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/6-Red&Black.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int cnt; 6 | int m, n; 7 | char grid[20][20]; 8 | void dfs(int x, int y) 9 | { 10 | if (x >= m || x < 0 || y >= n || y < 0) 11 | return; 12 | if (grid[x][y] == '#') 13 | return; 14 | cnt++; 15 | grid[x][y] = '#'; 16 | int dx[] = { 0, 1, 0, -1 }; 17 | int dy[] = { 1, 0, -1, 0 }; 18 | for (int i = 0; i < 4; ++i) 19 | dfs(x + dx[i], y + dy[i]); 20 | } 21 | 22 | int main() 23 | { 24 | while (cin >> n >> m && m) { 25 | int x, y; 26 | for (int i = 0; i < m; ++i) 27 | for (int j = 0; j < n; ++j) { 28 | cin >> grid[i][j]; 29 | if (grid[i][j] == '@') 30 | x = i, y = j; 31 | } 32 | cnt = 0; 33 | dfs(x, y); 34 | cout << cnt << endl; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/7-Rational.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int solve(int p, int q) 6 | { 7 | if (p == 1 && q == 1) 8 | return 1; 9 | if (p > q) 10 | return 2 * solve(p - q, q) + 1; 11 | return 2 * solve(p, q - p); 12 | } 13 | 14 | int main() 15 | { 16 | int n, p, q; 17 | cin >> n; 18 | while (n--) { 19 | cin >> p >> q; 20 | cout << solve(p, q) << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/8-BalanceMat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int res; 5 | 6 | int cal(int mat[][7], int n, int offset[]) 7 | { 8 | int ret = 0; 9 | for (int i = 0; i < n; ++i) { 10 | int tmp = 0; 11 | for (int j = 0; j < n; ++j) 12 | tmp += mat[j][(i + offset[j]) % n]; 13 | ret = max(ret, tmp); 14 | } 15 | return ret; 16 | } 17 | 18 | void dfs(int mat[][7], int depth, int n, int offset[]) 19 | { 20 | if (depth == n) { 21 | res = min(res, cal(mat, n, offset)); 22 | return; 23 | } 24 | for (int i = 0; i < n; ++i) { 25 | offset[depth] = i; 26 | dfs(mat, depth + 1, n, offset); 27 | } 28 | return; 29 | } 30 | 31 | int main() 32 | { 33 | int n, mat[7][7], offset[7]; 34 | while (cin >> n && n > 0) { 35 | res = 100000; 36 | for (int i = 0; i < n; ++i) 37 | for (int j = 0; j < n; ++j) 38 | cin >> mat[i][j]; 39 | dfs(mat, 0, n, offset); 40 | cout << res << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/9-Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Pos { 7 | char c; 8 | int idx; 9 | Pos(char cc, int iidx) : c(cc), idx(iidx) {} 10 | }; 11 | 12 | int main() 13 | { 14 | stack st; 15 | char s[110]; 16 | cin >> s; 17 | int l = strlen(s); 18 | char boy = s[0]; 19 | for (int i = 0; i < l; ++i) { 20 | if (st.empty() || s[i] == boy) 21 | st.push(Pos(s[i], i)); 22 | else { 23 | cout << st.top().idx << ' ' << i << endl; 24 | st.pop(); 25 | } 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/1.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/10.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/10.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/2.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/3.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/4.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/5.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/6-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/6-1.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/6-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/6-2.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/7.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/8.PNG -------------------------------------------------------------------------------- /第三阶段/第三阶段编程练习4/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第三阶段/第三阶段编程练习4/pics/9.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/1-BigSmallCase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char c; 7 | cin >> c; 8 | if (c <= 'z' && c >= 'a') 9 | cout << "lower case" << endl; 10 | else if (c <= 'Z' && c >= 'A') 11 | cout << "upper case" << endl; 12 | else 13 | cout << "invalid input" << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/2-Indep3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(int n) 5 | { 6 | return n % 3 == 0 || n / 10 == 3 || n % 10 == 3; 7 | } 8 | 9 | int main() 10 | { 11 | int n; 12 | while (cin >> n) 13 | cout << (check(n) ? "TRUE" : "FALSE") << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/3-BaseCalc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char chs[] = "0123456789abcdef"; 6 | 7 | int convert(char num[], int base) 8 | { 9 | int res = 0, i = 0; 10 | while (num[i] != '\0') { 11 | char c = num[i]; 12 | int tmp = (c >= '0' && c <= '9') ? c - '0' : c - 'a' + 10; 13 | res = res * base + tmp; 14 | i++; 15 | } 16 | return res; 17 | } 18 | 19 | int main() 20 | { 21 | char num1[1000], num2[1000]; 22 | int base1, base2, base3; 23 | cin >> base1 >> num1; 24 | cin >> base2 >> num2; 25 | cin >> base3; 26 | int sum = convert(num1, base1) + convert(num2, base2); 27 | int i = 0; 28 | while (sum) { 29 | num1[i++] = chs[sum % base3]; 30 | sum /= base3; 31 | } 32 | num1[i] = '\0'; 33 | reverse(num1, num1 + i); 34 | cout << num1 << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/4-Area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define PI 3.14159 6 | 7 | int main() 8 | { 9 | double r, h; 10 | cin >> r >> h; 11 | double area = 2 * PI * r * (r + h); 12 | cout << "Area = " << setprecision(3) 13 | << setiosflags(ios::fixed) << area << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/5-BinaryAdd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | char num1[110], num2[110]; 10 | int num3[110]; 11 | cin >> n; 12 | while (n--) { 13 | memset(num1, '0', sizeof(num1)); 14 | memset(num2, '0', sizeof(num2)); 15 | memset(num3, 0, sizeof(num3)); 16 | cin >> num1 >> num2; 17 | int l1 = strlen(num1); 18 | int l2 = strlen(num2); 19 | num1[l1] = num2[l2] = '0'; 20 | reverse(num1, num1 + l1); 21 | reverse(num2, num2 + l2); 22 | for (int i = 0; i < max(l1, l2); ++i) { 23 | int t = num1[i] - '0' + num2[i] - '0'; 24 | num3[i] += t; 25 | num3[i + 1] += num3[i] / 2; 26 | num3[i] %= 2; 27 | } 28 | int i = max(l1, l2); 29 | while (i >= 1 && num3[i] == 0) 30 | i--; 31 | while (i >= 0) 32 | cout << num3[i--]; 33 | cout << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/6-Count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char c; 7 | int cnt[3] = {0}; 8 | while ((c = cin.get()) != '\n') { 9 | if (c >= 'A' && c <= 'Z') 10 | cnt[0]++; 11 | else if (c >= 'a' && c <= 'z') 12 | cnt[1]++; 13 | else if (c >= '0' && c <= '9') 14 | cnt[2]++; 15 | } 16 | cout << cnt[0] << ' ' << cnt[1] << ' ' << cnt[2] << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/7-DiffNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | int res = 0, tmp; 9 | while (n--) { 10 | cin >> tmp; 11 | res ^= tmp; 12 | } 13 | cout << res << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ![](pics/1.PNG) 4 | 5 | ```cpp 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | char c; 12 | cin >> c; 13 | if (c <= 'z' && c >= 'a') 14 | cout << "lower case" << endl; 15 | else if (c <= 'Z' && c >= 'A') 16 | cout << "upper case" << endl; 17 | else 18 | cout << "invalid input" << endl; 19 | return 0; 20 | } 21 | ``` 22 | 23 | 24 | 25 | 26 | 27 | ![](pics/2.PNG) 28 | 29 | ```cpp 30 | #include 31 | using namespace std; 32 | 33 | bool check(int n) 34 | { 35 | return n % 3 == 0 || n / 10 == 3 || n % 10 == 3; 36 | } 37 | 38 | int main() 39 | { 40 | int n; 41 | while (cin >> n) 42 | cout << (check(n) ? "TRUE" : "FALSE") << endl; 43 | return 0; 44 | } 45 | ``` 46 | 47 | 48 | 49 | 50 | 51 | ![](pics/3.PNG) 52 | 53 | ```cpp 54 | #include 55 | #include 56 | using namespace std; 57 | 58 | char chs[] = "0123456789abcdef"; 59 | 60 | int convert(char num[], int base) 61 | { 62 | int res = 0, i = 0; 63 | while (num[i] != '\0') { 64 | char c = num[i]; 65 | int tmp = (c >= '0' && c <= '9') ? c - '0' : c - 'a' + 10; 66 | res = res * base + tmp; 67 | i++; 68 | } 69 | return res; 70 | } 71 | 72 | int main() 73 | { 74 | char num1[1000], num2[1000]; 75 | int base1, base2, base3; 76 | cin >> base1 >> num1; 77 | cin >> base2 >> num2; 78 | cin >> base3; 79 | int sum = convert(num1, base1) + convert(num2, base2); 80 | int i = 0; 81 | while (sum) { 82 | num1[i++] = chs[sum % base3]; 83 | sum /= base3; 84 | } 85 | num1[i] = '\0'; 86 | reverse(num1, num1 + i); 87 | cout << num1 << endl; 88 | return 0; 89 | } 90 | ``` 91 | 92 | 93 | 94 | 95 | 96 | ![](pics/4.PNG) 97 | 98 | ```cpp 99 | #include 100 | #include 101 | using namespace std; 102 | 103 | #define PI 3.14159 104 | 105 | int main() 106 | { 107 | double r, h; 108 | cin >> r >> h; 109 | double area = 2 * PI * r * (r + h); 110 | cout << "Area = " << setprecision(3) 111 | << setiosflags(ios::fixed) << area << endl; 112 | return 0; 113 | } 114 | ``` 115 | 116 | 117 | 118 | 119 | 120 | ![](pics/5.PNG) 121 | 122 | ```cpp 123 | #include 124 | #include 125 | #include 126 | using namespace std; 127 | 128 | int main() 129 | { 130 | int n; 131 | char num1[110], num2[110]; 132 | int num3[110]; 133 | cin >> n; 134 | while (n--) { 135 | memset(num1, '0', sizeof(num1)); 136 | memset(num2, '0', sizeof(num2)); 137 | memset(num3, 0, sizeof(num3)); 138 | cin >> num1 >> num2; 139 | int l1 = strlen(num1); 140 | int l2 = strlen(num2); 141 | num1[l1] = num2[l2] = '0'; 142 | reverse(num1, num1 + l1); 143 | reverse(num2, num2 + l2); 144 | for (int i = 0; i < max(l1, l2); ++i) { 145 | int t = num1[i] - '0' + num2[i] - '0'; 146 | num3[i] += t; 147 | num3[i + 1] += num3[i] / 2; 148 | num3[i] %= 2; 149 | } 150 | int i = max(l1, l2); 151 | while (i >= 1 && num3[i] == 0) 152 | i--; 153 | while (i >= 0) 154 | cout << num3[i--]; 155 | cout << endl; 156 | } 157 | return 0; 158 | } 159 | ``` 160 | 161 | 162 | 163 | 164 | 165 | ![](pics/6.PNG) 166 | 167 | ```cpp 168 | #include 169 | using namespace std; 170 | 171 | int main() 172 | { 173 | char c; 174 | int cnt[3] = {0}; 175 | while ((c = cin.get()) != '\n') { 176 | if (c >= 'A' && c <= 'Z') 177 | cnt[0]++; 178 | else if (c >= 'a' && c <= 'z') 179 | cnt[1]++; 180 | else if (c >= '0' && c <= '9') 181 | cnt[2]++; 182 | } 183 | cout << cnt[0] << ' ' << cnt[1] << ' ' << cnt[2] << endl; 184 | return 0; 185 | } 186 | ``` 187 | 188 | 189 | 190 | 191 | 192 | ![](pics/7.PNG) 193 | 194 | ```cpp 195 | #include 196 | using namespace std; 197 | 198 | int main() 199 | { 200 | int n; 201 | cin >> n; 202 | int res = 0, tmp; 203 | while (n--) { 204 | cin >> tmp; 205 | res ^= tmp; 206 | } 207 | cout << res << endl; 208 | return 0; 209 | } 210 | ``` 211 | 212 | -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习1/pics/1.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习1/pics/2.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习1/pics/3.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习1/pics/4.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习1/pics/5.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习1/pics/6.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习1/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习1/pics/7.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/1-doctor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num[100]; 7 | int n; 8 | cin >> n; 9 | for (int i = 0; i < n; ++i) 10 | cin >> num[i]; 11 | int minn = num[0]; 12 | for (int i = 1; i < n; ++i) 13 | minn = min(minn, num[i]); 14 | for (int i = 0; i < n; ++i) 15 | if (num[i] == minn) { 16 | cout << i + 1 << endl; 17 | break; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/2-greatk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, k, nums[1000]; 7 | cin >> n; 8 | for (int i = 0; i < n; ++i) 9 | cin >> nums[i]; 10 | cin >> k; 11 | for (int i = 0; i < n - 1; ++i) 12 | for (int j = 0; j < n - 1 - i; ++j) 13 | if (nums[j] < nums[j + 1]) 14 | swap(nums[j], nums[j + 1]); 15 | for (int i = 0; i < k; ++i) 16 | cout << nums[i] << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/3-specialnumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int changebase(char nums[], int base, int num) 7 | { 8 | int i = 0; 9 | while (num) { 10 | nums[i++] = num % base + '0'; 11 | num /= base; 12 | } 13 | return i; 14 | } 15 | 16 | int main() 17 | { 18 | char num1[10], num2[10]; 19 | int i; 20 | for (i = 0; i < 1000; ++i) { 21 | int a = changebase(num1, 7, i); 22 | int b = changebase(num2, 9, i); 23 | if (a == b && a == 3 && num1[0] == num2[2] && num1[1] == num2[1] && num1[2] == num2[0]) 24 | break; 25 | } 26 | int n; 27 | cin >> n; 28 | memset(num1, 0, sizeof(num1)); 29 | n = n == 1 ? 10 : n == 2 ? 7 : 9; 30 | changebase(num1, n, i); 31 | reverse(num1, num1 + 3); 32 | cout << num1 << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/4-dif_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int nums[100]; 7 | int n; 8 | cin >> n; 9 | for (int i = 0; i < n; ++i) 10 | cin >> nums[i]; 11 | int res = 0; 12 | int maxn = nums[0]; 13 | for (int i = 1; i < n; ++i) 14 | maxn = max(maxn, nums[i]); 15 | for (int i = 0; i < n; ++i) 16 | res += maxn == nums[i] ? 0 : nums[i]; 17 | cout << res << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/5-primepair.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool nprime[200001]; 6 | 7 | int main() 8 | { 9 | int m, n; 10 | char c; 11 | cin >> m >> c >> n; 12 | nprime[0] = nprime[1] = true; 13 | for (int i = 2; i <= n; ++i) { 14 | if (nprime[i]) 15 | continue; 16 | for (int j = i * 2; j <= n; j += i) 17 | nprime[j] = true; 18 | } 19 | bool flag = false; 20 | for (int i = m; i < n; ++i) 21 | for (int j = i + 1; j <= n; ++j) 22 | if (!nprime[i] && !nprime[j] && !nprime[j - i]) { 23 | printf("(%d,%d)\n", i, j); 24 | flag = true; 25 | } 26 | if (!flag) 27 | cout << "NULL" << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ![](pics/1.PNG) 4 | 5 | ```cpp 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int num[100]; 12 | int n; 13 | cin >> n; 14 | for (int i = 0; i < n; ++i) 15 | cin >> num[i]; 16 | int minn = num[0]; 17 | for (int i = 1; i < n; ++i) 18 | minn = min(minn, num[i]); 19 | for (int i = 0; i < n; ++i) 20 | if (num[i] == minn) { 21 | cout << i + 1 << endl; 22 | break; 23 | } 24 | return 0; 25 | } 26 | ``` 27 | 28 | 29 | 30 | 31 | 32 | ![](pics/2.PNG) 33 | 34 | ```cpp 35 | #include 36 | using namespace std; 37 | 38 | int main() 39 | { 40 | int n, k, nums[1000]; 41 | cin >> n; 42 | for (int i = 0; i < n; ++i) 43 | cin >> nums[i]; 44 | cin >> k; 45 | for (int i = 0; i < n - 1; ++i) 46 | for (int j = 0; j < n - 1 - i; ++j) 47 | if (nums[j] < nums[j + 1]) 48 | swap(nums[j], nums[j + 1]); 49 | for (int i = 0; i < k; ++i) 50 | cout << nums[i] << endl; 51 | return 0; 52 | } 53 | ``` 54 | 55 | 56 | 57 | 58 | 59 | ![](pics/3.PNG) 60 | 61 | ```cpp 62 | #include 63 | #include 64 | #include 65 | using namespace std; 66 | 67 | int changebase(char nums[], int base, int num) 68 | { 69 | int i = 0; 70 | while (num) { 71 | nums[i++] = num % base + '0'; 72 | num /= base; 73 | } 74 | return i; 75 | } 76 | 77 | int main() 78 | { 79 | char num1[10], num2[10]; 80 | int i; 81 | for (i = 0; i < 1000; ++i) { 82 | int a = changebase(num1, 7, i); 83 | int b = changebase(num2, 9, i); 84 | if (a == b && a == 3 && num1[0] == num2[2] && num1[1] == num2[1] && num1[2] == num2[0]) 85 | break; 86 | } 87 | int n; 88 | cin >> n; 89 | memset(num1, 0, sizeof(num1)); 90 | n = n == 1 ? 10 : n == 2 ? 7 : 9; 91 | changebase(num1, n, i); 92 | reverse(num1, num1 + 3); 93 | cout << num1 << endl; 94 | return 0; 95 | } 96 | ``` 97 | 98 | 99 | 100 | 101 | 102 | ![](pics/4.PNG) 103 | 104 | ```cpp 105 | #include 106 | using namespace std; 107 | 108 | int main() 109 | { 110 | int nums[100]; 111 | int n; 112 | cin >> n; 113 | for (int i = 0; i < n; ++i) 114 | cin >> nums[i]; 115 | int res = 0; 116 | int maxn = nums[0]; 117 | for (int i = 1; i < n; ++i) 118 | maxn = max(maxn, nums[i]); 119 | for (int i = 0; i < n; ++i) 120 | res += maxn == nums[i] ? 0 : nums[i]; 121 | cout << res << endl; 122 | return 0; 123 | } 124 | ``` 125 | 126 | 127 | 128 | 129 | 130 | ![](pics/5.PNG) 131 | 132 | ```cpp 133 | #include 134 | #include 135 | using namespace std; 136 | 137 | bool nprime[200001]; 138 | 139 | int main() 140 | { 141 | int m, n; 142 | char c; 143 | cin >> m >> c >> n; 144 | nprime[0] = nprime[1] = true; 145 | for (int i = 2; i <= n; ++i) { 146 | if (nprime[i]) 147 | continue; 148 | for (int j = i * 2; j <= n; j += i) 149 | nprime[j] = true; 150 | } 151 | bool flag = false; 152 | for (int i = m; i < n; ++i) 153 | for (int j = i + 1; j <= n; ++j) 154 | if (!nprime[i] && !nprime[j] && !nprime[j - i]) { 155 | printf("(%d,%d)\n", i, j); 156 | flag = true; 157 | } 158 | if (!flag) 159 | cout << "NULL" << endl; 160 | return 0; 161 | } 162 | ``` 163 | 164 | 165 | -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习2/pics/1.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习2/pics/2.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习2/pics/3.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习2/pics/4.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习2/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习2/pics/5.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/1-e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double res = 1, tmp = 1; 8 | int i = 1; 9 | while (tmp > 1e-7) { 10 | tmp /= i++; 11 | res += tmp; 12 | } 13 | cout << setprecision(6) << setiosflags(ios::fixed) << res << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/2-invoice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int id, num; 8 | char cls; 9 | double money; 10 | double peoples[3] = {0.}, classes[3] = {0.}; 11 | while (cin >> id) { 12 | cin >> num; 13 | while (num--) { 14 | cin >> cls >> money; 15 | classes[cls - 'A'] += money; 16 | peoples[id - 1] += money; 17 | } 18 | } 19 | for (int i = 0; i < 3; ++i) 20 | cout << i + 1 << " " << setprecision(2) 21 | << setiosflags(ios::fixed) << peoples[i] << endl; 22 | for (int i = 0; i < 3; ++i) 23 | cout << char('A' + i) << " " << setprecision(2) 24 | << setiosflags(ios::fixed) << classes[i] << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/3-lake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | for (int py = 1; py <= 4; ++py) 8 | for (int dt = 1; dt <= 4; ++dt) 9 | for (int tai = 1; tai <= 4; ++tai) 10 | for (int hz = 1; hz <= 4; ++hz) { 11 | int tmp[4] = {py, dt, tai, hz}; 12 | sort(tmp, tmp + 4); 13 | bool flag = false; 14 | for (int t = 0; t < 4; ++t) 15 | if (tmp[t] != t + 1) { 16 | flag = true; 17 | break; 18 | } 19 | if (flag) 20 | continue; 21 | int A = (dt == 1) + (hz == 4) + (py == 3); 22 | int B = (hz == 1) + (dt == 4) + (py == 2) + (tai == 3); 23 | int C = (hz == 4) + (dt == 3); 24 | int D = (py == 1) + (tai == 4) + (hz == 2) + (dt == 3); 25 | if (A == 1 && B == 1 && C == 1 && D == 1) { 26 | cout << py << endl << dt << endl; 27 | cout << tai << endl << hz << endl; 28 | goto end; 29 | } 30 | } 31 | end: 32 | return 0; 33 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/4-car.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | for (int i = 0; i < 16; ++i) { 8 | 9 | bool S1 = i & 1, S2 = (i >> 1) & 1; 10 | bool S3 = (i >> 2) & 1, S4 = (i >> 3) & 1; 11 | if (S1 + S2 + S3 + S4 != 1) 12 | continue; 13 | bool A = S2, B = S4, C = !S3, D = !S4; 14 | if (A + B + C + D == 1) { 15 | bool car[4] = {S1, S2, S3, S4}; 16 | bool expert[4] = {A, B, C, D}; 17 | for (int i = 0; i < 4; ++i) 18 | if (car[i]) 19 | cout << i + 1 << endl; 20 | for (int i = 0; i < 4; ++i) 21 | if (expert[i]) 22 | cout << char('A' + i) << endl; 23 | goto end; 24 | } 25 | } 26 | end: 27 | return 0; 28 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/5-jumpwater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | /* 8 | * 常规做法参考四大湖 9 | * 这里给了一个非常规做法,不要求掌握 10 | */ 11 | 12 | int main() 13 | { 14 | int ranks[] = {1, 2, 3, 4, 5}; 15 | bool have_next = true; 16 | while (have_next) { 17 | int A = (ranks[1] == 2) + (ranks[0] == 3); 18 | int B = (ranks[1] == 2) + (ranks[4] == 4); 19 | int C = (ranks[2] == 1) + (ranks[3] == 2); 20 | int D = (ranks[2] == 5) + (ranks[3] == 3); 21 | int E = (ranks[4] == 4) + (ranks[0] == 1); 22 | int v[] = {A, B, C, D, E}; 23 | sort(v, v + 5); 24 | if (v[0] == 1 && v[4] == 1) { 25 | for (int i = 0; i < 5; ++i) 26 | cout << ranks[i] << endl; 27 | goto end; 28 | } 29 | have_next = next_permutation(ranks, ranks + 5); 30 | } 31 | end: 32 | return 0; 33 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习3/pics/1.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习3/pics/2.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习3/pics/3.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习3/pics/4.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习3/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习3/pics/5.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/1-dif_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int nums[100]; 7 | int n; 8 | cin >> n; 9 | for (int i = 0; i < n; ++i) 10 | cin >> nums[i]; 11 | int res = 0; 12 | int maxn = nums[0]; 13 | for (int i = 1; i < n; ++i) 14 | maxn = max(maxn, nums[i]); 15 | for (int i = 0; i < n; ++i) 16 | res += maxn == nums[i] ? 0 : nums[i]; 17 | cout << res << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/2-mat_add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int a[100][100], tmp; 10 | int n; 11 | cin >> n; 12 | for (int i = 0; i < n; ++i) 13 | for (int j = 0; j < n; ++j) 14 | cin >> a[i][j]; 15 | for (int i = 0; i < n; ++i) 16 | for (int j = 0; j < n; ++j) { 17 | cin >> tmp; 18 | a[i][j] += tmp; 19 | } 20 | for (int i = 0; i < n; ++i) { 21 | for (int j = 0; j < n; ++j) 22 | if (j > 0) 23 | cout << " " << a[i][j]; 24 | else 25 | cout << a[i][j]; 26 | cout << endl; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/3-snake_fill.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool out(int n, int i, int j) 8 | { 9 | return i < 0 || j < 0 || i >= n || j >= n; 10 | } 11 | 12 | int main() 13 | { 14 | int mat[15][15]; 15 | int n; 16 | cin >> n; 17 | int i = 0, j = 0, cnt = 0; 18 | bool rightup = true; // moving direction 19 | while (cnt < n * n) { 20 | mat[i][j] = ++cnt; 21 | if (rightup) { 22 | if (out(n, i - 1, j + 1)) { // reach boudary of matrix 23 | if (out(n, i, j + 1)) // can not move to right, move down 24 | i++; 25 | else // can move to right 26 | j++; 27 | rightup = !rightup; // change direction if reach boudary 28 | } else { 29 | i--, j++; 30 | } 31 | } else { 32 | if (out(n, i + 1, j - 1)) { 33 | if (out(n, i + 1, j)) 34 | j++; 35 | else 36 | i++; 37 | rightup = !rightup; 38 | } else { 39 | i++, j--; 40 | } 41 | } 42 | } 43 | for (int i = 0; i < n; ++i) { 44 | for (int j = 0; j < n; ++j) 45 | if (j > 0) 46 | cout << " " << mat[i][j]; 47 | else 48 | cout << mat[i][j]; 49 | cout << endl; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/4-frog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int h, u, d; 10 | cin >> h >> u >> d; 11 | int res = 0, curh = 0; 12 | while (curh < h) { 13 | res++; 14 | curh += u; 15 | if (curh >= h) 16 | break; 17 | curh -= d; 18 | } 19 | cout << res << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/5-doctor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n, t[20]; 10 | cin >> n; 11 | for (int i = 0; i < n; ++i) 12 | cin >> t[i]; 13 | sort(t, t + n); // 排队 -> 排序 14 | for (int i = 0; i < n; ++i) // 8点前到达的人可以看作八点到达 15 | if (t[i] < 60) 16 | t[i] = 60; 17 | int T = 60, res = 0; 18 | for (int i = 0; i < n; ++i) { // T维护当前病人看完病的时间 19 | T = max(T, t[i]) + 10; 20 | if (T > 180) 21 | break; 22 | res++; 23 | } 24 | cout << res << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ![](pics/1.PNG) 4 | 5 | ```cpp 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int nums[100]; 12 | int n; 13 | cin >> n; 14 | for (int i = 0; i < n; ++i) 15 | cin >> nums[i]; 16 | int res = 0; 17 | int maxn = nums[0]; 18 | for (int i = 1; i < n; ++i) 19 | maxn = max(maxn, nums[i]); 20 | for (int i = 0; i < n; ++i) 21 | res += maxn == nums[i] ? 0 : nums[i]; 22 | cout << res << endl; 23 | return 0; 24 | } 25 | ``` 26 | 27 | 28 | 29 | 30 | 31 | ![](pics/2.PNG) 32 | 33 | ```cpp 34 | #include 35 | #include 36 | #include 37 | #include 38 | using namespace std; 39 | 40 | int main() 41 | { 42 | int a[100][100], tmp; 43 | int n; 44 | cin >> n; 45 | for (int i = 0; i < n; ++i) 46 | for (int j = 0; j < n; ++j) 47 | cin >> a[i][j]; 48 | for (int i = 0; i < n; ++i) 49 | for (int j = 0; j < n; ++j) { 50 | cin >> tmp; 51 | a[i][j] += tmp; 52 | } 53 | for (int i = 0; i < n; ++i) { 54 | for (int j = 0; j < n; ++j) 55 | if (j > 0) 56 | cout << " " << a[i][j]; 57 | else 58 | cout << a[i][j]; 59 | cout << endl; 60 | } 61 | return 0; 62 | } 63 | ``` 64 | 65 | 66 | 67 | 68 | 69 | ![](pics/3.PNG) 70 | 71 | ```cpp 72 | #include 73 | #include 74 | #include 75 | #include 76 | using namespace std; 77 | 78 | bool out(int n, int i, int j) 79 | { 80 | return i < 0 || j < 0 || i >= n || j >= n; 81 | } 82 | 83 | int main() 84 | { 85 | int mat[15][15]; 86 | int n; 87 | cin >> n; 88 | int i = 0, j = 0, cnt = 0; 89 | bool rightup = true; // moving direction 90 | while (cnt < n * n) { 91 | mat[i][j] = ++cnt; 92 | if (rightup) { 93 | if (out(n, i - 1, j + 1)) { // reach boudary of matrix 94 | if (out(n, i, j + 1)) // can not move to right, move down 95 | i++; 96 | else // can move to right 97 | j++; 98 | rightup = !rightup; // change direction if reach boudary 99 | } else { 100 | i--, j++; 101 | } 102 | } else { 103 | if (out(n, i + 1, j - 1)) { 104 | if (out(n, i + 1, j)) 105 | j++; 106 | else 107 | i++; 108 | rightup = !rightup; 109 | } else { 110 | i++, j--; 111 | } 112 | } 113 | } 114 | for (int i = 0; i < n; ++i) { 115 | for (int j = 0; j < n; ++j) 116 | if (j > 0) 117 | cout << " " << mat[i][j]; 118 | else 119 | cout << mat[i][j]; 120 | cout << endl; 121 | } 122 | return 0; 123 | } 124 | ``` 125 | 126 | 127 | 128 | 129 | 130 | ![](pics/4.PNG) 131 | 132 | ```cpp 133 | #include 134 | #include 135 | #include 136 | #include 137 | using namespace std; 138 | 139 | int main() 140 | { 141 | int h, u, d; 142 | cin >> h >> u >> d; 143 | int res = 0, curh = 0; 144 | while (curh < h) { 145 | res++; 146 | curh += u; 147 | if (curh >= h) 148 | break; 149 | curh -= d; 150 | } 151 | cout << res << endl; 152 | return 0; 153 | } 154 | ``` 155 | 156 | 157 | 158 | 159 | 160 | ![](pics/5.PNG) 161 | 162 | ```cpp 163 | #include 164 | #include 165 | #include 166 | #include 167 | using namespace std; 168 | 169 | int main() 170 | { 171 | int n, t[20]; 172 | cin >> n; 173 | for (int i = 0; i < n; ++i) 174 | cin >> t[i]; 175 | sort(t, t + n); // 排队 -> 排序 176 | for (int i = 0; i < n; ++i) // 8点前到达的人可以看作八点到达 177 | if (t[i] < 60) 178 | t[i] = 60; 179 | int T = 60, res = 0; 180 | for (int i = 0; i < n; ++i) { // T维护当前病人看完病的时间 181 | T = max(T, t[i]) + 10; 182 | if (T > 180) 183 | break; 184 | res++; 185 | } 186 | cout << res << endl; 187 | return 0; 188 | } 189 | ``` 190 | 191 | 192 | -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习4/pics/1.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习4/pics/2.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习4/pics/3.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习4/pics/4.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习4/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习4/pics/5.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/1-reverse_word.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int findend(char s[], int start, int maxlen) 7 | { 8 | int i = start; 9 | while (i < maxlen && s[i] <= 'z' && s[i] >= 'a' || s[i] <= 'Z' && s[i] >= 'A') 10 | i++; 11 | return i; 12 | } 13 | 14 | int main() 15 | { 16 | char s[1010]; 17 | cin.get(s, 110); 18 | int l = strlen(s); 19 | int i = 0; 20 | while (i < l) { 21 | int j = findend(s, i, l); // find a word [i, j) 22 | reverse(s + i, s + j); // reverse this word 23 | i = j; 24 | while (i < l && s[i] == ' ') 25 | i++; 26 | } 27 | reverse(s, s + l); // reverse the sentence 28 | cout << s << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/2-sort_char.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | char s[1010]; 9 | cin.get(s, 1000); 10 | int l = strlen(s); 11 | sort(s, s + l / 2); 12 | reverse(s, s + l); 13 | reverse(s, s + l / 2); 14 | cout << s << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/3-grades.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct STU { 7 | int id; 8 | int chinese; 9 | int math; 10 | int sum; 11 | } stus[100000]; 12 | 13 | int main() 14 | { 15 | int n; 16 | cin >> n; 17 | for (int i = 0; i < n; ++i) { 18 | cin >> stus[i].id >> stus[i].chinese >> stus[i].math; 19 | stus[i].sum = stus[i].chinese + stus[i].math; 20 | } 21 | for (int i = 0; i < 3; ++i) // execute bubble sort three times 22 | for (int j = n - i - 2; j >= 0; --j) 23 | if (stus[j].sum < stus[j + 1].sum) 24 | swap(stus[j], stus[j + 1]); 25 | for (int i = 0; i < 3; ++i) 26 | cout << stus[i].id << " " << stus[i].sum << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/4-palindrom.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool judge(char s[]) 6 | { 7 | int l = strlen(s); 8 | int i = 0, j = l - 1; 9 | while (i < j) { 10 | if (s[i] == s[j]) 11 | i++, j--; 12 | else 13 | return false; 14 | } 15 | return true; 16 | } 17 | 18 | int main() 19 | { 20 | char s[110]; 21 | cin >> s; 22 | cout << (judge(s) ? "yes" : "no") << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/5-count_words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char sent[10010]; 6 | char words[100][100]; 7 | int wordcnt[1000]; 8 | int wordidx = 0; 9 | const char puncs[20] = ",. ;:?!-()~"; 10 | 11 | bool punc(char c) 12 | { 13 | int l = strlen(puncs); 14 | for (int i = 0; i < l; ++i) 15 | if (puncs[i] == c) 16 | return true; 17 | return false; 18 | } 19 | 20 | int extract(char sent[], int start, int maxlen) 21 | { 22 | char curword[100]; 23 | int idx = 0; 24 | while (start < maxlen && punc(sent[start])) // find start of word 25 | start++; 26 | if (start == maxlen) 27 | return maxlen; 28 | while (start < maxlen && !punc(sent[start])) // find end of word 29 | curword[idx++] = sent[start++]; 30 | curword[idx] = '\0'; 31 | bool same = false; // insert word to word list 32 | for (int i = 0; i < wordidx; ++i) 33 | if (strcmp(words[i], curword) == 0) { 34 | wordcnt[i]++; 35 | same = true; 36 | break; 37 | } 38 | if (!same) { 39 | strcpy(words[wordidx], curword); 40 | wordcnt[wordidx] += 1; 41 | wordidx++; 42 | } 43 | return start; 44 | } 45 | 46 | int main() 47 | { 48 | cin.getline(sent, 10000); 49 | int l = strlen(sent); 50 | int i = 0; 51 | while (i < l) 52 | i = extract(sent, i, l); // extract a word and update index i 53 | for (int i = 0; i < wordidx; ++i) 54 | cout << words[i] << ' ' << wordcnt[i] << endl; 55 | return 0; 56 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习5/pics/1.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习5/pics/2.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习5/pics/3.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习5/pics/4.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/pics/5-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习5/pics/5-1.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习5/pics/5-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习5/pics/5-2.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/1-Doctor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n, t[20]; 10 | cin >> n; 11 | for (int i = 0; i < n; ++i) 12 | cin >> t[i]; 13 | sort(t, t + n); // 排队 -> 排序 14 | for (int i = 0; i < n; ++i) // 8点前到达的人可以看作八点到达 15 | if (t[i] < 60) 16 | t[i] = 60; 17 | int T = 60, res = 0; 18 | for (int i = 0; i < n; ++i) { // T维护当前病人看完病的时间 19 | T = max(T, t[i]) + 10; 20 | if (T > 180) 21 | break; 22 | res++; 23 | } 24 | cout << res << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/2-Battery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n; 10 | int nums[1000]; 11 | ios::sync_with_stdio(false); 12 | while (cin >> n) { 13 | double sum = 0, res, maxn = -1; 14 | for (int i = 0; i < n; ++i) { 15 | cin >> nums[i]; 16 | maxn = max(maxn, 1.0 * nums[i]); 17 | sum += nums[i]; 18 | } 19 | if (maxn * 2 > sum) 20 | res = sum - maxn; 21 | else 22 | res = sum / 2; 23 | printf("%.1lf\n", res); 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/3-Date.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool leap(int year) 7 | { 8 | return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; 9 | } 10 | 11 | int caldays(int year, int month, int day) 12 | { 13 | int months[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 14 | int d = 0; 15 | for (int i = 0; i < year; ++i) 16 | if (leap(i)) 17 | d += 366; 18 | else 19 | d += 365; 20 | if (leap(year)) 21 | months[2] = 29; 22 | for (int i = 1; i < month; ++i) 23 | d += months[i]; 24 | d += day - 1; 25 | return d; 26 | } 27 | 28 | int main() 29 | { 30 | int year1, month1, day1; 31 | int year2, month2, day2; 32 | cin >> year1 >> month1 >> day1; 33 | cin >> year2 >> month2 >> day2; 34 | cout << caldays(year2, month2, day2) - caldays(year1, month1, day1) << endl; 35 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/4-Prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool nprime[200001]; 6 | 7 | int main() 8 | { 9 | int m, n; 10 | char c; 11 | cin >> m >> c >> n; 12 | nprime[0] = nprime[1] = true; 13 | for (int i = 2; i <= n; ++i) { 14 | if (nprime[i]) 15 | continue; 16 | for (int j = i * 2; j <= n; j += i) 17 | nprime[j] = true; 18 | } 19 | bool flag = false; 20 | for (int i = m; i < n; ++i) 21 | for (int j = i + 2; j <= n; ++j) { 22 | if (!nprime[i] && !nprime[j] && !nprime[j - i]) { 23 | printf("(%d,%d)\n", i, j); 24 | flag = true; 25 | } 26 | if (i != 2) 27 | break; 28 | } 29 | if (!flag) 30 | cout << "NULL" << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/5-Short.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isShort(char s1[], char s2[]) 7 | { 8 | int l1 = strlen(s1); 9 | int l2 = strlen(s2); 10 | if (l1 >= l2) 11 | return false; 12 | int i = 0, j = 0; 13 | while (i < l1) { 14 | while (j < l2 && s2[j] != s1[i]) 15 | j++; 16 | if (j == l2) 17 | return false; 18 | i++, j++; 19 | } 20 | return true; 21 | } 22 | 23 | int main() 24 | { 25 | int n; 26 | cin >> n; 27 | cin.get(); 28 | char s1[32], s2[320]; 29 | while (n--) { 30 | cin.getline(s1, 31, ':'); 31 | cin.getline(s2, 310); 32 | cout << (isShort(s1, s2) ? "YES" : "NO") << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/6-MaxDis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | char s[320], s1[20], s2[20]; 9 | cin.getline(s, 302, ','); 10 | cin.getline(s1, 12, ','); 11 | cin.getline(s2, 12); 12 | int l = strlen(s); 13 | int l1 = strlen(s1); 14 | int l2 = strlen(s1); 15 | char *find1 = strstr(s, s1); 16 | char *find2 = NULL; 17 | int j = l - 1; 18 | while (find2 == NULL && j >= 0) 19 | find2 = strstr(s + j--, s2); 20 | if (find1 == NULL || find2 == NULL) 21 | cout << -1 << endl; 22 | else { 23 | int res = find2 - find1 - l1; 24 | cout << ((res >= 0) ? res : -1) << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习6/pics/1.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习6/pics/2.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习6/pics/3.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习6/pics/4.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习6/pics/5.PNG -------------------------------------------------------------------------------- /第二阶段/第二阶段编程练习6/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第二阶段/第二阶段编程练习6/pics/6.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/1-sort3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | char s[100]; 10 | scanf("%s", s); 11 | int l = strlen(s); 12 | bool flag = false; 13 | for (int i = 0; i < l; ++i) 14 | if (s[i] == '.') 15 | flag = true; 16 | int integer[3] = { 0 }; 17 | double d[3] = { 0 }; 18 | if (!flag) 19 | sscanf(s, "%d,%d,%d", integer, integer + 1, integer + 2); 20 | else 21 | sscanf(s, "%lf,%lf,%lf", d, d + 1, d + 2); 22 | sort(integer, integer + 3); 23 | sort(d, d + 3); 24 | if (!flag) 25 | printf("%d,%d,%d\n", integer[0], integer[1], integer[2]); 26 | else 27 | for (int i = 0; i < 3; ++i) 28 | if (abs(round(d[i]) - d[i]) < 1e-8) 29 | printf("%.0f%c", d[i], "\n,"[i < 2]); 30 | else 31 | printf("%.2f%c", d[i], "\n,"[i < 2]); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/10-road.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int n, m; 6 | int edge1[10][10][10]; 7 | int edge2[10][10][10]; 8 | int edgecnt[10][10]; 9 | int pv[10][10][10]; 10 | bool visited[10]; 11 | int mincost = 1e8; 12 | 13 | void dfs(int depth, int cost, int cur) 14 | { 15 | if (cur == n - 1) { 16 | mincost = min(mincost, cost); 17 | return; 18 | } 19 | for (int i = 0; i < n; ++i) { 20 | if (visited[i]) 21 | continue; 22 | for (int j = 0; j < edgecnt[cur][i]; ++j) { 23 | int path = visited[pv[cur][i][j]] ? edge1[cur][i][j] : edge2[cur][i][j]; 24 | visited[i] = true; 25 | dfs(depth + 1, cost + path, i); 26 | visited[i] = false; 27 | } 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | cin >> n >> m; 34 | for (int i = 0; i < m; ++i) { 35 | int from, to; 36 | cin >> from >> to; 37 | from -= 1; 38 | to -= 1; 39 | cin >> pv[from][to][edgecnt[from][to]] >> edge1[from][to][edgecnt[from][to]] >> edge2[from][to][edgecnt[from][to]]; 40 | pv[from][to][edgecnt[from][to]] -= 1; 41 | edgecnt[from][to] += 1; 42 | } 43 | visited[0] = true; 44 | dfs(1, 0, 0); 45 | if (mincost == 1e8) 46 | cout << "impossible" << endl; 47 | else 48 | cout << mincost << endl; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/2-matmul.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int m, n, k; 8 | int a[100][100], b[100][100], c[100][100]; 9 | int t; 10 | cin >> t; 11 | while (t--) { 12 | cin >> m >> n >> k; 13 | for (int i = 0; i < m; ++i) 14 | for (int j = 0; j < n; ++j) 15 | cin >> a[i][j]; 16 | for (int i = 0; i < n; ++i) 17 | for (int j = 0; j < k; ++j) 18 | cin >> b[i][j]; 19 | memset(c, 0, sizeof(c)); 20 | for (int i = 0; i < m; ++i) 21 | for (int j = 0; j < k; ++j) 22 | for (int l = 0; l < n; ++l) 23 | c[i][j] += a[i][l] * b[l][j]; 24 | for (int i = 0; i < m; ++i) { 25 | for (int j = 0; j < k - 1; ++j) 26 | cout << c[i][j] << ' '; 27 | cout << c[i][k - 1] << endl; 28 | } 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/3-las.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int num[5010]; 7 | int length[5010][5010]; 8 | 9 | int main() 10 | { 11 | int n; 12 | cin >> n; 13 | for (int i = 0; i < n; ++i) 14 | cin >> num[i]; 15 | sort(num, num + n); 16 | int maxn = 0, v1, v2; 17 | for (int i = 1; i < n; ++i) { 18 | int p = 0; 19 | for (int j = 0; j < i; ++j) { 20 | length[j][i] = 2; 21 | while (p < j && num[p] + num[i] < 2 * num[j]) 22 | p++; 23 | if (num[p] + num[i] == 2 * num[j]) 24 | length[j][i] = length[p][j] + 1; 25 | if (length[j][i] > maxn || 26 | length[j][i] == maxn && num[i] - num[j] >= num[v1] - num[v2]) { 27 | maxn = length[j][i]; 28 | v1 = i; 29 | v2 = j; 30 | } 31 | } 32 | } 33 | int i = 0, j = 0; 34 | while (true) { 35 | while (j < n && num[j] == num[i]) 36 | j++; 37 | if (j - i > maxn) { 38 | maxn = j - i; 39 | v1 = v2 = i; 40 | } 41 | if (j == n) 42 | break; 43 | i = j; 44 | } 45 | if (maxn < 3) { 46 | cout << "NO" << endl; 47 | } else { 48 | int val1 = num[v1]; 49 | int dif = val1 - num[v2]; 50 | cout << val1 - dif * (maxn - 1); 51 | for (int i = 0; i < maxn - 1; ++i) 52 | cout << ',' << val1 - dif * (maxn - 2 - i); 53 | cout << endl; 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/4-basek.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void addone(char s[], int k) 7 | { 8 | int l = strlen(s); 9 | reverse(s, s + l); 10 | s[l] = '0'; 11 | s[0] += 1; 12 | for (int i = 0; i < l; ++i) { 13 | s[i + 1] += (s[i] - '0') / k; 14 | s[i] = (s[i] - '0') % k + '0'; 15 | } 16 | if (s[l] != '0') 17 | l++; 18 | s[l] = '\0'; 19 | reverse(s, s + l); 20 | return; 21 | } 22 | 23 | int main() 24 | { 25 | int k, m; 26 | char s[1010], c; 27 | cin >> k >> c >> m >> c >> s; 28 | for (int i = 0; i < m; ++i) { 29 | addone(s, k); 30 | cout << s; 31 | if (i != m - 1) 32 | cout << (i % 5 == 4 ? '\n' : ','); 33 | } 34 | cout << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/5-ngram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char s[510]; 7 | char words[500][10]; 8 | int words_idx; 9 | char tmp[10]; 10 | int cnts[500]; 11 | 12 | int main() 13 | { 14 | int k; 15 | cin >> k >> s; 16 | int l = strlen(s); 17 | for (int i = 0; i < l - k + 1; ++i) { 18 | char saved = s[i + k]; 19 | s[i + k] = '\0'; 20 | strcpy(tmp, s + i); 21 | s[i + k] = saved; 22 | bool flag = false; 23 | for (int i = 0; i < words_idx; ++i) { 24 | if (strcmp(words[i], tmp) == 0) { 25 | cnts[i]++; 26 | flag = true; 27 | break; 28 | } 29 | } 30 | if (!flag) 31 | strcpy(words[words_idx++], tmp); 32 | } 33 | int maxn = 0; 34 | for (int i = 0; i < words_idx; ++i) 35 | maxn = max(maxn, cnts[i]); 36 | if (maxn == 0) { 37 | cout << "NO" << endl; 38 | return 0; 39 | } 40 | cout << maxn + 1 << endl; 41 | for (int i = 0; i < words_idx; ++i) 42 | if (cnts[i] == maxn) 43 | cout << words[i] << endl; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/6-peanut.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Peanut { 6 | int x, y; 7 | int value; 8 | } ps[400]; 9 | 10 | bool cmp(const Peanut &a, const Peanut &b) 11 | { 12 | return a.value > b.value || 13 | a.value == b.value && a.x > b.y || 14 | a.value == b.value && a.x == b.x && a.y > b.y; 15 | } 16 | 17 | int main() 18 | { 19 | int k; 20 | cin >> k; 21 | while (k--) { 22 | int m, n, t, idx = 0; 23 | cin >> m >> n >> t; 24 | for (int i = 0; i < m; ++i) 25 | for (int j = 0; j < n; ++j) { 26 | int tmp; 27 | cin >> tmp; 28 | if (tmp > 0) { 29 | ps[idx].value = tmp; 30 | ps[idx].x = i; 31 | ps[idx++].y = j; 32 | } 33 | } 34 | sort(ps, ps + idx, cmp); 35 | int res = 0, curx = 0, cury = 0, curidx = 0; 36 | while (t > 0 && curidx < idx) { 37 | int x = ps[curidx].x; 38 | int y = ps[curidx].y; 39 | if (res == 0) { 40 | if (t >= 2 * x + 3) { 41 | res += ps[curidx].value; 42 | curx = x; 43 | cury = y; 44 | t -= x + 2; 45 | curidx += 1; 46 | } else { 47 | break; 48 | } 49 | } else { 50 | int movcost = abs(x - curx) + abs(y - cury); 51 | if (t >= movcost + 1 + x + 1) { 52 | res += ps[curidx].value; 53 | curx = x; 54 | cury = y; 55 | t -= movcost + 1; 56 | curidx += 1; 57 | } else { 58 | break; 59 | } 60 | } 61 | } 62 | cout << res << endl; 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/7-cheatcheck.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void strip(char *s) 6 | { 7 | int l = strlen(s); 8 | int idx = 0; 9 | for (int i = 0; i <= l; ++i) 10 | if (s[i] != ' ') 11 | s[idx++] = s[i]; 12 | } 13 | 14 | void CNT(char *s, int &cnt1, int &cnt2) 15 | { 16 | strip(s); 17 | int l = strlen(s); 18 | const char word[4][10] = { "if", "switch", "while", "for" }; 19 | for (int i = 0; i < l; ++i) { 20 | for (int j = 0; j < 4; ++j) { 21 | char saved = s[i + strlen(word[j])]; 22 | s[i + strlen(word[j])] = '\0'; 23 | if (strcmp(word[j], s + i) == 0 && saved == '(') { 24 | if (i < 2) 25 | cnt1 += 1; 26 | else 27 | cnt2 += 1; 28 | } 29 | s[i + strlen(word[j])] = saved; 30 | } 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | char s[1010]; 37 | int n1, n2, cnt11 = 0, cnt12 = 0, cnt21 = 0, cnt22 = 0; 38 | cin >> n1; 39 | cin.get(); 40 | while (n1--) { 41 | cin.getline(s, 1000); 42 | CNT(s, cnt11, cnt12); 43 | } 44 | cin >> n2; 45 | cin.get(); 46 | while (n2--) { 47 | cin.getline(s, 1000); 48 | CNT(s, cnt21, cnt22); 49 | } 50 | bool res = cnt11 == cnt21 && cnt12 == cnt22; 51 | cout << (res ? "Yes" : "No") << endl; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/8-wpassword.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char s[110]; 8 | char s1[100], s2[100], s3[100]; 9 | int a, b, c; 10 | while (cin >> a >> b >> c && a) { 11 | cin.get(); 12 | cin.getline(s, 100); 13 | int idx1 = 0, idx2 = 0, idx3 = 0; 14 | int l = strlen(s); 15 | for (int i = 0; i < l; ++i) { 16 | if (s[i] >= 'a' && s[i] <= 'i') { 17 | s1[idx1++] = s[i]; 18 | } else if (s[i] >= 'j' && s[i] <= 'r') { 19 | s2[idx2++] = s[i]; 20 | } else { 21 | s3[idx3++] = s[i]; 22 | } 23 | } 24 | int idx21 = 0, idx22 = 0, idx23 = 0; 25 | for (int i = 0; i < l; ++i) { 26 | if (s[i] >= 'a' && s[i] <= 'i') { 27 | s[i] = s1[(idx21++ - a + 100000 * idx1) % idx1]; 28 | } else if (s[i] >= 'j' && s[i] <= 'r') { 29 | s[i] = s2[(idx22++ - b + 100000 * idx2) % idx2]; 30 | } else { 31 | s[i] = s3[(idx23++ - c + 100000 * idx3) % idx3]; 32 | } 33 | } 34 | cout << s << endl; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/9-maxprod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | int nums[100]; 9 | int i = 0; 10 | while (n >= i + 2) { 11 | nums[i] = i + 2; 12 | n -= i + 2; 13 | i++; 14 | } 15 | while (n > 0) { 16 | for (int j = i - 1; n > 0 && j >= 0; j--, n--) 17 | nums[j] += 1; 18 | } 19 | for (int j = 0; j < i - 1; ++j) 20 | cout << nums[j] << ' '; 21 | cout << nums[i - 1] << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/10.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/10.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/3.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/4.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/5.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/6-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/6-1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/6-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/6-2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/7.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/8-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/8-1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/8-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/8-2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习1/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习1/pics/9.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/2-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/2-1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/2-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/2-2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/3.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/4.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/5-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/5-1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/5-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/5-2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/6.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习2/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习2/pics/7.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习3/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习3/pics/1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习3/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习3/pics/2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习3/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习3/pics/3.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习3/pics/4-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习3/pics/4-1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习3/pics/4-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习3/pics/4-2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习3/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习3/pics/5.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习4/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习4/pics/1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习4/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习4/pics/2.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习4/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习4/pics/3.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习4/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习4/pics/4.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习4/pics/5-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习4/pics/5-1.PNG -------------------------------------------------------------------------------- /第四阶段/第四阶段编程练习4/pics/5-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/第四阶段/第四阶段编程练习4/pics/5-2.PNG -------------------------------------------------------------------------------- /编程基础练习/1-Leap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin >> n; 10 | cout << (((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) ? "Y" : "N") << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /编程基础练习/10-TypeSetting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char s[1000][100]; 6 | 7 | int main() 8 | { 9 | int n; 10 | cin >> n; 11 | for (int i = 0; i < n; ++i) 12 | cin >> s[i]; 13 | int curidx = 0; 14 | for (int i = 0; i < n; ++i) { 15 | int l = strlen(s[i]); 16 | if (curidx + l + 1 <= 80) { 17 | if (curidx != 0) { 18 | cout << ' '; 19 | curidx += 1; 20 | } 21 | cout << s[i]; 22 | curidx += l; 23 | } else { 24 | cout << endl << s[i]; 25 | curidx = l; 26 | } 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /编程基础练习/2-ReverseWord.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int findend(char s[], int start, int maxlen) 7 | { 8 | int i = start; 9 | while (i < maxlen && s[i] != ' ') 10 | i++; 11 | return i; 12 | } 13 | 14 | int main() 15 | { 16 | char s[1010]; 17 | cin.get(s, 110); 18 | int l = strlen(s); 19 | int i = 0; 20 | while (i < l) { 21 | int j = findend(s, i, l); // find a word [i, j) 22 | reverse(s + i, s + j); // reverse this word 23 | i = j; 24 | while (i < l && s[i] == ' ') 25 | i++; 26 | } // reverse the sentence 27 | cout << s << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /编程基础练习/3-Merge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int a[10], b[10], c[20]; 9 | for (int i = 0; i < 10; ++i) 10 | cin >> a[i]; 11 | for (int i = 0; i < 10; ++i) 12 | cin >> b[i]; 13 | int i = 0, j = 0, k = 0; 14 | while (i < 10 && j < 10) { 15 | if (a[i] < b[j]) 16 | c[k++] = a[i++]; 17 | else 18 | c[k++] = b[j++]; 19 | } 20 | while (i < 10) 21 | c[k++] = a[i++]; 22 | while (j < 10) 23 | c[k++] = b[j++]; 24 | cout << c[0]; 25 | for (int i = 1; i < 20; ++i) 26 | cout << ' ' << c[i]; 27 | cout << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /编程基础练习/4-BaseCal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char chs[] = "0123456789abcdef"; 6 | 7 | int convert(char num[], int base) 8 | { 9 | int res = 0, i = 0; 10 | while (num[i] != '\0') { 11 | char c = num[i]; 12 | int tmp = (c >= '0' && c <= '9') ? c - '0' : c - 'a' + 10; 13 | res = res * base + tmp; 14 | i++; 15 | } 16 | return res; 17 | } 18 | 19 | int main() 20 | { 21 | char num1[1000], num2[1000]; 22 | int base1, base2, base3; 23 | cin >> base1 >> num1; 24 | cin >> base2 >> num2; 25 | cin >> base3; 26 | int sum = convert(num1, base1) + convert(num2, base2); 27 | int i = 0; 28 | while (sum) { 29 | num1[i++] = chs[sum % base3]; 30 | sum /= base3; 31 | } 32 | num1[i] = '\0'; 33 | reverse(num1, num1 + i); 34 | cout << num1 << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /编程基础练习/5-AmicNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXN 10000 5 | bool nprime[MAXN +10]; 6 | bool factor1[MAXN], factor2[MAXN]; 7 | 8 | int main() 9 | { 10 | int a, b; 11 | char c; 12 | cin >> a >> c >> b; 13 | nprime[0] = nprime[1] = true; 14 | for (int i = 2; i < MAXN; ++i) { 15 | if (nprime[i]) 16 | continue; 17 | for (int j = i * 2; j < MAXN; j += i) 18 | nprime[j] = true; 19 | } 20 | for (int i = 2; i * i <= a; ++i) 21 | if (!nprime[i] && a % i == 0) { 22 | factor1[i] = true; 23 | while (a % i == 0) 24 | a /= i; 25 | } 26 | if (!nprime[a]) 27 | factor1[a] = true; 28 | for (int i = 2; i * i <= b; ++i) 29 | if (!nprime[i] && b % i == 0) { 30 | factor2[i] = true; 31 | while (b % i == 0) 32 | b /= i; 33 | } 34 | if (!nprime[b]) 35 | factor2[b] = true; 36 | bool flag = true; 37 | for (int i = 2; i <= max(a, b); ++i) { 38 | if (factor1[i] != factor2[i]) { 39 | flag = false; 40 | break; 41 | } 42 | } 43 | cout << (flag ? "YES" : "NO") << endl; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /编程基础练习/6-Doctor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num[100]; 7 | int n; 8 | cin >> n; 9 | for (int i = 0; i < n; ++i) 10 | cin >> num[i]; 11 | int minn = num[0]; 12 | for (int i = 1; i < n; ++i) 13 | minn = min(minn, num[i]); 14 | for (int i = 0; i < n; ++i) 15 | if (num[i] == minn) { 16 | cout << i + 1 << endl; 17 | break; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /编程基础练习/7-Activity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Interval { 7 | int start; 8 | int end; 9 | } intervals[100000]; 10 | 11 | bool cmp(const Interval &a, const Interval &b) 12 | { 13 | return a.end < b.end || a.end == b.end && a.start < b.start; 14 | } 15 | 16 | int main() 17 | { 18 | int n; 19 | ios::sync_with_stdio(false); 20 | cin >> n; 21 | for (int i = 0; i < n; ++i) 22 | cin >> intervals[i].start >> intervals[i].end; 23 | sort(intervals, intervals + n, cmp); 24 | int curend = 0, res = 0; 25 | for (int i = 0; i < n; ++i) { 26 | if (intervals[i].start < curend) 27 | continue; 28 | res++; 29 | curend = intervals[i].end; 30 | } 31 | cout << res << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /编程基础练习/8-CountWord.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char sent[10010]; 6 | char words[100][100]; 7 | int wordcnt[1000]; 8 | int wordidx = 0; 9 | const char puncs[20] = ",. ;:?!-()~"; 10 | 11 | bool punc(char c) 12 | { 13 | int l = strlen(puncs); 14 | for (int i = 0; i < l; ++i) 15 | if (puncs[i] == c) 16 | return true; 17 | return false; 18 | } 19 | 20 | int extract(char sent[], int start, int maxlen) 21 | { 22 | char curword[100]; 23 | int idx = 0; 24 | while (start < maxlen && punc(sent[start])) // find start of word 25 | start++; 26 | if (start == maxlen) 27 | return maxlen; 28 | while (start < maxlen && !punc(sent[start])) // find end of word 29 | curword[idx++] = sent[start++]; 30 | curword[idx] = '\0'; 31 | bool same = false; // insert word to word list 32 | for (int i = 0; i < wordidx; ++i) 33 | if (strcmp(words[i], curword) == 0) { 34 | wordcnt[i]++; 35 | same = true; 36 | break; 37 | } 38 | if (!same) { 39 | strcpy(words[wordidx], curword); 40 | wordcnt[wordidx] += 1; 41 | wordidx++; 42 | } 43 | return start; 44 | } 45 | 46 | int main() 47 | { 48 | cin.getline(sent, 10000); 49 | int l = strlen(sent); 50 | int i = 0; 51 | while (i < l) 52 | i = extract(sent, i, l); // extract a word and update index i 53 | for (int i = 0; i < wordidx; ++i) 54 | cout << words[i] << ' ' << wordcnt[i] << endl; 55 | return 0; 56 | } -------------------------------------------------------------------------------- /编程基础练习/9-Substr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char s1[60], s2[60]; 8 | cin >> s1 >> s2; 9 | int l1 = strlen(s1); 10 | int l2 = strlen(s2); 11 | for (int i = 0; i < l2 - l1 + 1; ++i) { 12 | int cnt = 0; 13 | for (int j = 0; j < l1; ++j) 14 | cnt += s1[j] == s2[j + i]; 15 | if (cnt == l1) { 16 | cout << i << endl; 17 | break; 18 | } 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /编程基础练习/pics/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/1.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/10.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/10.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/2.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/3.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/4.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/5.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/6.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/7.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/8-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/8-1.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/8-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/8-2.PNG -------------------------------------------------------------------------------- /编程基础练习/pics/9.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lizhmq/Introduction2Computing-Hw/d86c0c4bc4db59ec5970718b7c10ae820b681a30/编程基础练习/pics/9.PNG --------------------------------------------------------------------------------