- 博客(19)
- 收藏
- 关注
原创 哈希(hash)算法思路总结
在判断一系列被判断字符a[N]是否在另一系列判断字符std[N]中出现过时,可以将判断的字符由hashTable存起来:bool hashTable[128];//下标0-127分别对应ASCII表中相应的字符fill(hashTable,hashTable+128,false);for{ …… hashTable[std[i]]=true; ……}判断字符用hashTable存...
2019-05-17 19:58:58
346
原创 PAT A1092 To Buy or Not to Buy
代码#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char shop[1001];char Eva[1001];//初始状态两个数组都为零 ASC1存shop每个珠子的个数 ASC2存Eva每个珠子的个...
2019-05-16 22:26:26
146
原创 PAT B1043 输出PATest
代码#include<algorithm>#include<iostream>#include<cstring>using namespace std;char str[10001];int hashT[6];char a[6]={'P','A','T','e','s','t'};int main(){ fill(hashT,hashT+6,0...
2019-05-16 22:18:26
120
原创 PAT B1033 旧键盘打字
有一个测试点巨坑在没有一个按键是坏的情况下,第一行输入为空,只有一个 ‘\n’ ,因此要想办法单独处理这一种情况以下是代码#include<iostream>#include<cstring>#include<algorithm>using namespace std;bool ASCII[128];char broken[100001];c...
2019-05-16 16:18:41
129
原创 甲1015 德才论
题目地址代码#include<stdio.h>#include<algorithm>using namespace std;struct student{ long long No; int virtue; int talent; int sum; int level; int rank;}stu[100001];bool cmp(studen...
2019-05-14 22:02:28
125
原创 动态规划之数字三角形问题(POJ1163)的两种解法
题目代码解法1将每一次计算过的最大值存至数组maxSum中,避免重复计算,提高效率#include<iostream>#include<algorithm>using namespace std;#define MAX 101int D[MAX][MAX];int n;int maxSum[MAX][MAX];int MaxSum(int i,i...
2019-05-12 20:55:10
358
原创 分治之归并排序及其时间复杂度的计算(附快速排序函数)
分治的典型应用:归并排序归并排序的主要思路:将前一半排序将后一半排序将排完序的前后合并到tmp数组,再复制回原数组代码#include<iostream>using namespace std;void Merge (int a[],int s,int m,int e,int tmp[])//将数组a的局部a[s,m]和a[m+1]有序地合并到tmp//然后拷...
2019-05-12 16:37:52
917
原创 STL初步认识与实际应用之sort排序(c++实现)
STL (standard template library)标准模板库使用时需要#include<algorithm>使用STL中 sort() 进行排序用法一 从小到大排序实例如下:用法二 从大到小排序用法三 自定义排序代码实例1#include<iostream>#include<cstring>#include&l...
2019-05-11 22:31:53
195
原创 二分算法之二分法求单调方程根
说明:此代码只能计算单调函数的解,批注处为函数表达式,可根据需要修改题目要求代码#include<cstdio>#include<iostream>#include<cmath>using namespace std;double EPS = 1e-6;double f(double x){ return x*x*x - 5*x*x + ...
2019-05-11 21:19:12
304
原创 二分算法之二分查找函数实现
在包含size个函数且从小到大排序的int数组a里找元素p找到则返回元素下标,找不到返回-1代码//在包含size个函数且从小到大排序的int数组a里找元素p //找到则返回元素下标,找不到返回-1 int BinarySearch(int a[],int size,int p){ int L = 0;//查找区间的左端点 int R = size-1;//查找区间的右端点 ...
2019-05-11 21:13:30
226
原创 递归之计算24点
题目思路递归边界条件:最后剩一个数等于24代码#include<iostream>#include<cmath>using namespace std;#define EPS 1e-6double a[5];bool isZero(double x)//检验x是否为0 { return fabs(x) <= EPS; } boo...
2019-05-11 17:58:36
1063
原创 递归之爬楼梯、放苹果问题
爬楼梯问题题目思路边界条件三选一。代码#include<iostream>using namespace std;int f(int N){ if(N<3){ if(N==2)return 2; else return 1; } return f(N-1)+f(N-2);}int main(){ int N; cin >>...
2019-05-10 13:11:44
347
原创 递归之四则运算表达式求值问题
题目思路使用递归的思想定义表达式:代码#include<iostream>#include<cstring>#include<cstdio>using namespace std;int factor_value();int term_value();int expression_value();int main(){ cou...
2019-05-10 12:18:48
486
原创 递归之N皇后问题
题目在N×N格的国际象棋上摆放N个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。输出的棋盘中,“1”代表此位置有皇后,“0”代表此位置为空。代码#include<iostream>#include<cmath>using namespace std;//N个皇后int N;//存放皇后的位置//如que...
2019-05-07 20:03:39
244
原创 枚举之称硬币问题(POJ1013)
题目:代码:#include<iostream>#include<cstring>using namespace std;char Left[3][7];char Right[3][7];char result[3][7];//枚举每个硬币(c)//假设硬币是较轻的假币(light为true)或较重的假币(light为false)//带入看是否...
2019-05-06 21:01:23
313
原创 枚举之生理周期问题
题目如【5 20 34 325】三个高峰出现的日子分别是5 20 34,要算在325天后的三个高峰同时出现的日子N,输出N-325,输入-1 -1 -1 -1 终止。代码:#include<iostream>using namespace std;int main() { int a,b,c,n; while(cin>>a>>b>&...
2019-05-05 21:48:42
268
原创 枚举之完美立方
题目:代码:#include<iostream>using namespace std;int main(){ int n; cin >> n; for(int a=2;a<=n;a++){ for(int b = 2;b<a;b++){ for(int c = b;c<a;c++){ for(int d = c;d&...
2019-05-05 21:39:23
226
原创 递归之逆波兰表达式求值问题
递归思路运算符 逆波兰表达式 逆波兰表达式代码#include<iostream>#include<cstdlib>using namespace std;double exp(){//运算符 逆波兰表达式 逆波兰表达式 char s[20]; cin >> s; switch(s[0]) { case '+' : return exp...
2019-04-30 17:55:31
318
原创 递归之阶乘与Hanoi塔问题
阶乘问题int Factorial(int n){ if(n == 0){ return 1; }else{ return n * Factorial(n-1); }}Hanoi问题//将src坐上的n个盘子,以mid为中转,移动到dest座void Hanoi(int n,char src,char mid,char dest){ if(n == 1)...
2019-04-30 16:33:07
152
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人