
算法
g28_gerwulf
这个作者很懒,什么都没留下…
展开
-
DFS+剪枝解决0-1背包
有num件物品,每件物品的重量为w[i],价值为v[i]。现在需要选出若干件物品放入一个容量为capacity的背包中,使得在选入背包的物品重量和不超过容量capacity的前提下,让背包中的物品的价值之和最大,求最大价值(1 <= num <= 20)分析:可用DFS搜索所有情况,但缺点在于进行过多无用搜索。可以用当前重量与最大重量的关系来限制,当条件满足时再更新价值,进行下次搜索。...原创 2018-04-14 14:10:57 · 653 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1020
1020. Tree Traversals (25)时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueSuppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal...原创 2018-04-16 10:15:04 · 149 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1043
1043. Is It a Binary Search Tree (25)时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The...原创 2018-04-17 08:13:08 · 161 阅读 · 0 评论 -
丢手帕问题(约瑟夫问题-数组实现)
约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。 例如N=6,M=5,被杀掉的人的序号为5,4,6,2,3。最后剩下1号。输入两个整数N,M 比如 6 5 输出被杀掉的人的序号 比如5 4 6 2 3 1 再比如: 输入 11 3 输出 3 6 9 1 5 10 4 11 8 2 7分析:这里用数组实现约瑟夫问题。用数组从1开始,记录每个...原创 2018-03-31 22:04:48 · 429 阅读 · 0 评论 -
八皇后问题
八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行、纵行或斜线上。如下图就是一种符合要求的放置方法。分析:这里的处理方法是深搜回溯,用一个数组储存摆放结果,下标代表行数,下标对应的元素代表列数。由此达到没有皇后在同一行同一列的效果。每当确定一组行数与列数的组合,检测是否有...原创 2018-04-05 12:50:16 · 176 阅读 · 2 评论 -
蓝桥杯 ALGO-3 K好数
算法训练 K好数 时间限制:1.0s 内存限制:256.0MB 问题描述如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数。求L位K进制数中K好数的数目。例如K = 4,L = 2的时候,所有K好数为11、13、20、22、30、31、33 共7个。由于这个数目很大,请你输出它对1000000007取模后的值。输入格式输入包含两个正整数,...原创 2018-05-17 21:17:17 · 206 阅读 · 0 评论 -
PAT (Basic Level) Practice 1025
1025 反转链表 (25)(25 分)给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。输入格式:每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 10^5^)、以及正整数K(&l...原创 2018-05-19 20:19:11 · 302 阅读 · 0 评论 -
PAT (Basic Level) Practice 1029
1029 旧键盘(20)(20 分)旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。输入格式:输入在2行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过80个字符的串,由字母A-Z(包括大、小写)、数字0-9、以及下划线“_”(代表空格)组成。题目保证2个字符串均非空。输出格式:按照发现顺...原创 2018-05-22 21:29:33 · 224 阅读 · 0 评论 -
PAT (Basic Level) Practice 1033
1033 旧键盘打字(20)(20 分)旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?输入格式:输入在2行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过10^5^个字符的串。可用的字符包括字母[a-z, A-Z]、数字0-9、以及下划线“_”(代表空...原创 2018-07-31 02:58:55 · 177 阅读 · 0 评论 -
PAT (Basic Level) Practice 1038
1038 统计同成绩学生(20)(20 分)本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出。输入格式:输入在第1行给出不超过10^5^的正整数N,即学生总人数。随后1行给出N名学生的百分制整数成绩,中间以空格分隔。最后1行给出要查询的分数个数K(不超过N的正整数),随后是K个分数,中间以空格分隔。输出格式:在一行中按查询顺序给出得分等于指定分数的学生人数,中间以空...原创 2018-07-31 04:52:38 · 115 阅读 · 0 评论 -
PAT (Basic Level) Practice 1039
1039 到底买不买(20)(20 分)小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如在图1中,第3串是小红想做的珠串;那...原创 2018-07-31 05:32:05 · 169 阅读 · 0 评论 -
K-NN算法(K-Nearest Neighbor)
原理思想:若一个样本在特征空间中的K个最相似的样本中的大多数属于同一个类别,则该样本也属于这个类别。度量距离:欧式距离算法大致步骤: 1.计算已知集内的样本与待测样本的距离 2.对距离由小到大进行排序,找出前K个点 3.在这K个点中,统计每个类别出现的次数 4.选择出现次数最多的点作为待测样本的类别...原创 2018-08-04 17:25:50 · 613 阅读 · 0 评论 -
PAT (Basic Level) Practice 1035
1035 插入与归并(25 分)根据维基百科的定义:插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列。每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置。如此迭代直到全部元素有序。归并排序进行如下迭代操作:首先将原始序列看成 N 个只包含 1 个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下 1 个有序的序列。现给定原始序列和由...原创 2018-07-31 21:56:57 · 167 阅读 · 0 评论 -
PAT (Basic Level) Practice 1056
1056 组合数的和(15 分)给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字。要求所有可能组合出来的 2 位数字的和。例如给定 2、5、8,则可以组合出:25、28、52、58、82、85,它们的和为330。输入格式:输入在第一行中给出 N(1 < N < 10),随后一行给出 N 个不同的非 0 个位数字。数字间以空格分隔。...原创 2018-08-12 11:06:52 · 122 阅读 · 0 评论 -
K-Means算法(K-Means Clustering)
聚类(clustering):把相似的东西分成一组。与分类(classification)不同,聚类不需要从训练集中“学习”,只需要计算相似度就可以了。因此被归为无监督学习。原理思想:对每一个cluster,选出一个中心点(center),使该cluster中的所有点到该中心点的距离小于到其他cluster的中心的距离。算法大致步骤: 1.选定k个中心的初值,一般采用...原创 2018-08-12 14:30:08 · 429 阅读 · 0 评论 -
数据结构——线性表(Java实现)
此处包括一个泛型线性表抽象类,一个Integer型实现类,一个测试类。实现了线性表的以下功能:1.插入2.删除3.追加4.查找5.取元素6.判空7.求长度以下为代码:泛型线性表抽象类LinearList.java:package orderedlist;import java.lang.reflect.Array;public abstra...原创 2018-08-15 08:03:51 · 892 阅读 · 0 评论 -
PAT (Basic Level) Practice 1060
1060 爱丁顿数(25 分)英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数” E ,即满足有 E 天骑车超过 E 英里的最大整数 E。据说爱丁顿自己的 E 等于87。现给定某人 N 天的骑车距离,请你算出对应的爱丁顿数 E(≤N)。输入格式:输入第一行给出一个正整数 N (≤105),即连续骑车的天数;第二行给出 N 个非负整数,代表每天...原创 2018-08-13 02:44:00 · 366 阅读 · 0 评论 -
数据结构——顺序栈(Java实现)
此处包括一个泛型顺序栈抽象类,一个Integer型实现类,一个测试类。实现了栈的以下功能:1.入栈2.出栈3.判空4.求长度5.取栈顶6.扩充长度泛型顺序栈抽象类Stack.java:package stack;import java.lang.reflect.Array;import java.util.Arrays;public abstra...原创 2018-08-15 23:35:10 · 1164 阅读 · 0 评论 -
数据结构——顺序队列(Java实现)
此处包括一个泛型顺序队列抽象类,一个Integer型实现类,一个测试类。实现了队列的以下功能:1.队尾入队2.队首出队3.取队首4.判空5.求长度6.删除队首元素7.扩充长度泛型顺序队列抽象类Queue.java:package queue;import java.lang.reflect.Array;import java.util.Array...原创 2018-08-16 01:37:45 · 282 阅读 · 0 评论 -
数据结构——链表(Java实现)
此处包括一个泛型链表抽象类,一个Integer型实现类,一个测试类。实现了链表的以下功能:1.节点添加(第一个或最后一个)2.节点删除(第一个或最后一个)3.遍历(返回一个数组)4.排序5.删除指定元素6.取指定元素7.扩充长度8.判空9.取长度10.清空链表泛型链表抽象类LinkedList.java:package linkedlist...原创 2018-08-17 01:52:45 · 205 阅读 · 0 评论 -
数据结构——二叉树(Java实现)
此处包括一个泛型二叉树抽象类,一个Integer型实现类,一个测试类。实现了二叉树的以下功能:1.先序遍历2.中序遍历3.后序遍历4.求高度5.求节点总数6.取指定节点的双亲节点7.删除指定节点8.判空9.清空二叉树泛型二叉树抽象类BinaryTree.java:package binarytree;import java.util.Que...原创 2018-08-22 17:17:32 · 438 阅读 · 0 评论 -
PAT (Basic Level) Practice 1073
073 多选题常见计分法(20 分)批改多选题是比较麻烦的事情,有很多不同的计分方法。有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数;如果考生选择了任何一个错误的选项,则不能得分。本题就请你写个程序帮助老师批改多选题,并且指出哪道题的哪个选项错的人最多。输入格式:输入在第一行给出两个正整数 N(≤1000)和 M(≤100),分别是...原创 2018-08-17 14:28:27 · 151 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1003(Dijkstra)
1003 Emergency (25 分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams...原创 2018-09-15 17:11:44 · 175 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1003(Bellman-Ford)
1003 Emergency (25 分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams...原创 2018-09-15 20:17:00 · 202 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1013
1013 Battle Over Cities (25 分)It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We...原创 2018-09-16 00:51:21 · 227 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1030(Dijkstra)
1030 Travel Plan (30 分)A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to d...原创 2018-09-17 21:17:19 · 154 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1030(Dijkstra + DFS)
1030 Travel Plan (30 分)A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to d...原创 2018-09-17 21:20:33 · 184 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1018
1018 Public Bike Management (30 分)There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and ret...原创 2018-09-18 00:01:01 · 193 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1034
1034 Head of a Gang (30 分)One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a...原创 2018-09-23 16:21:10 · 185 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1076
1076 Forwards on Weibo (30 分)Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed wi...原创 2018-09-23 17:39:28 · 170 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1087
1087 All Roads Lead to Rome (30 分)Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most hap...原创 2018-09-23 19:05:11 · 244 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1091
1091 Acute Stroke (30 分)One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each M...原创 2018-09-23 22:32:49 · 231 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1111
1111 Online Map (30 分)Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other...原创 2018-09-25 17:48:12 · 150 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1037
1037 Magic Coupon (25 分)The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times t...原创 2018-10-04 21:41:12 · 148 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1106
1106 Lowest Price in Supply Chain (25 分)A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Start...原创 2018-10-11 23:36:03 · 1564 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1038
1038 Recover the Smallest Number (30 分)Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recove...原创 2018-10-04 22:27:58 · 148 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1007
1007 Maximum Subsequence Sum (25 分)Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1≤i≤j≤K. The Maximum Su...原创 2018-10-05 21:15:33 · 172 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1045
1045 Favorite Color Stripe (30 分)Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pi...原创 2018-10-08 19:24:50 · 199 阅读 · 0 评论 -
PAT (Advanced Level) Practice 1004
1004 Counting Leaves (30 分)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 on...原创 2018-10-09 19:42:17 · 181 阅读 · 0 评论 -
蓝桥杯 ALGO-1 区间k大数查询
算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个。输入格式第一行包含一个数n,表示序列长度。第二行包含n个正整数,表示给定的序列。第三个包含一个正整数m,表示询问个数。接下来m行,每行三个数l,r,K,表示询问序列从左往右第l个数到第r个数中,从大...原创 2019-01-31 12:48:29 · 111 阅读 · 0 评论