随笔分类 -  数论

摘要:http://www.cnblogs.com/pkuoliver/archive/2010/10/27/Convert-m-number-to-n-number.html从这道题中可以看出,数论中存在很大一部分是通过除以基数和取模来操作来得到单个的数,时间相加问题也是如此。 阅读全文
posted @ 2013-09-10 22:29 l851654152 阅读(194) 评论(0) 推荐(0)
摘要:int gcd(int x,int y){ return (!y)?x:gcd(y,x%y);} 阅读全文
posted @ 2013-09-03 23:50 l851654152 阅读(309) 评论(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 阅读(215) 评论(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 阅读(198) 评论(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 阅读(121) 评论(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 阅读(119) 评论(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 阅读(259) 评论(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 阅读(132) 评论(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 阅读(146) 评论(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 阅读(264) 评论(0) 推荐(0)
摘要:http://www.codecho.com/reading-large-file-using-cpp/ 阅读全文
posted @ 2013-07-09 16:38 l851654152 阅读(160) 评论(0) 推荐(0)
摘要:scanf()函数的原理想象输入设备(键盘)连接着一个叫“缓冲”的东西,把缓冲认为是一个字符数组。当你的程序执行到scanf时,会从你的缓冲区读东西,如果缓冲区是空的,就阻塞住,等待你从键盘输入。现在假设你的缓冲区里有:abcd\n1234\n (其中\n是回车符)执行:scanf("%s",name);的时候,由于scanf是读数据直到看见空白符(空白符:指空格符、制表符、回车符)就停止的输入函数。所以执行后,把abcd存到了name中。缓冲区于是变成了 : \n1234\n接下来的执行就有问题了,如果遇到了:scanf("%d",&numbe 阅读全文
posted @ 2013-07-09 16:01 l851654152 阅读(566) 评论(0) 推荐(0)