07 2013 档案

摘要:问题描述一堆数(例如6, 2, 2, 6, 3, 4, 6, 6, 6, 6),总共10个,其中”6“的个数超过总数的一半5,找出这个个数超过过半的那个数。思路从头到尾遍历,两个数相同接着往后遍历;否则删掉这两个数,接着往后遍历。因为所找的那个数过半,所以不同的数相抵,抵消掉最后还会至少剩下一个那个要找的数。图示代码#include using namespace std;typedef struct node{ int num; node *before; node *next;}node;int main(){ node *p_now, *p_next; no... 阅读全文
posted @ 2013-07-17 22:39 jihite 阅读(1409) 评论(0) 推荐(0) 编辑
摘要:出自c++ primer(4th)282页,26题题意数组ia[]={0,1,1,2,3,5,8,13,21,55,89};把ia复制到一个list容器中。使用单个迭代器参数版本的erase()函数将list容器中的奇数元素值删掉。代码#include #include using namespace std;int main(){ int ia[] = {0,1,1,2,3,5,8,13,21,55,89}; list ilist(ia, ia+11); list::iterator beg = ilist.begin(); for(;beg!=ilist.end()... 阅读全文
posted @ 2013-07-07 09:39 jihite 阅读(510) 评论(0) 推荐(0) 编辑
摘要:问题描述5, 5,-7, 5, 9, -1, 5, 1, 9, 4, 6 这堆数中两个数的和为10的组合有:5+5, 9+1, 4+6,如何快速的找出这样的组合?假定数组a[]存放元素,数组大小为len_a指定和为aim思路一先排序,low=0(最低位置),up=len_a(最高位置)当a[low]+a[up]>aim时,hig=high-1当a[low]+a[up] #include using namespace std; void printPairSums(int data[], int size, int sum); int main(int argc, char* a... 阅读全文
posted @ 2013-07-04 17:16 jihite 阅读(1374) 评论(0) 推荐(0) 编辑
摘要:在32位系统中,存储一位整型(int)数需要4个字节(4B),如果开辟一个空间,把其中的某个位1,就从原来的32b减少到1b,大大节省了空间。原理字符数组entry是存储位的数组,我们把数字N存到entry中,则把第N位置1:entry[nBits/8] = entry[nBits/8] | (1 << (nBits%8) )检验第N位是否为1:entry[nBits/8] & (1 << (nBits%8)图示函数void setBit(char entry[], int nBits){ entry[nBits/8] = entry[nBits/8] | (1 阅读全文
posted @ 2013-07-03 15:20 jihite 阅读(1387) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示