随笔分类 -  c/c++

上一页 1 2

一些经典的问题,算法等。
快速求幂算法
摘要:1 #include <stdio.h> 2 #include <math.h> 3 //递归算法 4 int recursion(int a,int b) 5 { 6 int tem = 1; 7 if(b==0)return 1; 8 else if(b==1)return a; 9 tem = recursion(a,b>>1);10 tem = tem*tem;11 if(b&1) tem = tem * a;12 return tem;13 }14 //循环算法15 int loop(int a,int b)16 {17 ... 阅读全文

posted @ 2012-04-25 22:06 NewPanderKing 阅读(2315) 评论(0) 推荐(0) 编辑

判断一个数字是否为素数的基于C语言的算法
摘要:1 #include <stdio.h> 2 3 bool If_prime(int a,int b) 4 { 5 int c; 6 while(b>0) 7 { 8 c = a%b; 9 a = b;10 b = c;11 }12 if(a==1)return true;13 else return false;14 }15 int main()16 {17 int num,i;18 while(true)19 {20 printf("input the... 阅读全文

posted @ 2012-04-25 18:27 NewPanderKing 阅读(649) 评论(0) 推荐(0) 编辑

高次幂求模
摘要:主要的思想是来自一个公式:a*b%c = (a%c) *(b%c) %c基本概念及思想对形如a^b mod m 的运算(b一般较大)但a,b,m都在long型范围内算法的主要思想是分治,分而治之。将大的问题分成若干个相似的较小的问题!具体实现是用递归的方法!举例2^100mod 3像这种运算如果先算出2^100 的值,然后再模上3,相信比较困难!我们可以将100变小点2^100=(2^50)^2 =((2^25)^2)^2=((((2^1)^2)^2)…)^2若我们已经得出250 mod 3的值,我们便很简单地得出2^100mod 3的值。按照上述的方法继续分下去…最终肯定会得到2^1mod 阅读全文

posted @ 2012-04-24 18:36 NewPanderKing 阅读(625) 评论(0) 推荐(0) 编辑

经典排序之插入排序
摘要:1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 int main() 7 { 8 int i,j,tem; 9 int length;10 int a[]={2,43,24,19,25,39,11,6,5};11 // length = a.length();12 length = sizeof(a)/sizeof(int);13 cout<<length<<endl;14 printf(" 阅读全文

posted @ 2012-03-25 12:37 NewPanderKing 阅读(209) 评论(0) 推荐(0) 编辑

经典排序之快排--数组模拟快排
摘要:1 #include <stdio.h> 2 3 int partition(int *a,int low ,int high) 4 { 5 int privotpos; 6 int tem = a[low]; 7 privotpos = a[low]; 8 while(low<high) 9 {10 while(low<high && a[high]>=privotpos)high--;11 a[low] = a[high];12 while(low<high && a[low]<=privotpos... 阅读全文

posted @ 2012-03-25 12:33 NewPanderKing 阅读(723) 评论(0) 推荐(0) 编辑

经典排序之选择排序
摘要:#include <stdio.h>int main(){ int i,j,index,k; int tem,length; int a[10]={2,24,3,19,45,12,1,66,34,7};// length = a.length(); length = 10; printf("Before ordered:\n"); for(i = 0; i < length; i++) printf("%d ",*(a+i)); printf("\n\n"); for(i = 0; i < length; i+ 阅读全文

posted @ 2012-03-25 12:27 NewPanderKing 阅读(197) 评论(0) 推荐(0) 编辑

经典排序之冒泡排序代码
摘要:1 #include <stdio.h> 2 int main() 3 { 4 int i,j,k; 5 int tem,length; 6 int a[10]={2,24,3,19,45,12,1,66,34,7}; 7 // length = a.length(); 8 length = 10; 9 printf("Before ordered:\n");10 for(i = 0; i < length; i++)11 printf("%d ",*(a+i));12 printf("\n\n");13 for(. 阅读全文

posted @ 2012-03-25 12:25 NewPanderKing 阅读(422) 评论(0) 推荐(0) 编辑

关于cout计算顺序,从右到左计算顺序。
摘要:1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 const int a[] = {10,20,30}; 7 const int *p = a; 8 cout<<"*p:"<<*p<<endl; 9 cout<<"*p:"<<*p<<"\t*p++:"<<*p++<<"\t*p"<<*p<<endl; 1 阅读全文

posted @ 2012-03-25 12:15 NewPanderKing 阅读(1880) 评论(0) 推荐(0) 编辑

交换两个数字,不借助于第三个变量的三种简单的方法。
摘要:1 #include <stdio.h> 2 int main() 3 { 4 int a = 10,b=20; 5 // solution 1: a = a+b; b = a-b; a = a-b; 6 printf("*********************************************\n"); 7 printf("Change two number without another number!!!!\n"); 8 printf("************************************* 阅读全文

posted @ 2012-03-23 16:18 NewPanderKing 阅读(1617) 评论(1) 推荐(0) 编辑

上一页 1 2

导航