摘要: //牛顿迭代法求平方根 1 double mysqrt(double num) 2 { 3 double x = num/2; 4 double y = 0; 5 do{ 6 x = x/2+num/(2*x); 7 y = x*x-num; ... 阅读全文
posted @ 2015-02-10 22:20 mjwk 阅读(1419) 评论(0) 推荐(0)
摘要: 从我知道有编程这回事以来,就把编程作为理想职业了,可惜我直到大二才知道有这回事。而且由于悟性不高见识不够,在成为一名真正意义上的程序员的路上走了不少弯路。直到我进入游戏行业之后,才找到了作为一个程序员的感觉。其实真正的程序员都是希望开发系统软件的,操作系统、编译器、数据库之类,不过游戏作为一种软... 阅读全文
posted @ 2015-01-31 13:50 mjwk 阅读(1323) 评论(0) 推荐(0)
摘要: 二叉树算法基本和递归有关,前中后序算法就不提了,主要看一下深度优先遍历和广度优先遍历。实现这2种遍历需要借助栈或者队列来保存中间结果,原因是遍历过程出现了回溯。 1 //笔试题:广度遍历二叉树、深度遍历二叉树 2 3 #include 4 #include 5 #include 6 7 usin... 阅读全文
posted @ 2014-03-09 19:48 mjwk 阅读(362) 评论(0) 推荐(0)
摘要: //一个简单的计算器代码,主要用来练习C++11新标准的编程技术和stl应用 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 9 10 type... 阅读全文
posted @ 2014-03-09 18:26 mjwk 阅读(1354) 评论(0) 推荐(0)
摘要: //这是一篇lua与C++交互的情景测试 #include #include #include #include #include #pragma region not_importent_static void stackDump (lua_State *L){ int i; int top = lua_gettop(L); for(i=1;ido_c 999--... 阅读全文
posted @ 2014-02-27 19:40 mjwk 阅读(970) 评论(0) 推荐(0)
摘要: 所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解。 chrono是一个time library, 源于boost,现在已经是C++标准。话说今年似乎又要出新标准了,好期待啊! 要使用chrono库,需要#include,其所有实现均在std::chrono namespace下。注意标准库里面的每个命名空间代表了一个独立的概念。所以下文中的概念均以命名空间的名字表示!chrono是一个模版库,使用简单,功能强大,只需要理解三个概念:duration、time_point、clock1.Durationsstd::chrono::dura... 阅读全文
posted @ 2014-02-21 19:45 mjwk 阅读(166068) 评论(2) 推荐(11)
摘要: 归并排序理论上时间复杂度只有O(NlogN),但是其中存在过多临时内存分配和copy操作而不适用于内存排序,却是外部排序的基本思路。下面是我的实现:#include<iostream>#include<algorithm>#include<cstring>using namespace std;int g=0;/*a,b is input array * c is output array * size of c is na+nb */template<typename T>void merge(T a[],int na,T b[],int nb 阅读全文
posted @ 2013-06-04 21:45 mjwk 阅读(491) 评论(0) 推荐(0)
摘要: 快排的思路就不说了,中轴分组,借助一个临时单元。时间复杂度O(NlogN)。下面是实现:#include<iostream>using namespace std;template<typename T>bool _less(T a,T b){ return a<b;}template<typename T,typename F>void qsort(T a[],int n,F f=_less<T>){ if(n<2)return; int first=0,last=n-1; T temp=a[0]; while(first!=las 阅读全文
posted @ 2013-06-04 12:24 mjwk 阅读(348) 评论(0) 推荐(0)
摘要: 基数排序是基于桶式排序的变种,主要是为了通过多次遍历减少空间占用(效果相当显著)。其思路可以描述为: 1.首先待排序的序列要满足基数排序的特征,比如有“基数”的概念(整数和字符串都可以套用) 2.建立一个大小为“基数”的数组,数组每个元素是一个队列(为了节省空间应该使用链表实现) 3.根据序列的“位数”决定遍历的次数m,每次遍历相当于按某一位做桶式排序。重复的元素push到队列中。 4.m次遍历后排序完成,最后一次遍历输出结果。因为数据不是按线性结构存储。 下面是我的实现,采用vector<list>结构: 1 #include<iostream> 2 #incl... 阅读全文
posted @ 2013-06-04 11:36 mjwk 阅读(310) 评论(0) 推荐(0)
摘要: 希尔排序的时间复杂度在O(N)到O(N2)之间,在大量数据排序中实践性能超过堆排序。特点是算法简单但是分析复杂。下面是实现:/* * a[] is an array to be sorted * n1 is the T array length * inc[] is the array to indecate the increasement * n2 is the inc array length */template<typename T>void shellsort(T a[],int n1,int inc[],int n2){ for(int i=0;i<n2;++i 阅读全文
posted @ 2013-06-04 11:02 mjwk 阅读(268) 评论(0) 推荐(0)