摘要: View Code 1 //Solution1:对1到n的每个数进行拆分 2 int Solution(int n) 3 { 4 int count=0; 5 for(int i=1;i<=n;++i) 6 { 7 j=i; 8 while(j) 9 {10 if(j%10==1)11 count++;12 j=j/10;13 }14 }15 return count++;16 }17 18 //考虑n每一位c... 阅读全文
posted @ 2012-03-26 14:29 Cavia 阅读(204) 评论(0) 推荐(0) 编辑
摘要: View Code 1 //有n个数表示ID,其中有一个ID出现的次数超过总数n的一半,求出这是哪个ID 2 3 //Solution1:对n个数进行排序,然后再扫描一遍排好序的ID列表,统计各个ID出现的次数,如果某个ID出现的次数超过总数n的一半,那么就输出这个ID 4 5 //Solution2:对n个数进行排序,则这个有序列表中的第n/2项一定会是要找的ID 6 7 //Solution3:每次删除两个不同的ID,那么在剩下的ID列表中,要找的ID出现的次数仍然超过总数的一半 8 Type Find(Type* ID,int n) 9 {10 int i,count;1... 阅读全文
posted @ 2012-03-26 13:58 Cavia 阅读(164) 评论(0) 推荐(0) 编辑
摘要: View Code 1 //n!进行质因数分解,n!=2^x+3^y+5^z...所以末尾0的个数为min(x,z),又因为x>z,所以只要求出z的值即可 2 int Solution(int n) 3 { 4 int count=0; 5 for(int i=1;i<=n;++i) 6 { 7 int j=i; 8 while(j%5==0) 9 {10 count++;11 j=j/5;12 }13 return count;14 ... 阅读全文
posted @ 2012-03-26 13:45 Cavia 阅读(204) 评论(0) 推荐(0) 编辑
摘要: View Code 1 //普通解法 2 int Solution(int v) 3 { 4 int count=0; 5 while(v) 6 { 7 if(v%2==1) 8 count++; 9 v=v/2;10 }11 return count;12 }13 14 //向右移位+与操作15 int Solution(int v)16 {17 int count=0;18 while(v)19 {20 if(v&0x1)21 c... 阅读全文
posted @ 2012-03-26 11:16 Cavia 阅读(213) 评论(0) 推荐(0) 编辑