上一页 1 ··· 3 4 5 6 7 8 9 10 11 下一页
摘要: input:m,n and m<noutput:a sorted list of m random integers in the range 0...n-1View Code 1 void genknuth(int m,int n) 2 { 3 for(int i=0;i<n;++i) 4 { 5 if((bigrand()%(n-i))<m) 6 { 7 cout <<i<<endl; 8 --m; 9 }10 }11 }12 13 14 void gensets(in... 阅读全文
posted @ 2012-05-03 10:43 Cavia 阅读(226) 评论(0) 推荐(0) 编辑
摘要: 1.Save state to avoid recomputation2.Preprocess information into data structures3.Divide-and-conquer algorithms4.Scanning algorithms5.Cumulatives 阅读全文
posted @ 2012-04-27 09:58 Cavia 阅读(121) 评论(0) 推荐(0) 编辑
摘要: View Code 1 //日期处理 2 3 //每个月的天数 4 int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 5 6 char* week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; 7 8 typedef struct Date 9 {10 int year;11 int month; 阅读全文
posted @ 2012-03-31 10:29 Cavia 阅读(273) 评论(0) 推荐(0) 编辑
摘要: View Code 1 //辗转相除法 2 //设k=x/y,b=x%y,则x=ky+b 3 //求x和y的最大公约数,也就是求y和b的最大公约数 4 int gcd(int x,int y) 5 { 6 while(y) 7 { 8 int b=x%y; 9 x=y;10 y=b;11 }12 return x;13 }View Code 1 int gcd(int x,int y) 2 { 3 while(x!=y) 4 { 5 if(x>y) 6 x... 阅读全文
posted @ 2012-03-28 16:35 Cavia 阅读(718) 评论(0) 推荐(0) 编辑
摘要: Rotate a one-dimensional vector of n elements left by i positions.Solution1 1 void Solution(char* arr,int n,int rotdist) 2 { 3 int i; 4 //循环n和rotdist的最大公约数次 5 for(i=0;i<gcd(n,rotdist);++i) 6 { 7 char t=arr[i]; 8 int index=i; 9 while((index+rotdist)%n!=i)10 ... 阅读全文
posted @ 2012-03-27 14:32 Cavia 阅读(231) 评论(0) 推荐(0) 编辑
摘要: 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) 编辑
摘要: View Code 1 //---------------------API------------------------------ 2 3 4 //downloads a block from Internet sequentially in each call 5 //return true, if the entire file is downloaded, otherwise false. 6 bool GetBlockFromNet(Block* out_block); 7 8 //writes a block to hard disk 9 bool W... 阅读全文
posted @ 2012-03-23 15:36 Cavia 阅读(718) 评论(0) 推荐(0) 编辑
上一页 1 ··· 3 4 5 6 7 8 9 10 11 下一页