0x03.查找算法之折半查找-2016 年 3 月 18 日 16:46:13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
int BinarySearch(int a[], int len, int key) { int low = 0, high = len - 1; if (a[low]==key) { return low; } if (a[high] == key) { return high; }
while (low <= high) {
int mid = (low + ((high - low) / 2)); if (a[mid] == key) { return mid; } else if (key > a[mid]) { low = mid + 1; } else { high = mid - 1; } } return -1;
}
|
0x02.获取数组长度
1 2 3 4 5 6
| #define GET_ARRAY_LEN(array,len){len = (sizeof(array) / sizeof(array[0]));}
int len; int a[] = {1, 5, 66}; GET_ARRAY_LEN(a, len);
|
0x01.LINUX 下编译运行指令
1 2 3 4
| gcc -o hello hello.c //gcc c; hello.c是源文件; -o hello 代表生成hello 如果不用-o会默认生成a.out g++ -o hello hello.c //g++ c++;
./hello //运行hello
|
0x00.”.”和”->”区别
->是指针指向其成员的运算符
.是结构体的成员运算符 如:
1 2 3 4 5 6 7 8 9 10 11
| struct A { int a; int b; };
A *point = malloc(sizeof(struct A)); point->a = 1;
A object; object.a = 1;
|