算法
文章平均质量分 66
hodd1988
linux先飞
展开
-
如何将一个整数快速向100取整
将2013取整为20003211取整为3200将32取整为0412取整为400987654321取整为987654300原创 2013-02-01 10:19:32 · 2137 阅读 · 1 评论 -
数组中数字的所有组合情况
#include #include #include using namespace std;// 列出数字的所有组合情况// 使用树的层次遍历void Combination(int arr[], int len){ if(arr == NULL || len <= 0) { return; } queue > q; // 第一层节点入队列 for(int i =原创 2015-03-05 13:34:50 · 1262 阅读 · 0 评论 -
八皇后 n皇后 问题
#include #include #include using namespace std;void show(vector& pos){ for(int i = 0; i < pos.size(); ++i) { cout << pos[i] << '\t'; } cout << endl;}bool isValid(vector& pos, int end){原创 2015-03-19 10:44:03 · 443 阅读 · 0 评论 -
循环递增数组中查找是否存在某个数
#include #include using namespace std;// 在循环递增数组中查看是否存在某个数// 返回-1表示数组中没有该元素int search(int arr[], int len, int value){ if(arr == NULL || len <= 0) { return -1; } int start = 0; int end = l原创 2015-03-03 11:39:01 · 1208 阅读 · 0 评论 -
链表插入删除操作
#include using namespace std;// 定义单向链表节点struct ListNode{ int data; ListNode* next;}; // end of ListNode// 将新节点插入链表头void insertList(ListNode** head, int insertData){ if(head == NULL) { re原创 2015-01-05 22:29:57 · 2012 阅读 · 0 评论 -
字符串原地压缩
#include #include #include using namespace std;// 字符串翻转void reverse(char* start, char* end){ while(start < end) { swap(*start++, *end--); }}// 将数字转换成字符串存储到numStr中// 返回被转换的数字串长度int setNu原创 2015-01-22 22:39:37 · 695 阅读 · 0 评论 -
递归,非递归生成数组的所有排列
#include #include #include using namespace std;void showArr(int* arr, int len){ for(int i = 0; i < len; ++i) { cout << arr[i] << '\t'; } cout << endl;}// 递归显示组合void recursiveComb(int arr原创 2015-01-22 18:36:07 · 613 阅读 · 0 评论 -
求出sum=1+11+111+1111+ .... + 111...111(1024个1),sum有多少个1
先列一个竖式: 1 11 111 1111 . . 1111111...11111(1023个1)11111111...11111(1024个原创 2015-01-02 11:43:37 · 4708 阅读 · 0 评论 -
解一道递归调用题
int ack(int m, int n){ if(m == 0) return n + 1; else if(n == 0) return ack(m - 1, 1); else return ack(m - 1, ack(m, n -1 ));} ack(3,3)的输出是什么?假设我们将ack(m,n)填充到一个二维矩阵的第m行第n列,从函数中可以看出第m行第n列原创 2015-01-01 23:54:38 · 618 阅读 · 0 评论 -
获取字符串中的数字串
#include #include #include #include using namespace std;// 获取字符串中的数字串void pickNum(const char* str, vector& numVec){ // 验证参数有效性 if(str == NULL || strlen(str) == 0) { cerr << "invalid parame原创 2015-01-01 16:53:27 · 651 阅读 · 0 评论 -
输入两个整数 n 和 m,从数列 1,2,3.......n 中随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
#include #include using namespace std;// 显示组合void showCombination(const vector& s){ for(int i = 0; i < s.size(); ++i) { cout << s[i] << ' '; } cout << endl;}// 1到n随机取出几个数使其和等于mvoid findC原创 2015-01-07 20:54:12 · 1267 阅读 · 0 评论 -
基本的排序算法
#include #include #include using namespace std;// 插入排序void insertSort(int arr[], int len){ if(arr == NULL || len <= 1) { return; } for(int i = 1; i < len; ++i) { int temp = arr[i]; in原创 2015-01-06 22:26:28 · 585 阅读 · 0 评论 -
二叉树建立,递归,非递归,前序,中序,后序遍历
#include #include #include #include using namespace std;struct TreeNode{ int data; TreeNode* left; TreeNode* right;}; // end of TreeNode// 前序遍历建立二叉树,0表示空节点void createTree(TreeNode** root,原创 2015-01-06 12:33:24 · 601 阅读 · 0 评论 -
我理解之KMP
KMP:字符串的模式匹配算法假设有字符串: 0 1 2 3 4 5 6 7 8A: x y x y x y y x xB: x y x y y欲判断B是否是A的一个子串。从位置0开始比较,到位置4的时候y不等于x所以根据KMP算法要向右滑动B才能与A进行重新匹配。传统的方式是将B的第一个字符与A的原创 2012-03-17 22:25:45 · 434 阅读 · 0 评论 -
挖雷游戏的概率
编程之美中有道挖雷游戏的概率: | |A | || |1 | || |B | || |2 | || |C | |40个地雷没被发现,求P(A),P(B),P(C)标红地带为上下部分的公共地带,分2种情况讨论:1.假设下半部分有一个雷在公共地带,下半部分有5种布局,公共地带有3种布局,公共地带以上就没有雷了,所以总共有3×5原创 2012-04-24 00:59:28 · 1212 阅读 · 2 评论 -
编程之美 2.7 最大公约数 扩展问题之逆转一个整数的二进制表示
int reverseBinaryStr(int x){int num = 0;int i = 0;for(i = 0; i {num = (num > i) & 1);}return num;}原创 2012-05-02 16:30:58 · 544 阅读 · 0 评论 -
输入一个字符串,如何求最大重复出现的字符串
输入一个字符串,如何求最大重复出现的字符串呢?比如输入 ttabcftrgabcd,输出结果为 abc,canffcancd,输出结果为 can。#include #include using namespace std;// 输出字符串中的最长重复子串,字串位置不重叠// 比如输入ttabcftrgabcd输出abc// 再比如输入canffcancd输出canvoid co原创 2015-03-06 17:07:19 · 1172 阅读 · 0 评论