![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
面试
tiutiu2011
这个作者很懒,什么都没留下…
展开
-
《编程之美》 求二进制数中1的个数
#include //常规解法,求余运算比较耗时可优化int countV1(int num){ int result = 0; while (num){ if (num % 2) result++; num /= 2; } return result;}//用位运算优化int countV2(int num){ int result = 0; while原创 2015-10-19 11:19:24 · 307 阅读 · 0 评论 -
《编程珠玑》2.3 将一个n元一维向量左旋转i个位置
#include #define SIZE 100//将一个size元一维向量vector向左旋转bits个位置void rotateV1(char vector[], int size, int bits){ //消耗辅助空间 char stack[SIZE]; int top = 0; int i; for (i = 0; i < bits; i++) stack[to原创 2015-10-12 10:06:58 · 407 阅读 · 0 评论 -
求最大子数组和的6个版本
#include int maxSubArrayV1(int array[], int size){ int maxSum = 0; int start, end; for (start = 0; start < size; start++) for (end = start; end < size; end++){ int sum = 0; int i; for原创 2015-10-09 09:58:52 · 291 阅读 · 0 评论 -
8种排序
#includevoid printArray(int array[], int size){ int i; for (i = 0; i < size; i++) printf("%d ", array[i]); printf("\n");}void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp;}v原创 2015-09-23 14:07:42 · 322 阅读 · 0 评论 -
求最大公约数3种方法5个版本
/*求两个数的最大公约数(greatest common divisor)*/#include void swap(int *a, int *b){ int c = *a; *a = *b; *b = c;}//辗转相处法迭代写法int GCD_V1(int divident, int divisor){ int mod; while ( (mod = divident原创 2015-10-12 09:26:16 · 463 阅读 · 0 评论 -
01背包
/* 物品只有放进背包和不放进背包两种选择,枚举所有可能的组合,复杂度是O(2^numOfItems) */#include #define MAX_ITEMS 101#define MAX_WEIGHT 101int weight[MAX_ITEMS];int value[MAX_ITEMS];int weightLimit;int numOfItems;int result;原创 2015-10-13 11:16:07 · 250 阅读 · 0 评论 -
完全背包
/* 动态规划*/#include #define MAX_ITEMS 101#define MAX_WEIGHT 101int weight[MAX_ITEMS];int value[MAX_ITEMS];int weightLimit;int numOfItems;int result;int maxValue[MAX_ITEMS][MAX_WEIGHT];void c原创 2015-10-13 13:39:29 · 257 阅读 · 0 评论 -
二分搜索
int binarySearchValueIteratively(int array[], int size, int value){ int left = 0; int right = size - 1; //为了防止(left + right)溢出,所以先算(right - left),并用移位操作加速,注意">>"优先级很低 int mid = left + ( (right -原创 2015-09-23 10:01:59 · 313 阅读 · 0 评论