摘要: bool isEven(int n){ return (n&1) == 0;}int gcd(int *a,int length){ int *pbegin = a; int *pend = a + length -1; while(pbegin < pend) { if (!isEven(*pbegin)) { pbegin++; continue; } if (isEven(*pend)) { pend--; continue; } int temp = *pbegin; *pbegin = *pend; *pend = temp; } return 0;... 阅读全文
posted @ 2013-09-03 23:56 l851654152 阅读(431) 评论(0) 推荐(0) 编辑
摘要: int gcd(int x,int y){ return (!y)?x:gcd(y,x%y);} 阅读全文
posted @ 2013-09-03 23:50 l851654152 阅读(303) 评论(0) 推荐(0) 编辑
摘要: 给定整数,判断是否是2的方幂。。我们来做分析:2->10 & 1->01 == 0 4->100 & 3->011 == 0 8->1000 & 7->0111 == 0 16->10000 & 15->01111 == 0bool change(int n){ return((n&n-1) == 0);} 阅读全文
posted @ 2013-09-03 23:41 l851654152 阅读(205) 评论(0) 推荐(0) 编辑
摘要: 1. A & B,得到的结果C中的1的位表明了A和B中相同的位都是1的位;2. A | B, 得到的结果D中的1的位表明了A和B在该位至少有一个为1的位,包含了A 与 B 都是1的位数,经过前两步的位运算,,C 中1的位表明了A 和 B在该位都是1,D中为0的位表明了A 和 B 在该位都是0 ,所以进行第三步。3. C ^ D,E 中为1的位表明了A 和 B不同的位。#includeusing namespace std;int getNum(int n){ if(n==0) return 0; int count=0; while(n) { n&=(n-... 阅读全文
posted @ 2013-09-03 23:36 l851654152 阅读(193) 评论(0) 推荐(0) 编辑
摘要: void change(int a,int b){ cout << "a:" << a << " b:" <<b <<endl; a = a^b; b = a^b; a = a^b; cout << "after change a:" << a << " after change b:" <<b << endl; } 阅读全文
posted @ 2013-09-03 23:31 l851654152 阅读(114) 评论(0) 推荐(0) 编辑
摘要: 原理:因为10由2*5组成,而构成2的因数比5多 所以最终转换成求5的个数int getNumber(int n){ int count = 0; while(n) { n = n/5; count = count +n; } return count;} 阅读全文
posted @ 2013-09-03 23:26 l851654152 阅读(109) 评论(0) 推荐(0) 编辑
摘要: 示例1,2...9,10,11中有四个1int getNumber(int n){ int count = 0; int factor = 1; int low = 0; int cur = 0; int high = 0; while (n /factor != 0) { low = n - (n / factor) * factor; cur = (n / factor) % 10; high = n / (factor * 10); switch(cur) { case 0: count += high * factor; break; case 1: cou... 阅读全文
posted @ 2013-09-03 23:19 l851654152 阅读(247) 评论(0) 推荐(0) 编辑
摘要: int main(){ int n; cin >> n; int num = 0; while(n) { n &= (n-1); num++; } cout << num <<endl; return 0;} 阅读全文
posted @ 2013-09-03 22:35 l851654152 阅读(124) 评论(0) 推荐(0) 编辑
摘要: int main(){ int n; cin >> n; for (int i = 2;i < n;i++) { while(n!=i) { if (n % i ==0) { cout << i << "*"; n = n/i; } else { break; } } } cout << n <<endl; return 0;} 阅读全文
posted @ 2013-09-03 22:12 l851654152 阅读(139) 评论(0) 推荐(0) 编辑
摘要: 这个是在博客园的博问里面看到的1、现有1000个苹果,10个盒子,现在要你将1000个苹果装入10个盒子中,使得用户无论购买多少个苹果(1-1000),都能由若干个盒子拼装而成(卖的时候是整个盒子卖,不能拆盒子的包装)解法:考虑1, 2, 4, 8这四个数,由这四个数可以组成1-15之间任意一个数,也即1,2, 4 ... 2n 可以组成1-2(n+1)-1之间所有的数。所以这十个盒子分别放入1, 2, 4, 8, 16, 32, 64, 128, 256, 489个苹果,即可组成1-1000内所有的数。2. 有1000瓶液体,无色无味,外表完全一样,其中一瓶是毒药,有10条警犬,警犬喝过毒药 阅读全文
posted @ 2013-09-03 21:52 l851654152 阅读(248) 评论(0) 推荐(0) 编辑