![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构与算法
收录算法题解 和 数据结构的应用
Seajunnn
hust 软件工程
展开
-
Palindromes _easy version
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。Input输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。Output如果一个字符串是回文串,则输出"yes",否则输出"no".Sample Input4levelabcdenoonhaha...原创 2020-01-19 16:35:31 · 145 阅读 · 0 评论 -
汉字统计
统计给定文本文件中汉字的个数。Input输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。Output对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。[Hint:]从汉字机内码的特点考虑~Sample Input2WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!马上就要期末考试了Are you ready?...原创 2020-01-19 16:47:55 · 179 阅读 · 0 评论 -
词组缩写
题目描述定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写。比如,C语言里常用的EOF就是end of file的缩写。输入输入的第一行是一个整数T,表示一共有T组测试数据。接下来有T行,每组测试数据占一行,每行有一个词组,每个词组由一个或多个单词组成;每组的单词个数不超过10个,每个单词有一个或多个大写或小写字母组成;单词长度不超过10,由一个或多个空格分隔这些单词。输出...原创 2020-01-23 16:58:39 · 741 阅读 · 0 评论 -
小明的烦恼
题目描述小明最近新买了一个房间,为了给它做装修,想要给它铺上地砖。然而现有的地砖只有两种规格分别为1米1米、2米2米,由于小明买的房间有点小,宽度只有3米,长度为N米。当然这样一个房间也足够他自己一个人住了。那么如果要给这个房间铺设地砖,且只用以上这两种规格的地砖,请问有几种铺设方案。输入输入的第一行是一个正整数C,表示有C组测试数据。接下来C行,每行输入一个正整数n(1<=n<...原创 2020-01-21 17:32:17 · 1015 阅读 · 0 评论 -
N皇后问题
在NN的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。你的任务是,对于给定的N,求出有多少种合法的放置方法。Input共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。Output共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。Sample Input185...原创 2020-01-21 10:03:34 · 185 阅读 · 0 评论 -
棋盘问题
题目描述在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。输入输入含有多组测试数据。每组数据的第一行是两个正整数n和k,用一个空格隔开,表示了将在一个nn的矩阵内描述棋盘,以及摆放棋子的数目。(n<=8,k<=n)当n和k均为-...原创 2020-01-21 19:54:27 · 1374 阅读 · 0 评论 -
彩票
题目描述有一种彩票的玩法是从1~49这49个整数中挑选6个数字。小明为此研究出一个选号的策略,就是从这49个数字中选出一个子集S,S中包含了k(k>6)个数字,然后从S中再选择6个数字作为最终选定的号码。你的任务是,给你k和集合S,输出从S中选择投注号码的所有组合。输入输入包含多组测试数据。每组输入首先是一个整数k(6<k<13)。然后是k个整数,表示集合S,这k个整数按...原创 2020-03-10 15:27:22 · 1000 阅读 · 0 评论 -
出租车费
题目描述某市出租车计价规则如下:起步4公里10元,即使你的行程没超过4公里;接下来的4公里,每公里2元;之后每公里2.4元。行程的最后一段即使不到1公里,也当作1公里计费。一个乘客可以根据行程公里数合理安排坐车方式来使自己的打车费最小。例如,整个行程为16公里,乘客应该将行程分成长度相同的两部分,每部分花费18元,总共花费36元。如果坐出租车一次走完全程要花费37.2元。现在给你整个行程的...原创 2020-02-13 19:16:30 · 254 阅读 · 0 评论 -
集合A-B
参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法运算。(当然,大家都知道集合的定义,就是同一个集合中不会有两个相同的元素,这里还是提醒大家一下)呵呵,很简单吧?Input每组输入数据占1行,每行数据的开始是2个整数n(0<=n<=100)和m(0<=m<=1...原创 2020-01-18 14:53:31 · 7437 阅读 · 0 评论 -
统计单词
题目描述编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。(凡是以一个或多个空格隔开的部分就为一个单词)输入输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。输出可能有多组测试数据,对于每组数据,输出字符串中每个单词包含的字母的个数。样例输入 Copyhello how are you....原创 2020-03-17 10:06:24 · 331 阅读 · 0 评论 -
倒叙输出字符串
*Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.InputThe input contains several test ca...原创 2020-01-17 15:56:14 · 298 阅读 · 0 评论 -
map习题
Every girl likes shopping,so does dandelion.Now she finds the shop is increasing the price every day because the Spring Festival is coming .She is fond of a shop which is called “memory”. Now she want...原创 2020-01-18 15:23:14 · 422 阅读 · 0 评论 -
医院排队
看病要排队这个是地球人都知道的常识。不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则...原创 2020-01-18 16:09:44 · 877 阅读 · 0 评论 -
士兵队列训练
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。Input本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不...原创 2020-01-17 15:08:09 · 763 阅读 · 0 评论 -
乒乓球赛
有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。球赛的规则如下:如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。Inp...原创 2020-01-17 23:05:15 · 732 阅读 · 0 评论 -
简单计算器
*读入一个只包含 +, -, , / 的非负整数计算表达式,计算该表达式的值。Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。Output对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。Sample Input1 + 24 + 2 * 5 - 7 ...原创 2020-01-17 20:30:54 · 224 阅读 · 0 评论 -
PAT-A-1053 Path of Equal Weight (30分)(dfs)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti . The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.Now given any weighted tre原创 2020-07-15 12:05:33 · 121 阅读 · 0 评论 -
PAT-算法笔记-3.6-问题 B: 首字母大写
题目描述对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(’ ‘)、制表符(’\t’)、回车符(’\r’)、换行符(’\n’)。输入输入一行:待处理的字符串(长度小于100)。输出可能有多组测试数据,对于每组数据,输出一行:转换后的字符串。样例输入 Copyif so, you already have a google account. you can sign in on the right.样原创 2020-05-26 10:04:43 · 201 阅读 · 0 评论 -
PAT-算法笔记-打印日期
题目描述给出年分m和一年中的第n天,算出第n天是几月几号。输入输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。输出可能有多组测试数据,对于每组数据,按 yyyy-mm-dd的格式将输入中对应的日期打印出来。样例输入 Copy2013 602012 3002011 3502000 211样例输出 Copy2013-03-012012-10-262011-12-162000-07-29这题的坑点在于输出时年份的格式不能错误不然会报50原创 2020-05-21 15:43:30 · 143 阅读 · 0 评论 -
PAT-算法笔记-4.4-问题 B: 出租车费
题目描述某市出租车计价规则如下:起步4公里10元,即使你的行程没超过4公里;接下来的4公里,每公里2元;之后每公里2.4元。行程的最后一段即使不到1公里,也当作1公里计费。一个乘客可以根据行程公里数合理安排坐车方式来使自己的打车费最小。例如,整个行程为16公里,乘客应该将行程分成长度相同的两部分,每部分花费18元,总共花费36元。如果坐出租车一次走完全程要花费37.2元。现在给你整个行程的公里数,请你计算坐出租车的最小花费。输入输入包含多组测试数据。每组输入一个正整数n(n<1000000原创 2020-05-28 23:37:20 · 763 阅读 · 0 评论 -
PAT-A-1027 Colors in Mars (20分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only原创 2020-07-23 09:46:02 · 85 阅读 · 0 评论 -
PAT-算法笔记-问题 D: 八皇后
题目描述会下国际象棋的人都很清楚:皇后可以在横、竖、斜线上不限步数地吃掉其他棋子。如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题。对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2…b8,其中bi为相应摆法中第i行皇后所处的列数。已经知道8皇后问题一共有92组解(即92个不同的皇后串)。给出一个数b,要求输出第b个串。串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小。输入第1行是测试数据的组数n,后面跟原创 2020-05-28 14:29:45 · 807 阅读 · 0 评论 -
PAT-A-1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can help to narrow down a speaker’s identity, which is useful when validating, for exampl原创 2020-07-09 15:55:38 · 86 阅读 · 0 评论 -
PAT-A-1054 The Dominant Color
Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than原创 2020-07-09 14:47:55 · 121 阅读 · 0 评论 -
PAT-Shortest Distance
题目描述The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.输入Each input file contains one test case. For each case, the first line contains an integer N原创 2020-05-19 15:48:23 · 92 阅读 · 0 评论 -
PAT-A-1022 Digital Library
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are suppose原创 2020-07-09 21:26:45 · 102 阅读 · 0 评论 -
PAT-A-1095 Cars on Campus (30分)
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the num原创 2020-07-24 12:23:28 · 103 阅读 · 0 评论 -
PAT-A-1064 Complete Binary Search Tree (30分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node’s key.The right subtree of a node contains only nodes with keys greater than原创 2020-07-15 16:38:44 · 106 阅读 · 0 评论 -
PAT-A-1043 Is It a Binary Search Tree (25分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node’s key.The right subtree of a node contains only nodes with keys greater than原创 2020-07-15 15:45:12 · 95 阅读 · 0 评论 -
PAT-算法笔记-4.4-问题 D: Repair the Wall
题目描述Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon.When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what原创 2020-06-04 10:02:39 · 139 阅读 · 0 评论 -
PAT-A-1077 Kuchiguse
The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker’s personality. Such a preference is called “Kuchiguse” and is often exaggerated artistically in Ani原创 2020-07-23 15:15:36 · 98 阅读 · 0 评论 -
PAT-A-1066 Root of AVL Tree (25分)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate t原创 2020-07-16 10:41:11 · 92 阅读 · 0 评论 -
PAT-A-1062 Talent and Virtue (25分)
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people’s talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a “sage(圣人)”; being less excellent but with o原创 2020-07-23 16:51:30 · 90 阅读 · 0 评论 -
PAT-A-1094 The Largest Generation【DFS+BFS】
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.Input Specification:Each input file contains one test case. Ea原创 2020-07-14 23:46:17 · 104 阅读 · 0 评论 -
PAT-算法笔记-问题 A: 谁是你的潜在朋友
题目描述“臭味相投”——这是我们描述朋友时喜欢用的词汇。两个人是朋友通常意味着他们存在着许多共同的兴趣。然而作为一个宅男,你发现自己与他人相互了解的机会 并不太多。幸运的是,你意外得到了一份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发现潜在的朋友。首先你对借阅记录进行了一番整理,把N个读者依次编号为1,2,…,N,把M本书依次编号为1,2,…,M。同时,按照“臭味相投”的原则,和你喜欢读同一本书的人,就是你的潜在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。输入每个案例原创 2020-05-27 12:13:02 · 432 阅读 · 0 评论 -
PAT-A-1044 Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the原创 2020-07-04 17:57:38 · 842 阅读 · 1 评论 -
PAT-A1086 Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(原创 2020-07-14 14:19:58 · 89 阅读 · 0 评论 -
PAT-算法笔记-问题 B: 数列
题目描述编写一个求斐波那契数列的递归函数,输入n 值,使用该递归函数,输出如下图形(参见样例)。输入输入第一行为样例数m,接下来有m行每行一个整数n,n不超过10。输出对应每个样例输出要求的图形(参见样例格式)。样例输入 Copy16样例输出 Copy00 1 10 1 1 2 30 1 1 2 3 5 80 1 1 2 3 5 8 13 210 1 1 2 3 5 8 13 21 34 55代码如下:/* 0 0 1 1 0原创 2020-05-28 10:42:29 · 125 阅读 · 0 评论 -
PAT-A-1056 Mice and Rice
Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse.First the pl原创 2020-07-10 15:03:19 · 115 阅读 · 0 评论 -
PAT-A-1004 Counting Leaves (30分)【DFS+BFS】
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes原创 2020-07-15 10:49:31 · 92 阅读 · 0 评论