百度试题
文章平均质量分 53
zhangtonghui816
这个作者很懒,什么都没留下…
展开
-
微软面试题系列:求子数组最大和
题目: 输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)。 #include using namespace std; void main() { int a[]={1,-8,6,3,-1,5,7,-2,0,1}; int num=sizeof(a)/sizeof(i原创 2013-01-02 09:47:36 · 710 阅读 · 0 评论 -
对于输入(使用cin)安全性和鲁棒性的小结
今天做了微软的几道试题,发现几乎都要求输入,那么使用C++输入我想最容易的是cin了吧,还想用stringstream? 针对cin其实有很多可以研究的地方如,cin.clear(),cin.sync().你要输入一个整数,如 int temp;cin>>temp;cin.fail()或者cin.good()不一定反映出错误,比如输入1.1不报错,因为temp为整形。 #inc原创 2013-01-02 14:25:01 · 1140 阅读 · 0 评论 -
腾讯的笔试题(1)
1.考虑函数原型void hello(int a,int b=7,char* pszC="*"),下面的函数调用中,属于 不合法调用的是: A hello(5) B.hello(5,8) C.hello(6,"#") D.hello(0,0,"#") 答案是C,经过验证,其实就是考函数的重载。 2.求输出结果 #include class CBase{原创 2013-02-22 15:18:39 · 1051 阅读 · 0 评论 -
不用加减乘除实现a+b
#include using namespace std; char* add(int a,int b){ char *c; c = (char *) a; return &c[b]; } void main() { int result; result=(int)add(20000,300); cout<<result; getcha转载 2013-01-21 23:26:56 · 810 阅读 · 0 评论 -
估计没几个人能看懂我的KMP算法了,搞得有点复杂,有蛮多注释
//KMP算法实现 #include #include using namespace std; //我承认KMP算法我搞得逻辑太复杂了 void KMP(char* a,char* b){ int next[10]={1}; int i,j,count1,count2,flag=1; //计算模式串b的next[j] next[0]=0;//如果第一位出错 next[1原创 2013-01-20 21:27:11 · 493 阅读 · 0 评论 -
二叉排序树转换成双向链表
// 1:构造二叉查找树; // 2:中序遍历二叉查找树,因此结点按从小到大顺序访问,假设之前访问过的结点已经调整为一个双向链表,那么 // 只需要将当前结点连接至双向链表的最后一个结点即可,访问完后,双向链表也就调整完了 #include using namespace std; struct BSTreeNode { int m_nValue;转载 2013-01-19 11:37:52 · 988 阅读 · 0 评论 -
二叉排序树向双向链表的转换
//将二叉排序树转换成双向链表 #include using namespace std; //定义节点 struct node{ int num; node* Lnext; node* Rnext; }; class list{ public: list(); void change(node* p); void transform(); v原创 2013-01-18 21:52:02 · 974 阅读 · 0 评论 -
阿里巴巴易错题(我知道内存对齐,但还是倒下了)
#pragma pack(2) #include using namespace std; class A { int i; union U { char buff[13]; int i; }u; void foo() { } typedef char* (*f)(void*); enum{red, green, blue} color;原创 2013-01-16 22:28:58 · 753 阅读 · 0 评论 -
求两有序数组是否有相同元素
//判断两个有序数组中是否存在相同的元素 #include using namespace std; bool comparation(int *a,int *b){ int i=0,j=0; while(i<sizeof(a)/sizeof(int) || j<sizeof(b)/sizeof(int)){ if(a[i]==b[j]) retu原创 2013-01-15 19:37:42 · 1389 阅读 · 0 评论 -
微软试题:查找数组中最小的k个元素
题目:输入n个整数,输出其中最小的k个。例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。 思路:一看到这道题思路就想直接从小到大排序,那么最小的k个为前k个元素,不过这样做复杂度为o(n^2).目测解决这题的复杂度规定在o(n),所以不采纳。那么用一个数组a[k]记录所存的k个最小值需要额外的空间复杂度,那么如果允许破坏原先的数组,那么就可以在原先的数组原创 2013-01-02 10:34:20 · 542 阅读 · 0 评论 -
(++a)+=(a++)和(++a)=(++a)+(a++)的区别
#include using namespace std; void main() { int a=4; cout<<((++a)+=(a++))<<endl; //结果为10 cout<<a<<endl; //结果为11 system("pause"); }#include using namespace std; void main() {原创 2013-03-01 21:18:46 · 15162 阅读 · 3 评论