2012年12月13日
摘要:
Byte Endian是指字节在内存中的组织,所以也称它为Byte Ordering,或Byte Order。对于数据中跨越多个字节的对象, 我们必须为它建立这样的约定:(1) 它的地址是多少?(2) 它的字节在内存中是如何组织的?针 对第一个问题,有这样的解释: 对于跨越多个字节的对象,一般它所占的字节都是连续的,它的地址等于它所占字节最低地址。(链表可能是个例外,但链表的地址可看作链表头的地址)。比如: int x, 它的地址为0x100。 那么它占据了内存中的0x100, 0x101, 0x102, 0x103这四个字节(32位系统,所以int占用4个字节)。上面只是内存字节组织的一种情
阅读全文
posted @ 2012-12-13 13:56
kkmm
阅读(487)
推荐(0)
摘要:
从后往前初始化,例如下例中,首先初始化n1,因为用n2+2来初始化n1,所以n1的值为不确定;而n2稍后被初始化为0。#include <iostream>using namespace std;class A{private: int n1; int n2;public: A() : n2(0), n1(n2 + 2) {} void Print(){cout<<n1<<endl<<n2<<endl;}};int main(){ A a; a.Print(); return 0;}EOF
阅读全文
posted @ 2012-12-13 12:56
kkmm
阅读(583)
推荐(1)
2012年12月12日
摘要:
使用了《编程珠玑》上的解法。#include <iostream>#include <string>using namespace std;//注意这里使用了XOR进行交换的方法,这种方法不能让一个数和自身交换void Reverse(string &str, int left, int right){ for(; left < right; left++, right--){ str[left] = str[left] ^ str[right]; str[right] = str[left] ^ str[right]; str[left] = ...
阅读全文
posted @ 2012-12-12 12:41
kkmm
阅读(379)
推荐(0)
摘要:
此题有2种变形:1、不能修改node结构,要求时间O(N).2、可以修改node结构,要求时间O(logN).这里对第一种变形作答。思路就是先看右子树中是否有符合条件的node,再看自己是不是符合,再看左子树中是否有符合条件的node。用递归的方法实现。用一个counter来表示当前访问到了第几大的节点。#include <iostream>using namespace std;//BST Nodestruct Node{ int data; Node *lchild; Node *rchild; void Init(int d, Node *l, Node *r){...
阅读全文
posted @ 2012-12-12 09:53
kkmm
阅读(521)
推荐(0)
摘要:
#include <iostream>#include <string>using namespace std;//牛顿求平方根法double CustomizedSqrt(double n){ double r=1.0; while(abs(r*r-n)>1e-9) r=(r+n/r)/2; return r;}int main(){ int n = 200; double root = CustomizedSqrt(n); cout<<root<<endl; return 0;}EOF
阅读全文
posted @ 2012-12-12 07:18
kkmm
阅读(287)
推荐(0)
2012年12月11日
摘要:
stl提供了三个最基本的容器:vector,list,deque。vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即[]操作符,但由于它的内存空间是连续的,所以在中间进行插入和删除会造成内存块的拷贝,另外,当该数组后的内存空间不够时,需要重新申请一块足够大的内存并进行内存的拷贝。这些都大大影响了vector的效率。list就是数据结构中的双向链表(根据sgi stl源代码),因此它的内存空间可以是不连续的,通过指针来进行数据的访问,这个特点使得它的随即存取变的非常没有效率,因此它没有提供[]操作符的重载。但由于链表的特点,它可.
阅读全文
posted @ 2012-12-11 22:30
kkmm
阅读(254)
推荐(0)
摘要:
转自:http://blog.csdn.net/crystal_avast/article/details/7071590先说一下计算机中二进制的算法: 整数 整数的二进制算法大家应该很熟悉,就是不断的除以2取余数,然后将余数倒序排列。比如求9的二进制: 9/2=4 余 1 4/2=2 余 0 2/2=1 余 0 1/2=0 余 1 一直计算到商为0为止,然后将得到的余数由下到上排列,就得到了9的二进制:1001。 从上面的算法我们可以看到,用整数除以2,最终都能够到0。因此,整数是可以用二进制来精确表示的。小数 小数的二进制算法和整数的大致相反,就是不断的拿小数部分乘以2取积...
阅读全文
posted @ 2012-12-11 15:54
kkmm
阅读(1500)
推荐(0)
摘要:
#include <iostream>#include <string>using namespace std;//整数十进制转二进制string DecimalToBinary(int dec){ string returnStr = ""; while (dec) { if (dec & 0x01 == 1){ returnStr.insert(0, "1"); dec >>= 1; } else{ returnStr.insert(0, "0"); ...
阅读全文
posted @ 2012-12-11 15:39
kkmm
阅读(209)
推荐(0)
摘要:
问题定义: You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree-it does not have to start at the root.解题思路: 一层一层的遍历,保存当前节点到根节点的完整路径,然后从当前节点向上扫描,如果找到了当前节点到某个节点的和等于给定值,则输出之。程序对每个节点都需要遍历一遍
阅读全文
posted @ 2012-12-11 14:51
kkmm
阅读(252)
推荐(0)
2012年12月10日
摘要:
#include <iostream>#include <vector>#include <queue>using namespace std;struct LinkedListNode{ int value; LinkedListNode *pNext; LinkedListNode(int v) : value(v) {}};//创建链表LinkedListNode *CreateLinkedList(int arr[], int len){ LinkedListNode *pHead = new LinkedListNode(arr[0]); Link
阅读全文
posted @ 2012-12-10 16:35
kkmm
阅读(254)
推荐(0)