自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

花开无言

不积跬步,无以至千里;不积小流,无以成江海

  • 博客(29)
  • 收藏
  • 关注

原创 计算0到n中数字2出现的次数

#include #include using namespace std;int countAtSomeDigit(int num, int d) //0到num之间,第d位2的个数{ int powerOf10 = (int)pow(10.0, d); int nextPower = powerOf10 * 10; int right = num % powerOf10; i

2015-05-31 17:28:30 2132

原创 从大小为n的数组中随机选出m个整数,要求被选中的概率相同

int* selectMElements(int* data, int n, int m){ int* res = new int[m]; for (int i = 0; i < m; i++) { res[i] = data[i]; } for (int i = m; i < n; i++) { int k = random(0, i); if (k < m) {

2015-05-31 16:46:24 705

原创 给定一个随机数发生器,完美洗牌

int random(int low, int high){ return low + rand() % RAND_MAX * (high - low + 1);}void shuffleCards(int* cards){ for (int i = 0; i < 52; i++) { int k = random(0, i); int t = cards[k]; ca

2015-05-31 16:39:32 432

原创 实现一个智能指针类

template class SmartPointer{public: SmartPointer(T* ptr) { ref = ptr; ref_count = (unsigned*)malloc(sizeof(unsigned)); *ref_count = 1; } SmartPointer(SmartPointer& sptr) { ref = sptr.r

2015-05-31 16:21:38 576

原创 整数数组从1到32000,4KB内存,输出重复的数字

#include using namespace std;//位向量法解决class BitSet{private: int* bitset;public: BitSet(int size) { bitset = new int[size >> 5]; // (>> 5) == (/ 32) } bool get(int pos) { return (bitset

2015-05-31 15:41:14 678

原创 一个文件含有40亿个非负整数,使用10MB内存,找到一个不在该文件中的整数

#include #include using namespace std;/*使用位向量法解决,分成两次扫描文件,按照内存大小把数据分成若干块,第一次扫描找到哪一块包含缺少的数据,第二次扫描找到那一块的位置*/void findNumber(){ int bitSize = 1024 * 1024; //每块存放2的20次方bit数据 int blockNum = 4096

2015-05-31 14:53:53 822 1

原创 一个文件含有40亿个非负整数,使用1GB内存,找到一个不在该文件中的整数

#include #include using namespace std;/*使用位向量法解决*/void findNumber(){ long long numberOfInt = (long long)INT_MAX + 1; int len = (int)(numberOfInt / 32); int* array = new int[len]; ifstream

2015-05-31 13:57:12 980 1

原创 回溯法应用:1,2,5,10四个数任意次数相加得到一个数N

#include #include using namespace std; vector vec; const int a[4] = {1, 2, 5, 10}; void backup(int N) { if (N == 0) {

2015-05-27 12:36:30 1335

原创 连个实习都找不到,我该怎么办

在知乎上发了个帖子,也没多少人看,大概是我没有邀请那些大V回答吧。现在搬过来,留作纪念,程序员找不到实习怎么办?本人研二,计算机专业,在一家小公司做过一点很水的C++项目,windows平台,GUI使用的是qt,项目内容涉及到多线程和网络编程。平时也自学一点java,看过think in java,以前学过web方面的一点内容,但是没有使用过框架,仅仅是会servlet,j

2015-05-27 11:05:12 8315 6

原创 集合的子集

#include #include using namespace std;vector > getSubset(vector& set){ vector > res; res.clear(); int max = 1 << set.size(); for (int i = 0; i < max; i++) { vector sub

2015-05-15 15:25:40 589

原创 八皇后问题

#include using namespace std;int total = 0;const int n = 8;int col[n];void search(int cur){ if (cur == n) { total++; } else { for (int i = 0; i < n; i++)

2015-05-15 12:46:33 347

原创 生成1-n的全排列

#include using namespace std;void print_permutation(int n, int* arr, int cur){ if (cur == n) { for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }

2015-05-15 12:24:37 621

转载 01背包和完全背包

1、问题描述已知:有一个容量为V的背包和N件物品,第i件物品的重量是weight[i],收益是cost[i]。条件:每种物品都有无限件,能放多少就放多少。问题:在不超过背包容量的情况下,最多能获得多少价值或收益举例:物品个数N = 3,背包容量为V = 5,则背包可以装下的最大价值为40.---------------------------------------------

2015-05-12 10:01:53 381

转载 快速排序

#include #include #include #include using namespace std; const int N = 10; int arr[N]; /******************快速排序稳定版本******************************

2015-05-11 21:02:05 235

转载 求所有LCS

//求取所有的最长公共子序列 #include using namespace std; const int X = 100, Y = 100; //串的最大长度 char result[X+1]; //用于保存结果 int count = 0;

2015-05-11 16:36:34 416

转载 LCS

#include using namespace std;void LCS_Print(const char *X, const char *Y, int **R, int row, int col) { if(R[row][col] == 1) { LCS_Print(X, Y, R, row-1, col-1); c

2015-05-11 16:06:56 322

原创 二分查找的两种正确实现

#include using namespace std;int binarySearch1(int a[], int n, int x) //[l, u)区间{ int l, u, m; l = 0; //左区间闭合 u = n; //右区间开放 while(l < u) //用<做判断,不用<= { m =

2015-05-11 10:27:28 742

原创 BFS求迷宫的最短路径

#include #includeusing namespace std;const int INF = 10000000;typedef pair P;//保存状态,即点的位置//迷宫,'#'表示墙壁, '.'表示通道,S表示起点,G表示终点char maze[10][10] = { '#','S','#','#','#','#','#','#','.','#',

2015-05-08 15:45:26 758

原创 有一个X*Y的网格,只能向右、向下移动,从(0, 0)走到(X-1, Y-1),中间某些位置有障碍物,打印一条路径(优化)

#include #include #includeusing namespace std;int maze[4][4] = {1,1,1,0,1,1,0,1,1,1,1,1,0,0,1,1};//0表示障碍,1表示可以通过struct Point{ int x; int y; bool operator < (const Point& p) cons

2015-05-08 13:37:26 3208

原创 有一个X*Y的网格,只能向右、向下移动,从(0, 0)走到(X-1, Y-1),中间某些位置有障碍物,打印一条路径(

#include #include using namespace std;int maze[4][4] = {1,1,1,0,1,1,0,1,1,1,1,1,0,0,1,1};//0表示障碍,1表示可以通过struct Point{ int x; int y;};bool getPath(int x, int y, list& path){ Poin

2015-05-08 10:47:36 2101

原创 有一个X*Y的网格,只能向右、向下移动,从(0, 0)走到(X-1, Y-1),中间某些位置有障碍物,打印所有可能的路径

#include #include using namespace std;int maze[4][4] = {1,1,1,0,1,1,0,1,1,1,1,1,0,0,1,1};//0表示障碍,1表示可以通过struct Point{ int x; int y;};bool getPath(int x, int y, list& path){ Poin

2015-05-08 10:37:03 1408

原创 求出第k个素因子只有3,5,7的数字

#include #include using namespace std;int min(int a, int b){ return a < b ? a : b;}int getKthNumberBy357(int k){ if (k < 0) { return 0; } int v = 0; queue q3,

2015-05-07 17:53:32 4840

原创 DP求菲波那切数列

#include using namespace std;//DP的思想不过是缓存中间结果,消除递归带来的重复计算,以空间换时间罢了int fib[1000] = {0};//初始化为0,当然,不同场景下dp初始化不同int fibonacci(int i){ if (i <= 1)//边界 { return i; } if (fib[i

2015-05-07 13:44:24 526

原创 素数筛法,找到0到max之间的所有素数

#include using namespace std;void primeFilter(int max){ bool* flags = new bool[max + 1]; flags[0] = flags[1] = false; for (int i = 2; i <= max; i++) { flags[i] = true;

2015-05-07 12:53:50 413

原创 位操作基本知识

//判断一个数字的某一位是不是1bool getBit(int num, int i){ return num & (1 << i);}//把一个数字的某一位置1int setBit(int num, int i){ return num | (1 << i);}//把一个数字某一位置0int clearBit(int num, int i){ in

2015-05-07 10:55:10 373

原创 交换一个整数的相邻奇数位与偶数位(即第0位与第1位交换,第2位与第3位交换...)

#include using namespace std;int swapOddAndEvenBits(int x){ return ( ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));//用不同的掩码分别提取奇数位与偶数位,奇数位右移1位变成偶数位,偶数位左移1位变成奇数位,然后按位或}int main(){ cout

2015-05-06 22:52:42 565

原创 确定需要改变几个位,才能把整数a变成整数b

#include using namespace std;/*算法思想:先把两个数字异或,异或的结果中有几个1就表示有这些位不同,即,改变这些位(1变成0,0变成1)就能使他们相同,此外,使用(c & (c - 1))可以使c的最低有效位(1的位)变成0*/int bitNeedChanged(int a, int b){ int count = 0; for (int c

2015-05-06 22:13:28 630

原创 给定一个正整数,找出一个数:与其二进制表示中1的个数相同,比该数小,而且最接近

#include using namespace std;/*input: 正整数n(32位)return:比n小,最接近n,而且二进制中1的个数与n相同(当然,0的个数也就相同了) 算法思想:从右往左找到第一个1,他的右边是0,记录下他右边1和0的个数,分别为x和y,对于返回的结果,这个1左边的位肯定不变,不用考虑,把这个1变成0,这样数字变小,同时0的个数+1,1的个数-1,为

2015-05-06 21:59:51 1673

原创 给定一个正整数,找出一个数:与其二进制表示中1的个数相同,比该数大,而且最接近

#include using namespace std;int getNext(int n){ int t = n; int c0 = 0; int c1 = 0; while ((t & 1) == 0 && (t != 0)) { c0++; t >>= 1; } while ((t & 1) == 1) { c1++; t >>= 1; } if

2015-05-06 21:35:11 1542

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除