
算法历练
文章平均质量分 58
Jcy
用忍者的心态学习
用武士的态度战斗
展开
-
字符串操作
题目:给定一串英文字符串,输出其对应的数字代码:#include #include #include #include #include #include using namespace std;int main(){ //freopen("in.txt","r",stdin); char line[256]; int ans;原创 2012-05-13 11:20:34 · 519 阅读 · 0 评论 -
大数运算(C++类实现)
#include#includeusing namespace std;const int maxn = 200;struct bign{ int len, s[maxn]; bign() { memset(s, 0, sizeof(s)); len = 1; } bign(int num) { *this = num; } bign原创 2012-12-14 19:25:00 · 2955 阅读 · 0 评论 -
最大公约数与最小公倍数算法
#include #include #define MAX(a,b) (a > b ? a : b)template T gcd(T a, T b){ return a > b ? (b == 0 ? b : a%b) : gcd(b,a%b);}int main(void){ int a,b; int resLcm = 0; int resG原创 2012-12-04 19:08:15 · 848 阅读 · 0 评论 -
基本排序之冒泡排序
简单的利用了一个标志量:#include #include int main(void){ int arr[] = {65,42,13,65,73,75,2,34}; int flag = 0; int temp =0; int n = sizeof(arr)/sizeof(int); // printf("%d\n",n); for (i原创 2012-12-04 18:01:12 · 674 阅读 · 0 评论 -
二叉树建立与遍历
本算法主要是利用扩展先序遍历法创建二叉树,再用先序遍历遍历二叉树,最后按照树形输出整个二叉树:关于本文中的扩展先序遍历法,可参照:http://baike.baidu.com/view/5050145.htm#include #include #include typedef int DataType;typedef struct Node { DataTy原创 2012-11-20 19:09:56 · 1035 阅读 · 0 评论 -
队列基本操作
下面是按严蔚敏书第65页上要求写的数据结构里的队列的一些基本操作,在codeblocks10.5中测试正确,C语言实现:#include #include #define MAXQSIZE 100#define OK 1#define ERROR 0typedef int Status;typedef int QElemType;typedef struct { QE原创 2012-11-02 17:04:36 · 564 阅读 · 0 评论 -
字符串匹配之首尾匹配算法
上一篇谈的是朴素算法(有的也称为BF算法),也就是最简单,最笨拙的方法,不过也是最容易写出的算法了,下面介绍的是一种在特定情况下稍好于朴素算法的方法,称之为首尾匹配算法。大致思路如下:主串:ghdwdgdwhduedfh子串:dwh主串索引:i;子串索引:j;从主串的第一个字符开始,用子串的首字符跟主串当前位置比较,如果一样,则比较子串最后一位对应的主串位置的字符,如果不一原创 2012-11-19 16:42:00 · 4898 阅读 · 1 评论 -
字符串匹配之朴素算法
此算法原型是按照严蔚敏书第79页上的求子串位置的定位函数来写的,其实百度能看到很多关于字符串朴素算法的例子,其中不乏用c和c++语言实现的。不过大多数都是不带参数的,即要不是将串定义为全局的,要不就是在子函数中定义。而严蔚敏的书中是作为参数,并且带起始搜索位置的参数。下面是代码:#include #include #include int Index(char *str_1,c原创 2012-11-18 17:59:49 · 2269 阅读 · 0 评论 -
字符串查找之字符次数
这篇文章想要说的是在一个字符串中查找第一次出现在字符串中并且指出现过一次的字符。我的做法就是先遍历整个字符数组,统计好每个字符出现的次数,然后再次遍历,提取出第一次出现的值为1次的那个字符即可。#include #include char find_first(char* str1){ if (NULL == str1) // string is null原创 2012-12-02 15:34:42 · 967 阅读 · 0 评论 -
qsort用法
在我们平时排序的时候,莫不过是sort,stable_sort……这些STL里面的排序模版了,在C中也有一个很厉害的排序算法:qsort,今天简单说说他的一个用法。#include #include #include using namespace std;inline int intcmp(const void* a,const void* b){ int aa原创 2012-10-31 19:56:55 · 1293 阅读 · 0 评论 -
栈模拟迷宫
#include #include #define STACK_SIZE 1#define STACK_ADD 1#define FLASE 0#define TRUE 1#define N 10#define M 10typedef int Elemtype;typedef int Status;int curstep = 1; //探索的步数int map[M][原创 2012-11-01 19:04:57 · 795 阅读 · 0 评论 -
汉诺塔(递归实现)
这是按照严蔚敏书55页上写的。#include #include int c = 0; //表示移动的次数void move(char x, int n, char z){ printf("Move disk %d from %c to %c\n", n, x, z);}void hanoi(int n,char x, char y, char z){a原创 2012-10-31 21:24:11 · 756 阅读 · 0 评论 -
数制转换(c语言)
以下是用链式栈模拟数制转换的例子,只是用来练一练手感。#include #include #define true 1#define false 0typedef struct Node{ int data; struct Node *pNext;}NODE, *PNODE;typedef struct Stack{ PNODE pTop;原创 2012-10-31 19:41:59 · 2267 阅读 · 0 评论 -
全排列
#include using namespace std;template void Perm(Type list[], int k, int m){ if (k == m) { for (int i=0; i<=m; ++i) { cout << list[i]; } cout <原创 2012-12-18 16:17:09 · 704 阅读 · 0 评论