├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── generate-README.yml ├── .gitignore ├── .vscode ├── build.bat └── tasks.json ├── Data Structures and Algorithms (English) ├── 6-10_Strongly Connected Components (30).c ├── 6-11_Shortest Path [1] (25).c ├── 6-12_Shortest Path [2] (25).c ├── 6-13_Topological Sort (25).c ├── 6-14_Count Connected Components (20).c ├── 6-15_Iterative Mergesort (25).c ├── 6-16_Shortest Path [3] (25).c ├── 6-17_Shortest Path [4] (25).c ├── 6-1_Deque (25).c ├── 6-2_Two Stacks In One Array (20).c ├── 6-3_Add Two Polynomials (20).c ├── 6-4_Reverse Linked List (20).c ├── 6-5_Evaluate Postfix Expression (25).c ├── 6-6_Level-order Traversal (25).c ├── 6-7_Isomorphic (20).c ├── 6-8_Percolate Up and Down (20).c ├── 6-9_Sort Three Distinct Keys (20).c ├── 7-10_Saving James Bond - Easy Version (25).py ├── 7-10_Saving James Bond - Hard Version (30).py ├── 7-12_How Long Does It Take (25).py ├── 7-13_Insert or Merge (25).cpp ├── 7-14_Insertion or Heap Sort (25).cpp ├── 7-15_PAT Judge (25).cpp ├── 7-16_Sort with Swap(0, i) (25).cpp ├── 7-17_Hashing (25).cpp ├── 7-18_Hashing - Hard Version (30).py ├── 7-19_Battle Over Cities - Hard Version (35).cpp ├── 7-1_Maximum Subsequence Sum (25).py ├── 7-2_Reversing Linked List (25).cpp ├── 7-3_Pop Sequence (25).cpp ├── 7-4_List Leaves (25).py ├── 7-5_Tree Traversals Again (25).cpp ├── 7-6_Root of AVL Tree (25).cpp ├── 7-7_Complete Binary Search Tree (30).cpp ├── 7-8_File Transfer (25).py └── 7-9_Huffman Codes (30).py ├── GenerateREADME.py ├── PAT (Advanced Level) Practice ├── 1001_A+B Format (20).cpp ├── 1002_A+B for Polynomials (25).cpp ├── 1003_Emergency (25).cpp ├── 1004_Counting Leaves (30).cpp ├── 1005_Spell It Right (20).cpp ├── 1006_Sign In and Sign Out (25).cpp ├── 1007_Maximum Subsequence Sum (25).cpp ├── 1008_Elevator (20).cpp ├── 1009_Product of Polynomials (25).cpp ├── 1010_Radix (25).cpp ├── 1011_World Cup Betting (20).cpp ├── 1012_The Best Rank (25).cpp ├── 1013_Battle Over Cities (25).cpp ├── 1014_Waiting in Line (30).cpp ├── 1015_Reversible Primes (20).cpp ├── 1016_Phone Bills (25).cpp ├── 1017_Queueing at Bank (25).cpp ├── 1018_Public Bike Management (30).cpp ├── 1019_General Palindromic Number (20).cpp ├── 1020_Tree Traversals (25).cpp ├── 1021_Deepest Root (25).cpp ├── 1022_Digital Library (30).cpp ├── 1023_Have Fun with Numbers (20).cpp ├── 1024_Palindromic Number (25).java ├── 1025_PAT Ranking (25).cpp ├── 1026_Table Tennis (30).cpp ├── 1027_Colors in Mars (20).cpp ├── 1028_List Sorting (25).cpp ├── 1029_Median (25).cpp ├── 1030_Travel Plan (30).cpp ├── 1031_Hello World for U (20).cpp ├── 1032_Sharing (25).cpp ├── 1033_To Fill or Not to Fill (25).cpp ├── 1034_Head of a Gang (30).cpp ├── 1035_Password (20).cpp ├── 1036_Boys vs Girls (25).cpp ├── 1037_Magic Coupon (25).cpp ├── 1038_Recover the Smallest Number (30).cpp ├── 1039_Course List for Student (25).cpp ├── 1040_Longest Symmetric String (25).cpp ├── 1041_Be Unique (20).cpp ├── 1042_Shuffling Machine (20).cpp ├── 1043_Is It a Binary Search Tree (25).cpp ├── 1044_Shopping in Mars (25).cpp ├── 1045_Favorite Color Stripe (30).cpp ├── 1046_Shortest Distance (20).cpp ├── 1047_Student List for Course (25).cpp ├── 1048_Find Coins (25).cpp ├── 1049_Counting Ones (30).cpp ├── 1050_String Subtraction (20).cpp ├── 1051_Pop Sequence (25).cpp ├── 1052_Linked List Sorting (25).cpp ├── 1053_Path of Equal Weight (30).cpp ├── 1054_The Dominant Color (20).cpp ├── 1055_The World's Richest (25).cpp ├── 1056_Mice and Rice (25).cpp ├── 1057_Stack (30).cpp ├── 1058_A+B in Hogwarts (20).cpp ├── 1059_Prime Factors (25).cpp ├── 1060_Are They Equal (25).cpp ├── 1061_Dating (20).cpp ├── 1062_Talent and Virtue (25).cpp ├── 1063_Set Similarity (25).cpp ├── 1064_Complete Binary Search Tree (30).cpp ├── 1065_A+B and C (64bit) (20).cpp ├── 1066_Root of AVL Tree (25).cpp ├── 1067_Sort with Swap(0,) (25).cpp ├── 1068_Find More Coins (30).cpp ├── 1069_The Black Hole of Numbers (20).cpp ├── 1070_Mooncake (25).cpp ├── 1071_Speech Patterns (25).cpp ├── 1072_Gas Station (30).cpp ├── 1073_Scientific Notation (20).cpp ├── 1074_Reversing Linked List (25).cpp ├── 1075_PAT Judge (25).cpp ├── 1076_Forwards on Weibo (30).cpp ├── 1077_Kuchiguse (20).cpp ├── 1078_Hashing (25).cpp ├── 1079_Total Sales of Supply Chain (25).cpp ├── 1080_Graduate Admission (30).cpp ├── 1081_Rational Sum (20).cpp ├── 1082_Read Number in Chinese (25).cpp ├── 1083_List Grades (25).cpp ├── 1084_Broken Keyboard (20).py ├── 1085_Perfect Sequence (25).cpp ├── 1086_Tree Traversals Again (25).cpp ├── 1087_All Roads Lead to Rome (30).cpp ├── 1088_Rational Arithmetic (20).cpp ├── 1089_Insert or Merge (25).cpp ├── 1090_Highest Price in Supply Chain (25).cpp ├── 1090_Highest Price in Supply Chain (25).py ├── 1091_Acute Stroke (30).cpp ├── 1092_To Buy or Not to Buy (20).cpp ├── 1093_Count PAT's (25).cpp ├── 1094_The Largest Generation (25).cpp ├── 1095_Cars on Campus (30).cpp ├── 1096_Consecutive Factors (20).cpp ├── 1097_Deduplication on a Linked List (25).cpp ├── 1098_Insertion or Heap Sort (25).cpp ├── 1099_Build A Binary Search Tree (30).cpp ├── 1100_Mars Numbers (20).py ├── 1101_Quick Sort (25).cpp ├── 1102_Invert a Binary Tree (25).cpp ├── 1103_Integer Factorization (30).cpp ├── 1104_Sum of Number Segments (20).py ├── 1105_Spiral Matrix (25).cpp ├── 1106_Lowest Price in Supply Chain (25).cpp ├── 1107_Social Clusters (30).cpp ├── 1108_Find Average (20).cpp ├── 1109_Group Photo (25).cpp ├── 1110_Complete Binary Tree (25).cpp ├── 1111_Online Map (30).cpp ├── 1112_Stucked Keyboard (20).cpp ├── 1113_Integer Set Partition (25).cpp ├── 1114_Family Property (25).cpp ├── 1115_Counting Nodes in a BST (30).py ├── 1116_Come on! Let's C (20).py ├── 1117_Eddington Number(25).py ├── 1118_Birds in Forest (25).py ├── 1119_Pre- and Post-order Traversals (30).py ├── 1120_Friend Numbers (20).py ├── 1121_Damn Single (25).py ├── 1122_Hamiltonian Cycle (25).py ├── 1123_Is It a Complete AVL Tree (30).py ├── 1124_Raffle for Weibo Followers (20).py ├── 1125_Chain the Ropes (25).py ├── 1126_Eulerian Path (25).cpp ├── 1127_ZigZagging on a Tree (30).py ├── 1128_N Queens Puzzle (20).cpp ├── 1129_Recommendation System (25).cpp ├── 1130_Infix Expression (25).py ├── 1131_Subway Map (30).py ├── 1132_Cut Integer (20).py ├── 1133_Splitting A Linked List (25).cpp ├── 1134_Vertex Cover (25).py ├── 1135_Is It A Red-Black Tree (30).py ├── 1136_A Delayed Palindrome (20).py ├── 1137_Final Grading (25).cpp ├── 1138_Postorder Traversal (25).cpp ├── 1139_First Contact (30).cpp ├── 1140_Look-and-say Sequence (20).cpp ├── 1141_PAT Ranking of Institutions (25).cpp ├── 1142_Maximal Clique (25).cpp ├── 1143_Lowest Common Ancestor (30).cpp ├── 1144_The Missing Number (20).cpp ├── 1145_Hashing - Average Search Time (25).cpp ├── 1146_Topological Order (25).cpp ├── 1147_Heaps (30).cpp ├── 1148_Werewolf - Simple Version (20).cpp ├── 1149_Dangerous Goods Packaging (25).cpp ├── 1150_Travelling Salesman Problem (25).cpp ├── 1151_LCA in a Binary Tree (30).cpp ├── 1152_Google Recruitment (20).cpp ├── 1153_Decode Registration Card of PAT (25).cpp ├── 1154_Vertex Coloring (25).cpp ├── 1155_Heap Paths (30).cpp ├── 1156_Sexy Primes (20).py ├── 1157_Anniversary (25).py ├── 1158_Telefraud Detection (25).cpp ├── 1159_Structure of a Binary Tree (30).cpp ├── 1160_Forever (20).py ├── 1161_Merging Linked Lists (25).cpp ├── 1162_Postfix Expression (25).cpp ├── 1163_Dijkstra Sequence (30).cpp ├── 1164_Good in C (20).py ├── 1165_Block Reversing (25).cpp ├── 1166_Summit (25).cpp ├── 1167_Cartesian Tree (30).cpp ├── 1168_Prime Day (20).py ├── 1169_The Judger (25).cpp ├── 1170_Safari Park (25).cpp ├── 1171_Replacement Selection (30).cpp ├── 1172_Panda and PP Milk (20).py ├── 1173_How Many Ways to Buy a Piece of Land (25).cpp ├── 1174_Left-View of Binary Tree (25).cpp ├── 1175_Professional Ability Test (30).cpp ├── 1176_The Closest Fibonacci Number (25).py ├── 1177_Subsequence in Substring (25).cpp ├── 1178_File Path (25).cpp ├── 1178_File Path (25).py └── 1179_Chemical Equation (30).cpp ├── PAT (Basic Level) Practice (中文) ├── 1001_害死人不偿命的(3n+1)猜想 (15).cpp ├── 1002_写出这个数 (20).cpp ├── 1003_我要通过!(20).cpp ├── 1004_成绩排名 (20).cpp ├── 1005_继续(3n+1)猜想 (25).cpp ├── 1006_换个格式输出整数 (15).cpp ├── 1007_素数对猜想 (20).cpp ├── 1008_数组元素循环右移问题 (20).cpp ├── 1009_说反话 (20).cpp ├── 1010_一元多项式求导 (25).cpp ├── 1011_A+B和C (15).cpp ├── 1012_数字分类 (20).cpp ├── 1013_数素数 (20).cpp ├── 1014_福尔摩斯的约会 (20).cpp ├── 1015_德才论 (25).cpp ├── 1016_部分A+B (15).cpp ├── 1017_A除以B (20).java ├── 1018_锤子剪刀布 (20).cpp ├── 1019_数字黑洞 (20).cpp ├── 1020_月饼 (25).cpp ├── 1021_个位数统计 (15).cpp ├── 1022_D进制的A+B (20).cpp ├── 1023_组个最小数 (20).cpp ├── 1024_科学计数法 (20).cpp ├── 1025_反转链表 (25).cpp ├── 1026_程序运行时间 (15).py ├── 1027_打印沙漏 (20).py ├── 1028_人口普查 (20).cpp ├── 1029_旧键盘 (20).py ├── 1030_完美数列 (25).cpp ├── 1031_查验身份证 (15).cpp ├── 1032_挖掘机技术哪家强 (20).cpp ├── 1033_旧键盘打字 (20).cpp ├── 1034_有理数四则运算 (20).cpp ├── 1035_插入与归并 (25).cpp ├── 1036_跟奥巴马一起编程(15).cpp ├── 1037_在霍格沃茨找零钱(20).cpp ├── 1038_统计同成绩学生(20).cpp ├── 1039_到底买不买(20).cpp ├── 1040_有几个PAT(25).cpp ├── 1041_考试座位号(15).cpp ├── 1042_字符统计(20).cpp ├── 1043_输出PATest(20).cpp ├── 1044_火星数字(20).cpp ├── 1045_快速排序(25).cpp ├── 1046_划拳(15).py ├── 1047_编程团体赛(20).py ├── 1048_数字加密(20).py ├── 1049_数列的片段和(20).py ├── 1050_螺旋矩阵(25).cpp ├── 1051_复数乘法(15).cpp ├── 1052_卖个萌 (20).cpp ├── 1053_住房空置率(20).cpp ├── 1054_求平均值(20).cpp ├── 1055_集体照(25).cpp ├── 1056_组合数的和(15).py ├── 1057_数零壹(20).py ├── 1058_选择题(20).py ├── 1059_C语言竞赛(20).py ├── 1060_爱丁顿数(25).py ├── 1061_判断题(15).py ├── 1062_最简分数(20).py ├── 1063_计算谱半径(20).py ├── 1064_朋友数(20).py ├── 1065_单身狗(25).py ├── 1066_图像过滤(15).py ├── 1067_试密码(20).py ├── 1068_万绿丛中一点红(20).cpp ├── 1069_微博转发抽奖(20).py ├── 1070_结绳(25).py ├── 1071_小赌怡情(15).py ├── 1072_开学寄语(20).py ├── 1073_多选题常见计分法(20).py ├── 1074_宇宙无敌加法器(20).py ├── 1075_链表元素分类(25).cpp ├── 1076_Wifi密码 (15).py ├── 1077_互评成绩计算 (20).py ├── 1078_字符串压缩与解压 (20).py ├── 1079_延迟的回文数 (20).py ├── 1080_MOOC期终成绩 (25).cpp ├── 1081_检查密码 (15).cpp ├── 1082_射击比赛 (20).cpp ├── 1083_是否存在相等的差 (20).cpp ├── 1084_外观数列 (20).cpp ├── 1085_PAT单位排行 (25).cpp ├── 1086_就不告诉你 (15).cpp ├── 1087_有多少不同的值 (20).cpp ├── 1088_三人行 (20).cpp ├── 1089_狼人杀-简单版 (20).cpp ├── 1090_危险品装箱 (25).cpp ├── 1091_N-自守数 (15).cpp ├── 1092_最好吃的月饼 (20).cpp ├── 1093_字符串A+B (20).cpp ├── 1094_谷歌的招聘 (20).cpp ├── 1095_解码PAT准考证 (25).cpp ├── 1096_大美数 (25).py ├── 1097_矩阵行平移 (20).py ├── 1098_岩洞施工 (20).py ├── 1099_性感素数 (20).py ├── 1100_校庆 (25).py ├── 1101_B是A的多少倍 (15).py ├── 1102_教超冠军卷 (20).py ├── 1103_缘分数 (20).py ├── 1104_天长地久 (20).py ├── 1105_链表合并 (25).cpp ├── 1106_2019数列 (15).py ├── 1107_老鼠爱大米 (20).py ├── 1108_String复读机 (20).py ├── 1109_擅长C (20).py ├── 1110_区块反转 (25).cpp ├── 1111_对称日 (15).py ├── 1112_超标区间 (20).py ├── 1113_钱串子的加法 (20).py ├── 1114_全素日 (20).py ├── 1115_裁判机 (25).cpp ├── 1116_多二了一点 (15).py ├── 1117_数字之王 (20).py ├── 1118_如需挪车请致电 (20).py ├── 1119_胖达与盆盆奶 (20).py ├── 1120_买地攻略 (25).cpp ├── 1121_祖传好运 (15).py ├── 1122_找奇葩 (20).py ├── 1123_舍入 (20).py ├── 1124_最近的斐波那契数 (20).py └── 1125_子串与子列 (25).cpp ├── PAT (Top Level) Practice ├── 1001_Battle Over Cities - Hard Version (35).cpp ├── 1002_Business (35).cpp ├── 1003_Universal Travel Sites (35).cpp ├── 1004_To Buy or Not to Buy - Hard Version (35).cpp ├── 1005_Programming Pattern (35).cpp ├── 1006_Tree Traversals - Hard Version (35).py ├── 1007_Red-black Tree (35).cpp ├── 1008_Airline Routes (35).cpp ├── 1009_Triple Inversions (35).cpp ├── 1010_Lehmer Code (35).cpp ├── 1011_Cut Rectangles (35).py ├── 1012_Greedy Snake (35).cpp ├── 1013_Image Segmentation (35).py ├── 1014_Circles of Friends (35).cpp ├── 1015_Letter-moving Game (35).cpp ├── 1016_Uniqueness of MST (35).cpp ├── 1017_The Best Peak Shape (35).py ├── 1018_Subnumbers (35).py ├── 1019_Separate the Animals (35).cpp ├── 1020_Delete At Most Two Characters (35).cpp ├── 1021_Safe Fruit (35).cpp ├── 1022_Werewolf (35).cpp ├── 1023_The Best Polygon (35).cpp ├── 1024_Currency Exchange Centers (35).cpp ├── 1025_Keep at Most 100 Characters (35).cpp ├── 1026_String of Colorful Beads (35).cpp ├── 1027_Larry and Inversions (35).cpp ├── 1028_Do All Roads Lead to Rome (35).cpp ├── 1029_Array Cutting Score (35).cpp ├── 1030_Beautiful Subsequence (35).cpp ├── 1032_Fighting the Monsters (35).cpp ├── 1033_Strings of Red and Blue (35).cpp ├── 1034_Number Game (35).cpp └── 1035_Color the Tree (35).cpp ├── README.md ├── 《数据结构学习与实验指导》实验项目集 ├── 2-05_求集合数据的均方差.cpp ├── 2-06_数列求和.cpp ├── 2-07_素因子分解.cpp ├── 2-08_用扑克牌计算24点.cpp ├── 2-09_装箱问题模拟.cpp ├── 2-10_海盗分赃.cpp ├── 2-11_两个有序链表序列的合并.cpp ├── 2-12_两个有序链表序列的交集.cpp ├── 2-13_两个有序序列的中位数.cpp ├── 3-04_一元多项式的乘法与加法运算.cpp ├── 3-05_求链式线性表的倒数第K项.cpp ├── 3-06_表达式转换.cpp ├── 3-07_求前缀表达式.cpp ├── 3-08_堆栈模拟队列.cpp ├── 3-09_队列中的元素排序.cpp ├── 4-05_家谱处理.cpp ├── 4-06_搜索树判断.cpp ├── 4-07_修理牧场.cpp ├── 4-08_目录树.cpp ├── 4-09_笛卡尔树.cpp ├── 5-05_QQ帐户的申请与登陆.cpp ├── 5-06_航空公司VIP客户查询.cpp ├── 6-06_任务调度的合理性.cpp ├── 6-07_哈利'波特的考试.cpp ├── 6-08_城市间紧急救援.cpp ├── 6-09_社交网络图中结点的'重要性'计算.cpp ├── 6-10_关键活动.cpp ├── 7-05_魔法优惠券.cpp ├── 7-06_奥运排行榜.cpp ├── 7-07_PAT排名汇总.cpp ├── 8-04_打印学生选课清单.cpp └── 8-06_畅通工程之局部最小花费问题.cpp ├── 中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题 ├── 自测-1_打印沙漏 (20).py ├── 自测-2_素数对猜想 (20).py ├── 自测-3_数组元素循环右移问题 (20).py ├── 自测-4_Have Fun with Numbers (20).py └── 自测-5_Shuffling Machine (20).py ├── 团体程序设计天梯赛-练习集 ├── L1-001_Hello World.go ├── L1-001_Hello World.py ├── L1-002_打印沙漏.py ├── L1-003_个位数统计.py ├── L1-004_计算摄氏温度.go ├── L1-004_计算摄氏温度.py ├── L1-005_考试座位号.py ├── L1-006_连续因子.py ├── L1-007_念数字.py ├── L1-008_求整数段和.py ├── L1-009_N个数求和.py ├── L1-010_比较大小.py ├── L1-011_A-B.py ├── L1-012_计算指数.py ├── L1-013_计算阶乘和.py ├── L1-014_简单题.py ├── L1-015_跟奥巴马一起画方块.py ├── L1-016_查验身份证.py ├── L1-017_到底有多二.py ├── L1-018_大笨钟.py ├── L1-019_谁先倒.py ├── L1-020_帅到没朋友.py ├── L1-021_重要的话说三遍.py ├── L1-022_奇偶分家.py ├── L1-023_输出GPLT.py ├── L1-024_后天.py ├── L1-025_正整数A+B.py ├── L1-026_I Love GPLT.py ├── L1-027_出租.py ├── L1-028_判断素数.py ├── L1-029_是不是太胖了.py ├── L1-030_一帮一.py ├── L1-031_到底是不是太胖了.py ├── L1-032_Left-pad.py ├── L1-033_出生年.py ├── L1-034_点赞.py ├── L1-035_情人节.py ├── L1-036_A乘以B.py ├── L1-037_A除以B.py ├── L1-038_新世界.py ├── L1-039_古风排版.py ├── L1-040_最佳情侣身高差.py ├── L1-041_寻找250.py ├── L1-042_日期格式化.py ├── L1-043_阅览室.py ├── L1-044_稳赢.py ├── L1-045_宇宙无敌大招呼.py ├── L1-046_整除光棍.py ├── L1-047_装睡.py ├── L1-048_矩阵A乘以B.py ├── L1-049_天梯赛座位分配.py ├── L1-050_倒数第N个字符串.py ├── L1-051_打折.py ├── L1-052_2018我们要赢.py ├── L1-053_电子汪.py ├── L1-054_福到了.py ├── L1-055_谁是赢家.py ├── L1-056_猜数字.py ├── L1-057_PTA使我精神焕发.py ├── L1-058_6翻了.py ├── L1-059_敲笨钟.py ├── L1-060_心理阴影面积.py ├── L1-061_新胖子公式.py ├── L1-062_幸运彩票.py ├── L1-063_吃鱼还是吃肉.py ├── L1-064_估值一亿的AI核心代码.py ├── L1-065_嫑废话上代码.py ├── L1-066_猫是液体.py ├── L1-067_洛希极限.py ├── L1-068_调和平均.py ├── L1-069_胎压监测.py ├── L1-070_吃火锅.py ├── L1-071_前世档案.py ├── L1-072_刮刮彩票.py ├── L1-073_人与神.py ├── L1-074_两小时学完C语言.py ├── L1-075_强迫症.py ├── L1-076_降价提醒机器人.py ├── L1-077_大笨钟的心情.py ├── L1-078_吉老师的回归.py ├── L1-079_天梯赛的善良.py ├── L1-080_乘法口诀数列.py ├── L1-081_今天我要赢.py ├── L1-082_种钻石.py ├── L1-083_谁能进图书馆.py ├── L1-084_拯救外星人.py ├── L1-085_试试手气.py ├── L1-086_斯德哥尔摩火车上的题.py ├── L1-087_机工士姆斯塔迪奥.py ├── L1-088_静静的推荐.py ├── L1-089_最好的文档.go ├── L1-090_什么是机器学习.py ├── L1-091_程序员买包子.py ├── L1-092_进化论.py ├── L1-093_猜帽子游戏.py ├── L1-094_剪切粘贴.py ├── L1-095_分寝室.py ├── L1-096_谁管谁叫爹.py ├── L2-001_紧急救援.cpp ├── L2-002_链表去重.cpp ├── L2-003_月饼.cpp ├── L2-004_这是二叉搜索树吗?.cpp ├── L2-005_集合相似度.cpp ├── L2-006_树的遍历.cpp ├── L2-007_家庭房产.cpp ├── L2-008_最长对称子串.c ├── L2-009_抢红包.cpp ├── L2-010_排座位.cpp ├── L2-011_玩转二叉树.cpp ├── L2-012_关于堆的判断.cpp ├── L2-013_红色警报.cpp ├── L2-014_列车调度.cpp ├── L2-014_列车调度.go ├── L2-015_互评成绩.cpp ├── L2-016_愿天下有情人都是失散多年的兄妹.cpp ├── L2-017_人以群分.cpp ├── L2-018_多项式A除以B.cpp ├── L2-019_悄悄关注.cpp ├── L2-020_功夫传人.cpp ├── L2-021_点赞狂魔.cpp ├── L2-022_重排链表.cpp ├── L2-023_图着色问题.cpp ├── L2-024_部落.cpp ├── L2-025_分而治之.cpp ├── L2-026_小字辈.cpp ├── L2-027_名人堂与代金券.cpp ├── L2-028_秀恩爱分得快.cpp ├── L2-029_特立独行的幸福.cpp ├── L2-033_简单计算器.cpp └── L2-035_完全二叉树的层序遍历.cpp ├── 基础编程题目集 ├── 6-10_阶乘计算升级版 (20).c ├── 6-11_求自定类型元素序列的中位数 (25).c ├── 6-12_判断奇偶性 (10).c ├── 6-13_折半查找 (15).c ├── 6-1_简单输出整数 (10).c ├── 6-2_多项式求值 (20).c ├── 6-3_简单求和 (10).c ├── 6-4_求自定类型元素的平均 (10).c ├── 6-5_求自定类型元素的最大值 (10).c ├── 6-6_求单链表结点的阶乘和 (15).c ├── 6-7_统计某类完全平方数 (20).c ├── 6-8_简单阶乘计算 (10).c ├── 6-9_统计个位数字 (15).c ├── 7-10_计算工资 (15).py ├── 7-11_分段计算居民水费 (10).py ├── 7-12_两个数的简单计算器 (10).py ├── 7-13_日K蜡烛图 (15).py ├── 7-14_求整数段和 (15).py ├── 7-15_计算圆周率 (15).py ├── 7-16_求符合给定条件的整数集 (15).py ├── 7-17_爬动的蠕虫 (15).py ├── 7-18_二分法求多项式单根 (20).py ├── 7-19_支票面额 (15).py ├── 7-1_厘米换算英尺英寸 (15).py ├── 7-20_打印九九口诀表 (15).py ├── 7-21_求特殊方程的正整数解 (15).py ├── 7-22_龟兔赛跑 (20).py ├── 7-23_币值转换 (20).py ├── 7-24_约分最简分式 (15).py ├── 7-25_念数字 (15).py ├── 7-26_单词长度 (15).py ├── 7-27_冒泡法排序 (20).py ├── 7-28_猴子选大王 (20).py ├── 7-29_删除字符串中的子串 (20).py ├── 7-2_然后是几点 (15).py ├── 7-30_字符串的冒泡排序 (20).py ├── 7-31_字符串循环左移 (20).py ├── 7-32_说反话-加强版 (20).py ├── 7-33_有理数加法 (15).py ├── 7-34_通讯录的录入与显示 (10).py ├── 7-35_有理数均值 (20).py ├── 7-36_复数四则运算 (15).py ├── 7-37_整数分解为若干项之和 (20).py ├── 7-38_数列求和-加强版 (20).py ├── 7-3_逆序的三位数 (10).py ├── 7-4_BCD解密 (10).py ├── 7-5_表格输出 (5).py ├── 7-6_混合类型数据格式化输入 (5).py ├── 7-7_12-24小时制 (15).py ├── 7-8_超速判断 (10).py └── 7-9_用天平找小球 (10).py └── 数据结构与算法题目集(中文) ├── 6-10_二分查找 (20).c ├── 6-11_先序输出叶结点 (15).c ├── 6-12_二叉搜索树的操作集 (30).c ├── 6-1_单链表逆转 (20).c ├── 6-2_顺序表操作集 (20).c ├── 6-3_求链式表的表长 (10).c ├── 6-4_链式表的按序号查找 (10).c ├── 6-5_链式表操作集 (20).c ├── 6-6_带头结点的链式表操作集 (20).c ├── 6-7_在一个数组中实现两个堆栈 (20).c ├── 6-8_求二叉树高度 (20).c ├── 6-9_二叉树的遍历 (25).c ├── 7-10_公路村村通 (30).cpp ├── 7-11_关键活动 (30).cpp ├── 7-12_排序 (25).cpp ├── 7-13_统计工龄 (20).py ├── 7-14_电话聊天狂人 (25).cpp ├── 7-16_一元多项式求导 (20).cpp ├── 7-17_汉诺塔的非递归实现 (25).cpp ├── 7-18_银行业务队列简单模拟 (25).cpp ├── 7-19_求链式线性表的倒数第K项 (20).cpp ├── 7-1_最大子列和问题 (20).py ├── 7-21_求前缀表达式的值 (25).py ├── 7-22_堆栈模拟队列 (25).cpp ├── 7-23_还原二叉树 (25).py ├── 7-24_树种统计 (25).cpp ├── 7-25_朋友圈 (25).cpp ├── 7-26_Windows消息队列 (25).cpp ├── 7-28_搜索树判断 (25).cpp ├── 7-29_修理牧场 (25).cpp ├── 7-2_一元多项式的乘法与加法运算 (20).py ├── 7-30_目录树 (30).cpp ├── 7-31_笛卡尔树 (25).cpp ├── 7-32_哥尼斯堡的“七桥问题” (25).cpp ├── 7-33_地下迷宫探索 (30).cpp ├── 7-34_任务调度的合理性 (25).cpp ├── 7-35_城市间紧急救援 (25).cpp ├── 7-36_社交网络图中结点的“重要性”计算 (30).cpp ├── 7-37_模拟EXCEL排序 (25).cpp ├── 7-38_寻找大富翁 (25).cpp ├── 7-39_魔法优惠券 (25).cpp ├── 7-3_树的同构 (25).py ├── 7-40_奥运排行榜 (25).cpp ├── 7-41_PAT排名汇总 (25).cpp ├── 7-42_整型关键字的散列映射 (25).cpp ├── 7-43_字符串关键字的散列映射 (25).cpp ├── 7-44_基于词频的文件相似度 (30).py ├── 7-45_航空公司VIP客户查询 (25).cpp ├── 7-46_新浪微博热门话题 (30).cpp ├── 7-47_打印选课学生名单 (25).cpp ├── 7-48_银行排队问题之单窗口“夹塞”版 (30).cpp ├── 7-49_打印学生选课清单 (25).cpp ├── 7-4_是否同一棵二叉搜索树 (25).py ├── 7-50_畅通工程之局部最小花费问题 (35).cpp ├── 7-51_两个有序链表序列的合并 (20).cpp ├── 7-52_两个有序链表序列的交集 (25).cpp ├── 7-53_两个有序序列的中位数 (25).cpp ├── 7-5_堆中的路径 (25).py ├── 7-6_列出连通集 (25).py ├── 7-7_六度空间 (30) .cpp ├── 7-8_哈利·波特的考试 (25).cpp └── 7-9_旅游规划 (25).cpp /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | # all files 5 | [*] 6 | indent_style = tab 7 | indent_size = 2 8 | charset = utf-8 -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.github/workflows/generate-README.yml: -------------------------------------------------------------------------------- 1 | name: Generate README 2 | on: 3 | push: 4 | branches: 5 | - master 6 | 7 | jobs: 8 | build-and-deploy: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout 12 | run: git clone https://github.com/tiny656/PAT.git . 13 | 14 | - name: Setup python 15 | uses: actions/setup-python@v4 16 | with: 17 | python-version: '3.x' 18 | 19 | - name: Generate README 20 | run: python GenerateREADME.py 21 | 22 | - name: Push 23 | env: 24 | GH_TOKEN: ${{ secrets.ACTION_TOKEN }} 25 | GH_REF: github.com/tiny656/PAT.git 26 | run: | 27 | git config user.name "tiny656" #修改成自己的github用户名 28 | git config user.email "tiny656@hotmail.com" #修改成自己的GitHub邮箱 29 | git add README.md 30 | git diff-index --quiet HEAD || git commit -m "update README.md by Github Actions" --allow-empty && git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:master 31 | -------------------------------------------------------------------------------- /.vscode/build.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | pushd %cd% 3 | call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64 4 | popd 5 | echo %cd% 6 | set compilerflags=/Od /EHsc /Zi 7 | set linkerflags=/OUT:"%~n1.exe" 8 | cl.exe %compilerflags% %1 /link %linkerflags% -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-11_Shortest Path [1] (25).c: -------------------------------------------------------------------------------- 1 | void ShortestDist(LGraph Graph, int dist[], Vertex S) { 2 | // Initialize queue for BFS 3 | int queue[MaxVertexNum]; 4 | int front = 0, rear = 0; 5 | bool visited[MaxVertexNum] = {false}; 6 | 7 | // Initialize all distances to -1 (unreachable) 8 | for (int i = 0; i < Graph->Nv; i++) { 9 | dist[i] = -1; 10 | } 11 | 12 | // Set distance to source as 0 and enqueue 13 | dist[S] = 0; 14 | queue[rear++] = S; 15 | visited[S] = true; 16 | 17 | // BFS 18 | while (front < rear) { 19 | // Dequeue a vertex 20 | Vertex V = queue[front++]; 21 | 22 | // Traverse all adjacent vertices 23 | PtrToAdjVNode currentNode = Graph->G[V].FirstEdge; 24 | while (currentNode) { 25 | Vertex W = currentNode->AdjV; 26 | if (!visited[W]) { 27 | // Update distance and enqueue 28 | dist[W] = dist[V] + 1; 29 | queue[rear++] = W; 30 | visited[W] = true; 31 | } 32 | currentNode = currentNode->Next; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-14_Count Connected Components (20).c: -------------------------------------------------------------------------------- 1 | int CountConnectedComponents( LGraph Graph ) { 2 | int count = 0; 3 | bool visited[MaxVertexNum]; 4 | int queue[MaxVertexNum]; 5 | int front = 0, rear = 0; 6 | for (int i = 0; i < Graph->Nv; i++) visited[i] = false; 7 | for (int i = 0; i < Graph->Nv; i++) { 8 | if (visited[i] == true) continue; 9 | count++; 10 | visited[i] = true; 11 | queue[rear++] = i; 12 | while (front != rear) { 13 | int v = queue[front++]; 14 | PtrToAdjVNode p = Graph->G[v].FirstEdge; 15 | while (p) { 16 | if (visited[p->AdjV] == false) { 17 | visited[p->AdjV] = true; 18 | queue[rear++] = p->AdjV; 19 | } 20 | p = p->Next; 21 | } 22 | } 23 | } 24 | return count; 25 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-2_Two Stacks In One Array (20).c: -------------------------------------------------------------------------------- 1 | Stack CreateStack( int MaxElements ) { 2 | Stack stack = (Stack)malloc(sizeof(struct StackRecord)); 3 | stack->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElements); 4 | stack->Top1 = -1; 5 | stack->Top2 = MaxElements; 6 | stack->Capacity = MaxElements; 7 | return stack; 8 | } 9 | 10 | int IsEmpty( Stack S, int Stacknum ) { 11 | if (Stacknum == 1) return S->Top1 == -1; 12 | else if (Stacknum == 2) return S->Top2 == S->Capacity; 13 | } 14 | 15 | int IsFull( Stack S ) { 16 | if (S->Top1 + 1 == S->Top2) return 1; 17 | else return 0; 18 | } 19 | 20 | int Push( ElementType X, Stack S, int Stacknum ) { 21 | if (IsFull(S)) return 0; 22 | if (Stacknum == 1) S->Array[++S->Top1] = X; 23 | else if (Stacknum == 2) S->Array[--S->Top2] = X; 24 | return 1; 25 | 26 | } 27 | 28 | ElementType Top_Pop( Stack S, int Stacknum ) { 29 | if (IsEmpty(S, Stacknum)) return ERROR; 30 | if (Stacknum == 1) return S->Array[S->Top1--]; 31 | else if (Stacknum == 2) return S->Array[S->Top2++]; 32 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-3_Add Two Polynomials (20).c: -------------------------------------------------------------------------------- 1 | Polynomial Add( Polynomial a, Polynomial b ) { 2 | Polynomial head, cur; 3 | cur = head = a; 4 | a = a->Next; 5 | b = b->Next; 6 | while (a && b) { 7 | if (a->Exponent > b->Exponent) { 8 | cur->Next = a; 9 | a = a->Next; 10 | cur = cur->Next; 11 | } else if (a->Exponent < b->Exponent) { 12 | cur->Next = b; 13 | b = b->Next; 14 | cur = cur->Next; 15 | } else { 16 | if (a->Coefficient + b->Coefficient != 0) { 17 | cur->Next = a; 18 | a->Coefficient += b->Coefficient; 19 | cur = cur->Next; 20 | } 21 | a = a->Next; 22 | b = b->Next; 23 | } 24 | } 25 | if (a) cur->Next = a; 26 | else cur->Next = b; 27 | 28 | return head; 29 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-4_Reverse Linked List (20).c: -------------------------------------------------------------------------------- 1 | List Reverse( List L ) { 2 | if (!L || !L->Next) return L; 3 | Position p, q, r; 4 | p = L->Next; 5 | q = p->Next; 6 | p->Next = NULL; 7 | while (q) { 8 | r = q->Next; 9 | q->Next = p; 10 | p = q; 11 | q = r; 12 | } 13 | L->Next = p; 14 | return L; 15 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-6_Level-order Traversal (25).c: -------------------------------------------------------------------------------- 1 | void Level_order ( Tree T, void (*visit)(Tree ThisNode) ) { 2 | if (T == NULL) return; 3 | 4 | Tree queue[MaxTree+1]; 5 | int front = 0, rear = 0; 6 | queue[rear++] = T; 7 | while (rear != front) { 8 | T = queue[front++]; 9 | visit(T); 10 | if (T->Left != NULL) queue[rear++] = T->Left; 11 | if (T->Right != NULL) queue[rear++] = T->Right; 12 | } 13 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-7_Isomorphic (20).c: -------------------------------------------------------------------------------- 1 | int Isomorphic( Tree T1, Tree T2 ) { 2 | return 3 | (T1 == NULL && T2 == NULL) || ( 4 | T1 != NULL && T2 != NULL && 5 | T1->Element == T2->Element && 6 | ((Isomorphic(T1->Left, T2->Left) && Isomorphic(T1->Right, T2->Right)) 7 | || (Isomorphic(T1->Left, T2->Right) && Isomorphic(T1->Right, T2->Left)))); 8 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-8_Percolate Up and Down (20).c: -------------------------------------------------------------------------------- 1 | void PercolateUp( int p, PriorityQueue H ) { 2 | while (p > 1) { 3 | int parent = p / 2; 4 | if (H->Elements[p] < H->Elements[parent]) { 5 | ElementType tmp = H->Elements[p]; 6 | H->Elements[p] = H->Elements[parent]; 7 | H->Elements[parent] = tmp; 8 | p = parent; 9 | } else break; 10 | } 11 | } 12 | 13 | void PercolateDown( int p, PriorityQueue H ) { 14 | while (p * 2 <= H->Size) { 15 | int child = p * 2; 16 | if (child + 1 <= H->Size && H->Elements[child + 1] < H->Elements[child]) child++; 17 | if (H->Elements[p] > H->Elements[child]) { 18 | ElementType tmp = H->Elements[p]; 19 | H->Elements[p] = H->Elements[child]; 20 | H->Elements[child] = tmp; 21 | p = child; 22 | } else break; 23 | } 24 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/6-9_Sort Three Distinct Keys (20).c: -------------------------------------------------------------------------------- 1 | /* 2 | Suppose you have an array of N elements, containing three distinct keys, "true", "false", and "maybe". Given an O(N) algorithm to rearrange the list so that all "false" elements precede "maybe" elements, which in turn precede "true" elements. You may use only constant extra space. 3 | */ 4 | void MySort( ElementType A[], int N ) { 5 | int falseCount = 0, maybeCount = 0, trueCount = 0; 6 | for (int i = 0; i < N; i++) { 7 | if (A[i] == false) falseCount++; 8 | else if (A[i] == maybe) maybeCount++; 9 | else trueCount++; 10 | } 11 | for (int i = 0; i < falseCount; i++) A[i] = false; 12 | for (int i = falseCount; i < falseCount + maybeCount; i++) A[i] = maybe; 13 | for (int i = falseCount + maybeCount; i < N; i++) A[i] = true; 14 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/7-12_How Long Does It Take (25).py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, m = map(int, input().split()) 4 | adj = [[] for _ in range(n)] 5 | indeg = [0] * n 6 | for _ in range(m): 7 | s, e, l = map(int, input().split()) 8 | adj[s].append((e, l)) 9 | indeg[e] += 1 10 | 11 | # topological sort with max distance 12 | q = deque(i for i in range(n) if indeg[i] == 0) 13 | dist = [0] * n 14 | visited = 0 15 | while q: 16 | u = q.popleft() 17 | visited += 1 18 | for v, w in adj[u]: 19 | # 最长路径转移 20 | if dist[u] + w > dist[v]: 21 | dist[v] = dist[u] + w 22 | indeg[v] -= 1 23 | if indeg[v] == 0: 24 | q.append(v) 25 | 26 | if visited != n: # has cycle 27 | print("Impossible") 28 | else: 29 | print(max(dist)) 30 | -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/7-1_Maximum Subsequence Sum (25).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = list(map(int, input().split())) 3 | 4 | max_val = max(arr) 5 | if max_val < 0: 6 | print(0, arr[0], arr[-1]) 7 | elif max_val == 0: 8 | idx = arr.index(0) 9 | print(0, arr[idx], arr[idx]) 10 | else: 11 | curr, ans, start, l, r = 0, 0, 0, 0, 0 12 | for i, x in enumerate(arr): 13 | curr += x 14 | if curr > ans: 15 | ans, l, r = curr, start, i 16 | if curr < 0: 17 | curr = 0 18 | start = i + 1 19 | print(ans, arr[l], arr[r]) 20 | -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/7-3_Pop Sequence (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int m,n,k,cur,i,val; 9 | scanf("%d %d %d", &m, &n, &k); 10 | while(k--) 11 | { 12 | stack s; 13 | while(!s.empty()) s.pop(); 14 | s.push(1); 15 | cur = 2; 16 | int ok = 1; 17 | for(i = 1; i <= n; i++) 18 | { 19 | scanf("%d", &val); 20 | while( (s.empty() || val != s.top()) && (cur <= n) && ((int)s.size() < m) ) 21 | { 22 | s.push(cur); 23 | cur++; 24 | } 25 | if(val == s.top()) 26 | { 27 | s.pop(); 28 | } 29 | else 30 | { 31 | ok = 0; 32 | } 33 | } 34 | if(!ok) puts("NO"); 35 | else puts("YES"); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/7-4_List Leaves (25).py: -------------------------------------------------------------------------------- 1 | n = int(input().strip()) 2 | nodes = [tuple(int(x) if x != "-" else -1 for x in input().split()) for _ in range(n)] 3 | is_root = [True] * n 4 | for i, (l, r) in enumerate(nodes): 5 | if l != -1: 6 | is_root[l] = False 7 | if r != -1: 8 | is_root[r] = False 9 | 10 | root = is_root.index(True) 11 | leaves = [] 12 | q = [root] 13 | while q: 14 | u = q.pop(0) 15 | l, r = nodes[u] 16 | if l == r == -1: 17 | leaves.append(u) 18 | if l != -1: 19 | q.append(l) 20 | if r != -1: 21 | q.append(r) 22 | 23 | print(*leaves) 24 | -------------------------------------------------------------------------------- /Data Structures and Algorithms (English)/7-7_Complete Binary Search Tree (30).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | /* 15 | 参考别人好思路,先排序,依据完全二叉树和二叉排序树中序遍历的有序性特征来构造 16 | 构造完全二叉树依据堆的思想,左孩子2*cur,右孩子2*cur+1,完全二叉树恰好是一个完整数组 17 | */ 18 | 19 | int e[1001],n,m,ans[1001]; 20 | 21 | void f(int cur) 22 | { 23 | if( cur*2 <= n ) 24 | f(2*cur); 25 | ans[cur] = e[m++]; 26 | if( cur*2+1 <= n ) 27 | f(2*cur+1); 28 | } 29 | 30 | int main() 31 | { 32 | int i; 33 | scanf("%d",&n); 34 | for(i = 0; i < n; i++) 35 | scanf("%d",&e[i]); 36 | sort(e,e+n); 37 | m = 0; 38 | f(1); 39 | printf("%d", ans[1]); 40 | for(i = 2; i <= n; i++) 41 | { 42 | printf(" %d", ans[i]); 43 | } 44 | puts(""); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1002_A+B for Polynomials (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | double a[1001],b[1001]; 7 | int main() 8 | { 9 | int i; 10 | for (i=0;i<=1000;i++) a[i]=b[i]=0; 11 | int n,k; 12 | double v; 13 | cin >> n; 14 | for(i = 1; i <= n; i++) 15 | { 16 | cin >> k >> v; 17 | a[k] = v; 18 | } 19 | cin >> n; 20 | for(i = 1; i <= n; i++) 21 | { 22 | cin >> k >> v; 23 | b[k] = v; 24 | } 25 | int ans = 0; 26 | for(i = 0; i <= 1000; i++) 27 | { 28 | a[i] += b[i]; 29 | if(a[i]) ans++; 30 | } 31 | cout << ans; 32 | for(i = 1000; i >= 0; i--) 33 | { 34 | if(a[i]) printf(" %d %.1lf", i, a[i]); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1005_Spell It Right (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | string map[10]; 7 | 8 | void Init() 9 | { 10 | map[0] = "zero"; map[1] = "one"; map[2] = "two"; map[3] = "three"; map[4] = "four"; 11 | map[5] = "five"; map[6] = "six"; map[7] = "seven"; map[8] = "eight"; map[9] = "nine"; 12 | } 13 | 14 | int main() 15 | { 16 | Init(); 17 | char str[105]; 18 | int bit[105]; 19 | int i; 20 | while(cin >> str) 21 | { 22 | int ans = 0; 23 | for(i = 0; i < strlen(str); i++) ans += str[i]-'0'; 24 | int len = 0; 25 | if(!ans) bit[len++] = 0; 26 | while(ans) 27 | { 28 | bit[len++] = ans%10; 29 | ans/=10; 30 | } 31 | for(i = len-1; i > 0; i--) 32 | { 33 | cout << map[bit[i]] << " "; 34 | } 35 | cout << map[bit[0]] << endl; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1008_Elevator (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n,tmp,i; 7 | while(cin >> n) 8 | { 9 | int cur = 0; 10 | int ans = 0; 11 | for(i = 0; i < n; i++) 12 | { 13 | cin >> tmp; 14 | ans += (tmp > cur) ? (tmp-cur)*6 : (cur-tmp)*4; 15 | cur = tmp; 16 | } 17 | ans += n*5; 18 | cout << ans << endl; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1009_Product of Polynomials (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | double a[1001],b[1001],c[2002]; 7 | int main() 8 | { 9 | int i,j; 10 | for (i=0;i<=1000;i++) a[i]=b[i]=c[i]=0; 11 | int n,k; 12 | double v; 13 | cin >> n; 14 | for(i = 1; i <= n; i++) 15 | { 16 | cin >> k >> v; 17 | a[k] = v; 18 | } 19 | cin >> n; 20 | for(i = 1; i <= n; i++) 21 | { 22 | cin >> k >> v; 23 | b[k] = v; 24 | } 25 | int ans = 0; 26 | for(i = 0; i <= 1000; i++) 27 | { 28 | for(j = 0; j <= 1000; j++) 29 | { 30 | c[i+j] += a[i]*b[j]; 31 | } 32 | } 33 | for(i = 0; i <= 2000; i++) 34 | { 35 | if(c[i]) ans++; 36 | } 37 | cout << ans; 38 | for(i = 2000; i >= 0; i--) 39 | { 40 | if(c[i]) printf(" %d %.1lf", i, c[i]); 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1011_World Cup Betting (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int i,j; 8 | double ans = 1; 9 | double e[3]; 10 | string w = ""; 11 | for(i = 0; i < 3; i++) 12 | { 13 | double max = -1; 14 | int index; 15 | for(j = 0; j < 3; j++) 16 | { 17 | cin >> e[j]; 18 | if(max < e[j]) 19 | { 20 | index = j; 21 | max = e[j]; 22 | } 23 | } 24 | switch(index) 25 | { 26 | case 0: w += "W "; break; 27 | case 1: w += "T "; break; 28 | case 2: w += "L "; break; 29 | } 30 | ans *= max; 31 | } 32 | cout << w; 33 | printf("%.2lf\n", (ans*0.65-1.0)*2.0+1e-5); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1016_Phone Bills (25).cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/PAT (Advanced Level) Practice/1016_Phone Bills (25).cpp -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1019_General Palindromic Number (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool is_Palindromic(int e[],int len) 6 | { 7 | for(int i = 0; i < len/2; i++) 8 | { 9 | if(e[i] != e[len-i-1]) 10 | { 11 | return false; 12 | } 13 | } 14 | return true; 15 | } 16 | 17 | void work(int n,int d) 18 | { 19 | int e[1000]; 20 | int len = 0; 21 | while(n) 22 | { 23 | e[len++] = n % d; 24 | n /= d ; 25 | 26 | } 27 | if(is_Palindromic(e,len)) 28 | { 29 | cout << "Yes" << endl; 30 | } 31 | else 32 | { 33 | cout << "No" << endl; 34 | } 35 | for(int i = len-1; i > 0; i--) 36 | { 37 | cout << e[i] << " "; 38 | } 39 | cout << e[0] << endl; 40 | 41 | } 42 | 43 | 44 | int main() 45 | { 46 | int n,d; 47 | while(cin >> n >> d) 48 | { 49 | work(n,d); 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1027_Colors in Mars (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | string convert(int x) 11 | { 12 | string ans = ""; 13 | while(x) 14 | { 15 | switch(x%13) 16 | { 17 | case 10: ans += 'A'; break; 18 | case 11: ans += 'B'; break; 19 | case 12: ans += 'C'; break; 20 | default: ans += (x%13+'0'); 21 | } 22 | x /= 13; 23 | } 24 | reverse(ans.begin(),ans.end()); 25 | return ans; 26 | } 27 | 28 | int main() 29 | { 30 | int a,b,c; 31 | while(cin >> a >> b >> c) 32 | { 33 | cout << "#" << setfill('0') << setw(2) << convert(a) << setw(2) << convert(b) << setw(2) << convert(c) << endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1031_Hello World for U (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string str; 8 | cin >> str; 9 | int n1,n2,N,l,r,j,i; 10 | N = str.size(); 11 | for(n1 = N; ; n1--) 12 | { 13 | n2 = N+2-2*n1; 14 | if(n2 >= 3 && n2 <= N && n1 <= n2) break; 15 | } 16 | int space = n2-2; 17 | int times = n1-1; 18 | for(l = 0, r = N-1; times-- ;l++,r--) 19 | { 20 | cout << str[l]; 21 | for(i = 1; i <= space; i++) cout << " "; 22 | cout << str[r] << endl; 23 | } 24 | for(i = l; i <= r; i++) 25 | cout << str[i]; 26 | cout << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1032_Sharing (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | map Hash; 9 | map Elem; 10 | 11 | int main() 12 | { 13 | int A,B; 14 | int n,i; 15 | int s,d; 16 | char emp; 17 | scanf("%d %d %d", &A, &B, &n); 18 | for(i = 0; i < n; i++) 19 | { 20 | scanf("%d %c %d", &s,&emp,&d); 21 | Elem[s] = d; 22 | } 23 | if(A == B) {printf("%05d\n", A); return 0;} 24 | int ok = 0; 25 | while(A != B) 26 | { 27 | if(A == -1 || B == -1) 28 | { 29 | break; 30 | } 31 | if(Hash[A] || Hash[B]) 32 | { 33 | ok = 1; 34 | break; 35 | } 36 | Hash[A] = Hash[B] = 1; 37 | A = Elem[A]; 38 | B = Elem[B]; 39 | } 40 | if(ok) 41 | { 42 | if(Hash[A]) printf("%05d\n", A); 43 | else printf("%05d\n", B); 44 | } 45 | else 46 | { 47 | puts("-1"); 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1040_Longest Symmetric String (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int work(char *str) 6 | {//分奇偶情况,向字符的两边枚举 7 | char *left; 8 | char *right; 9 | int ans = 1; 10 | char *ptr = str; 11 | while(*ptr != '\0') 12 | { 13 | //奇数情况 14 | left = ptr - 1; 15 | right = ptr + 1; 16 | while(left >= str && *right != '\0' && *left == *right) 17 | { 18 | left--; 19 | right++; 20 | } 21 | if(right - left - 1 > ans) 22 | ans = right - left - 1; 23 | //偶数情况 24 | left = ptr; 25 | right = ptr+1; 26 | while(left >= str && *right != '\0' && *left == *right) 27 | { 28 | left--; 29 | right++; 30 | } 31 | if(right - left - 1 > ans) 32 | ans = right - left - 1; 33 | ptr++; 34 | } 35 | return ans; 36 | } 37 | 38 | int main() 39 | { 40 | char str[1005]; 41 | gets(str); 42 | printf("%d\n", work(str)); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1041_Be Unique (20).cpp: -------------------------------------------------------------------------------- 1 | //cout 会超时 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int e[100005]; 7 | int cnt[10001]; 8 | 9 | int main() 10 | { 11 | int n,i,j; 12 | while(scanf("%d", &n) == 1) 13 | { 14 | memset(cnt,0,sizeof(cnt)); 15 | for(i = 0; i < n; i++) 16 | { 17 | scanf("%d",&e[i]); 18 | cnt[e[i]]++; 19 | } 20 | int ok = 0; 21 | for(i = 0; i < n; i++) 22 | { 23 | if(cnt[e[i]] == 1) 24 | { 25 | ok = 1; 26 | printf("%d\n", e[i]); 27 | break; 28 | } 29 | } 30 | if(!ok) puts("None"); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1050_String Subtraction (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int flag['z'+1]; 6 | char str[10005],sub[10005]; 7 | int main() 8 | { 9 | int i; 10 | gets(str); gets(sub); 11 | memset(flag, 0, sizeof(flag)); 12 | int len1 = strlen(sub); 13 | for(i = 0; i < len1; i++) flag[sub[i]] = 1; 14 | int len2 = strlen(str); 15 | for(i = 0; i < len2; i++) 16 | { 17 | if(!flag[str[i]]) 18 | { 19 | printf("%c", str[i]); 20 | } 21 | } 22 | puts(""); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1051_Pop Sequence (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int m,n,k,cur,i,val; 9 | scanf("%d %d %d", &m, &n, &k); 10 | while(k--) 11 | { 12 | stack s; 13 | while(!s.empty()) s.pop(); 14 | s.push(1); 15 | cur = 2; 16 | int ok = 1; 17 | for(i = 1; i <= n; i++) 18 | { 19 | scanf("%d", &val); 20 | while( (s.empty() || val != s.top()) && (cur <= n) && ((int)s.size() < m) ) 21 | { 22 | s.push(cur); 23 | cur++; 24 | } 25 | if(val == s.top()) 26 | { 27 | s.pop(); 28 | } 29 | else 30 | { 31 | ok = 0; 32 | } 33 | } 34 | if(!ok) puts("NO"); 35 | else puts("YES"); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1054_The Dominant Color (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | static inline bool get(int &v) 7 | { 8 | int s = 1, c; 9 | while(!isdigit(c = getchar())&&c != '-') 10 | { 11 | if(c == EOF) 12 | break ; 13 | } 14 | if(c == EOF) return 0; 15 | if(c == '-') s = 0 , v = 0; 16 | else v = c^48; 17 | for(;isdigit(c = getchar());v = (v << 1) + (v << 3) + (c ^ 48)); 18 | v = (s ? v : -v); 19 | return 1 ; 20 | } 21 | /* 22 | 用map来计数,超过m*n/2就是答案 23 | */ 24 | long long matrix[805][605]; 25 | map cnt; 26 | 27 | int main() 28 | { 29 | int m,n,i,j; 30 | long long tmp,ans; 31 | get(m),get(n); 32 | for(i = 1; i <= m; i++) 33 | { 34 | for(j = 1; j <= n; j++) 35 | { 36 | scanf("%lld", &tmp); 37 | cnt[tmp]++; 38 | if(cnt[tmp] > (m*n)/2) 39 | ans = tmp; 40 | } 41 | } 42 | printf("%lld\n",ans); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1058_A+B in Hogwarts (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct magic 6 | { 7 | int A,B,C; 8 | }Hog1,Hog2,Hog3; 9 | 10 | int main() 11 | { 12 | scanf("%d.%d.%d %d.%d.%d", &Hog1.A,&Hog1.B,&Hog1.C,&Hog2.A,&Hog2.B,&Hog2.C); 13 | int cnt = 0; 14 | Hog3.C = (Hog1.C + Hog2.C + cnt) % 29; 15 | cnt = (Hog1.C + Hog2.C + cnt) / 29; 16 | Hog3.B = (Hog1.B + Hog2.B + cnt) % 17; 17 | cnt = (Hog1.B + Hog2.B + cnt) / 17; 18 | Hog3.A = Hog1.A + Hog2.A + cnt; 19 | printf("%d.%d.%d\n",Hog3.A,Hog3.B,Hog3.C); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1059_Prime Factors (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | 12 | 13 | int main() 14 | { 15 | long long n,i,val; 16 | cin >> n; 17 | val = n; 18 | cout << n << "="; 19 | int first = 1; 20 | int ok = 0; 21 | for(i = 2; i <= floor(sqrt((double)val)+0.5); i++) 22 | { 23 | int cnt = 0; 24 | while(n % i == 0) 25 | { 26 | 27 | cnt++; 28 | n /= i; 29 | } 30 | if(cnt) 31 | { 32 | ok = 1; 33 | if(first) 34 | { 35 | if(cnt == 1) cout << i; 36 | else cout << i << "^" << cnt; 37 | first = 0; 38 | } 39 | else 40 | { 41 | if(cnt == 1) cout << "*" << i; 42 | else cout << "*" << i << "^" << cnt; 43 | } 44 | } 45 | } 46 | if(!ok) cout << val; 47 | cout << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1064_Complete Binary Search Tree (30).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | /* 15 | 参考别人好思路,先排序,依据完全二叉树和二叉排序树中序遍历的有序性特征来构造 16 | 构造完全二叉树依据堆的思想,左孩子2*cur,右孩子2*cur+1,完全二叉树恰好是一个完整数组 17 | */ 18 | 19 | int e[1001],n,m,ans[1001]; 20 | 21 | void f(int cur) 22 | { 23 | if( cur*2 <= n ) 24 | f(2*cur); 25 | ans[cur] = e[m++]; 26 | if( cur*2+1 <= n ) 27 | f(2*cur+1); 28 | } 29 | 30 | int main() 31 | { 32 | int i; 33 | scanf("%d",&n); 34 | for(i = 0; i < n; i++) 35 | scanf("%d",&e[i]); 36 | sort(e,e+n); 37 | m = 0; 38 | f(1); 39 | printf("%d", ans[1]); 40 | for(i = 2; i <= n; i++) 41 | { 42 | printf(" %d", ans[i]); 43 | } 44 | puts(""); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1068_Find More Coins (30).cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/PAT (Advanced Level) Practice/1068_Find More Coins (30).cpp -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1077_Kuchiguse (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | //模拟从后往前比较末尾字符串,存储后的结果翻转一下 9 | string str[105]; 10 | int pos[105]; 11 | int main() { 12 | int n, i, loop = 260, j; 13 | cin >> n; 14 | getchar(); 15 | for (i = 0; i < n; i++) { 16 | getline(cin, str[i], '\n'); 17 | loop = min(loop, (int)str[i].size()); 18 | } 19 | int cur_rpos = 0; 20 | string ans_str = ""; 21 | for (i = 0; i < loop; i++) { 22 | char ch = str[0][str[0].size() - cur_rpos - 1]; 23 | int ok = 1; 24 | for (j = 1; j < n; j++) { 25 | if (str[j][str[j].size() - cur_rpos - 1] != ch) { 26 | ok = 0; 27 | break; 28 | } 29 | } 30 | if (ok) ans_str += ch; 31 | else break; 32 | cur_rpos++; 33 | } 34 | if (ans_str == "") cout << "nai" << endl; 35 | else { 36 | reverse(ans_str.begin(), ans_str.end()); 37 | cout << ans_str << endl; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1084_Broken Keyboard (20).py: -------------------------------------------------------------------------------- 1 | s1 = list(raw_input()) 2 | s2 = list(raw_input()) 3 | ans = [] 4 | while len(s1) != 0 and len(s2) != 0: 5 | if s1[0] == s2[0]: 6 | del s1[0] 7 | del s2[0] 8 | else: 9 | if s1[0].isalpha(): 10 | if s1[0].upper() not in ans: 11 | ans.append(s1[0].upper()) 12 | else: 13 | if s1[0] not in ans: 14 | ans.append(s1[0]) 15 | del s1[0] 16 | while len(s1): 17 | if s1[0].isalpha(): 18 | if s1[0].upper() not in ans: 19 | ans.append(s1[0].upper()) 20 | else: 21 | if s1[0] not in ans: 22 | ans.append(s1[0]) 23 | del s1[0] 24 | print ''.join(ans) -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1085_Perfect Sequence (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | vector vec; 14 | 15 | int main() { 16 | int n; 17 | long long p, e; 18 | scanf("%d%lld", &n, &p); 19 | for (int i = 0; i < n; i++) { 20 | scanf("%lld", &e); 21 | vec.push_back(e); 22 | } 23 | sort(vec.begin(), vec.end()); 24 | int ans = 0; 25 | for (int i = 0; i < n; i++) { 26 | int pos = upper_bound(vec.begin(), vec.end(), vec[i]*p) - vec.begin(); 27 | if (pos-1 >= 0) pos -= 1; 28 | // cout << vec[i]*p << " " << pos << endl; 29 | ans = max(ans, pos-i+1); 30 | } 31 | printf("%d\n", ans); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1090_Highest Price in Supply Chain (25).py: -------------------------------------------------------------------------------- 1 | # Pyton 超时 2 | n, p, r = map(float, raw_input().split()) 3 | relation = map(int, raw_input().split()) 4 | adjList = {} 5 | root = -1 6 | for idx, val in enumerate(relation): 7 | adjList.setdefault(val, []).append(idx) 8 | if val == -1: 9 | root = idx 10 | 11 | max_dep = 0 12 | max_dep_cnt = 0 13 | 14 | q = [] 15 | q.append((root, 0)) 16 | while len(q) != 0: 17 | cur, dep = q.pop(0) 18 | if dep > max_dep: 19 | max_dep, max_dep_cnt = dep, 1 20 | elif dep == max_dep: 21 | max_dep_cnt += 1 22 | if adjList.get(cur): 23 | for v in adjList.get(cur): 24 | q.append((v, dep+1)) 25 | 26 | print "%.2f %d" % (p*(1+r/100)**max_dep, max_dep_cnt) 27 | 28 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1092_To Buy or Not to Buy (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // unordered_map 统计一下 原始 和 目标的字符数量 5 | // 计算两者差值,设置ok变量标记是缺少还是多余,并分别用两个变量维护 6 | int main(int argc, char const *argv[]) { 7 | string origin, target; 8 | cin >> origin >> target; 9 | unordered_map cntOri, cntTar; 10 | for (int i = 0; i < target.size(); i++) 11 | cntTar[target[i]]++; 12 | for (int i = 0; i < origin.size(); i++) 13 | cntOri[origin[i]]++; 14 | int sum = origin.size(), lack = 0; 15 | bool ok = true; 16 | for (auto it = cntTar.begin(); it != cntTar.end(); it++) { 17 | if (cntOri[it->first] < it->second) { 18 | ok = false; 19 | lack += it->second - cntOri[it->first]; 20 | } else { 21 | sum -= it->second; 22 | } 23 | } 24 | printf("%s %d\n", ok ? "Yes": "No", ok ? sum : lack); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1093_Count PAT's (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // 预处理sumT[i],表示从i到末尾有多少个T 4 | // 枚举A的位置,统计前面P的个数乘以当前位置到后面T的个数 5 | int sumT[100005]; 6 | int main(int argc, char const *argv[]) { 7 | string str; 8 | cin >> str; 9 | for (int i = str.size()-1; i >= 0; i--) { 10 | if (str[i] == 'T') sumT[i] = sumT[i+1] + 1; 11 | else sumT[i] = sumT[i+1]; 12 | } 13 | 14 | int sum = 0, numP = 0; 15 | for (int i = 0; i < str.size(); i++) { 16 | if (str[i] == 'P') numP++; 17 | else if (str[i] == 'A') { 18 | sum += (numP * sumT[i+1]) % 1000000007; 19 | sum %= 1000000007; 20 | } 21 | } 22 | cout << sum << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1096_Consecutive Factors (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // O(sqrt(n)*C)算法,枚举连续乘积 4 | int main(int argc, char const *argv[]) { 5 | long long n; 6 | long long range = 0; 7 | pair result; 8 | cin >> n; 9 | long long limit = (int)sqrt((double)n+0.5); 10 | for (long long i = 2; i <= limit; i++) { 11 | long long product = 1; 12 | for (long long j = i; j <= limit; j++) { 13 | product *= j; 14 | if (product > n) break; 15 | if (n % product == 0) { 16 | if (j-i+1 > range) { 17 | range = j-i+1; 18 | result = {i, j}; 19 | } 20 | } 21 | } 22 | } 23 | if (range) { 24 | cout << range << endl; 25 | cout << result.first; 26 | for (int i = result.first+1; i <= result.second; i++) 27 | cout << "*" << i; 28 | cout << endl; 29 | } else { 30 | cout << 1 << endl << n < 2 | using namespace std; 3 | const int maxn = 100005; 4 | bool ok[maxn]; 5 | int num[maxn]; 6 | vector ans; 7 | int main() { 8 | memset(ok, false, sizeof(ok)); 9 | int n; 10 | scanf("%d", &n); 11 | for (int i = 0; i < n; i++) scanf("%d", &num[i]); 12 | int tar = num[0]; 13 | for (int i = 0; i < n; i++) { 14 | tar = max(num[i], tar); 15 | if (num[i] < tar) ok[i] = true; 16 | } 17 | tar = num[n-1]; 18 | for (int i = n-1; i >= 0; i--) { 19 | tar = min(num[i], tar); 20 | if (num[i] > tar) ok[i] = true; 21 | } 22 | for (int i = 0; i < n; i++) 23 | if (!ok[i]) ans.push_back(num[i]); 24 | printf("%d\n", ans.size()); 25 | if (!ans.empty()) { 26 | printf("%d", ans[0]); 27 | for (int i = 1; i < ans.size(); i++) 28 | printf(" %d",ans[i]); 29 | } 30 | // 坑爹的格式 31 | printf("\n"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1104_Sum of Number Segments (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | n = int(raw_input()) 4 | v = map(float, raw_input().split(' ')) 5 | ans = 0.0 6 | for i in range(1, n+1): 7 | ans += float(i*(n-i+1)) * v[i-1] 8 | print "%.2f" % ans -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1106_Lowest Price in Supply Chain (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /*小心精度问题,这里队列维护深度整数值*/ 4 | int main() { 5 | int n, c; 6 | double p, r; 7 | scanf("%d %lf %lf", &n, &p, &r); 8 | vector> g(n); 9 | for (int i = 0; i < n; i++) { 10 | scanf("%d", &c); 11 | g[i].resize(c); 12 | for (int j = 0; j < c; j++) 13 | scanf("%d", &g[i][j]); 14 | } 15 | queue> q; 16 | q.push({0, 0}); 17 | int dist = 0x7fffffff, nums = 1; 18 | while (!q.empty()) { 19 | auto cur = q.front(); q.pop(); 20 | if (g[cur.first].empty()) { 21 | if (cur.second < dist) { 22 | dist = cur.second; 23 | nums = 1; 24 | } else if (cur.second == dist) nums++; 25 | } 26 | for (auto &e : g[cur.first]) 27 | q.push({e, cur.second+1}); 28 | } 29 | printf("%.4f %d\n", p*pow((1.+r/100.), dist), nums); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1112_Stucked Keyboard (20).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 模拟题意,使用count记录连续的键位,然后更新状态 3 | */ 4 | #include 5 | using namespace std; 6 | 7 | int isStucked['z'+1]; // 记录键盘是否被卡住 8 | 9 | int main() { 10 | int k, cnt; 11 | char c; 12 | string s, stuckedKey = ""; 13 | cin >> k >> s; 14 | c = s[0]; 15 | cnt = 0; 16 | for (int i = 0; i < s.size(); i++) { 17 | if (s[i] == c) cnt++; 18 | if (s[i] != c || i == s.size()-1) { 19 | if (!(cnt % k) && (isStucked[c] != 1)) { 20 | if (isStucked[c] == 0) stuckedKey += c; 21 | isStucked[c] = 2; 22 | } else isStucked[c] = 1; 23 | c = s[i]; 24 | cnt = 1; 25 | } 26 | } 27 | 28 | string ans = ""; 29 | for (const char &c : stuckedKey) { 30 | if (isStucked[c] == 2) ans += c; 31 | } 32 | ans += "\n"; 33 | for (int i = 0; i < s.size(); ) { 34 | ans += s[i]; 35 | i += (isStucked[s[i]] == 2 ? k : 1); 36 | } 37 | cout << ans << endl; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1113_Integer Set Partition (25).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 排序,平分大端的和减去小端的和,注意偶数个数量之差为0,奇数个数量之差为1 3 | */ 4 | #include 5 | using namespace std; 6 | 7 | int arr[100005]; 8 | 9 | int main () { 10 | int n, sum = 0; 11 | scanf("%d", &n); 12 | 13 | for (int i = 0; i < n; i++) { 14 | scanf("%d", &arr[i]); 15 | sum += arr[i]; 16 | } 17 | sort(arr, arr+n); 18 | int s1 = 0; 19 | for (int i = 0; i < n/2; i++) 20 | s1 += arr[i]; 21 | printf("%d %d\n", n%2, (sum - 2*s1)); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1116_Come on! Let's C (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | ''' 4 | 6 5 | 1111 6 | 6666 7 | 8888 8 | 1234 9 | 5555 10 | 0001 11 | 6 12 | 8888 13 | 0001 14 | 1111 15 | 2222 16 | 8888 17 | 2222 18 | ''' 19 | 20 | 21 | prim = [True]*10001 22 | prim[0] = prim[1] = False 23 | for i in xrange(2, 10001): 24 | if prim[i]: 25 | for j in xrange(2, 10001): 26 | if i*j > 10000: break 27 | prim[i*j] = False 28 | N = int(raw_input()) 29 | data = { raw_input() : i+1 for i in xrange(N) } 30 | K = int(raw_input()) 31 | for i in xrange(K): 32 | id = raw_input() 33 | 34 | if data.has_key(id) and data[id] == 'Checked': 35 | print '%s: Checked' % id 36 | continue 37 | 38 | if not data.has_key(id): 39 | print '%s: Are you kidding?' % id 40 | else: 41 | if data[id] == 1: 42 | print '%s: Mystery Award' % id 43 | elif prim[data[id]]: 44 | print '%s: Minion' % id 45 | else: 46 | print '%s: Chocolate' % id 47 | data[id] = 'Checked' -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1117_Eddington Number(25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # 2 3 6 6 7 7 8 8 9 10 4 | 5 | N = int(raw_input()) 6 | dis = map(int, raw_input().split()) 7 | dis.sort(reverse=True) 8 | dis.append(-1) 9 | ans = 1 10 | for i in xrange(N+1): 11 | if dis[i] <= i+1: # wtf 把超过看成小于等于。。。 12 | ans = i 13 | break 14 | print ans -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1120_Friend Numbers (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | raw_input() 4 | s = set([]) 5 | s = set(map(lambda x: sum(map(int, list(x))), raw_input().split())) 6 | print len(s) 7 | print ' '.join(map(str, sorted(list(s)))) 8 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1121_Damn Single (25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | n = int(raw_input()) 4 | couple = {} 5 | for i in xrange(n): 6 | a, b = raw_input().split() 7 | couple[a], couple[b] = b, a 8 | m = int(raw_input()) 9 | ans = [] 10 | vip = set(raw_input().split()) 11 | for sir in vip: 12 | if couple.has_key(sir) and couple[sir] in vip: 13 | continue 14 | ans.append(sir) 15 | print len(ans) 16 | if ans: print ' '.join(sorted(ans)) 17 | 18 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1122_Hamiltonian Cycle (25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | # 1. cycle长度等于n+1 3 | # 2. cycle首尾相同 4 | # 3. cycle元素不重复且长度等于n 5 | # 4. cycle前后元素连通 6 | 7 | def is_hamicycle(cycle, adj_list): 8 | if len(cycle) != n+1: return False 9 | if cycle[0] != cycle[-1]: return False 10 | if len(set(cycle)) != n: return False 11 | for i in xrange(len(cycle)-1): 12 | if cycle[i+1] not in adj_list[cycle[i]]: 13 | return False 14 | return True 15 | 16 | n, m = map(int, raw_input().split()) 17 | adj_list = [[] for i in xrange(n+1)] 18 | for i in xrange(m): 19 | p, q = map(int, raw_input().split()) 20 | adj_list[p].append(q) 21 | adj_list[q].append(p) 22 | 23 | 24 | k = int(raw_input()) 25 | for i in xrange(k): 26 | cycle = map(int, raw_input().split()[1:]) 27 | print 'YES' if is_hamicycle(cycle, adj_list) else 'NO' 28 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1124_Raffle for Weibo Followers (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | def solve(m, n, s, forward): 4 | if s > m: 5 | print 'Keep going...' 6 | return 7 | 8 | win = set([forward[s-1]]) 9 | print forward[s-1] 10 | cur, tar = s-1, s+n 11 | while True: 12 | if cur >= len(forward): break 13 | if cur == tar-1: 14 | if forward[tar-1] not in win: 15 | print forward[tar-1] 16 | win.add(forward[tar-1]) 17 | tar = tar+n 18 | else: tar = tar+1 19 | cur += 1 20 | 21 | m, n, s = map(int, raw_input().split()) 22 | forward = [raw_input() for i in xrange(m)] 23 | solve(m,n,s,forward) 24 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1125_Chain the Ropes (25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | n = int(raw_input()) 3 | elem = map(int, raw_input().split()) 4 | elem.sort() 5 | tot = elem[0] 6 | for i in xrange(1, len(elem)): 7 | tot = (tot + elem[i]) / 2. 8 | 9 | print int(tot) 10 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1128_N Queens Puzzle (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // 位运算,r表示该列的禁位,ld和rd表示当前列对角线的禁位 4 | int main() { 5 | int K, N, q; 6 | bitset<1000> ld, rd, r; 7 | scanf("%d", &K); 8 | for (int i = 0; i < K; i++) { 9 | scanf("%d", &N); 10 | ld.reset(); rd.reset(); r.reset(); 11 | bool ok = true; 12 | for (int j = 0; j < N; j++) { 13 | scanf("%d", &q); 14 | if (!ok) continue; 15 | if (ld.test(q-1) or rd.test(q-1) or r.test(q-1)) ok = false; 16 | else { 17 | ld.set(q-1); rd.set(q-1); 18 | ld <<= 1; rd >>= 1; 19 | r.set(q-1); 20 | } 21 | } 22 | printf("%s\n", ok ? "YES" : "NO"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1130_Infix Expression (25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # 中序遍历 4 | 5 | def inorder_traverse(node): 6 | global ans 7 | if node == -1: return 8 | lchild = node_dict[node[1]] if node[1] in node_dict else -1 9 | rchild = node_dict[node[2]] if node[2] in node_dict else -1 10 | if lchild == -1 and rchild == -1: 11 | ans += node[0] 12 | else: 13 | ans += '(' 14 | inorder_traverse(lchild) 15 | ans += node[0] 16 | inorder_traverse(rchild) 17 | ans += ')' 18 | 19 | n = int(raw_input()) 20 | tot = n*(n+1) / 2 21 | node_dict = {} 22 | 23 | for i in xrange(n): 24 | data, left, right = raw_input().split() 25 | left, right = int(left), int(right) 26 | if left != -1: tot -= left 27 | if right != -1: tot -= right 28 | node_dict[i+1] = (data, left, right) 29 | 30 | root_id, ans = tot, '' 31 | inorder_traverse(node_dict[root_id]) 32 | if ans.startswith('('): 33 | print ans[1:-1] 34 | else: 35 | print ans 36 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1132_Cut Integer (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | n = int(raw_input()) 3 | for i in xrange(n): 4 | z = raw_input() 5 | a, b = int(z[:len(z)/2]), int(z[len(z)/2:]) 6 | z = int(z) 7 | check = (a*b != 0) and z % (a*b) == 0 8 | print '%s' % 'Yes' if check else 'No' 9 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1134_Vertex Cover (25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | # 枚举所有边,判断是否能被点集覆盖 3 | n, m = map(int, raw_input().split()) 4 | edges = [None] * m 5 | for i in xrange(m): 6 | a, b = map(int, raw_input().split()) 7 | edges[i] = (a, b) 8 | 9 | k = int(raw_input()) 10 | for i in xrange(k): 11 | v_set = set(map(int, raw_input().split()[1:])) 12 | for e_a, e_b in edges: 13 | if e_a not in v_set and e_b not in v_set: 14 | print 'No' 15 | break 16 | else: 17 | print 'Yes' 18 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1136_A Delayed Palindrome (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | def isPalindromicNumber(num): 4 | num = str(num) 5 | splitPos = len(num)/2 6 | return num[:splitPos] == num[::-1][:splitPos] 7 | 8 | A = raw_input() 9 | if isPalindromicNumber(int(A)): 10 | print '%s is a palindromic number.' % A 11 | else: 12 | times = 0 13 | while True: 14 | B = A[::-1] 15 | C = int(A) + int(B) 16 | print '%d + %d = %d' % (int(A), int(B), C) 17 | if isPalindromicNumber(C): 18 | print '%d is a palindromic number.' % C 19 | break 20 | else: 21 | times += 1 22 | if times >= 10: 23 | print 'Not found in 10 iterations.' 24 | break 25 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1140_Look-and-say Sequence (20).cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/PAT (Advanced Level) Practice/1140_Look-and-say Sequence (20).cpp -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1144_The Missing Number (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | vector vec; 8 | int n, v; 9 | cin >> n; 10 | for (int i = 0; i < n; i++) { 11 | cin >> v; 12 | if (v > 0) vec.push_back(v); 13 | } 14 | sort(vec.begin(), vec.end()); 15 | int ans; 16 | if (vec.empty()) ans = 1; 17 | else { 18 | ans = vec[0] - 1 == 0 ? -1 : 1; 19 | for (int i = 0; i < vec.size() - 1; i++) { 20 | if (vec[i + 1] - vec[i] >= 2) { 21 | ans = vec[i] + 1; 22 | break; 23 | } 24 | } 25 | if (ans == -1) ans = vec[vec.size() - 1] + 1; 26 | } 27 | cout << ans << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1149_Dangerous Goods Packaging (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int n, m, k, e; 8 | vector> rules; 9 | pair r; 10 | cin >> n >> m; 11 | for (int i = 0; i < n; i++) { 12 | cin >> r.first >> r.second; 13 | rules.push_back(r); 14 | } 15 | for (int i = 0; i < m; i++) { 16 | cin >> k; 17 | bool isOk = true; 18 | unordered_set menu; 19 | for (int j = 0; j < k; j++) { 20 | cin >> e; 21 | menu.insert(e); 22 | } 23 | for (const auto &p : rules) { 24 | if (menu.find(p.first) != menu.end() && menu.find(p.second) != menu.end()) { 25 | isOk = false; 26 | break; 27 | } 28 | } 29 | cout << (isOk ? "Yes" : "No") < 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isPrime(int k) { 7 | for (int i = 2; i <= (int)sqrt((double)k); i++) { 8 | if (k % i == 0) return false; 9 | } 10 | return true; 11 | } 12 | 13 | int GetInteger(string s, int pos, int numOfBis) { 14 | int r = 0; 15 | for (int i = 0; i < numOfBis; i++, pos++) { 16 | r = r*10 + s[pos] - '0'; 17 | } 18 | return r; 19 | } 20 | 21 | int main() { 22 | int L, K; 23 | bool isOk = false; 24 | string N; 25 | cin >> L >> K >> N; 26 | for (int i = 0; i <= L-K; i++) { 27 | int val = GetInteger(N, i, K); 28 | if (isPrime(val)) { 29 | isOk = true; 30 | cout << N.substr(i, K) << endl; // 前导0也需要输出 31 | break; 32 | } 33 | } 34 | if (!isOk) cout << "404" << endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1156_Sexy Primes (20).py: -------------------------------------------------------------------------------- 1 | def isPrime(n): 2 | if n <= 1: return False 3 | for i in range(2, int(n**0.5)+1): 4 | if n % i == 0: 5 | return False 6 | return True 7 | 8 | ans = None 9 | n = int(input()) 10 | if isPrime(n): 11 | if n-6 > 0 and isPrime(n-6): 12 | ans = "Yes\n%d" % (n-6) 13 | elif n+6 > 0 and isPrime(n+6): 14 | ans = "Yes\n%d" % (n+6) 15 | 16 | if not ans: 17 | ans = "No\n" 18 | seed = n+1 19 | while True: 20 | if isPrime(seed) and (seed-6 > 0 and isPrime(seed-6) or isPrime(seed+6)): 21 | ans += str(seed) 22 | break 23 | seed += 1 24 | print(ans) 25 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1157_Anniversary (25).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | alumnis = set() 3 | for i in range(n): 4 | alumnis.add(input()) 5 | 6 | alumni_count = 0 7 | elder = [None, 0] # 0: id, 1: alumni(1) or not(0) 8 | m = int(input()) 9 | for i in range(m): 10 | person = input() 11 | if person in alumnis: 12 | alumni_count += 1 13 | if elder[0] is None or elder[1] == 0 or (elder[1] == 1 and person[6:14] < elder[0][6:14]): 14 | elder = (person, 1) 15 | else: 16 | if elder[0] is None or (elder[1] == 0 and person[6:14] < elder[0][6:14]): 17 | elder = (person, 0) 18 | 19 | print(alumni_count) 20 | print(elder[0]) -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1164_Good in C (20).py: -------------------------------------------------------------------------------- 1 | import string 2 | alphabet = {} 3 | for ch in string.ascii_uppercase: 4 | alphabet[ch] = [] 5 | for i in range(7): 6 | alphabet[ch].append(input()) 7 | 8 | word = "" 9 | words = [] 10 | sentence = input() 11 | for idx, ch in enumerate(sentence): 12 | if ch in string.ascii_uppercase: 13 | word += ch 14 | else: 15 | if word: 16 | words.append(word) 17 | word = "" 18 | 19 | if word: 20 | words.append(word) 21 | 22 | is_first_word = True 23 | for w in words: 24 | # deal with blank line 25 | if is_first_word: 26 | is_first_word = False 27 | else: 28 | print() 29 | 30 | # row scan from top to bottom 31 | # in each row, characters of word scan 32 | # record the charcacter pattern of word in current row. 33 | for i in range(7): 34 | ans = [] 35 | for ch in w: 36 | ans.append(alphabet[ch][i]) 37 | print(" ".join(ans)) -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1168_Prime Day (20).py: -------------------------------------------------------------------------------- 1 | def isPrime(n): 2 | if n == 1: 3 | return False 4 | for i in range(2, int(n**0.5)+1): 5 | if n % i == 0: 6 | return False 7 | return True 8 | 9 | d = input() 10 | 11 | ok = True 12 | for i in range(len(d)): 13 | t = d[i:] 14 | r = isPrime(int(t)) 15 | ok &= r 16 | print("{} {}".format(t, "Yes" if r else "No")) 17 | if ok: 18 | print("All Prime!") -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1172_Panda and PP Milk (20).py: -------------------------------------------------------------------------------- 1 | # construct peak array from left and right, then find the max of each element in the two arrays 2 | # stands for the max height, then add 1 and multiply 100 is required the volume of milk 3 | n = int(input()) 4 | arr = list(map(int, input().split())) 5 | 6 | left, right = [1], [1] 7 | for i in range(1, n): 8 | if arr[i] > arr[i-1]: 9 | left.append(left[i-1] + 1) 10 | elif arr[i] == arr[i-1]: 11 | left.append(left[i-1]) 12 | else: 13 | left.append(1) 14 | 15 | for i in range(n-2, -1, -1): 16 | if arr[i] > arr[i+1]: 17 | right.append(right[n-i-2] + 1) 18 | elif arr[i] == arr[i+1]: 19 | right.append(right[n-i-2]) 20 | else: 21 | right.append(1) 22 | 23 | right = list(reversed(right)) 24 | 25 | ans = 0 26 | for i in range(n): 27 | ans += (max(left[i], right[i]) + 1) * 100 28 | print(ans) -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1176_The Closest Fibonacci Number (25).py: -------------------------------------------------------------------------------- 1 | fib = [0, 1] 2 | 3 | count = 0 4 | while fib[-1] <= 10**8: 5 | new = fib[-1] + fib[-2] 6 | fib.append(new) 7 | 8 | v = int(input()) 9 | ans, diff = -1, 10**8 10 | for i in range(len(fib)): 11 | if fib[i] >= v: 12 | if abs(fib[i] - v) < diff: 13 | ans = fib[i] 14 | break 15 | else: 16 | ans = fib[i] 17 | diff = abs(fib[i] - v) 18 | print(ans) 19 | -------------------------------------------------------------------------------- /PAT (Advanced Level) Practice/1177_Subsequence in Substring (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | string s, p; 8 | cin >> s >> p; 9 | 10 | pair ans{10e5+10, ""}; 11 | // brute force match p in s and find the shortest substring 12 | for (int i = 0; i < s.size(); i++) { 13 | if (s[i] != p[0]) continue; 14 | int l = i, r = l+1; 15 | int k = 1; 16 | for (; r < s.size() && k != p.size(); r++) { 17 | if (s[r] == p[k]) k++; 18 | if (k == p.size()) break; 19 | } 20 | 21 | int len = r-l+1; 22 | if (k == p.size() && len < ans.first) { 23 | ans.first = len; 24 | ans.second = s.substr(l, len); 25 | } 26 | } 27 | 28 | cout << ans.second << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1001_害死人不偿命的(3n+1)猜想 (15).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int n,step = 0; 13 | cin >> n; 14 | while(n != 1) 15 | { 16 | if(n&1) n = (3*n+1)>>1; 17 | else n = n>>1; 18 | step++; 19 | } 20 | cout << step << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1002_写出这个数 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | string name[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; 11 | vector ans; 12 | int main() 13 | { 14 | char c; 15 | int sum = 0; 16 | while((c = getchar()) != '\n') 17 | { 18 | sum += c-'0'; 19 | } 20 | if(!sum) ans.push_back( name[0] ); 21 | while(sum) 22 | { 23 | ans.push_back( name[ sum%10 ] ); 24 | sum /= 10; 25 | } 26 | cout << ans[ans.size()-1]; 27 | for(int i = ans.size()-2; i >= 0; i--) 28 | cout << " " << ans[i]; 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1004_成绩排名 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | struct stu 10 | { 11 | string name; 12 | int grade; 13 | string course; 14 | void setStu(string Name, string Course, int Grade) 15 | { 16 | name = Name; 17 | course = Course; 18 | grade = Grade; 19 | }; 20 | }high,low,tmp; 21 | 22 | int main() 23 | { 24 | int n,i; 25 | cin >> n; 26 | high.setStu("","",-100); 27 | low.setStu("","",200); 28 | for(i = 0; i < n; i++) 29 | { 30 | cin >> tmp.name >> tmp.course >> tmp.grade; 31 | if(tmp.grade > high.grade) 32 | { 33 | high.setStu(tmp.name, tmp.course, tmp.grade); 34 | } 35 | if(tmp.grade < low.grade) 36 | { 37 | low.setStu(tmp.name, tmp.course, tmp.grade); 38 | } 39 | } 40 | cout << high.name << " " << high.course << endl; 41 | cout << low.name << " " << low.course << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1005_继续(3n+1)猜想 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int f[100000]; 9 | vector ee; 10 | vector ans; 11 | int main() 12 | { 13 | int n,i,e; 14 | memset(f, 0, sizeof(f)); 15 | cin >> n; 16 | for(i = 1; i <= n; i++) 17 | { 18 | cin >> e; 19 | ee.push_back(e); 20 | } 21 | for(i = 0; i < ee.size(); i++) 22 | { 23 | int tmp = ee[i]; 24 | while(tmp != 1) 25 | { 26 | if(tmp & 1) 27 | { 28 | tmp = (3*tmp+1) >> 1; 29 | } 30 | else 31 | { 32 | tmp >>= 1; 33 | } 34 | f[tmp] = 1; 35 | } 36 | } 37 | for(i = 0; i < ee.size(); i++) 38 | { 39 | if(!f[ee[i]]) ans.push_back(ee[i]); 40 | } 41 | sort(ans.begin(), ans.end(), greater()); 42 | cout << ans[0]; 43 | for(i = 1; i < ans.size(); i++) 44 | { 45 | cout << " " << ans[i]; 46 | } 47 | cout << endl; 48 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1006_换个格式输出整数 (15).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n,i; 7 | scanf("%d" ,&n); 8 | int B = n/100; 9 | int S = n/10 % 10; 10 | int G = n % 10; 11 | for(i = 1; i <= B; i++) printf("B"); 12 | for(i = 1; i <= S; i++) printf("S"); 13 | for(i = 1; i <= G; i++) printf("%d", i); 14 | printf("\n"); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1007_素数对猜想 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool is_prime[500001]; 6 | int prime[500001]; 7 | /*筛法*/ 8 | int n, len = 0; 9 | void init() 10 | { 11 | int i,j; 12 | memset(is_prime, 0, sizeof(int)*(n+2)); 13 | is_prime[0] = 1; is_prime[1] = 1; 14 | for(i = 2; i * i <= n; i++) 15 | { 16 | if(is_prime[i]) continue; 17 | for(j = i * i; j <= n; j += i) is_prime[j] = 1; 18 | } 19 | for(i = 1; i <= n; i++) 20 | { 21 | if(!is_prime[i]) prime[len++] = i; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int i, ans = 0; 28 | scanf("%d" ,&n); 29 | init(); 30 | for(i = 1; i < len && prime[i] <= n; i++) 31 | { 32 | if(prime[i]-prime[i-1] == 2) ans++; 33 | } 34 | printf("%d\n",ans); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1008_数组元素循环右移问题 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void reverse(int *elem, int start , int end) 7 | { 8 | int i; 9 | for(i = 0; i <= (end-start)>>1; i++) 10 | { 11 | int tmp = elem[start+i]; 12 | elem[start+i] = elem[end-i]; 13 | elem[end-i] = tmp; 14 | } 15 | } 16 | 17 | void move(int *elem, int n, int dis) 18 | { 19 | dis = dis % n; 20 | reverse(elem, 0, n-dis-1); 21 | reverse(elem, n-dis,n-1); 22 | reverse(elem, 0, n-1); 23 | } 24 | 25 | int a[105]; 26 | int main() 27 | { 28 | int n, dis,i; 29 | scanf("%d %d", &n, &dis); 30 | for(i = 0; i < n; i++) 31 | scanf("%d", &a[i]); 32 | move(a, n, dis); 33 | printf("%d", a[0]); 34 | for(i = 1; i < n; i++) printf(" %d", a[i]); 35 | printf("\n"); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1009_说反话 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void reverse(string *e, int start , int end) 8 | { 9 | int i; 10 | for(i = 0; i <= (end-start)>>1; i++) 11 | { 12 | string tmp = e[start+i]; 13 | e[start+i] = e[end-i]; 14 | e[end-i] = tmp; 15 | } 16 | } 17 | string e[500]; 18 | char str[85]; 19 | int main() 20 | { 21 | gets(str); 22 | int len = strlen(str), lene = 0,i; 23 | string tmp = ""; 24 | for(i = 0; i < len; i++) 25 | { 26 | if(str[i] == ' ') 27 | { 28 | e[lene++] = tmp; 29 | tmp = ""; 30 | } 31 | else 32 | { 33 | tmp += str[i]; 34 | } 35 | } 36 | if(tmp != "") e[lene++] = tmp; 37 | reverse(e, 0, lene-1); 38 | printf("%s", e[0].c_str()); 39 | for(i = 1; i < lene; i++) 40 | printf(" %s", e[i].c_str()); 41 | printf("\n"); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1017_A除以B (20).java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.Scanner; 3 | 4 | 5 | public class Main 6 | { 7 | public static void main(String[] args) 8 | { 9 | Scanner in = new Scanner(System.in); 10 | BigInteger A = in.nextBigInteger(); 11 | BigInteger B = in.nextBigInteger(); 12 | System.out.print(A.divide(B)); 13 | System.out.print(" "); 14 | System.out.print(A.mod(B)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1026_程序运行时间 (15).py: -------------------------------------------------------------------------------- 1 | c1, c2 = map(int, raw_input().split(' ')) 2 | t = round((c2 - c1) / 100.0) 3 | 4 | s = t % 60 5 | m = t / 60 % 60 6 | h = t / 60 / 60 7 | print "%02d:%02d:%02d" % (h, m, s) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1027_打印沙漏 (20).py: -------------------------------------------------------------------------------- 1 | n, ch = map(str, raw_input().split(' ')) 2 | n = int(n) 3 | num = int(((n+1)/2.0) ** 0.5) 4 | left = n - 2 * (num ** 2) + 1 5 | symbol = [2*i-1 for i in range(num, 0, -1)] + [2*i+1 for i in range(1, num, 1)] 6 | whitespace = [i for i in range(0, num, 1)] + [i for i in range(num-2, -1, -1)] 7 | for i in range(len(symbol)): 8 | line = "" 9 | for j in range(whitespace[i]): 10 | line += " " 11 | for j in range(symbol[i]): 12 | line += ch 13 | print line 14 | print left -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1029_旧键盘 (20).py: -------------------------------------------------------------------------------- 1 | s1 = list(raw_input()) 2 | s2 = list(raw_input()) 3 | ans = [] 4 | while len(s1) != 0 and len(s2) != 0: 5 | if s1[0] == s2[0]: 6 | del s1[0] 7 | del s2[0] 8 | else: 9 | if s1[0].isalpha(): 10 | if s1[0].upper() not in ans: 11 | ans.append(s1[0].upper()) 12 | else: 13 | if s1[0] not in ans: 14 | ans.append(s1[0]) 15 | del s1[0] 16 | while len(s1): 17 | if s1[0].isalpha(): 18 | if s1[0].upper() not in ans: 19 | ans.append(s1[0].upper()) 20 | else: 21 | if s1[0] not in ans: 22 | ans.append(s1[0]) 23 | del s1[0] 24 | print ''.join(ans) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1030_完美数列 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | vector vec; 14 | 15 | int main() { 16 | int n; 17 | long long p, e; 18 | scanf("%d%lld", &n, &p); 19 | for (int i = 0; i < n; i++) { 20 | scanf("%lld", &e); 21 | vec.push_back(e); 22 | } 23 | sort(vec.begin(), vec.end()); 24 | int ans = 0; 25 | for (int i = 0; i < n; i++) { 26 | int pos = upper_bound(vec.begin(), vec.end(), vec[i]*p) - vec.begin(); 27 | if (pos-1 >= 0) pos -= 1; 28 | // cout << vec[i]*p << " " << pos << endl; 29 | ans = max(ans, pos-i+1); 30 | } 31 | printf("%d\n", ans); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1031_查验身份证 (15).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int weight[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; 5 | int check[] = {1,0,10,9,8,7,6,5,4,3,2}; 6 | 7 | int main() { 8 | int n; 9 | string id; 10 | vector id_arr; 11 | cin >> n; 12 | for (int i = 0; i < n; i++) { 13 | cin >> id; 14 | int z = 0; 15 | bool ok = true; 16 | for (int j = 0; j < id.size()-1; j++) { 17 | if (id[j] == 'X') ok = false; 18 | else z = (z + weight[j] * (id[j]-'0')) % 11; 19 | } 20 | int check_bit = (id[id.size()-1] == 'X') ? 10 : id[id.size()-1]-'0'; 21 | if (ok && check_bit != check[z]) ok = false; 22 | if (ok) continue; 23 | else id_arr.push_back(id); 24 | } 25 | if (id_arr.size() == 0) cout << "All passed" << endl; 26 | else { 27 | for (int i = 0; i < id_arr.size(); i++) 28 | cout << id_arr[i] << endl; 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1032_挖掘机技术哪家强 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int maxn = 100010; 4 | int stu[maxn]; 5 | int main() { 6 | int n, id, score; 7 | int ansId, ansScore = -1; 8 | cin >> n; 9 | while (n--) { 10 | cin >> id >> score; 11 | stu[id] += score; 12 | if (stu[id] > ansScore) { 13 | ansId = id; 14 | ansScore = stu[id]; 15 | } 16 | } 17 | cout << ansId << " " << ansScore << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1033_旧键盘打字 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool brokenKey[256]; 5 | 6 | int main() { 7 | string brokenKeyStr; 8 | string inputStr; 9 | memset(brokenKey, false, sizeof(brokenKey)); 10 | // 必须第一行,test case可能有空白字符 11 | getline(cin, brokenKeyStr); 12 | getline(cin, inputStr); 13 | cin >> brokenKeyStr >> inputStr; 14 | for (int i = 0; i < brokenKeyStr.size(); i++) { 15 | char ch = brokenKeyStr[i]; 16 | brokenKey[ch] = true; 17 | } 18 | string outputStr = ""; 19 | for (int i = 0; i < inputStr.size(); i++) { 20 | char ch = inputStr[i]; 21 | if (ch <= 'Z' && ch >= 'A' && (brokenKey[ch] || brokenKey['+'])) continue; 22 | else if (ch <= 'z' && ch >= 'a' && (brokenKey[ch-'a'+'A'])) continue; 23 | else if (brokenKey[ch]) continue; 24 | else outputStr += ch; 25 | } 26 | cout << outputStr << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1036_跟奥巴马一起编程(15).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // 直接打印 5 | int main() { 6 | int col, row; 7 | char ch; 8 | cin >> col >> ch; 9 | row = (int)((double)col / 2.0 + 0.5); 10 | for (int i = 0; i < col; i++) cout << ch; 11 | cout << endl; 12 | for (int i = 0; i < row-2; i++) { 13 | cout << ch; 14 | for (int j = 0; j < col-2; j++) cout << " "; 15 | cout << ch << endl; 16 | } 17 | for (int i = 0; i < col; i++) cout << ch; 18 | cout << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1038_统计同成绩学生(20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // unordered_map维护 5 | int main(int argc, char const *argv[]) { 6 | int n, score, query; 7 | unordered_map gradeCount; 8 | scanf("%d", &n); 9 | while (n--) { 10 | scanf("%d", &score); 11 | ++gradeCount[score]; 12 | } 13 | scanf("%d", &query); 14 | for (int i = 0; i < query; i++) { 15 | scanf("%d", &score); 16 | if (i == 0) printf("%d", gradeCount[score]); 17 | else printf(" %d", gradeCount[score]); 18 | } 19 | printf("\n"); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1039_到底买不买(20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // unordered_map 统计一下 原始 和 目标的字符数量 5 | // 计算两者差值,设置ok变量标记是缺少还是多余,并分别用两个变量维护 6 | int main(int argc, char const *argv[]) { 7 | string origin, target; 8 | cin >> origin >> target; 9 | unordered_map cntOri, cntTar; 10 | for (int i = 0; i < target.size(); i++) 11 | cntTar[target[i]]++; 12 | for (int i = 0; i < origin.size(); i++) 13 | cntOri[origin[i]]++; 14 | int sum = origin.size(), lack = 0; 15 | bool ok = true; 16 | for (auto it = cntTar.begin(); it != cntTar.end(); it++) { 17 | if (cntOri[it->first] < it->second) { 18 | ok = false; 19 | lack += it->second - cntOri[it->first]; 20 | } else { 21 | sum -= it->second; 22 | } 23 | } 24 | printf("%s %d\n", ok ? "Yes": "No", ok ? sum : lack); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1040_有几个PAT(25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // 预处理sumT[i],表示从i到末尾有多少个T 4 | // 枚举A的位置,统计前面P的个数乘以当前位置到后面T的个数 5 | int sumT[100005]; 6 | int main(int argc, char const *argv[]) { 7 | string str; 8 | cin >> str; 9 | for (int i = str.size()-1; i >= 0; i--) { 10 | if (str[i] == 'T') sumT[i] = sumT[i+1] + 1; 11 | else sumT[i] = sumT[i+1]; 12 | } 13 | 14 | int sum = 0, numP = 0; 15 | for (int i = 0; i < str.size(); i++) { 16 | if (str[i] == 'P') numP++; 17 | else if (str[i] == 'A') { 18 | sum += (numP * sumT[i+1]) % 1000000007; 19 | sum %= 1000000007; 20 | } 21 | } 22 | cout << sum << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1041_考试座位号(15).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | pair seat[1005]; 6 | int n, test, real; 7 | string id; 8 | cin >> n; 9 | for (int i = 0; i < n; i++) { 10 | cin >> id >> test >> real; 11 | seat[test] = {id, real}; 12 | } 13 | cin >> n; 14 | for (int i = 0; i < n; i++) { 15 | cin >> test; 16 | cout << seat[test].first << " " << seat[test].second << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1042_字符统计(20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int cnt['z'+1]; 6 | memset(cnt, 0, sizeof(cnt)); 7 | string line; 8 | getline(cin, line); 9 | for (int i = 0; i < line.size(); i++) { 10 | if (line[i] >= 'a' && line[i] <= 'z') 11 | cnt[line[i]]++; 12 | else if (line[i] <= 'Z' && line[i] >= 'A') 13 | cnt[line[i]-'A'+'a']++; 14 | } 15 | int maxCnt = 0; 16 | char ch; 17 | for (int i = 'a'; i <= 'z'; i++) { 18 | if (cnt[i] > maxCnt) { 19 | maxCnt = cnt[i]; 20 | ch = (char)i; 21 | } 22 | } 23 | cout << ch << " " << maxCnt << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1043_输出PATest(20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string trans = "PATest"; 6 | int cnt[6]; 7 | memset(cnt, 0 , sizeof(cnt)); 8 | string line; 9 | cin >> line; 10 | for (int i = 0; i < line.size(); i++) { 11 | for (int j = 0; j < trans.size(); j++) { 12 | if (trans[j] == line[i]) { 13 | cnt[j]++; 14 | break; 15 | } 16 | } 17 | } 18 | while (cnt[0] || cnt[1] || cnt[2] || cnt[3] || cnt[4] || cnt[5]) { 19 | for (int i = 0; i < trans.size(); i++) { 20 | if (cnt[i]) { 21 | cout << trans[i]; 22 | cnt[i]--; 23 | } 24 | } 25 | } 26 | cout << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1045_快速排序(25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int maxn = 100005; 4 | bool ok[maxn]; 5 | int num[maxn]; 6 | vector ans; 7 | int main() { 8 | memset(ok, false, sizeof(ok)); 9 | int n; 10 | scanf("%d", &n); 11 | for (int i = 0; i < n; i++) scanf("%d", &num[i]); 12 | int tar = num[0]; 13 | for (int i = 0; i < n; i++) { 14 | tar = max(num[i], tar); 15 | if (num[i] < tar) ok[i] = true; 16 | } 17 | tar = num[n-1]; 18 | for (int i = n-1; i >= 0; i--) { 19 | tar = min(num[i], tar); 20 | if (num[i] > tar) ok[i] = true; 21 | } 22 | for (int i = 0; i < n; i++) 23 | if (!ok[i]) ans.push_back(num[i]); 24 | printf("%d\n", ans.size()); 25 | if (!ans.empty()) { 26 | printf("%d", ans[0]); 27 | for (int i = 1; i < ans.size(); i++) 28 | printf(" %d",ans[i]); 29 | } 30 | // 坑爹的格式 31 | printf("\n"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1046_划拳(15).py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | 3 | n = int(raw_input()) 4 | w = [0, 0] 5 | for i in range(n): 6 | a0, a1, b0, b1 = map(int, raw_input().split()) 7 | if a1 != b1: 8 | w[0] += 1 if a1 == a0+b0 else 0 9 | w[1] += 1 if b1 == a0+b0 else 0 10 | print w[1], w[0] -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1047_编程团体赛(20).py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | 3 | n = int(raw_input()) 4 | scoreCount = {} 5 | ans = [0, 0] 6 | for i in range(n): 7 | team, score = raw_input().split(' ') 8 | teamID = team[:team.find('-')] 9 | scoreCount[teamID] = scoreCount.get(teamID, 0) + int(score) 10 | if scoreCount[teamID] > ans[1]: 11 | ans[0], ans[1] = teamID, scoreCount[teamID] 12 | print ans[0], ans[1] 13 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1048_数字加密(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | A, B = raw_input().split() 4 | A, B = A[::-1], B[::-1] 5 | alphabet, ans = "0123456789JQK", "" 6 | for i in range(max(len(B), len(A))): 7 | a = int(A[i]) if i 2 | using namespace std; 3 | /* 4 | 1. 实部或者虚部小于0.01时候,保留两位结果为0 5 | 2. 如果虚部为整数,添加+符号,负数为-符号 6 | */ 7 | int main() { 8 | double r[2], p[2]; 9 | scanf("%lf%lf%lf%lf", &r[0], &p[0], &r[1], &p[1]); 10 | double tmp = r[0]*r[1]; 11 | double real = tmp * cos(p[0]+p[1]); 12 | double complex = tmp * sin(p[0]+p[1]); 13 | if (fabs(real) < 0.01) real = 0; 14 | if (fabs(complex) < 0.01) complex = 0; 15 | printf("%.2f", real); 16 | if (complex < 0) printf("-%.2fi\n", fabs(complex)); 17 | else printf("+%.2fi\n", complex); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1053_住房空置率(20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | 1. 超过一半日子电量低于阈值e,可能空置 6 | 2. 满足上一条,观察期天数超过阈值D天,为空置 7 | */ 8 | int main() { 9 | int n, d, k; 10 | double e, ce; 11 | cin >> n >> e >> d; 12 | int tot = n; 13 | int mayEmpty = 0, empty = 0; 14 | while (n--) { 15 | cin >> k; 16 | int watch = 0; 17 | for (int i = 0; i < k; i++) { 18 | cin >> ce; 19 | if (ce < e) watch++; 20 | } 21 | if ((double)watch > (double)k / 2.0) { 22 | if (k > d) empty++; 23 | else mayEmpty++; 24 | } 25 | } 26 | cout << setiosflags(ios::fixed) << setprecision(1) << (double)mayEmpty*100.0/tot << "% " << (double)empty*100.0/tot << "%" << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1056_组合数的和(15).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | # python 大法好 3 | nums = map(int, raw_input().split()[1:]) 4 | print sum(set([nums[i]*10+nums[j] for i in xrange(len(nums)) for j in xrange(len(nums)) if i!=j ])) 5 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1057_数零壹(20).py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | 3 | s = raw_input() 4 | tot, has_alpha = 0, False 5 | for x in s: 6 | if x.isalpha(): 7 | tot += ord(x.lower())-ord('a')+1 8 | has_alpha = True 9 | if not has_alpha: print 0, 0 # 边界情况,没有字母的时候 10 | else: 11 | v = bin(tot) 12 | print v.count('0')-1, v.count('1') -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1058_选择题(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | def process_question(sol_inp): 3 | return (sol_inp[0], ''.join(sorted(sol_inp[3:]))) 4 | 5 | student_num, question_num = map(int, raw_input().split()) 6 | stu_scores, wrong_stats, max_wrong_nums = [0]*student_num, [0]*question_num, 0 7 | 8 | solutions = [process_question(raw_input().split()) for i in xrange(question_num)] 9 | 10 | for i in xrange(student_num): 11 | stu_answers = raw_input()[1:-1].split(') (') 12 | for idx, ans in enumerate(stu_answers): 13 | if ''.join(sorted(ans.split(' ')[1:])) == solutions[idx][1]: 14 | stu_scores[i] += int(solutions[idx][0]) 15 | else: 16 | wrong_stats[idx] += 1 17 | max_wrong_nums = max(wrong_stats[idx], max_wrong_nums) 18 | 19 | for i in xrange(student_num): 20 | print stu_scores[i] 21 | 22 | if max_wrong_nums == 0: 23 | print 'Too simple' 24 | else: 25 | output = '%d' % max_wrong_nums 26 | for idx, wrong_nums in enumerate(wrong_stats): 27 | if wrong_nums == max_wrong_nums: 28 | output += ' %d' % (idx+1) 29 | print output -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1059_C语言竞赛(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | ''' 4 | 6 5 | 1111 6 | 6666 7 | 8888 8 | 1234 9 | 5555 10 | 0001 11 | 6 12 | 8888 13 | 0001 14 | 1111 15 | 2222 16 | 8888 17 | 2222 18 | ''' 19 | 20 | 21 | prim = [True]*10001 22 | prim[0] = prim[1] = False 23 | for i in xrange(2, 10001): 24 | if prim[i]: 25 | for j in xrange(2, 10001): 26 | if i*j > 10000: break 27 | prim[i*j] = False 28 | N = int(raw_input()) 29 | data = { raw_input() : i+1 for i in xrange(N) } 30 | K = int(raw_input()) 31 | for i in xrange(K): 32 | id = raw_input() 33 | 34 | if data.has_key(id) and data[id] == 'Checked': 35 | print '%s: Checked' % id 36 | continue 37 | 38 | if not data.has_key(id): 39 | print '%s: Are you kidding?' % id 40 | else: 41 | if data[id] == 1: 42 | print '%s: Mystery Award' % id 43 | elif prim[data[id]]: 44 | print '%s: Minion' % id 45 | else: 46 | print '%s: Chocolate' % id 47 | data[id] = 'Checked' -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1060_爱丁顿数(25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # 2 3 6 6 7 7 8 8 9 10 4 | 5 | N = int(raw_input()) 6 | dis = map(int, raw_input().split()) 7 | dis.sort(reverse=True) 8 | dis.append(-1) 9 | ans = 1 10 | for i in xrange(N+1): 11 | if dis[i] <= i+1: # wtf 把超过看成小于等于。。。 12 | ans = i 13 | break 14 | print ans -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1061_判断题(15).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | n,m = map(int, raw_input().split()) 3 | score = map(int, raw_input().split()) 4 | correct_ans = raw_input().split() 5 | 6 | for i in xrange(n): 7 | stu_ans = raw_input().split() 8 | tot = 0 9 | for j in xrange(len(stu_ans)): 10 | if correct_ans[j] == stu_ans[j]: 11 | tot += score[j] 12 | print tot -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1062_最简分数(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | def gcd(a,b): 3 | if b==0: return a 4 | else: return gcd(b, a%b) 5 | 6 | a,b,c = raw_input().split() 7 | n1, m1 = map(int, a.split('/')) 8 | n2, m2 = map(int, b.split('/')) 9 | # 考虑顺序 10 | if n1 * m2 > n2 * m1: 11 | n1,n2,m1,m2 = n2,n1,m2,m1 12 | 13 | c = int(c) 14 | ans = [] 15 | x = n1*c/m1+1 16 | while n1*cx*m2: 17 | if gcd(x, c) == 1: 18 | ans.append('%d/%d' % (x, c)) 19 | x += 1 20 | print ' '.join(ans) 21 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1063_计算谱半径(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | n = int(raw_input()) 3 | ans = -1 4 | for i in xrange(n): 5 | a, b = map(float, raw_input().split()) 6 | ans = max((a**2+b**2)**0.5, ans) 7 | print '%.2f' % round(ans, 2) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1064_朋友数(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | raw_input() 4 | s = set([]) 5 | s = set(map(lambda x: sum(map(int, list(x))), raw_input().split())) 6 | print len(s) 7 | print ' '.join(map(str, sorted(list(s)))) 8 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1065_单身狗(25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | n = int(raw_input()) 4 | couple = {} 5 | for i in xrange(n): 6 | a, b = raw_input().split() 7 | couple[a], couple[b] = b, a 8 | m = int(raw_input()) 9 | ans = [] 10 | vip = set(raw_input().split()) 11 | for sir in vip: 12 | if couple.has_key(sir) and couple[sir] in vip: 13 | continue 14 | ans.append(sir) 15 | print len(ans) 16 | if ans: print ' '.join(sorted(ans)) 17 | 18 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1066_图像过滤(15).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | n, m, a, b, v = map(int, raw_input().split()) 4 | for i in xrange(n): 5 | result = map(lambda x: ('%03d' % v)if a<=int(x)<=b else ('%03d' % int(x)), raw_input().split()) 6 | print ' '.join(result) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1067_试密码(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | pwd, n = raw_input().split() 4 | n = int(n) 5 | while True: 6 | inp = raw_input() 7 | if inp == '#': 8 | break 9 | else: 10 | if inp == pwd: 11 | print 'Welcome in' 12 | break 13 | else: 14 | print 'Wrong password: %s' % inp 15 | n -= 1 16 | if n == 0: 17 | print 'Account locked' 18 | break 19 | 20 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1069_微博转发抽奖(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | def solve(m, n, s, forward): 4 | if s > m: 5 | print 'Keep going...' 6 | return 7 | 8 | win = set([forward[s-1]]) 9 | print forward[s-1] 10 | cur, tar = s-1, s+n 11 | while True: 12 | if cur >= len(forward): break 13 | if cur == tar-1: 14 | if forward[tar-1] not in win: 15 | print forward[tar-1] 16 | win.add(forward[tar-1]) 17 | tar = tar+n 18 | else: tar = tar+1 19 | cur += 1 20 | 21 | m, n, s = map(int, raw_input().split()) 22 | forward = [raw_input() for i in xrange(m)] 23 | solve(m,n,s,forward) 24 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1070_结绳(25).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | # 排序,从小到大组合 3 | # res = a0/2^(n-1) + a1/2^(n-2) + ... + an/2 , so a0...an升序,res值最大 4 | n = int(raw_input()) 5 | elem = map(int, raw_input().split()) 6 | elem.sort() 7 | tot = elem[0] 8 | for i in xrange(1, len(elem)): 9 | tot = (tot + elem[i]) / 2. 10 | print int(tot) 11 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1071_小赌怡情(15).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import sys 3 | 4 | t, k = map(int, raw_input().split()) 5 | for i in xrange(k): 6 | n1, b, t_, n2 = map(int, raw_input().split()) 7 | if t < t_: 8 | print 'Not enough tokens. Total = %d.' % t 9 | else: 10 | isWin = (n1 > n2 and b == 0) or (n1 < n2 and b == 1) 11 | if isWin: 12 | t += t_ 13 | print 'Win %d! Total = %d.' % (t_, t) 14 | else: 15 | t -= t_ 16 | print 'Lose %d. Total = %d.' % (t_, t) 17 | if t == 0: 18 | print 'Game Over.' 19 | sys.exit(0) 20 | 21 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1072_开学寄语(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | n, m = map(int, raw_input().split()) 4 | forbid_list = set(raw_input().split()) 5 | black_user_list = [] 6 | black_user_thing = {} 7 | forbid_cnt = 0 8 | for i in xrange(n): 9 | line = raw_input().split() 10 | user, things = line[0], line[2:] 11 | needRecord = False 12 | for t in things: 13 | if t in forbid_list: 14 | forbid_cnt += 1 15 | needRecord = True 16 | black_user_thing.setdefault(user, []).append(t) 17 | if needRecord: 18 | black_user_list.append(user) 19 | for u in black_user_list: 20 | s = u + ':' 21 | for t in black_user_thing[u]: 22 | s += ' ' + t 23 | print s 24 | print len(black_user_list), forbid_cnt 25 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1074_宇宙无敌加法器(20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # 模拟加法,注意0的边界值判断 4 | 5 | carry_tmpl = map(int, list(raw_input())) 6 | carry_tmpl.reverse() 7 | a = map(int, list(raw_input())) 8 | b = map(int, list(raw_input())) 9 | a.reverse() 10 | b.reverse() 11 | c, carry = [], 0 12 | 13 | cnt = max(len(a), len(b), len(carry_tmpl)) 14 | a.extend([0]*(cnt-len(a)+1)) 15 | b.extend([0]*(cnt-len(b)+1)) 16 | 17 | for i in xrange(max(len(a), len(b))): 18 | mod = 10 if i >= len(carry_tmpl) or carry_tmpl[i] == 0 else carry_tmpl[i] 19 | bit = (a[i] + b[i] + carry) % mod 20 | carry = (a[i] + b[i] + carry) / mod 21 | c.append(bit) 22 | 23 | result = ''.join(map(str, c)).rstrip('0')[::-1] 24 | if result == '': 25 | print 0 26 | else: 27 | print result 28 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1076_Wifi密码 (15).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | n = int(raw_input()) 4 | ans = '' 5 | for i in xrange(n): 6 | for sel in raw_input().split(): 7 | if sel[-1] == 'T': 8 | ans += str(ord(sel[0]) - ord('A') + 1) 9 | print ans 10 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1077_互评成绩计算 (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | n, m = map(int, raw_input().split()) 3 | for i in xrange(n): 4 | scores = map(int, raw_input().split()) 5 | G1 = scores[0] 6 | otherGroup = filter(lambda x: 0 <= x <= m, scores[1:]) 7 | G2 = (sum(otherGroup) - max(otherGroup) - min(otherGroup)) / (len(otherGroup) - 2.) 8 | print '%.0f' % round((G1+G2)/2.) 9 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1079_延迟的回文数 (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | def isPalindromicNumber(num): 4 | num = str(num) 5 | splitPos = len(num)/2 6 | return num[:splitPos] == num[::-1][:splitPos] 7 | 8 | A = raw_input() 9 | if isPalindromicNumber(int(A)): 10 | print '%s is a palindromic number.' % A 11 | else: 12 | times = 0 13 | while True: 14 | B = A[::-1] 15 | C = int(A) + int(B) 16 | print '%d + %d = %d' % (int(A), int(B), C) 17 | if isPalindromicNumber(C): 18 | print '%d is a palindromic number.' % C 19 | break 20 | else: 21 | times += 1 22 | if times >= 10: 23 | print 'Not found in 10 iterations.' 24 | break 25 | A = str(C) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1081_检查密码 (15).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | string checkPassword(const string &password) { 7 | string ret = "Your password "; 8 | 9 | if (password.size() < 6) return ret + "is tai duan le."; 10 | 11 | int hasAlpha = 0, hasDigit = 0; 12 | for (auto const &ch : password) { 13 | hasAlpha |= isalpha(ch); 14 | hasDigit |= isdigit(ch); 15 | if (!(ch == '.' || isdigit(ch) || isalpha(ch))) 16 | return ret + "is tai luan le."; 17 | } 18 | 19 | if (!hasAlpha && hasDigit) return ret + "needs zi mu."; 20 | else if (hasAlpha && !hasDigit) return ret + "needs shu zi."; 21 | else if (hasAlpha && hasDigit) return ret + "is wan mei."; 22 | } 23 | 24 | 25 | int main() { 26 | int n; 27 | string password; 28 | cin >> n; cin.get(); 29 | while (n--) { 30 | getline(cin, password, '\n'); 31 | cout << checkPassword(password) << endl; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1082_射击比赛 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Record { 6 | public: 7 | int id; 8 | int dis; 9 | } Rookie{ -1, -1 }, Champion{ -1, 0x7fffffff }; 10 | 11 | 12 | int main() { 13 | int n, id, x, y, dis; 14 | cin >> n; 15 | for (int i = 0; i < n; i++) { 16 | cin >> id >> x >> y; 17 | dis = x * x + y * y; 18 | if (dis < Champion.dis) { 19 | Champion.dis = dis; 20 | Champion.id = id; 21 | } 22 | if (dis > Rookie.dis) { 23 | Rookie.dis = dis; 24 | Rookie.id = id; 25 | } 26 | } 27 | cout << setfill('0') << setw(4) << Champion.id << " " << setfill('0') << setw(4) << Rookie.id << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1083_是否存在相等的差 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, v; 7 | cin >> n; 8 | vector diffVec(n, 0); 9 | for (int i = 0; i < n; i++) { 10 | cin >> v; 11 | ++diffVec[abs(i + 1 - v)]; 12 | } 13 | for (int i = diffVec.size() - 1; i >= 0; i--) { 14 | if (diffVec[i] > 1) cout << i << " " << diffVec[i] << endl; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1084_外观数列 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 根据当前item生成下一项 6 | string next(const string &item) { 7 | int cnt = 1; 8 | string ret = ""; 9 | for (int i = 0; i < item.size()-1; i++) { 10 | if (item[i] == item[i + 1]) cnt++; 11 | else { 12 | ret += item[i] + to_string(cnt); 13 | cnt = 1; 14 | } 15 | } 16 | ret += item[item.size() - 1] + to_string(cnt); 17 | return ret; 18 | } 19 | 20 | int main() { 21 | string d; 22 | int n; 23 | cin >> d >> n; 24 | for (int i = 1; i < n; i++) d = next(d); 25 | cout << d << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1086_就不告诉你 (15).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | cin >> a >> b; 8 | int c = a * b; 9 | if (c == 0) cout << c << endl; 10 | else { 11 | bool firstNotZero = false; 12 | while (c != 0) { 13 | if (c % 10 == 0) { 14 | if (firstNotZero) 15 | cout << c % 10; 16 | } else { 17 | firstNotZero = true; 18 | cout << c % 10; 19 | } 20 | c /= 10; 21 | } 22 | cout << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1087_有多少不同的值 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int len = 10000 / 2 * 3; 5 | bool isVis[len]; 6 | 7 | int main() { 8 | int n, count = 0, v; 9 | cin >> n; 10 | for (int i = 1; i <= n; i++) { 11 | v = i / 2 + i / 3 + i / 5; 12 | count += isVis[v] ? 0 : 1; 13 | isVis[v] = true; 14 | } 15 | cout << count << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1088_三人行 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | string Compare(double me, double other) { 6 | if (other > me) return "Cong"; 7 | else if (other < me) return "Gai"; 8 | else return "Ping"; 9 | } 10 | 11 | 12 | int main() { 13 | int a, b, c, m, x, y; 14 | double result[3] = {.0, .0, .0}; 15 | cin >> m >> x >> y; 16 | for (a = 10; a <= 99; a++) { 17 | b = a%10*10 + a/10; 18 | if (abs(a - b) * y == b * x && a >= result[0]) 19 | { 20 | result[0] = a; 21 | result[1] = b; 22 | result[2] = b * 1.0 / y; 23 | } 24 | } 25 | if (result[0] == 0) { 26 | cout << "No Solution" << endl; 27 | } else { 28 | cout << result[0] 29 | << " " << Compare(m, result[0]) 30 | << " " << Compare(m, result[1]) 31 | << " " << Compare(m, result[2]) 32 | << endl; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1089_狼人杀-简单版 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // 枚举两个狼人,检查所有人说的话,狼人不是这两个人,或者好人是这两个狼人,那么这个角色说的就是假话 4 | 5 | int main() { 6 | int member[101]; 7 | int n; 8 | cin >> n; 9 | pair result = {0,0}; 10 | for (int i = 1; i <= n; i++) 11 | cin >> member[i]; 12 | for (int i = 1; i < n && result.first == 0; i++) { 13 | for (int j = i+1; j <= n && result.first == 0; j++) { 14 | int lier = 0, wolf = 0; 15 | for (int k = 1; k <= n; k++) { 16 | if (member[k] < 0 && abs(member[k]) != i && abs(member[k]) != j 17 | || member[k] > 0 && (abs(member[k]) == i || abs(member[k]) == j)) { 18 | if (k != i && k != j) lier += 1; 19 | else wolf += 1; 20 | } 21 | } 22 | if (lier == 1 && wolf == 1) 23 | result = {i, j}; 24 | } 25 | } 26 | if (result.first == 0) cout << "No Solution" << endl; 27 | else cout << result.first << " " << result.second << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1090_危险品装箱 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int n, m, k, e; 8 | vector> rules; 9 | pair r; 10 | cin >> n >> m; 11 | for (int i = 0; i < n; i++) { 12 | cin >> r.first >> r.second; 13 | rules.push_back(r); 14 | } 15 | for (int i = 0; i < m; i++) { 16 | cin >> k; 17 | bool isOk = true; 18 | unordered_set menu; 19 | for (int j = 0; j < k; j++) { 20 | cin >> e; 21 | menu.insert(e); 22 | } 23 | for (const auto &p : rules) { 24 | if (menu.find(p.first) != menu.end() && menu.find(p.second) != menu.end()) { 25 | isOk = false; 26 | break; 27 | } 28 | } 29 | cout << (isOk ? "Yes" : "No") < 2 | #include 3 | using namespace std; 4 | 5 | int GetBits(int k) { 6 | int b; 7 | for (b = 0; k != 0; b++) k /= 10; 8 | return b; 9 | } 10 | 11 | int main() { 12 | int n, m, k; 13 | cin >> m; 14 | for (int i = 0; i < m; i++) { 15 | cin >> k; 16 | bool isOk = false; 17 | for (int n = 1; n < 10; n++) { 18 | if (n*k*k % int(pow(10, GetBits(k))) == k) { 19 | isOk = true; 20 | cout << n << " " << n*k*k << endl; 21 | break; 22 | } 23 | } 24 | if (!isOk) cout << "No" << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1092_最好吃的月饼 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sales[1005] = {0}; 5 | 6 | int main() { 7 | int n, m, v; 8 | int maxSales = 0; 9 | cin >> n >> m; 10 | for (int i = 0; i < m; i++) { 11 | for (int j = 0; j < n; j++) { 12 | cin >> v; 13 | sales[j] += v; 14 | maxSales = maxSales > sales[j] ? maxSales : sales[j]; 15 | } 16 | } 17 | cout << maxSales << endl; 18 | bool first = true; 19 | for (int i = 0; i < n; i++) { 20 | if (maxSales == sales[i]) { 21 | if (first) { 22 | cout << i+1; 23 | first = false; 24 | } else { 25 | cout << " " << i+1; 26 | } 27 | } 28 | } 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1093_字符串A+B (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | unordered_map character; 8 | string a, b; 9 | getline(cin, a); getline(cin, b); 10 | for (auto const &ch : a+b) { 11 | if (!character[ch]) cout << ch; 12 | character[ch] = true; 13 | } 14 | cout << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1094_谷歌的招聘 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isPrime(int k) { 7 | for (int i = 2; i <= (int)sqrt((double)k); i++) { 8 | if (k % i == 0) return false; 9 | } 10 | return true; 11 | } 12 | 13 | int GetInteger(string s, int pos, int numOfBis) { 14 | int r = 0; 15 | for (int i = 0; i < numOfBis; i++, pos++) { 16 | r = r*10 + s[pos] - '0'; 17 | } 18 | return r; 19 | } 20 | 21 | int main() { 22 | int L, K; 23 | bool isOk = false; 24 | string N; 25 | cin >> L >> K >> N; 26 | for (int i = 0; i <= L-K; i++) { 27 | int val = GetInteger(N, i, K); 28 | if (isPrime(val)) { 29 | isOk = true; 30 | cout << N.substr(i, K) << endl; // 前导0也需要输出 31 | break; 32 | } 33 | } 34 | if (!isOk) cout << "404" << endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1096_大美数 (25).py: -------------------------------------------------------------------------------- 1 | def check(n, factors): 2 | # check all combinations if valid 3 | for n1 in range(0, len(factors)): 4 | for n2 in range(n1+1, len(factors)): 5 | for n3 in range(n2+1, len(factors)): 6 | for n4 in range(n3+1, len(factors)): 7 | s = factors[n1] + factors[n2] + factors[n3] + factors[n4] 8 | if s % n == 0: 9 | return True 10 | return False 11 | 12 | k = int(input()) 13 | nums = map(int, input().split()) 14 | for t in nums: 15 | # find all factors 16 | factors = [] 17 | for i in range(1, t//2+1): 18 | if t % i == 0: 19 | factors.append(i) 20 | factors.append(t) 21 | 22 | print("Yes" if check(t, factors) else "No") 23 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1097_矩阵行平移 (20).py: -------------------------------------------------------------------------------- 1 | n, k, x = map(int, input().split()) 2 | s = [0]*n 3 | r_k = 1 # sequence iterator 4 | for r in range(1, n+1): 5 | e = list(map(int, input().split())) 6 | if r % 2 == 1: 7 | # offset supplement 8 | for i in range(r_k): 9 | s[i] += x 10 | # original element 11 | for i in range(r_k, n): 12 | s[i] += e[i-r_k] 13 | # update iterator 14 | r_k += 1 15 | r_k = 1 if r_k == k+1 else r_k 16 | else: 17 | for i in range(0, n): 18 | s[i] += e[i] 19 | 20 | print(' '.join(map(str, s))) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1098_岩洞施工 (20).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | top = map(int, input().split()) 3 | bottom = map(int, input().split()) 4 | high = min(top) 5 | low = max(bottom) 6 | if high > low: 7 | print("Yes", high-low) 8 | else: 9 | print("No", low-high+1) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1099_性感素数 (20).py: -------------------------------------------------------------------------------- 1 | def isPrime(n): 2 | if n <= 1: return False 3 | for i in range(2, int(n**0.5)+1): 4 | if n % i == 0: 5 | return False 6 | return True 7 | 8 | ans = None 9 | n = int(input()) 10 | if isPrime(n): 11 | if n-6 > 0 and isPrime(n-6): 12 | ans = "Yes\n%d" % (n-6) 13 | elif n+6 > 0 and isPrime(n+6): 14 | ans = "Yes\n%d" % (n+6) 15 | 16 | if not ans: 17 | ans = "No\n" 18 | seed = n+1 19 | while True: 20 | if isPrime(seed) and (seed-6 > 0 and isPrime(seed-6) or isPrime(seed+6)): 21 | ans += str(seed) 22 | break 23 | seed += 1 24 | print(ans) 25 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1100_校庆 (25).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | alumnis = set() 3 | for i in range(n): 4 | alumnis.add(input()) 5 | 6 | alumni_count = 0 7 | elder = [None, 0] # 0: id, 1: alumni(1) or not(0) 8 | m = int(input()) 9 | for i in range(m): 10 | person = input() 11 | if person in alumnis: 12 | alumni_count += 1 13 | if elder[0] is None or elder[1] == 0 or (elder[1] == 1 and person[6:14] < elder[0][6:14]): 14 | elder = (person, 1) 15 | else: 16 | if elder[0] is None or (elder[1] == 0 and person[6:14] < elder[0][6:14]): 17 | elder = (person, 0) 18 | 19 | print(alumni_count) 20 | print(elder[0]) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1101_B是A的多少倍 (15).py: -------------------------------------------------------------------------------- 1 | a, d = input().split() 2 | d = int(d) 3 | b = int(a[-d:] + a[:-d]) 4 | print("%.2f" % (b/int(a))) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1102_教超冠军卷 (20).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | tot = [0, 0] 3 | ans = [None, None] 4 | for i in range(n): 5 | id, price, count = input().split() 6 | price, count = int(price), int(count) 7 | if count >= tot[0]: 8 | tot[0] = count 9 | ans[0] = id 10 | 11 | if count * price >= tot[1]: 12 | tot[1] = count * price 13 | ans[1] = id 14 | 15 | print("%s %d" % (ans[0], tot[0])) 16 | print("%s %d" % (ans[1], tot[1])) 17 | 18 | 19 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1103_缘分数 (20).py: -------------------------------------------------------------------------------- 1 | def is_square(x): 2 | lo, hi = 1, x 3 | while lo <= hi: 4 | mid = (lo + hi) // 2 5 | if mid * mid == x: return True 6 | elif mid * mid > x: hi = mid - 1 7 | else: lo = mid + 1 8 | return False 9 | 10 | 11 | m, n = map(int, input().split()) 12 | 13 | ans = [] 14 | for a in range(m, n+1): 15 | c_2 = a*a*a - (a-1)*(a-1)*(a-1) 16 | if not is_square(c_2): continue 17 | c = int(c_2**0.5) 18 | 19 | t = 2*c-1 20 | if not is_square(t): continue 21 | t_0_5 = int(t**0.5) 22 | if t_0_5 % 2 == 0: continue 23 | b = (1+t_0_5) // 2 24 | if b <= 0 or b == 1: continue # (1, 1) is not a valid result ???? 25 | ans.append("%d %d" % (a, b)) 26 | if not ans: 27 | print("No Solution") 28 | else: 29 | print('\n'.join(ans)) 30 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1106_2019数列 (15).py: -------------------------------------------------------------------------------- 1 | f = [2, 0, 1, 9] 2 | ans = "" 3 | n = int(input()) 4 | for i in range(n): 5 | if i >= 4: 6 | f[i%4] = sum(f) % 10 7 | ans += str(f[i%4]) 8 | print(ans) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1107_老鼠爱大米 (20).py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | ans = [] 3 | for i in range(n): 4 | g = map(int, input().split()) 5 | ans.append(max(g)) 6 | 7 | print(" ".join(map(str, ans))) 8 | print(max(ans)) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1108_String复读机 (20).py: -------------------------------------------------------------------------------- 1 | s = input() 2 | count = {} 3 | for ch in s: 4 | if ch in "String": 5 | count[ch] = count.get(ch, 0) + 1 6 | 7 | ans = [] 8 | while sum(count.values()) != 0: 9 | for ch in "String": 10 | if count.get(ch, 0) > 0: 11 | ans += ch 12 | count[ch] -= 1 13 | 14 | print("".join(ans)) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1109_擅长C (20).py: -------------------------------------------------------------------------------- 1 | import string 2 | alphabet = {} 3 | for ch in string.ascii_uppercase: 4 | alphabet[ch] = [] 5 | for i in range(7): 6 | alphabet[ch].append(input()) 7 | 8 | word = "" 9 | words = [] 10 | sentence = input() 11 | for idx, ch in enumerate(sentence): 12 | if ch in string.ascii_uppercase: 13 | word += ch 14 | else: 15 | if word: 16 | words.append(word) 17 | word = "" 18 | 19 | if word: 20 | words.append(word) 21 | 22 | is_first_word = True 23 | for w in words: 24 | # deal with blank line 25 | if is_first_word: 26 | is_first_word = False 27 | else: 28 | print() 29 | 30 | # row scan from top to bottom 31 | # in each row, characters of word scan 32 | # record the charcacter pattern of word in current row. 33 | for i in range(7): 34 | ans = [] 35 | for ch in w: 36 | ans.append(alphabet[ch][i]) 37 | print(" ".join(ans)) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1111_对称日 (15).py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | def isPalindromic(s): 4 | return s == s[::-1] 5 | 6 | month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 7 | 8 | n = int(input()) 9 | for i in range(n): 10 | s = input() 11 | md, y = s.split(',') 12 | y = y.strip().zfill(4) 13 | m, d = md.split(' ') 14 | m = str(month.index(m) + 1).zfill(2) 15 | d = d.zfill(2) 16 | date = y + m + d 17 | print('Y' if isPalindromic(date) else 'N', end=' ') 18 | print(date) 19 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1112_超标区间 (20).py: -------------------------------------------------------------------------------- 1 | n, t = map(int, input().split()) 2 | points = list(map(int, input().split())) 3 | 4 | m = max(points) 5 | if m <= t: 6 | print(m) 7 | else: 8 | begin = -1 9 | for i in range(n): 10 | if points[i] > t: 11 | if begin == -1: 12 | begin = i 13 | if points[i] <= t and begin != -1: 14 | print("[{}, {}]".format(begin, i-1)) 15 | begin = -1 16 | if begin != -1: 17 | print("[{}, {}]".format(begin, n-1)) 18 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1113_钱串子的加法 (20).py: -------------------------------------------------------------------------------- 1 | bs="0123456789abcdefghijklmnopqrstuvwxyz" 2 | 3 | def add(a, b, base): 4 | a, b = a[::-1], b[::-1] 5 | c = "" 6 | carry = 0 7 | for i in range(max(len(a), len(b))): 8 | if i < len(a): 9 | carry += bs.index(a[i]) 10 | if i < len(b): 11 | carry += bs.index(b[i]) 12 | c += bs[carry % base] 13 | carry //= base 14 | if carry: 15 | c += bs[carry] 16 | while len(c) > 1 and c[-1] == "0": 17 | c = c[:-1] 18 | return c[::-1] 19 | 20 | a, b = input().split() 21 | print(add(a, b, 30)) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1114_全素日 (20).py: -------------------------------------------------------------------------------- 1 | def isPrime(n): 2 | if n == 1: 3 | return False 4 | for i in range(2, int(n**0.5)+1): 5 | if n % i == 0: 6 | return False 7 | return True 8 | 9 | d = input() 10 | 11 | ok = True 12 | for i in range(len(d)): 13 | t = d[i:] 14 | r = isPrime(int(t)) 15 | ok &= r 16 | print("{} {}".format(t, "Yes" if r else "No")) 17 | if ok: 18 | print("All Prime!") -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1116_多二了一点 (15).py: -------------------------------------------------------------------------------- 1 | s = input() 2 | if len(s) % 2 == 1: 3 | print('Error: %d digit(s)' % len(s)) 4 | else: 5 | a, b = int(s[:len(s)//2]), int(s[len(s)//2:]) 6 | if b - a == 2: 7 | print('Yes: %d - %d = 2' % (b, a)) 8 | else: 9 | print('No: %d - %d != 2' % (b, a)) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1117_数字之王 (20).py: -------------------------------------------------------------------------------- 1 | def func(arr): 2 | is_all_single = True 3 | mem = [] 4 | for i, v in enumerate(arr): 5 | if v >= 10: is_all_single = False 6 | 7 | r = 0 if v == 0 else 1 8 | while v != 0: 9 | r *= (v % 10) ** 3 10 | v //= 10 11 | 12 | v, r = r, 0 13 | while v != 0: 14 | r += v % 10 15 | v //= 10 16 | 17 | mem.append((i, r)) 18 | if is_all_single: return 19 | else: 20 | for i, v in mem: 21 | arr[i] = v 22 | func(arr) 23 | 24 | n1, n2 = map(int, input().split()) 25 | arr = [i for i in range(n1, n2+1)] 26 | 27 | func(arr) 28 | max_count = 0 29 | ans = [] 30 | for v in set(arr): 31 | cnt = arr.count(v) 32 | max_count = max(max_count, cnt) 33 | ans.append((v, cnt)) 34 | 35 | ans = filter(lambda x: x[1] == max_count, ans) 36 | ans = sorted(ans, key=lambda x: x[0]) 37 | print(max_count) 38 | print(' '.join(map(lambda x: str(x[0]), ans))) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1118_如需挪车请致电 (20).py: -------------------------------------------------------------------------------- 1 | pinyin = ['ling', 'yi', 'er', 'san', 'si', 'wu', 'liu', 'qi', 'ba', 'jiu'] 2 | 3 | def get_number(s): 4 | if 'sqrt' in s: 5 | n = int(s[4:]) 6 | return int(n ** 0.5) 7 | 8 | if s in pinyin: 9 | return pinyin.index(s) 10 | 11 | if '^' in s: 12 | n, m = map(int, s.split('^')) 13 | return int(n ** m) 14 | 15 | return int(eval(s)) 16 | 17 | phone = [] 18 | for i in range(11): 19 | s = input() 20 | phone.append(get_number(s)) 21 | print(''.join(map(str, phone))) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1119_胖达与盆盆奶 (20).py: -------------------------------------------------------------------------------- 1 | # construct peak array from left and right, then find the max of each element in the two arrays 2 | # stands for the max height, then add 1 and multiply 100 is required the volume of milk 3 | n = int(input()) 4 | arr = list(map(int, input().split())) 5 | 6 | left, right = [1], [1] 7 | for i in range(1, n): 8 | if arr[i] > arr[i-1]: 9 | left.append(left[i-1] + 1) 10 | elif arr[i] == arr[i-1]: 11 | left.append(left[i-1]) 12 | else: 13 | left.append(1) 14 | 15 | for i in range(n-2, -1, -1): 16 | if arr[i] > arr[i+1]: 17 | right.append(right[n-i-2] + 1) 18 | elif arr[i] == arr[i+1]: 19 | right.append(right[n-i-2]) 20 | else: 21 | right.append(1) 22 | 23 | right = list(reversed(right)) 24 | 25 | ans = 0 26 | for i in range(n): 27 | ans += (max(left[i], right[i]) + 1) * 100 28 | print(ans) -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1121_祖传好运 (15).py: -------------------------------------------------------------------------------- 1 | def is_lucky(num_str): 2 | result, val = True, 0 3 | for i in range(0, len(num_str)): 4 | val = val * 10 + int(num_str[i]) 5 | result &= val % (i + 1) == 0 6 | return result 7 | 8 | 9 | input() 10 | num_str_arr = input().split() 11 | for num_str in num_str_arr: 12 | print("Yes" if is_lucky(num_str) else "No") 13 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1122_找奇葩 (20).py: -------------------------------------------------------------------------------- 1 | input() 2 | arr = map(int, input().split()) 3 | # filter out even numbers 4 | arr = list(filter(lambda v: v % 2 == 1, arr)) 5 | result = arr[0] 6 | # xor remove odd number pair 7 | for i in range(1, len(arr)): 8 | result ^= arr[i] 9 | print(result) 10 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1123_舍入 (20).py: -------------------------------------------------------------------------------- 1 | from decimal import * 2 | 3 | 4 | def round_number(method, number, precision): 5 | context = getcontext() 6 | context.prec = 300 # 精度位数一定要大 7 | if method == "1": 8 | context.rounding = ROUND_HALF_UP 9 | elif method == "2": 10 | context.rounding = ROUND_DOWN 11 | elif method == "3": 12 | context.rounding = ROUND_HALF_EVEN 13 | 14 | d = Decimal(number).quantize(Decimal("0." + "0" * precision)) 15 | if d == 0: 16 | d = abs(d) 17 | ans = str(d) 18 | if ans[0] == "+": 19 | ans = ans[1:] 20 | return ans 21 | 22 | 23 | n, d = map(int, input().split()) 24 | for i in range(n): 25 | op, num = input().split() 26 | print(round_number(op, num, d)) 27 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1124_最近的斐波那契数 (20).py: -------------------------------------------------------------------------------- 1 | fib = [0, 1] 2 | 3 | count = 0 4 | while fib[-1] <= 10**8: 5 | new = fib[-1] + fib[-2] 6 | fib.append(new) 7 | 8 | v = int(input()) 9 | ans, diff = -1, 10**8 10 | for i in range(len(fib)): 11 | if fib[i] >= v: 12 | if abs(fib[i] - v) < diff: 13 | ans = fib[i] 14 | break 15 | else: 16 | ans = fib[i] 17 | diff = abs(fib[i] - v) 18 | print(ans) 19 | -------------------------------------------------------------------------------- /PAT (Basic Level) Practice (中文)/1125_子串与子列 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | string s, p; 8 | cin >> s >> p; 9 | 10 | pair ans{10e5+10, ""}; 11 | // brute force match p in s and find the shortest substring 12 | for (int i = 0; i < s.size(); i++) { 13 | if (s[i] != p[0]) continue; 14 | int l = i, r = l+1; 15 | int k = 1; 16 | for (; r < s.size() && k != p.size(); r++) { 17 | if (s[r] == p[k]) k++; 18 | if (k == p.size()) break; 19 | } 20 | 21 | int len = r-l+1; 22 | if (k == p.size() && len < ans.first) { 23 | ans.first = len; 24 | ans.second = s.substr(l, len); 25 | } 26 | } 27 | 28 | cout << ans.second << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /PAT (Top Level) Practice/1015_Letter-moving Game (35).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | 移动最少的字符另S->T,即等价于保留原有字符中最多相对应不移动的字符 5 | 则S和T的公共串关系决定答案,即最长公共的S子序列和T子串是最多不移动的字符 6 | */ 7 | int main() { 8 | string s, t; 9 | cin >> s >> t; 10 | int size = s.size(), v = 0; 11 | for (int i = 0; i < size; i++) { 12 | for (int j = 0, k= i; j < size; j++) { 13 | if (k >= size) break; 14 | if (s[j] == t[k]) k++; 15 | v = max(v, k-i); 16 | } 17 | } 18 | cout << size - v << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /PAT (Top Level) Practice/1018_Subnumbers (35).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | ''' 3 | 统计每位的贡献值 4 | 定义f[i]是第i位的贡献值,比如abc,i为a这位,那么f[i] = a+ab+abc = a + a*10 + b + a*100 + b*10 + c 5 | f[i]的递推关系:比如xabc 6 | 那么当前x为 7 | f[i] = x + xa + xab + xabc = x*1 + x*10+a + x*100+a*10+b + x*1000+a*100+b*10+c = x + x*10 + x*100 + x*1000 + (a + a*10 + b + a*100 + b*10 + c) = x*(1+10+100+1000) + f[i+1] 8 | = x*1111 + f[i+1] 9 | ''' 10 | MOD = 1000000007 11 | n = map(int, list(raw_input())) 12 | 13 | f, b = [0] * (len(n)+1), 0 14 | for i in xrange(len(n)-1, -1, -1): 15 | b = (b*10 + 1) % MOD 16 | f[i] = (n[i]*b) % MOD + f[i+1] 17 | f[i] %= MOD 18 | 19 | ret = 0 20 | for i in xrange(len(n)): 21 | ret += 0 if n[i] == 0 else f[i] 22 | ret %= MOD 23 | print ret -------------------------------------------------------------------------------- /PAT (Top Level) Practice/1020_Delete At Most Two Characters (35).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /* 7 | dp[i][j] 表示前i个字母,删去j个字符的结果,在第j位的状态转移是s[j]是否需要删除 8 | dp[i][0] = 1 9 | dp[i][1] = dp[i-1][0] + dp[i-1][1], 如果s[i]==s[i-1],比如abb,会多一次重复计算 10 | dp[i][2] = dp[i-1][2] + dp[i-1][1], 分三种情况abcdee, abcede, abcde, 11 | 前两种会有重复计算分别是(dp[i-2][1], a|b|c|d e), (1, ed de) 12 | */ 13 | typedef long long int64; 14 | 15 | int64 dp[1000005][3]; 16 | 17 | int main() { 18 | string s; 19 | cin >> s; 20 | int n = s.size(); 21 | for (int i = 0; i < n; i++) dp[i][0] = 1; 22 | dp[0][1] = 1; 23 | for (int i = 1; i < n; i++) dp[i][1] = (s[i] == s[i-1] ? 0 : 1) + dp[i-1][1]; 24 | dp[1][2] = 1; 25 | for (int i = 2; i < n; i++) { 26 | int64 t = dp[i-1][2] + dp[i-1][1]; 27 | if (s[i] == s[i-1]) dp[i][2] = t - dp[i-2][1]; // abcdee 28 | else if (s[i] == s[i-2]) dp[i][2] = t - 1; // abcede 29 | else dp[i][2] = t; // abcde 30 | } 31 | cout << dp[n-1][0] + dp[n-1][1] + dp[n-1][2] << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/2-05_求集合数据的均方差.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | vector elem; 11 | 12 | int main() 13 | { 14 | int n,i; 15 | double e,tot = 0; 16 | scanf("%d", &n); 17 | for(i = 1; i <= n; i++) 18 | { 19 | scanf("%lf", &e); 20 | tot += e; 21 | elem.push_back(e); 22 | } 23 | double avg = tot / n; 24 | double tmp = 0.0; 25 | for(i = 0; i < n; i++) 26 | { 27 | tmp += (elem[i]-avg)*(elem[i]-avg); 28 | } 29 | printf("%.5lf\n", sqrt(tmp/n)); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/2-06_数列求和.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | vector ans; 11 | 12 | int main() 13 | { 14 | int n,i,a,c; 15 | scanf("%d %d", &a, &n); 16 | 17 | i = 0; 18 | c = 0; 19 | while(n) 20 | { 21 | ans.push_back((c + a * n) % 10); 22 | c = (c + a * n) / 10; 23 | n--; 24 | } 25 | if(c) ans.push_back(c); 26 | if(ans.size() == 0) printf("0"); 27 | else 28 | { 29 | for( i = ans.size()-1 ; i >= 0; i--) 30 | { 31 | printf("%d", ans[i]); 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/2-07_素因子分解.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | 12 | 13 | int main() 14 | { 15 | long long n,i,val; 16 | cin >> n; 17 | val = n; 18 | cout << n << "="; 19 | int first = 1; 20 | int ok = 0; 21 | for(i = 2; i <= floor(sqrt((double)val)+0.5); i++) 22 | { 23 | int cnt = 0; 24 | while(n % i == 0) 25 | { 26 | 27 | cnt++; 28 | n /= i; 29 | } 30 | if(cnt) 31 | { 32 | ok = 1; 33 | if(first) 34 | { 35 | if(cnt == 1) cout << i; 36 | else cout << i << "^" << cnt; 37 | first = 0; 38 | } 39 | else 40 | { 41 | if(cnt == 1) cout << "*" << i; 42 | else cout << "*" << i << "^" << cnt; 43 | } 44 | } 45 | } 46 | if(!ok) cout << val; 47 | cout << endl; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/2-10_海盗分赃.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 宝石分配方案 | P 7 | D-1 0 1 | 3 8 | D-3 0 1 2 | 4 9 | D-3 0 1 2 0 | 5 10 | D-4 0 1 2 0 1 | 6 11 | D-4 0 1 2 0 1 0 | 7 12 | ...... 13 | 满足利益最大化 14 | */ 15 | 16 | int main() 17 | { 18 | int d,p,i; 19 | int ans[101] = {0,0,0,1,3,3}; 20 | int cur = 4; 21 | for(i = 6; i <= 100; ) 22 | { 23 | int cnt = 2; 24 | while(cnt--) 25 | { 26 | ans[i++] = cur; 27 | } 28 | cur++; 29 | } 30 | 31 | cin >> d >> p; 32 | cout << d-ans[p] << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/2-11_两个有序链表序列的合并.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | list A,B,C; 11 | 12 | 13 | int main() 14 | { 15 | int e; 16 | while(scanf("%d", &e), e!= -1) 17 | { 18 | A.push_back(e); 19 | } 20 | while(scanf("%d", &e), e!= -1) 21 | { 22 | B.push_back(e); 23 | } 24 | while(!A.empty() && !B.empty()) 25 | { 26 | if(A.front() > B.front()) 27 | { 28 | C.push_back(B.front()); 29 | B.pop_front(); 30 | } 31 | else 32 | { 33 | C.push_back(A.front()); 34 | A.pop_front(); 35 | } 36 | } 37 | while(!A.empty()) 38 | { 39 | C.push_back(A.front()); 40 | A.pop_front(); 41 | } 42 | while(!B.empty()) 43 | { 44 | C.push_back(B.front()); 45 | B.pop_front(); 46 | } 47 | if(C.empty()) cout << "NULL" << endl; 48 | else 49 | { 50 | cout << C.front(); 51 | C.pop_front(); 52 | while(!C.empty()) 53 | { 54 | cout << " " << C.front(); 55 | C.pop_front(); 56 | } 57 | cout << endl; 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/2-12_两个有序链表序列的交集.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | list A,B,C; 11 | 12 | 13 | int main() 14 | { 15 | int e; 16 | while(scanf("%d", &e), e!= -1) 17 | { 18 | A.push_back(e); 19 | } 20 | while(scanf("%d", &e), e!= -1) 21 | { 22 | B.push_back(e); 23 | } 24 | while(!A.empty() && !B.empty()) 25 | { 26 | if(A.front() < B.front()) 27 | { 28 | A.pop_front(); 29 | } 30 | else if(A.front() > B.front()) 31 | { 32 | B.pop_front(); 33 | } 34 | else 35 | { 36 | C.push_back(A.front()); 37 | A.pop_front(); 38 | B.pop_front(); 39 | } 40 | } 41 | if(C.empty()) cout << "NULL" << endl; 42 | else 43 | { 44 | cout << C.front(); 45 | C.pop_front(); 46 | while(!C.empty()) 47 | { 48 | cout << " " << C.front(); 49 | C.pop_front(); 50 | } 51 | cout << endl; 52 | } 53 | return 0; 54 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/2-13_两个有序序列的中位数.cpp: -------------------------------------------------------------------------------- 1 | http://blog.csdn.net/Hackbuteer1/article/details/7584838 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int A[100005],B[100005]; 12 | 13 | int findMidNum(int A[], int B[], int len) 14 | { 15 | if(len == 1) return A[0] > B[0] ? B[0] : A[0]; 16 | int mid = (len-1) / 2; 17 | if(A[mid] == B[mid]) return A[mid]; 18 | else if(A[mid] < B[mid]) return findMidNum(&A[len-mid-1], &B[0], mid+1); 19 | else return findMidNum(&A[0], &B[len-mid-1], mid+1); 20 | } 21 | int main() 22 | { 23 | int n,i; 24 | scanf("%d", &n); 25 | for(i = 0; i < n; i++) 26 | { 27 | scanf("%d", &A[i]); 28 | } 29 | for(i = 0; i < n; i++) 30 | { 31 | scanf("%d", &B[i]); 32 | } 33 | int ans = findMidNum(A, B, n); 34 | cout << ans << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/4-07_修理牧场.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | /* 8 | hoffman树,权值最优二叉树 ,哈夫曼树 9 | */ 10 | 11 | 12 | priority_queue, greater > pq; 13 | 14 | int main() 15 | { 16 | int n,i,e,ans = 0; 17 | cin >> n; 18 | for(i = 1; i <= n; i++) 19 | { 20 | cin >> e; 21 | pq.push(e); 22 | } 23 | while(pq.size() != 1) 24 | { 25 | int first = pq.top(); 26 | pq.pop(); 27 | int second = pq.top(); 28 | pq.pop(); 29 | ans += first+second; 30 | pq.push(first+second); 31 | } 32 | cout << ans << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/6-06_任务调度的合理性.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | /* 5 | 拓扑排序 6 | */ 7 | vector AdjList[150]; 8 | int in[150]; 9 | int vis[150]; 10 | bool TopoSort(int n) 11 | { 12 | int limit = n, i; 13 | while(limit--) 14 | { 15 | int find = 0; 16 | for(i = 1; i <= n; i++) 17 | { 18 | if(!in[i] && !vis[i]) 19 | { 20 | find = 1; 21 | break; 22 | } 23 | } 24 | if(!find) return false; 25 | int cur = i; 26 | vis[cur] = 1; 27 | for(i = 0; i < AdjList[cur].size(); i++) 28 | { 29 | in[ AdjList[cur][i] ] --; 30 | } 31 | } 32 | return true; 33 | } 34 | 35 | int main() 36 | { 37 | int n,i,task,k,j; 38 | cin >> n; 39 | for(i = 1; i <= n; i++) 40 | { 41 | cin >> k; 42 | in[i] += k; 43 | for(j = 1; j <= k; j++) 44 | { 45 | cin >> task; 46 | AdjList[task].push_back(i); 47 | } 48 | } 49 | if(TopoSort(n)) 50 | cout << "1" << endl; 51 | else 52 | cout << "0" << endl; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/7-05_魔法优惠券.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | /* 6 | 贪心算法 正数*正数 负数*负数 7 | */ 8 | int main() 9 | { 10 | deque A,B; 11 | int m,n,i,e; 12 | cin >> m; 13 | for (i = 0; i < m; i++) 14 | { 15 | cin >> e; 16 | A.push_back(e); 17 | } 18 | cin >> n; 19 | for (i = 0; i < n; i++) 20 | { 21 | cin >> e; 22 | B.push_back(e); 23 | } 24 | sort(A.begin(), A.end()); 25 | sort(B.begin(), B.end()); 26 | int ans = 0; 27 | while(!A.empty() && !B.empty() && A.back() > 0 && B.back() > 0) 28 | { 29 | ans += A.back()*B.back(); 30 | A.pop_back(); 31 | B.pop_back(); 32 | } 33 | while(!A.empty() && !B.empty() && A.front() < 0 && B.front() < 0) 34 | { 35 | ans += A.front()*B.front(); 36 | A.pop_front(); 37 | B.pop_front(); 38 | } 39 | cout << ans << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /《数据结构学习与实验指导》实验项目集/8-06_畅通工程之局部最小花费问题.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/《数据结构学习与实验指导》实验项目集/8-06_畅通工程之局部最小花费问题.cpp -------------------------------------------------------------------------------- /中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题/自测-1_打印沙漏 (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import math 3 | 4 | n, ch = raw_input().split() 5 | n = int(n) 6 | 7 | lvl = int(math.sqrt((n+1)/2)) 8 | total = 2*lvl**2-1 # fomula: Sn = 2*n^2 -1 9 | 10 | def BuildSandGlass(lvl, whitespace, ch): 11 | if (lvl == 1): 12 | return ' ' * whitespace + ch 13 | else: 14 | s = ' ' * whitespace + ch*(2*lvl-1) 15 | return '%s\n%s\n%s' % (s, BuildSandGlass(lvl-1, whitespace+1, ch), s) 16 | 17 | print BuildSandGlass(lvl, 0, ch) 18 | print n - total -------------------------------------------------------------------------------- /中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题/自测-2_素数对猜想 (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # 筛法求所有小于等于n的素数 4 | n = int(raw_input()) 5 | isPrime = [True] * (n+1) 6 | isPrime[0] = isPrime[1] = False 7 | primes = [] 8 | for i in xrange(2, n+1, 1): 9 | if not isPrime[i]: continue 10 | primes.append(i) 11 | for j in xrange(i+i, n+1, i): 12 | isPrime[j] = False 13 | 14 | count = 0 15 | for i in xrange(1, len(primes)): 16 | if primes[i] - primes[i-1] == 2: 17 | count += 1 18 | print count -------------------------------------------------------------------------------- /中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题/自测-3_数组元素循环右移问题 (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # (A'B')' = BA, '表示转置 4 | 5 | n, m = map(int, raw_input().split()) 6 | arr = raw_input().split() 7 | 8 | pos = m % n 9 | # print ' '.join(reversed(list(reversed(arr[:-pos])) + list(reversed(arr[-pos:])))) 10 | print ' '.join((arr[n-pos-1::-1] + arr[:n-pos-1:-1])[::-1]) -------------------------------------------------------------------------------- /中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题/自测-4_Have Fun with Numbers (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | n = int(raw_input()) 4 | n2 = 2*n 5 | 6 | isFunNumber = ''.join(sorted(list(str(n)))) == ''.join(sorted(list(str(n2)))) 7 | print 'Yes' if isFunNumber else 'No' 8 | print n2 9 | 10 | -------------------------------------------------------------------------------- /中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题/自测-5_Shuffling Machine (20).py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | cards = [None] 4 | cards = cards + ['S' + str(i) for i in xrange(1, 14, 1)] 5 | cards = cards + ['H' + str(i) for i in xrange(1, 14, 1)] 6 | cards = cards + ['C' + str(i) for i in xrange(1, 14, 1)] 7 | cards = cards + ['D' + str(i) for i in xrange(1, 14, 1)] 8 | cards = cards + ['J1', 'J2'] 9 | result = [None]*len(cards) 10 | 11 | k = int(raw_input()) 12 | order = map(int, raw_input().split()) 13 | order.insert(0, None) 14 | for i in xrange(1, len(order)): 15 | p = i 16 | for j in xrange(k): 17 | p = order[p] 18 | result[p] = cards[i] 19 | print ' '.join(result[1:]) 20 | 21 | 22 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-001_Hello World.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-001_Hello World.go -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-001_Hello World.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-001_Hello World.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-002_打印沙漏.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-002_打印沙漏.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-003_个位数统计.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-003_个位数统计.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-004_计算摄氏温度.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-004_计算摄氏温度.go -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-004_计算摄氏温度.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-004_计算摄氏温度.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-005_考试座位号.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-005_考试座位号.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-006_连续因子.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-006_连续因子.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-007_念数字.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-007_念数字.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-008_求整数段和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-008_求整数段和.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-009_N个数求和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-009_N个数求和.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-010_比较大小.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-010_比较大小.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-011_A-B.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-011_A-B.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-012_计算指数.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-012_计算指数.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-013_计算阶乘和.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-013_计算阶乘和.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-014_简单题.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-014_简单题.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-015_跟奥巴马一起画方块.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-015_跟奥巴马一起画方块.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-016_查验身份证.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-016_查验身份证.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-017_到底有多二.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-017_到底有多二.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-018_大笨钟.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-018_大笨钟.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-019_谁先倒.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-019_谁先倒.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-020_帅到没朋友.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-020_帅到没朋友.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-021_重要的话说三遍.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-021_重要的话说三遍.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-022_奇偶分家.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-022_奇偶分家.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-023_输出GPLT.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-023_输出GPLT.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-024_后天.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-024_后天.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-025_正整数A+B.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-025_正整数A+B.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-026_I Love GPLT.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-026_I Love GPLT.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-027_出租.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-027_出租.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-028_判断素数.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-028_判断素数.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-029_是不是太胖了.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-029_是不是太胖了.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-030_一帮一.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-030_一帮一.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-031_到底是不是太胖了.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-031_到底是不是太胖了.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-032_Left-pad.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-032_Left-pad.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-033_出生年.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-033_出生年.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-034_点赞.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-034_点赞.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-035_情人节.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-035_情人节.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-036_A乘以B.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-036_A乘以B.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-037_A除以B.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-037_A除以B.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-038_新世界.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-038_新世界.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-039_古风排版.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-039_古风排版.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-040_最佳情侣身高差.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-040_最佳情侣身高差.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-041_寻找250.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-041_寻找250.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-042_日期格式化.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-042_日期格式化.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-043_阅览室.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | book = {} 4 | cnt, readingTime = 0, 0 5 | 6 | while n > 0: 7 | bookID, key, time = input().split() 8 | bookID = int(bookID) 9 | h, m = time.split(':') 10 | timestamp = int(h) * 60 + int(m) 11 | 12 | if bookID == 0: 13 | n -= 1 14 | print(cnt, "{:.0f}".format(0 if cnt == 0 else readingTime/cnt)) 15 | cnt, readingTime = 0, 0 16 | book.clear() 17 | continue 18 | 19 | if key == 'S': 20 | book[bookID] = timestamp 21 | 22 | if key == 'E': 23 | if bookID in book: 24 | cnt += 1 25 | readingTime += timestamp - book[bookID] 26 | del book[bookID] -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-044_稳赢.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-044_稳赢.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-045_宇宙无敌大招呼.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-045_宇宙无敌大招呼.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-046_整除光棍.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-046_整除光棍.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-047_装睡.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-047_装睡.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-048_矩阵A乘以B.py: -------------------------------------------------------------------------------- 1 | ra, ca = map(int, input().split()) 2 | mat1 = [[0] for _ in range(ra)] 3 | for i in range(ra): 4 | mat1[i] = [int(v) for v in input().split()] 5 | 6 | rb, cb = map(int, input().split()) 7 | mat2 = [[0] for _ in range(rb)] 8 | for i in range(rb): 9 | mat2[i] = [int(v) for v in input().split()] 10 | 11 | if ca != rb: 12 | print("Error: %d != %d" % (ca, rb)) 13 | else: 14 | print("%d %d" % (ra, cb)) 15 | mat3 = [[0] * cb for i in range(ra)] 16 | for i in range(ra): 17 | for j in range(cb): 18 | for k in range(ca): 19 | mat3[i][j] += mat1[i][k] * mat2[k][j] 20 | for i in range(ra): 21 | print(" ".join(map(str, mat3[i]))) 22 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-049_天梯赛座位分配.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | teams = list(map(int, input().split())) 3 | 4 | total = sum(teams)*10 5 | seats = {} 6 | prevTeamId, teamId = -1, 0 7 | seatId = 0 8 | while total > 0: 9 | if teamId not in seats: 10 | seats[teamId] = [] 11 | if len(seats[teamId]) != teams[teamId]*10: 12 | total -= 1 13 | if prevTeamId != teamId: 14 | seatId += 1 15 | seats[teamId].append(seatId) 16 | else: 17 | seatId += 2 18 | seats[teamId].append(seatId) 19 | 20 | prevTeamId = teamId 21 | teamId += 1 22 | teamId %= n 23 | 24 | for index, v in enumerate(teams): 25 | print("#%d" % (index+1)) 26 | for i in range(v): 27 | print(" ".join(map(str, seats[index][i*10:(i+1)*10]))) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-050_倒数第N个字符串.py: -------------------------------------------------------------------------------- 1 | l, n = map(int, input().split()) 2 | wide = l 3 | r = 0 4 | while l != 0: 5 | r = r * 26 + 25 6 | l -= 1 7 | 8 | target = r - n + 1 9 | ans = [] 10 | while target != 0: 11 | ans.append(chr(ord('a') + target % 26)) 12 | target //= 26 13 | 14 | # add the missing 'a' at the beginning 15 | if len(ans) < wide: 16 | ans += ['a'] * (wide - len(ans)) 17 | print("".join(ans[::-1])) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-051_打折.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-051_打折.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-052_2018我们要赢.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-052_2018我们要赢.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-053_电子汪.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-053_电子汪.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-054_福到了.py: -------------------------------------------------------------------------------- 1 | ch, rows = input().split() 2 | rows = int(rows) 3 | mat = [[0] for i in range(rows)] 4 | mat_new = [[0] for i in range(rows)] 5 | 6 | for i in range(rows): 7 | mat[i] = "%*s" % (rows, input().replace('@', ch)) 8 | mat_new[i] =mat[i][::-1] 9 | 10 | mat_new = mat_new[::-1] 11 | 12 | isSame = True 13 | for i in range(rows): 14 | if mat[i] != mat_new[rows - i - 1]: 15 | isSame = False 16 | break 17 | 18 | if isSame: 19 | print("bu yong dao le") 20 | 21 | for r in mat_new: 22 | print(r) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-055_谁是赢家.py: -------------------------------------------------------------------------------- 1 | pa, pb = map(int, input().split()) 2 | judge = list(map(int, input().split())) 3 | ja = 3 - sum(judge) 4 | jb = sum(judge) 5 | if (pa > pb and ja > 0) or (pa < pb and ja == 3): 6 | print("The winner is a: %d + %d" % (pa, ja)) 7 | else: 8 | print("The winner is b: %d + %d" % (pb, jb)) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-056_猜数字.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = 0 3 | d = [] 4 | for i in range(n): 5 | name, val = input().split() 6 | val = int(val) 7 | d += [(name, val)] 8 | s += val 9 | avg = s // n // 2 10 | 11 | winner = d[0][0] 12 | t = d[0][1] 13 | 14 | for i in range(n): 15 | if abs(d[i][1] - avg) < t: 16 | t = abs(d[i][1] - avg) 17 | winner = d[i][0] 18 | 19 | print("%d %s" % (avg, winner)) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-057_PTA使我精神焕发.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-057_PTA使我精神焕发.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-058_6翻了.py: -------------------------------------------------------------------------------- 1 | import re 2 | s = input() 3 | s = re.sub('6{10,}', '27', s) 4 | s = re.sub('6{4,9}', '9', s) 5 | print(s) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-059_敲笨钟.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n): 3 | poem = input() 4 | left, right = poem.split(',') 5 | if left.endswith('ong') and right.endswith('ong.'): 6 | print(left + ',' + ' '.join(right.split(' ')[:-3]) + ' qiao ben zhong.') 7 | else: 8 | print('Skipped') 9 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-060_心理阴影面积.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-060_心理阴影面积.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-061_新胖子公式.py: -------------------------------------------------------------------------------- 1 | w, h = map(float, input().split()) 2 | v = w / (h * h) 3 | 4 | print("%.1f" % v) 5 | print("PANG" if v > 25 else "Hai Xing") -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-062_幸运彩票.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n): 3 | t = list(map(int, list(input()))) 4 | if sum(t[:3]) == sum(t[-3:]): 5 | print("You are lucky!") 6 | else: 7 | print("Wish you good luck.") -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-063_吃鱼还是吃肉.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n): 3 | g, h, w = map(int, input().split()) 4 | if g == 1: 5 | hs, ws = 130, 27 6 | else: 7 | hs, ws = 129, 25 8 | 9 | ans = None 10 | if h < hs: 11 | ans = 'duo chi yu!' 12 | elif h == hs: 13 | ans = 'wan mei!' 14 | else: 15 | ans = 'ni li hai!' 16 | 17 | if w < ws: 18 | ans += ' duo chi rou!' 19 | elif w == ws: 20 | ans += ' wan mei!' 21 | else: 22 | ans += ' shao chi rou!' 23 | print(ans) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-064_估值一亿的AI核心代码.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | n = int(input()) 4 | for i in range(n): 5 | q = input() 6 | print(q) 7 | 8 | # 把行首尾的空格全部删掉 9 | q = q.strip() 10 | # 消除原文中多余空格, 把相邻单词间的多个空格换成 1 个空格 11 | q = re.sub(r"\s+", " ", q) 12 | # 把原文中所有大写英文字母变成小写,除了 I 13 | q = re.sub(r"(?![I])[A-Z]", lambda pat: pat.group(0).lower(), q) 14 | # 把标点符号前面的空格删掉 15 | q = re.sub(r"\s+([^a-zA-Z0-9])", r"\1", q) 16 | # 避免原文中的 I 被替换成 you, 先用占位符替换 I, 然后再把占位符替换成 you 17 | # /b 表示单词边界 18 | q = re.sub(r"\bI\b", r"place_holder", q) 19 | # 把原文中所有独立的 can you、could you 对应地换成 I can、I could 20 | q = re.sub(r"\bcan you\b", r"I can", q) 21 | q = re.sub(r"\bcould you\b", r"I could", q) 22 | # 把原文中所有独立的 I(占位符) 对应地换成 you 23 | q = re.sub(r"(place_holder|\bme\b)", r"you", q) 24 | # 把原文中所有的问号 ? 换成惊叹号 ! 25 | q = re.sub(r"\?", r"!", q) 26 | 27 | print("AI: %s" % q) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-065_嫑废话上代码.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-065_嫑废话上代码.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-066_猫是液体.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-066_猫是液体.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-067_洛希极限.py: -------------------------------------------------------------------------------- 1 | v, b, c = input().split() 2 | v, c = float(v), float(c) 3 | 4 | roche = v * (2.455 if b == '0' else 1.26) 5 | print("%.2f %s" % (roche, 'T_T' if roche > c else '^_^')) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-068_调和平均.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = list(map(lambda x: 1/float(x), input().split())) 3 | print('%.2f' % (1/sum(s)*n)) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-069_胎压监测.py: -------------------------------------------------------------------------------- 1 | p1, p2, p3, p4, low, limit = map(int, input().split()) 2 | p = [p1, p2, p3, p4] 3 | maxp = max(p) 4 | 5 | c1 = list(map(lambda p: maxp-p <= limit and p >= low, p)) 6 | 7 | if all(val for val in c1): 8 | print('Normal') 9 | elif c1.count(False) >= 2: 10 | print('Warning: please check all the tires!') 11 | else: 12 | print('Warning: please check #%d!' % (1+c1.index(False))) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-070_吃火锅.py: -------------------------------------------------------------------------------- 1 | is_first = True 2 | first_index = -1 3 | huoguo_count = 0 4 | message_count = 0 5 | while True: 6 | message = input() 7 | if message == '.': break 8 | 9 | message_count += 1 10 | if 'chi1 huo3 guo1' in message: 11 | huoguo_count += 1 12 | if is_first: 13 | first_index = message_count 14 | is_first = False 15 | 16 | print(message_count) 17 | if huoguo_count > 0: 18 | print(first_index, huoguo_count) 19 | else: 20 | print('-_-#') -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-071_前世档案.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | for i in range(m): 3 | lo, hi = 0, 2**n-1 4 | ans = input() 5 | for ch in ans: 6 | if ch == 'y': 7 | hi = (lo + hi) // 2 8 | else: 9 | lo = (lo + hi) // 2 + 1 10 | print(lo+1) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-073_人与神.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-073_人与神.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-074_两小时学完C语言.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L1-074_两小时学完C语言.py -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-075_强迫症.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | if len(s) == 4: 3 | if int(s[:2]) < 22: 4 | print("20%s-%s" % (s[0:2], s[2:4])) 5 | else: 6 | print("19%s-%s" % (s[0:2], s[2:4])) 7 | else: 8 | print("%s-%s" % (s[0:4], s[4:6])) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-076_降价提醒机器人.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | for i in range(n): 3 | p = float(input()) 4 | if p < m: 5 | print("On Sale! %.1f" % p) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-077_大笨钟的心情.py: -------------------------------------------------------------------------------- 1 | feeling = list(map(int, input().split())) 2 | while True: 3 | q = int(input()) 4 | if q < 0 or q > 23 : break 5 | 6 | v = feeling[q] 7 | r = 'Yes' if v > 50 else 'No' 8 | print('%d %s' % (v, r)) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-078_吉老师的回归.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | ans = 'Wo AK le' 3 | for i in range(n): 4 | q = input() 5 | if 'qiandao' not in q and 'easy' not in q: 6 | m -= 1 7 | if m == -1: ans = q 8 | print(ans) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-079_天梯赛的善良.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | power = list(map(int, input().split())) 3 | minPower, maxPower = min(power), max(power) 4 | print(min(power), power.count(minPower)) 5 | print(max(power), power.count(maxPower)) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-080_乘法口诀数列.py: -------------------------------------------------------------------------------- 1 | a1, a2, n = map(int, input().split()) 2 | count = n 3 | ans = [a1, a2] 4 | i = 0 5 | while n > 2: 6 | c = ans[i] * ans[i+1] 7 | if c >= 10: 8 | ans.append(c // 10) 9 | ans.append(c % 10) 10 | i += 1 11 | n -= 2 12 | else: 13 | ans.append(c) 14 | i += 1 15 | n -= 1 16 | 17 | print(' '.join(map(str, ans[:count]))) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-081_今天我要赢.py: -------------------------------------------------------------------------------- 1 | print("I'm gonna win! Today!") 2 | print("2022-04-23") -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-082_种钻石.py: -------------------------------------------------------------------------------- 1 | n, v = map(int, input().split()) 2 | print(n // v) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-083_谁能进图书馆.py: -------------------------------------------------------------------------------- 1 | a, b, c, d = map(int, input().split()) 2 | 3 | if c < a and d < a: 4 | print('%s-N %s-N' % (c, d)) 5 | print('zhang da zai lai ba') 6 | elif c >= a and d >= a: 7 | print('%s-Y %s-Y' % (c, d)) 8 | print('huan ying ru guan') 9 | elif c < a and d >= b: 10 | print('%s-Y %s-Y' % (c, d)) 11 | print('qing 2 zhao gu hao 1') 12 | elif d < a and c >= b: 13 | print('%s-Y %s-Y' % (c, d)) 14 | print('qing 1 zhao gu hao 2') 15 | elif c >= a: 16 | print('%s-Y %s-N' % (c, d)) 17 | print('1: huan ying ru guan') 18 | elif d >= a: 19 | print('%s-N %s-Y' % (c, d)) 20 | print('2: huan ying ru guan') -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-084_拯救外星人.py: -------------------------------------------------------------------------------- 1 | import math 2 | a, b = map(int, input().split()) 3 | print(math.factorial(a+b)) 4 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-085_试试手气.py: -------------------------------------------------------------------------------- 1 | arr = list(map(int, input().split())) 2 | n = int(input()) 3 | ans = [] 4 | for v in arr: 5 | std = [1,2,3,4,5,6] 6 | idx = std.index(v) 7 | std[0], std[idx] = std[idx], std[0] 8 | std[1:] = sorted(std[1:]) 9 | ans.append(std[-n]) 10 | print(' '.join(map(str, ans))) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-086_斯德哥尔摩火车上的题.py: -------------------------------------------------------------------------------- 1 | def gets(a): 2 | s = 0 3 | for i in range(1, len(a)): 4 | if a[i] % 2 == a[i-1] % 2: 5 | s = s * 10 + max(a[i], a[i-1]) 6 | return s 7 | 8 | a1, a2 = list(map(int, list(input()))), list(map(int, list(input()))) 9 | r1, r2 = gets(a1), gets(a2) 10 | if r1 == r2: 11 | print(r1) 12 | else: 13 | print(r1) 14 | print(r2) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-087_机工士姆斯塔迪奥.py: -------------------------------------------------------------------------------- 1 | n, m, q = map(int, input().split()) 2 | tot = n * m 3 | nRow, nCol = set([]), set([]) 4 | for i in range(q): 5 | t, c = map(int, input().split()) 6 | if t == 0: 7 | nRow.add(c) 8 | else: 9 | nCol.add(c) 10 | print(tot - len(nRow) * m - len(nCol) * n + len(nRow) * len(nCol)) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-088_静静的推荐.py: -------------------------------------------------------------------------------- 1 | n, k, s = map(int, input().split()) 2 | score = {} 3 | ans = 0 4 | for i in range(n): 5 | s1, s2 = map(int, input().split()) 6 | if s1 < 175: continue 7 | if s2 >= s: ans += 1 8 | else: score[s1] = score.get(s1, 0) + 1 9 | 10 | for i in range(175, 291): 11 | if score.get(i, 0) >= k: ans += k 12 | else: ans += score.get(i, 0) 13 | print(ans) 14 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-089_最好的文档.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Printf("Good code is its own best documentation.") 7 | } 8 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-090_什么是机器学习.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | print(a+b-16) 3 | print(a+b-3) 4 | print(a+b-1) 5 | print(a+b) 6 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-091_程序员买包子.py: -------------------------------------------------------------------------------- 1 | n, x, m, k = input().split() 2 | n, m, k = int(n), int(m), int(k) 3 | 4 | if k == n: 5 | print('mei you mai {} de'.format(x)) 6 | elif k == m: 7 | print('kan dao le mai {} de'.format(x)) 8 | else: 9 | print('wang le zhao mai {} de'.format(x)) 10 | 11 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-092_进化论.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n): 3 | a, b, c = map(int, input().split()) 4 | if c == a * b: 5 | print('Lv Yan') 6 | elif c == a + b: 7 | print('Tu Dou') 8 | else: 9 | print('zhe du shi sha ya!') -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-093_猜帽子游戏.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | ans = list(map(int, input().split())) 3 | k = int(input()) 4 | for i in range(k): 5 | guess = list(map(int, input().split())) 6 | isOk = True 7 | for j in range(n): 8 | if guess[j] != 0 and guess[j] != ans[j]: 9 | isOk = False 10 | break 11 | isOk &= sum(guess) != 0 12 | print("Da Jiang!!!" if isOk else "Ai Ya") -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-094_剪切粘贴.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | n = int(input()) 3 | for i in range(n): 4 | copy_begin, copy_end, sub_begin, sub_end = input().split() 5 | copy_begin, copy_end = int(copy_begin), int(copy_end) 6 | cut = s[copy_begin-1:copy_end] 7 | new_s = s[:copy_begin-1] + s[copy_end:] 8 | insert_begin = new_s.find(sub_begin+sub_end) 9 | insert_end = new_s.find(sub_end, insert_begin+len(sub_begin)) 10 | if insert_begin == -1 or insert_end == -1: 11 | s = new_s + cut 12 | else: 13 | s = new_s[:insert_begin+len(sub_begin)]+cut+new_s[insert_end:] 14 | print(s) 15 | 16 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-095_分寝室.py: -------------------------------------------------------------------------------- 1 | def get_divisor(n): 2 | divisor = [] 3 | for i in range(2, n+1): 4 | if n % i == 0: 5 | divisor.append(i) 6 | return divisor 7 | 8 | 9 | n0, n1, n = map(int, input().split()) 10 | divisor0 = set(get_divisor(n0)) 11 | divisor1 = set(get_divisor(n1)) 12 | diff = 10**5+1 13 | ans = (-1, -1) 14 | for f in divisor0: 15 | m = n1 // (n-n0//f) 16 | if n1 % (n-n0//f) == 0 and m in divisor1 and abs(f-m) < diff: 17 | diff = abs(f-m) 18 | ans = (n0//f, n-n0//f) 19 | 20 | if ans == (-1, -1): 21 | print("No Solution") 22 | else: 23 | print(ans[0], ans[1]) -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L1-096_谁管谁叫爹.py: -------------------------------------------------------------------------------- 1 | def get_digit_sum(n): 2 | sum = 0 3 | while n > 0: 4 | sum += n % 10 5 | n //= 10 6 | return sum 7 | 8 | n = int(input()) 9 | for i in range(n): 10 | a, b = map(int, input().split()) 11 | sa, sb = get_digit_sum(a), get_digit_sum(b) 12 | if a % sb == 0 and b % sa != 0: 13 | print("A") 14 | elif b % sa == 0 and a % sb != 0: 15 | print("B") 16 | else: 17 | print("A" if a > b else "B") -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-008_最长对称子串.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int work(char *str) 5 | {//分奇偶情况,向字符的两边枚举 6 | char *left; 7 | char *right; 8 | int ans = 1; 9 | char *ptr = str; 10 | while(*ptr != '\0') 11 | { 12 | //奇数情况 13 | left = ptr - 1; 14 | right = ptr + 1; 15 | while(left >= str && *right != '\0' && *left == *right) 16 | { 17 | left--; 18 | right++; 19 | } 20 | if(right - left - 1 > ans) 21 | ans = right - left - 1; 22 | //偶数情况 23 | left = ptr; 24 | right = ptr+1; 25 | while(left >= str && *right != '\0' && *left == *right) 26 | { 27 | left--; 28 | right++; 29 | } 30 | if(right - left - 1 > ans) 31 | ans = right - left - 1; 32 | ptr++; 33 | } 34 | return ans; 35 | } 36 | 37 | int main() 38 | { 39 | char str[1005]; 40 | gets(str); 41 | printf("%d\n", work(str)); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-014_列车调度.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // case 1,2,3 timeout 6 | func main() { 7 | var n int 8 | fmt.Scanf("%d", &n) 9 | 10 | s := make([]int, 0) 11 | for i := 0; i < n; i++ { 12 | var t int 13 | fmt.Scan(&t) 14 | p := UpperBound(s, t) 15 | if p == len(s) { 16 | s = append(s, t) 17 | } else { 18 | s[p] = t 19 | } 20 | } 21 | fmt.Println(len(s)) 22 | } 23 | 24 | func UpperBound(arr []int, target int) int { 25 | lo, hi := 0, len(arr)-1 26 | for lo <= hi { 27 | mid := lo + (hi-lo)/2 28 | if arr[mid] > target { 29 | hi = mid - 1 30 | } else { 31 | lo = mid + 1 32 | } 33 | } 34 | return lo 35 | } 36 | -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-015_互评成绩.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | int n, k, m; 10 | cin >> n >> k >> m; 11 | vector scores(n); 12 | for (int i = 0; i < n; i++) { 13 | int lo = 100, hi = 0; 14 | int sum = 0; 15 | for (int j = 0; j < k; j++) { 16 | int score; 17 | cin >> score; 18 | sum += score; 19 | lo = min(lo, score); 20 | hi = max(hi, score); 21 | } 22 | sum -= lo + hi; 23 | scores[i] = sum / (float)(k - 2); 24 | } 25 | sort(scores.begin(), scores.end()); 26 | cout << fixed << setprecision(3) << scores[n - m]; 27 | for (int i = n - m + 1; i < n; i++) { 28 | cout << " " << fixed << setprecision(3) << scores[i]; 29 | } 30 | cout << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-017_人以群分.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | int n; 9 | cin >> n; 10 | vector v(n); 11 | for (int i = 0; i < n; i++) cin >> v[i]; 12 | sort(v.begin(), v.end()); 13 | 14 | int lo = 0, hi = v.size() - 1; 15 | int diff = 0; 16 | while (lo <= hi) { 17 | if (lo == hi) { 18 | diff += v[lo]; 19 | break; 20 | } 21 | diff += v[hi] - v[lo]; 22 | lo++; 23 | hi--; 24 | } 25 | cout << "Outgoing #: " << (n + 1) / 2 << endl; 26 | cout << "Introverted #: " << n / 2 << endl; 27 | cout << "Diff = " << diff << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-019_悄悄关注.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | 10 | unordered_set names; 11 | string name; 12 | int n, m, t; 13 | cin >> n; 14 | for (int i = 0; i < n; i++) { 15 | cin >> name; 16 | names.insert(name); 17 | } 18 | 19 | vector> ans; 20 | int tot = 0; 21 | cin >> m; 22 | for (int i = 0; i < m; i++) { 23 | cin >> name >> t; 24 | tot += t; 25 | if (names.find(name) == names.end()) 26 | ans.push_back({name, t}); 27 | } 28 | 29 | float target = tot / m; 30 | for (auto it = ans.begin(); it != ans.end(); ) { 31 | if (it->second <= target) it = ans.erase(it); 32 | else it++; 33 | } 34 | 35 | if (ans.size() == 0) cout << "Bing Mei You" << endl; 36 | else { 37 | sort(ans.begin(), ans.end(), [&](pair a, pair b) { 38 | return a.first < b.first; 39 | }); 40 | for (auto it = ans.begin(); it != ans.end(); it++) { 41 | cout << it->first << endl; 42 | } 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-020_功夫传人.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void DFS(const vector> &p, const vector &isMaster, int id, double &ans, double power, double r) { 7 | if (abs(isMaster[id]) > 1e-3) { 8 | ans += isMaster[id] * power; 9 | return; 10 | } 11 | for (int i = 0; i < p[id].size(); i++) { 12 | DFS(p, isMaster, p[id][i], ans, power * (1 - r / 100.), r); 13 | } 14 | } 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); 18 | int n, k, id; 19 | double z, r, x; 20 | cin >> n >> z >> r; 21 | 22 | vector> p(n, vector()); 23 | vector isMaster(n, 0); 24 | 25 | for (int i = 0; i < n; i++) { 26 | cin >> k; 27 | if (k == 0) { 28 | cin >> x; 29 | isMaster[i] = x; 30 | } else { 31 | for (int j = 0; j < k; j++) { 32 | cin >> id; 33 | p[i].push_back(id); 34 | } 35 | } 36 | } 37 | double ans = 0; 38 | DFS(p, isMaster, 0, ans, z, r); 39 | cout << (int)ans << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-023_图着色问题.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | int v, e, k, a, b, n; 9 | cin >> v >> e >> k; 10 | vector> edges; 11 | for (int i = 0; i < e; i ++) { 12 | cin >> a >> b; 13 | edges.push_back({a, b}); 14 | } 15 | cin >> n; 16 | for (int i = 0; i < n; i ++) { 17 | vector colors(v+1); 18 | unordered_set colorSet; 19 | for (int j = 1; j <= v; j ++) { 20 | cin >> colors[j]; 21 | colorSet.insert(colors[j]); 22 | } 23 | bool isOk = true && colorSet.size() == k; 24 | for (int j = 0; j < edges.size() && isOk; j++) { 25 | if (colors[edges[j].first] == colors[edges[j].second]) { 26 | isOk = false; 27 | } 28 | } 29 | if (isOk) cout << "Yes" << endl; 30 | else cout << "No" << endl; 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-027_名人堂与代金券.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Student 7 | { 8 | string mail; 9 | int score; 10 | int rank; 11 | }; 12 | 13 | int main() { 14 | ios::sync_with_stdio(false); 15 | int n, g, k; 16 | cin >> n >> g >> k; 17 | 18 | int coupon = 0; 19 | vector stu(n); 20 | for (int i = 0; i < n; i++) { 21 | cin >> stu[i].mail >> stu[i].score; 22 | if (stu[i].score >= g) coupon += 50; 23 | else if (stu[i].score >= 60) coupon += 20; 24 | } 25 | 26 | sort(stu.begin(), stu.end(), [&](const Student &a, const Student &b) { 27 | return a.score == b.score ? a.mail < b.mail : a.score > b.score; 28 | }); 29 | 30 | int rank = 1; 31 | for (int i = 0; i < n; i++) { 32 | if (i > 0 && stu[i].score < stu[i - 1].score) rank = i + 1; 33 | stu[i].rank = rank; 34 | } 35 | 36 | cout << coupon << endl; 37 | for (auto &s : stu) { 38 | if (s.rank > k) break; 39 | cout << s.rank << " " << s.mail << " " << s.score << endl; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-028_秀恩爱分得快.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/团体程序设计天梯赛-练习集/L2-028_秀恩爱分得快.cpp -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-033_简单计算器.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | int n, v; 8 | char op; 9 | cin >> n; 10 | stack s1; 11 | stack s2; 12 | for (int i = 0; i < n; i++) { 13 | cin >> v; 14 | s1.push(v); 15 | } 16 | for (int i = 0; i < n-1; i++) { 17 | cin >> op; 18 | s2.push(op); 19 | } 20 | 21 | while (!s2.empty()) { 22 | int a = s1.top(); s1.pop(); 23 | int b = s1.top(); s1.pop(); 24 | char op = s2.top(); s2.pop(); 25 | if (op == '+') s1.push(b+a); 26 | else if (op == '-') s1.push(b-a); 27 | else if (op == '*') s1.push(b*a); 28 | else if (op == '/') { 29 | if (a == 0) { 30 | cout << "ERROR: " << b << "/0" << endl; 31 | return 0; 32 | } 33 | s1.push(b/a); 34 | } 35 | } 36 | cout << s1.top() << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /团体程序设计天梯赛-练习集/L2-035_完全二叉树的层序遍历.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void DFS(int index, vector &tree) { 7 | if (index >= tree.size()) return; 8 | DFS(index << 1, tree); 9 | DFS(index << 1 | 1, tree); 10 | int node; 11 | cin >> node; 12 | tree[index] = node; 13 | } 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); 17 | int n; 18 | cin >> n; 19 | vector nodes(n+1); 20 | DFS(1, nodes); 21 | cout << nodes[1]; 22 | for (int i = 2; i <= n; i++) 23 | cout << " " << nodes[i]; 24 | cout << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /基础编程题目集/6-10_阶乘计算升级版 (20).c: -------------------------------------------------------------------------------- 1 | void Print_Factorial ( const int N ) 2 | { 3 | if (N < 0) 4 | { 5 | printf("Invalid input"); 6 | return; 7 | } 8 | int c[20001]; 9 | c[0] = 1; 10 | int digit = 1; 11 | for (int i = 2; i <= N; i++) 12 | { 13 | int num = 0; 14 | for (int d = 0; d < digit; d++) 15 | { 16 | num = c[d] * i + num; 17 | c[d] = num % 10; 18 | num /= 10; 19 | } 20 | while (num) 21 | { 22 | c[digit] = num % 10; 23 | num /= 10; 24 | digit++; 25 | } 26 | } 27 | for (int i = digit-1; i >= 0; i--) 28 | printf("%d", c[i]); 29 | } -------------------------------------------------------------------------------- /基础编程题目集/6-11_求自定类型元素序列的中位数 (25).c: -------------------------------------------------------------------------------- 1 | ElementType Median(ElementType A[], int N) 2 | { 3 | ElementType aux[MAXN]; 4 | for (int i = 0; i < N; i++) aux[i] = A[i]; 5 | merge_help_sort(aux, A, 0, N - 1); 6 | return A[N/2]; 7 | } 8 | 9 | void merge_help_sort(ElementType src[], ElementType dst[], int lo, int hi) { 10 | if (hi <= lo) return; 11 | int mid = lo + ((hi - lo) >> 1); 12 | merge_help_sort(dst, src, lo, mid); 13 | merge_help_sort(dst, src, mid + 1, hi); 14 | merge(src, dst, lo, mid, hi); 15 | } 16 | 17 | void merge(ElementType src[], ElementType dst[], int lo, int mid, int hi) { 18 | int i = lo, j = mid + 1; 19 | for (int k = lo; k <= hi; k++) { 20 | if (i > mid) dst[k] = src[j++]; 21 | else if (j > hi) dst[k] = src[i++]; 22 | else if (src[i] < src[j]) dst[k] = src[i++]; 23 | else dst[k] = src[j++]; 24 | } 25 | } -------------------------------------------------------------------------------- /基础编程题目集/6-12_判断奇偶性 (10).c: -------------------------------------------------------------------------------- 1 | int even( int n ) 2 | { 3 | return n % 2 == 0; 4 | } -------------------------------------------------------------------------------- /基础编程题目集/6-13_折半查找 (15).c: -------------------------------------------------------------------------------- 1 | int Search_Bin(SSTable T, KeyType k) 2 | { 3 | int lo = 1, hi = T.length; 4 | while (lo < hi) 5 | { 6 | int mid = lo + (hi-lo) / 2; 7 | if (T.R[mid].key == k) return mid; 8 | else if (T.R[mid].key < k) lo = mid+1; 9 | else hi = mid-1; 10 | } 11 | return 0; 12 | } -------------------------------------------------------------------------------- /基础编程题目集/6-1_简单输出整数 (10).c: -------------------------------------------------------------------------------- 1 | void PrintN(int n) 2 | { 3 | for (int i = 1; i <= n; i++) 4 | { 5 | printf("%d\n", i); 6 | } 7 | } -------------------------------------------------------------------------------- /基础编程题目集/6-2_多项式求值 (20).c: -------------------------------------------------------------------------------- 1 | double f(int n, double a[], double x) 2 | { 3 | double sum = 0; 4 | double base = 1; 5 | for (int i = 0; i <= n; i++) { 6 | sum += a[i] * base; 7 | base *= x; 8 | } 9 | return sum; 10 | } -------------------------------------------------------------------------------- /基础编程题目集/6-3_简单求和 (10).c: -------------------------------------------------------------------------------- 1 | int Sum (int List[], int N) 2 | { 3 | int sum = 0; 4 | for (int i = 0; i < N; i++) 5 | { 6 | sum += List[i]; 7 | } 8 | return sum; 9 | } -------------------------------------------------------------------------------- /基础编程题目集/6-4_求自定类型元素的平均 (10).c: -------------------------------------------------------------------------------- 1 | ElementType Average(ElementType S[], int N) 2 | { 3 | ElementType sum = 0; 4 | for (int i = 0; i < N; i++) 5 | { 6 | sum += S[i]; 7 | } 8 | return sum / N; 9 | } -------------------------------------------------------------------------------- /基础编程题目集/6-5_求自定类型元素的最大值 (10).c: -------------------------------------------------------------------------------- 1 | ElementType Max(ElementType S[],int N) 2 | { 3 | ElementType result = S[0]; 4 | for (int i = 1; i < N; i++) 5 | { 6 | result = S[i] > result ? S[i] : result; 7 | } 8 | return result; 9 | } -------------------------------------------------------------------------------- /基础编程题目集/6-6_求单链表结点的阶乘和 (15).c: -------------------------------------------------------------------------------- 1 | int FactorialSum( List L ) 2 | { 3 | int sum = 0; 4 | while (L != NULL) 5 | { 6 | int fac = 1; 7 | for (int i = L->Data; i >= 2; i--) 8 | { 9 | fac *= i; 10 | } 11 | sum += fac; 12 | L = L->Next; 13 | } 14 | return sum; 15 | } -------------------------------------------------------------------------------- /基础编程题目集/6-7_统计某类完全平方数 (20).c: -------------------------------------------------------------------------------- 1 | int IsTheNumber (const int N) 2 | { 3 | int n = N; 4 | int b = (int)sqrt(n); 5 | int sq = b*b == n; 6 | int same = 0; 7 | int bit[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; 8 | do { 9 | if (bit[n % 10] == 1) { 10 | same = 1; 11 | break; 12 | } 13 | bit[n % 10] = 1; 14 | n /= 10; 15 | } while (n != 0); 16 | return sq && same; 17 | 18 | } -------------------------------------------------------------------------------- /基础编程题目集/6-8_简单阶乘计算 (10).c: -------------------------------------------------------------------------------- 1 | int Factorial( const int N ) 2 | { 3 | int result = 1; 4 | for (int i = 2; i <= N; i++) 5 | { 6 | result *= i; 7 | } 8 | return N >= 0 ? result : 0; 9 | } -------------------------------------------------------------------------------- /基础编程题目集/6-9_统计个位数字 (15).c: -------------------------------------------------------------------------------- 1 | int Count_Digit ( const int N, const int D ) 2 | { 3 | int n = abs(N); 4 | int countD = 0; 5 | do 6 | { 7 | if (n % 10 == D) countD++; 8 | n /= 10; 9 | } while (n != 0); 10 | return countD; 11 | } -------------------------------------------------------------------------------- /基础编程题目集/7-10_计算工资 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-10_计算工资 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-11_分段计算居民水费 (10).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-11_分段计算居民水费 (10).py -------------------------------------------------------------------------------- /基础编程题目集/7-12_两个数的简单计算器 (10).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-12_两个数的简单计算器 (10).py -------------------------------------------------------------------------------- /基础编程题目集/7-13_日K蜡烛图 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-13_日K蜡烛图 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-14_求整数段和 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-14_求整数段和 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-15_计算圆周率 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-15_计算圆周率 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-16_求符合给定条件的整数集 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-16_求符合给定条件的整数集 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-17_爬动的蠕虫 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-17_爬动的蠕虫 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-18_二分法求多项式单根 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-18_二分法求多项式单根 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-19_支票面额 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-19_支票面额 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-1_厘米换算英尺英寸 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-1_厘米换算英尺英寸 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-20_打印九九口诀表 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-20_打印九九口诀表 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-21_求特殊方程的正整数解 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-21_求特殊方程的正整数解 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-22_龟兔赛跑 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-22_龟兔赛跑 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-23_币值转换 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-23_币值转换 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-24_约分最简分式 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-24_约分最简分式 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-25_念数字 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-25_念数字 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-26_单词长度 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-26_单词长度 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-27_冒泡法排序 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-27_冒泡法排序 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-28_猴子选大王 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-28_猴子选大王 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-29_删除字符串中的子串 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-29_删除字符串中的子串 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-2_然后是几点 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-2_然后是几点 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-30_字符串的冒泡排序 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-30_字符串的冒泡排序 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-31_字符串循环左移 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-31_字符串循环左移 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-32_说反话-加强版 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-32_说反话-加强版 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-33_有理数加法 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-33_有理数加法 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-34_通讯录的录入与显示 (10).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-34_通讯录的录入与显示 (10).py -------------------------------------------------------------------------------- /基础编程题目集/7-35_有理数均值 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-35_有理数均值 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-36_复数四则运算 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-36_复数四则运算 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-37_整数分解为若干项之和 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-37_整数分解为若干项之和 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-38_数列求和-加强版 (20).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-38_数列求和-加强版 (20).py -------------------------------------------------------------------------------- /基础编程题目集/7-3_逆序的三位数 (10).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-3_逆序的三位数 (10).py -------------------------------------------------------------------------------- /基础编程题目集/7-4_BCD解密 (10).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-4_BCD解密 (10).py -------------------------------------------------------------------------------- /基础编程题目集/7-5_表格输出 (5).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-5_表格输出 (5).py -------------------------------------------------------------------------------- /基础编程题目集/7-6_混合类型数据格式化输入 (5).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-6_混合类型数据格式化输入 (5).py -------------------------------------------------------------------------------- /基础编程题目集/7-7_12-24小时制 (15).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-7_12-24小时制 (15).py -------------------------------------------------------------------------------- /基础编程题目集/7-8_超速判断 (10).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-8_超速判断 (10).py -------------------------------------------------------------------------------- /基础编程题目集/7-9_用天平找小球 (10).py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiny656/PAT/8b758bd880f1b154ccd64c2bdd9bfabe426aedb0/基础编程题目集/7-9_用天平找小球 (10).py -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/6-10_二分查找 (20).c: -------------------------------------------------------------------------------- 1 | Position BinarySearch( List L, ElementType X ) 2 | { 3 | Position lo = 1, hi = L->Last; 4 | while (lo <= hi) 5 | { 6 | Position mid = lo + ((hi-lo)>>1); 7 | if (L->Data[mid] == X) return mid; 8 | else if (L->Data[mid] > X) hi = mid-1; 9 | else lo = mid+1; 10 | } 11 | return NotFound; 12 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/6-11_先序输出叶结点 (15).c: -------------------------------------------------------------------------------- 1 | void PreorderPrintLeaves( BinTree BT ) 2 | { 3 | if (BT == NULL) return; 4 | if (BT->Left == NULL && BT->Right == NULL) printf(" %c", BT->Data); 5 | PreorderPrintLeaves(BT->Left); 6 | PreorderPrintLeaves(BT->Right); 7 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/6-1_单链表逆转 (20).c: -------------------------------------------------------------------------------- 1 | List Reverse( List L ) 2 | { 3 | PtrToNode prev = NULL, cur = L, tmp; 4 | while (cur) 5 | { 6 | tmp = cur->Next; 7 | cur->Next = prev; 8 | prev = cur; 9 | cur = tmp; 10 | } 11 | return prev; 12 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/6-3_求链式表的表长 (10).c: -------------------------------------------------------------------------------- 1 | int Length( List L ) 2 | { 3 | return L == NULL ? 0 : (1 + Length(L->Next)); 4 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/6-4_链式表的按序号查找 (10).c: -------------------------------------------------------------------------------- 1 | ElementType FindKth( List L, int K ) 2 | { 3 | int cnt = 1; 4 | while (cnt != K && L != NULL) 5 | { 6 | cnt++; 7 | L = L->Next; 8 | } 9 | return (cnt == K && L != NULL) ? L->Data : ERROR; 10 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/6-8_求二叉树高度 (20).c: -------------------------------------------------------------------------------- 1 | int max(int a, int b) 2 | { 3 | return a > b ? a : b; 4 | } 5 | 6 | int GetHeight( BinTree BT ) 7 | { 8 | return BT == NULL ? 0 : (max(GetHeight(BT->Left), GetHeight(BT->Right)) + 1); 9 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/6-9_二叉树的遍历 (25).c: -------------------------------------------------------------------------------- 1 | void InorderTraversal( BinTree BT ) 2 | { 3 | if (BT == NULL) return; 4 | InorderTraversal(BT->Left); 5 | printf(" %c", BT->Data); 6 | InorderTraversal(BT->Right); 7 | 8 | } 9 | void PreorderTraversal( BinTree BT ) 10 | { 11 | if (BT == NULL) return; 12 | printf(" %c", BT->Data); 13 | PreorderTraversal(BT->Left); 14 | PreorderTraversal(BT->Right); 15 | } 16 | 17 | void PostorderTraversal( BinTree BT ) 18 | { 19 | if (BT == NULL) return; 20 | PostorderTraversal(BT->Left); 21 | PostorderTraversal(BT->Right); 22 | printf(" %c", BT->Data); 23 | } 24 | 25 | void LevelorderTraversal( BinTree BT ) 26 | { 27 | if (BT == NULL) return; 28 | Position q[1000]; 29 | int head = 0, rear = 0; 30 | q[rear++] = BT; 31 | while (head < rear) 32 | { 33 | Position p = q[head++]; 34 | printf(" %c", p->Data); 35 | if (p->Left) q[rear++] = p->Left; 36 | if (p->Right) q[rear++] = p->Right; 37 | } 38 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-12_排序 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int arr[100005]; 6 | 7 | int main () { 8 | int n; 9 | scanf("%d", &n); 10 | for (int i = 0; i < n; i++) { 11 | scanf("%d", &arr[i]); 12 | } 13 | sort(arr, arr+n); 14 | printf("%d", arr[0]); 15 | for (int i = 1; i < n; i++) printf(" %d", arr[i]); 16 | printf("\n"); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-13_统计工龄 (20).py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | ages = map(int, raw_input().split()) 3 | ageDict = {} 4 | for age in ages: 5 | ageDict[age] = ageDict.get(age, 0) + 1 6 | for key in sorted(ageDict.keys()): 7 | print '%d:%d' % (key, ageDict[key]) -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-14_电话聊天狂人 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void update(string phone, int count, tuple &ans) { 8 | if (count > get<1>(ans)) { 9 | get<0>(ans) = phone; 10 | get<1>(ans) = count; 11 | get<2>(ans) = 1; 12 | } else if (count == get<1>(ans)) { 13 | get<0>(ans) = phone < get<0>(ans) ? phone : get<0>(ans); 14 | get<2>(ans)++; 15 | } 16 | } 17 | 18 | int main() { 19 | int n; 20 | string a, b; 21 | tuple ans("", 0, 0); 22 | unordered_map record; 23 | cin >> n; 24 | for (int i = 1; i <= n; i++) { 25 | cin >> a >> b; 26 | record[a] += 1; 27 | record[b] += 1; 28 | } 29 | 30 | for (auto &v : record) 31 | update(v.first, v.second, ans); 32 | 33 | cout << get<0>(ans) << " " << get<1>(ans); 34 | if (get<2>(ans) != 1) 35 | cout << " " << get<2>(ans); 36 | cout << endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-17_汉诺塔的非递归实现 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Param { 6 | int n; 7 | char from, to, temp; 8 | }; 9 | 10 | /* 11 | n, A, C, B 12 | => n-1, A, B, C 13 | => n, A, C, B 14 | => n-1, B, C, A 15 | */ 16 | 17 | void Hanoi(int n, char from, char to, char temp) { 18 | stack s; 19 | Param call = {n, from, to, temp}; 20 | while (call.n != 0 || !s.empty()) { 21 | Param p = call; 22 | while (p.n > 0) { 23 | s.push(p); 24 | p.n--; 25 | swap(p.to, p.temp); 26 | } 27 | 28 | call = s.top(); s.pop(); 29 | printf("%c -> %c\n", call.from, call. to); 30 | 31 | call.n--; 32 | swap(call.from, call.temp); 33 | } 34 | } 35 | 36 | int main() { 37 | int n; 38 | scanf("%d", &n); 39 | Hanoi(n, 'a', 'c', 'b'); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-18_银行业务队列简单模拟 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, v; 7 | queue A, B; 8 | cin >> n; 9 | for (int i = 0; i < n; i++) { 10 | cin >> v; 11 | if (v % 2 != 0) A.push(v); 12 | else B.push(v); 13 | 14 | } 15 | 16 | vector result; 17 | 18 | while (!A.empty() || !B.empty()) { 19 | if (!A.empty()) { result.push_back(A.front()); A.pop(); } 20 | if (!A.empty()) { result.push_back(A.front()); A.pop(); } 21 | if (!B.empty()) { result.push_back(B.front()); B.pop(); } 22 | } 23 | 24 | for (int i = 0; i < result.size(); i++) { 25 | if (i == 0) cout << result[0]; 26 | else cout << " " << result[i]; 27 | } 28 | cout << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-19_求链式线性表的倒数第K项 (20).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | Node *next; 7 | int data; 8 | 9 | Node(int data):data(data) { } 10 | }; 11 | 12 | Node* BuildLinkList() { 13 | int v; 14 | Node *s = NULL, *tmp = NULL; 15 | while (scanf("%d", &v) != EOF && v >= 0) { 16 | Node *node = new Node(v); 17 | if (tmp == NULL) { 18 | s = node; 19 | tmp = node; 20 | } else { 21 | tmp->next = node; 22 | tmp = tmp->next; 23 | } 24 | } 25 | return s; 26 | } 27 | 28 | int main() { 29 | int k; 30 | scanf("%d", &k); 31 | auto s = BuildLinkList(); 32 | auto p1 = s, p2 = s; 33 | while (k != 0 && p1 != NULL) { 34 | p1 = p1 -> next; 35 | k--; 36 | } 37 | if (k != 0) printf("NULL\n"); 38 | else { 39 | while (p1 != NULL) { 40 | p1 = p1->next; 41 | p2 = p2->next; 42 | } 43 | printf("%d\n", p2->data); 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-1_最大子列和问题 (20).py: -------------------------------------------------------------------------------- 1 | # 令dp[i] 表示以列表A第i个元素结尾的最大子序列和,所以一个列表的最大子序列和=max{dp[i] | i<-[1,...,n]} 2 | # dp[0] = max(0, A[0]) 3 | # dp[i] = max(dp[i-1]+A[i], 0) 4 | # 滚动数组优化 5 | 6 | def MaxSum(arr): 7 | result, t = 0, 0 8 | for v in arr: 9 | t += v 10 | if t < 0: 11 | t = 0 12 | if result < t: 13 | result = t 14 | return result 15 | 16 | k = int(raw_input()) 17 | arr = map(int, raw_input().split()) 18 | print MaxSum(arr) -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-21_求前缀表达式的值 (25).py: -------------------------------------------------------------------------------- 1 | def isNumber(e): 2 | if len(e) >= 1 and e[0].isdigit(): return True 3 | elif len(e) > 1 and (e[0] == '-' or '+'): return True 4 | else: return False 5 | 6 | s = raw_input().split() 7 | r = [] 8 | 9 | for e in s: 10 | r.append(e) 11 | while len(r) >= 3 and not isNumber(r[-3]) and isNumber(r[-2]) and isNumber(r[-1]): 12 | try: 13 | ex = 'float(%s)%sfloat(%s)' % (r[-2], r[-3], r[-1]) # avoid int dividen 14 | result = eval(ex) 15 | except: 16 | print 'ERROR' 17 | exit(0) 18 | r = r[:-3] 19 | r.append(str(result)) 20 | 21 | if len(r) == 1 and isNumber(r[0]): 22 | print '%.1f' % float(r[0]) 23 | else: 24 | print 'ERROR' -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-23_还原二叉树 (25).py: -------------------------------------------------------------------------------- 1 | class TreeNode(object): 2 | def __init__(self): 3 | self.left = None 4 | self.right = None 5 | self.data = None 6 | 7 | def BuildTree(inOrd, inBegin, inEnd, preOrd, preBegin, preEnd): 8 | root = TreeNode() 9 | root.data = preOrd[preBegin] 10 | p = inOrd.index(root.data) 11 | leftSize = p - inBegin 12 | rightSize = inEnd - p 13 | if leftSize > 0: 14 | root.left = BuildTree(inOrd, inBegin, p-1, preOrd, preBegin+1, preBegin+leftSize) 15 | if rightSize > 0: 16 | root.right = BuildTree(inOrd, p+1, inEnd, preOrd, preEnd-rightSize+1, preEnd) 17 | return root 18 | 19 | def GetHeight(root): 20 | if root is None: return 0 21 | leftHeight = GetHeight(root.left) 22 | rightHeight = GetHeight(root.right) 23 | return max(leftHeight, rightHeight) + 1 24 | 25 | n = int(raw_input()) 26 | preOrd = list(raw_input().strip()) 27 | inOrd = list(raw_input().strip()) 28 | root = BuildTree(inOrd, 0, n-1, preOrd, 0, n-1) 29 | print GetHeight(root) -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-24_树种统计 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | string s; 10 | int n, count = 0; 11 | map records; 12 | cin >> n; 13 | getline(cin, s); 14 | for (int i = 0; i < n; i++) { 15 | getline(cin, s); 16 | records[s]++; 17 | count++; 18 | } 19 | 20 | for (auto &r : records) { 21 | cout.setf(ios::fixed); 22 | cout << r.first << " " << setprecision(4) << 100 * r.second / (float) count << "%" << endl; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-26_Windows消息队列 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Message { 7 | int priority; 8 | string content; 9 | 10 | bool operator<(const Message &a) const { 11 | return priority > a.priority; // 小顶堆 12 | } 13 | }; 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); 17 | priority_queue pq; 18 | int n; 19 | string cmd; 20 | Message m; 21 | cin >> n; 22 | for (int i = 0; i < n; i++) { 23 | cin >> cmd; 24 | if (cmd == "PUT") { 25 | cin >> m.content >> m.priority; 26 | pq.push(m); 27 | } else if (cmd == "GET") { 28 | if (pq.empty()) { 29 | cout << "EMPTY QUEUE!" << endl; 30 | } else { 31 | cout << pq.top().content << endl; 32 | pq.pop(); 33 | } 34 | } 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-29_修理牧场 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 哈夫曼编码 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | priority_queue, greater> pq; 9 | int n, v; 10 | cin >> n; 11 | for (int i = 0; i < n; i++) { 12 | cin >> v; 13 | pq.push(v); 14 | } 15 | 16 | int ans = 0; 17 | while (pq.size() != 1) { 18 | int a = pq.top(); pq.pop(); 19 | int b = pq.top(); pq.pop(); 20 | int c = a+b; 21 | ans += c; 22 | pq.push(c); 23 | } 24 | cout << ans << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-2_一元多项式的乘法与加法运算 (20).py: -------------------------------------------------------------------------------- 1 | def PrintAns(C): 2 | for idx in list(C.keys()): # make sure not changes elements for loop item 3 | if C[idx] == 0: 4 | del C[idx] 5 | if not C: 6 | print '0 0' 7 | else: 8 | result = [] 9 | for idx in sorted(C.keys(), reverse=True): 10 | result.append(C[idx]) 11 | result.append(idx) 12 | print ' '.join(map(str, result)) 13 | 14 | A, B, C = {}, {}, {} 15 | 16 | inp = map(int, raw_input().split()[1:]) 17 | for i in xrange(0, len(inp), 2): 18 | A[inp[i+1]] = inp[i] 19 | 20 | inp = map(int, raw_input().split()[1:]) 21 | for i in xrange(0, len(inp), 2): 22 | B[inp[i+1]] = inp[i] 23 | 24 | for A_idx, A_val in A.iteritems(): 25 | for B_idx, B_val in B.iteritems(): 26 | C[A_idx+B_idx] = C.setdefault(A_idx+B_idx, 0) + A_val*B_val 27 | PrintAns(C) 28 | 29 | C.clear() 30 | for A_idx, A_val in A.iteritems(): 31 | C[A_idx] = C.setdefault(A_idx, 0) + A_val 32 | for B_idx, B_val in B.iteritems(): 33 | C[B_idx] = C.setdefault(B_idx, 0) + B_val 34 | PrintAns(C) -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-38_寻找大富翁 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | // sort algo can meet the data requirements 6 | // otherwise maintain a small heap with M elements 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | int n, m; 10 | cin >> n >> m; 11 | vector elems(n); 12 | for (int i = 0; i < n; i++) { 13 | cin >> elems[i]; 14 | } 15 | sort(elems.begin(), elems.end(), greater()); 16 | for (int i = 0; i < (n < m ? n : m); i++) { 17 | if (i == 0) cout << elems[i]; 18 | else cout << " " << elems[i]; 19 | } 20 | cout << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-39_魔法优惠券 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | /* 6 | 贪心算法 正数*正数 负数*负数 7 | */ 8 | int main() 9 | { 10 | deque A,B; 11 | int m,n,i,e; 12 | cin >> m; 13 | for (i = 0; i < m; i++) 14 | { 15 | cin >> e; 16 | A.push_back(e); 17 | } 18 | cin >> n; 19 | for (i = 0; i < n; i++) 20 | { 21 | cin >> e; 22 | B.push_back(e); 23 | } 24 | sort(A.begin(), A.end()); 25 | sort(B.begin(), B.end()); 26 | int ans = 0; 27 | while(!A.empty() && !B.empty() && A.back() > 0 && B.back() > 0) 28 | { 29 | ans += A.back()*B.back(); 30 | A.pop_back(); 31 | B.pop_back(); 32 | } 33 | while(!A.empty() && !B.empty() && A.front() < 0 && B.front() < 0) 34 | { 35 | ans += A.front()*B.front(); 36 | A.pop_front(); 37 | B.pop_front(); 38 | } 39 | cout << ans << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-44_基于词频的文件相似度 (30).py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | def GetContent(): 4 | result = [] 5 | while True: 6 | s = raw_input() 7 | if s.strip() == '#': 8 | return '\n'.join(result) 9 | else: 10 | result.append(s) 11 | 12 | n = int(raw_input()) 13 | fileSet = [None] * n 14 | for i in range(n): 15 | content = GetContent().lower() 16 | # regrex find all words only contain alphabet 17 | words = re.findall(r'([a-z]{3,})', content, re.S) 18 | fileSet[i] = set() 19 | for word in words: 20 | fileSet[i].add(word if len(word) <= 10 else word[:10] ) 21 | 22 | m = int(raw_input()) 23 | for i in range(m): 24 | a, b = map(int, raw_input().split()) 25 | union = fileSet[a-1] | fileSet[b-1] 26 | intersection = fileSet[a-1] & fileSet[b-1] 27 | rate = len(intersection) / float(len(union)) * 100. 28 | print '%.1f%%' % rate 29 | 30 | ''' 31 | 3 32 | Aaa Bbb Ccc 33 | # 34 | Bbb Ccc Ddd 35 | # 36 | Aaa2 ccc Eee 37 | is at Ddd@Fff 38 | # 39 | 2 40 | 1 2 41 | 1 3 42 | ''' -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-45_航空公司VIP客户查询 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | unordered_map score; 8 | string id; 9 | int n, k, v; 10 | cin >> n >> k; 11 | for (int i = 0; i < n; i++) { 12 | cin >> id >> v; 13 | score[id] += (v < k ? k : v); 14 | } 15 | cin >> n; 16 | for (int i = 0; i < n; i++) { 17 | cin >> id; 18 | if (score.find(id) == score.end()) { 19 | cout << "No Info" << endl; 20 | } else { 21 | cout << score[id] << endl; 22 | } 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-47_打印选课学生名单 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int n, k, cnt, id; 8 | string name; 9 | scanf("%d%d", &n, &k); 10 | vector> course(k+1); 11 | for (int i = 0; i < n; i++) { 12 | cin >> name; 13 | scanf("%d", &cnt); 14 | for (int j = 0; j < cnt; j++) { 15 | scanf("%d", &id); 16 | course[id].push_back(name); 17 | } 18 | } 19 | for (int i = 1; i <= k; i++) { 20 | sort(course[i].begin(), course[i].end()); 21 | printf("%d %d\n", i, course[i].size()); 22 | for (auto &c : course[i]) { 23 | printf("%s\n", c.c_str()); 24 | } 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-53_两个有序序列的中位数 (25).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int maxn = 100005; 5 | int s1[maxn], s2[maxn]; 6 | 7 | int main() { 8 | int n; 9 | cin >> n; 10 | for (int i = 0; i < n; i++) cin >> s1[i]; 11 | for (int i = 0; i < n; i++) cin >> s2[i]; 12 | 13 | int lo = 0, hi = 0, pos = 0; 14 | while (lo < n || hi < n) { 15 | int v; 16 | if (lo >= n) v = s2[hi++]; 17 | else if (hi >= n) v = s1[lo++]; 18 | else if (s1[lo] < s2[hi]) v = s1[lo++]; 19 | else v = s2[hi++]; 20 | pos += 1; 21 | if (pos == n) { 22 | cout << v << endl; 23 | break; 24 | } 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-5_堆中的路径 (25).py: -------------------------------------------------------------------------------- 1 | def Insert(heap, val): 2 | heap.append(val) 3 | pos = len(heap)-1 4 | while pos/2 >= 1 and heap[pos/2] > heap[pos]: 5 | heap[pos/2], heap[pos] = heap[pos], heap[pos/2] 6 | pos = pos/2 7 | 8 | def PrintPath(heap, i): 9 | ans = [] 10 | while i >= 1: 11 | ans.append(heap[i]) 12 | i /= 2 13 | print ' '.join(map(str, ans)) 14 | 15 | 16 | n, m = map(int, raw_input().split()) 17 | vals = map(int, raw_input().split()) 18 | heap = [None] 19 | for v in vals: 20 | Insert(heap, v) 21 | q = map(int, raw_input().split()) 22 | for i in q: 23 | PrintPath(heap, i) -------------------------------------------------------------------------------- /数据结构与算法题目集(中文)/7-7_六度空间 (30) .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, m, a, b; 7 | scanf("%d %d", &n, &m); 8 | vector> dist(n+1, vector(n+1, 0x3fffffff)); 9 | for (int i = 1; i <= n ; i++) dist[i][i] = 0; 10 | for (int i = 0; i < m; i++) { 11 | scanf("%d %d", &a, &b); 12 | dist[a][b] = dist[b][a] = 1; 13 | } 14 | 15 | // Floyd short path 16 | for (int k = 1; k <= n; k++) { 17 | for (int i = 1; i <= n; i++) { 18 | for (int j = 1; j <= n; j++) { 19 | if (dist[i][k] + dist[k][j] < dist[i][j]) { 20 | dist[i][j] = dist[i][k] + dist[k][j]; 21 | } 22 | } 23 | } 24 | } 25 | 26 | for (int i = 1; i <= n; i++) { 27 | int count = 0; 28 | for (int j = 1; j <= n; j++) { 29 | if (dist[i][j] <= 6) count++; 30 | } 31 | printf("%d: %.2f%%\n", i, count / (float)n * 100); 32 | } 33 | 34 | return 0; 35 | } 36 | --------------------------------------------------------------------------------