
算法编程
久许
朋友拍了拍我,说我可不是什么幺蛾子
展开
-
5 小数化分数
Ray 在数学课上听老师说,任何小数都能表示成分数的形式,他开始了化了起来,很快他就完成了,但他又想到一个问题,如何把一个循环小数化成分数呢 ?请你写一个程序不但可以将普通小数化成最简分数,也可以把循环小数化成最简分数。输入第一行是一个整数 N,表示有多少组数据。每组数据只有一个纯小数,也就是整数部分为 0。小数的位数不超过 9 位,循环部分用 () 括起来。输出对每一个对应的小数...原创 2019-02-22 14:14:50 · 586 阅读 · 0 评论 -
Sum It Up HOJ1258
Sum It UpTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8382Accepted Submission(s): 4406Problem DescriptionGiven a specified total t...原创 2019-03-07 19:08:21 · 172 阅读 · 0 评论 -
Accepted Necklace HOJ2660
Accepted NecklaceTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4686 Accepted Submission(s): 1861Problem DescriptionI have N precious...原创 2019-03-08 13:02:55 · 185 阅读 · 0 评论 -
01背包
#include<cstdio>#include<algorithm>using namespace std;#define C 10 //背包的呃容量#define n 5 //物品的个数int w[n] = { 2, 2, 6, 5, 4 }; //物品的重量int v[n] = { 6, 3, 5, 4, 6 }; //物品的价值int x[n]...原创 2019-03-22 19:50:16 · 126 阅读 · 0 评论 -
dp 从一个数组中选取若干项,求最大和(项与项之间不相邻)
#include<cstdio>#include<algorithm>using namespace std;#define n 7int arr[n] = {1,2,4,1,7,8,3};int dis_sum[100];void dp(){ dis_sum[0] = arr[0]; dis_sum[1] = max(arr[0],arr[1]);...原创 2019-03-23 10:32:57 · 975 阅读 · 0 评论 -
dp 最大连续子序列和
#include<cstdio>#include<algorithm>using namespace std;#define n 8int arr[n] = { 1, -2, 4, 1, -5, 7, -8, 3 };int near_s[100];void dp() { near_s[0] = arr[0]; for (int i = 1; i &...原创 2019-03-23 11:02:26 · 225 阅读 · 0 评论 -
斐波那契数列
#include<cstdio>#include<algorithm>using namespace std;#define n 40int fib[100];void so_fib(){ fib[0] = 1; fib[1] = 1; for(int i=2;i<n;i++){ fib[i] = fib[i-1] + fib[i-2];...原创 2019-03-23 11:11:21 · 131 阅读 · 0 评论 -
动态规划,判断一个数列的若干项之和是否等于某一个给定的数
#include<cstdio>using namespace std;#define n 6int arr[6] = { 3, 34, 4, 12, 5, 2 };int subset[6][100];void dp(int C) { //C是要凑的数 for (int i = 0; i < n; i++) subset[i][0] = 1; //不管对...原创 2019-03-23 09:51:17 · 978 阅读 · 0 评论 -
四种类型的排列
排列可以分为两类,第一类:是否有序(每一个元素值是否都比前一个元素值大),第二类:数组元素是否可以重复(数组中每个元素是否可以使用多次)。那么这两类结合在一起,就构成了四种情况。即:一、有序可重复二、有序不可重复三、无序可重复四、无序不可重复(可使用回溯)1、有序可重复 代码#include<cstdio>using namespace std;int...原创 2019-03-24 10:09:14 · 1271 阅读 · 0 评论 -
Trie树(字典树)
trie树,又称字典树或前缀树,是一种有序 的、用于统计、排序和存储字符串的数据 结构,它与二叉查找树不同,关键字不是 直接保存在节点中,而是由节点在树中的 位置决定。 一个节点的所有子孙都有相同的前缀,也 就是这个节点对应的字符串,而根节点对 应空字符串。一般情况下,不是所有的节点 都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。 trie树的最大优点就是利用字符串的公共前 缀来...原创 2019-06-07 15:45:28 · 460 阅读 · 0 评论 -
Hash表的简单实现
Hash表的定义哈希表(Hash table,也叫散列表),是根据关键字值(key)直接进行访问的数据结构,它通过把关键字值 映射到表中一个位置 (数组下标 )来直接访问,以加快查找 关键字值的速度。这个映射函数叫做 哈希(散列)函数,存放记录的数组 叫做哈希 ( )(散列)表。 给定表M,存在函数f(key),对任意的关键字值key,代入函数后若能得到包含该 关键字的表中地址,称表M为哈希(...原创 2019-06-07 17:07:38 · 1780 阅读 · 1 评论 -
B树的删除和插入以及B+树的概念
B树的插入与删除B树的插入B树的删除当删除的关键字k不在终端结点时当所删除的关键字在终端结点时1直接删除2兄弟够借3兄弟不够借B+树的概念m阶B+树与B树主要差别B+树示例参考:王道2019数据结构考研复习指导...原创 2019-06-05 08:24:38 · 770 阅读 · 0 评论 -
正则表达式
[0-8] 0到8必须出现一次[0-8]? 0到8可以出现一次,也可以不出现,但是尽量匹配1次[0-8]?? 0到8可以出现一次,也可以不出现,但是尽量匹配0次[0-8]* 0到8可以出现0次到n次,但是尽量匹配n次[0-8]*? 0到8可以出现0次到n次,但是尽量匹配0次[0-8]+ 0到8可以出现1次到n次,但是尽量匹配n次[0-8]+? 0到8可以出现1次到n次,...原创 2019-06-21 21:47:22 · 512 阅读 · 0 评论 -
红黑树——算法导论
转自:https://www.cnblogs.com/dongkuo/p/4922058.html1. 什么是红黑树(1) 简介 上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树。但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快;即当树的高度较高(甚至一种极端情况是树变成了1条链)时,这些集合操作并不比在链表上执行的快。 于是我们需要构建出...转载 2019-07-28 11:55:55 · 215 阅读 · 0 评论 -
3. 无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: “abcabcbb”输出: 3解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。示例 2:输入: “bbbbb”输出: 1解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。示例 3:输入: “pwwkew”输出: 3解释: 因为无重复字符的最长子串是 “wke”,所...原创 2019-09-29 15:38:33 · 127 阅读 · 0 评论 -
深搜05(深搜+减枝+存储字符串) Additive equations
ZOJ Problem Set - 1204Additive equationsTime Limit:10 Seconds Memory Limit:32768 KBWe all understand that an integer set is a collection of distinct integers. Now the question is: giv...翻译 2019-03-04 12:00:32 · 297 阅读 · 0 评论 -
自然数的拆分
1318:【例5.3】自然数的拆分时间限制: 1000 ms 内存限制: 65536 KB提交数: 4108 通过数: 2465【题目描述】任何一个大于1的自然数n,总可以拆分成若干个小于n的自然数之和。当n=7共14种拆分方法:7=1+1+1+1+1+1+17=1+1+1+1+1+27=1+1+1+1+37=1+1+1+2+27=...原创 2019-03-09 10:43:37 · 1243 阅读 · 0 评论 -
6 全排列
描述任意输入 n 个不重复的整数序列,输出序列的全排列。输入测试数据有多组,第一行是整数 t (0<t<20 ),代表测试组数。每组测试数据有两行,第一行是整数的个数 n(0<n<6) ,第二行是 n 个不重复的整数。输出按递增的顺序输出序列的全排列。每个测试数据后面输出一个空行。样例输入131 3 5样例输出1 3 51 5 33 1 53 ...原创 2019-02-22 17:38:21 · 1067 阅读 · 0 评论 -
7 (1+x)^n
(1+x)^nTime Limit: 2 Seconds Memory Limit: 65536 KBPlease calculate the coefficient modulo 2 of x^i in (1+x)^n.InputFor each case, there are two integers n, i (0<=i<=n<=2^31-1)...翻译 2019-02-23 09:30:42 · 2266 阅读 · 0 评论 -
8 Summing divisors
描述In the 18th century, L. Euler invented a function he called sigma to study properties of whole numbers. He was interestedin comparing a positive number to the sum of its positive divisors. In this...翻译 2019-02-23 11:25:40 · 261 阅读 · 0 评论 -
使用ifstream读入统计单词
ifstream的特点是,只能识别空格,其他的符号不识别(也可以理解为他把其他的字符当做字母来处理),它一次读入时,将逗号、引号、等等符号和单词都一并读入。这时我们需要将单词分离出来,有两种特殊情况需要考虑:hello,world my name 考虑这个,有个逗号,ifstream会将其分割为 hello,world my name 共三个部分。跳到一个新的单词时,i等于0。在h...原创 2019-05-23 22:09:23 · 615 阅读 · 0 评论 -
模拟从终端读入,分析单词,直到遇到回车
#include<cstdio>#include<iostream>#include<cstring>#include<fstream>#include<queue>using namespace std;typedef struct Food{ char word[20]; int frequency;}Wha...原创 2019-05-23 22:08:36 · 163 阅读 · 0 评论 -
11 快速取模
People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in theircellar, others like using Windows, and some like difficult mathematical games....翻译 2019-03-01 09:45:24 · 2870 阅读 · 0 评论 -
深搜01(深搜) Counting Sheep
A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day mygrandmother suggested I tried counting sheep after I'd gone to bed. As always whe...原创 2019-03-01 13:15:32 · 492 阅读 · 0 评论 -
速算24点(排列+枚举)
HOJ,problem1427速算24点Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6730Accepted Submission(s): 1753Problem Description速算24点相信绝大多数...翻译 2019-03-05 10:40:45 · 1358 阅读 · 0 评论 -
深搜+回溯 (ZOJ 1004 ) Anagrams by Stack
Anagrams by StackTime Limit:2 Seconds Memory Limit:65536 KBHow can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to T...原创 2019-03-05 13:22:36 · 269 阅读 · 0 评论 -
深搜02(深搜+回溯) Fire Net
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns,each representing a street or a piece of wall.A blockhouse is a small castle th...翻译 2019-03-02 11:20:33 · 314 阅读 · 0 评论 -
深搜03(深搜+回溯+奇偶减枝) Tempter of the Bone
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze beganto shake, and the doggie could feel the ground sinking. He realized that the bone...翻译 2019-03-02 14:57:56 · 180 阅读 · 0 评论 -
01 Wolf and Rabbit
描述There is a hill with n holes around. The holes are signed from 0 to n-1.A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get intois th...翻译 2019-02-21 08:44:47 · 165 阅读 · 0 评论 -
2 a^b
描述给定 a 和 b,输出 a^b 的最后一个数字。输入输入数据有多组,每组数据占一行,每行为 a 和 b 的值(0<a,b<=2^30 )输出对每组输入数据,输出 a^b 的最后一位数字,每组数据占一行。样例输入2 23 4样例输出41计算m的n次方时,可以将n裁开成1,2,4,8,16......的方式,如a的平方等于a的平方,a的平方的平方等于a的四...原创 2019-02-21 10:19:13 · 378 阅读 · 0 评论 -
3 筛选法求素数
描述请使用筛选法输出 [a, b] 之间的所有素数。输入输入数据有多组,每组数据占一行,每行 2 个正整数 a 和 b,其中 2<=a<=b<=1000000。输出每组数据按从小到大的顺序输出 [a, b] 中所有的素数,每行最多输出 10 个素数。每组数据之后空一行。样例输入2 32 50样例输出2 32 3 5 7 11 13 17 19 23 29...原创 2019-02-21 11:12:04 · 710 阅读 · 0 评论 -
4 The ones to remain
描述There are N soldiers standing in one line. They are marked from 1 to N, from right to left. And they are given a numberm. Then the soldiers numbered off, straight from the right-hand man. The one...翻译 2019-02-21 12:09:53 · 375 阅读 · 0 评论 -
搜索04 Mine Sweeper
题目链接DescriptionThe game Minesweeper is played on an n by n grid. In this grid are hidden m mines, each at a distinct grid location. The player repeatedly touches grid positions. If a position with...翻译 2019-03-03 11:55:44 · 241 阅读 · 0 评论 -
10 GCD & LCM
给定 x,y(2 <= x <= 100,000, 2 <= y <= 1,000,000) ,你要像下面那样计算出 p 和 q:1) p,q 是正整数 ;2) GCD(p, q) = x; 即 p,q 的最大公约数是 x3) LCM(p, q) = y. 即 p,q 的最小公倍数是 y输入输入多组测试数据,每组包含两个整数 x y输出每组输出满足条件的 p...原创 2019-02-27 13:52:34 · 286 阅读 · 0 评论 -
求最长回文子串的中心扩展算法
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。示例 1:输入: “babad”输出: “bab”注意: “aba” 也是一个有效答案。示例 2:输入: “cbbd”输出: “bb”来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-palindromic-subs...原创 2019-10-04 11:21:23 · 381 阅读 · 0 评论