- 博客(7)
- 资源 (2)
- 收藏
- 关注
原创 编程之美上的一些题目
二进制中1的个数 解法:自己与比其小1的数按位与,即可得到1的个数 //计算一个数的二进制中1的个数 #include using namespace std; //求1的个数 int solve(int x) { int cnt = 0; while(x){ cnt++;
2012-10-17 20:31:49
360
原创 c++继承下的内存布局
单一继承: 使用如下的例子: class A { public: virtual void f(){} virtual void h(){} int a; }; class B:public A { public: virtual void f(){} virtual void g(){}
2012-04-08 10:34:42
506
原创 各种排序的实现
#include using namespace std; //堆排 //维护堆 void maxheapify(int *a, int cur, int low, int high) { int large = 2 * low + 1; while(large < high) { if(large < high && a[larg
2012-03-16 13:26:38
281
转载 一些面试题
一个数组是由一个递减数列左移若干位形成的,比如{4,3,2,1,6,5}是由{6,5,4,3,2,1}左移两位形成的,在这种数组中查找某一个数。 解法:二分查找,只需要将下标移位就可以了。 #include using namespace std; int A[] = {4,3,2,1,6,5}; //左为正,右为负 int offset; int n; //偏移寻址 inline
2012-03-09 20:21:13
295
原创 i++,++i,i=i+1 分析
首先三者都是等价的操作,三者的效率也都是等价的,这是vs2010的反汇编,三者都被转成相同的汇编代码: 01041375 mov eax,dword ptr [i] 01041378 add eax,1 0104137B mov dword ptr [i],eax
2012-03-09 19:45:04
681
原创 求一个数组的最长递减子序列
比如{9,4,3,2,5,4,3,2}的最长递减子序列为{9,5,4,3,2} 解法:动态规划,时间复杂度O(n^2) 状态方程,设r[i]表示以i结束的最长子序列的元素个数,则 r[i] = max{ s[j] , r[j] + 1} , 条件是a[i] #include using namespace std; int a[]={9,4,3,2,5,4}; //re
2012-03-09 19:33:51
676
原创 链表的基本操作
链表排序 二路归并(暂未实现) 基本思想:维护一个队列,从头便利链表,找到一段按降序排列的链表段,一段一段的分开,然后将它们按顺序入队,每次从对头取出两个进行归并,归并后的结果入队。 #include using namespace std; struct node{ int d; node *next; node(int x){d = x;next
2012-03-09 19:07:45
275
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人