欢迎来到天天文库
浏览记录
ID:29619597
大小:47.51 KB
页数:9页
时间:2018-12-21
《c语言常用算法集合》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、1.定积分近似计算:/*梯形法*/doubleintegral(doublea,doubleb,longn){longi;doubles,h,x;h=(b-a)/n;s=h*(f(a)+f(b))/2;x=a;for(i=1;i2、+=h;}return(t);}2.生成斐波那契数列:/*直接计算*/intfib(intn){inti,f1=1,f2=1,f;for(i=3;i<=n;i++){f=f1+f2;f1=f2;f2=f;}if(n==13、4、n==2)return1;elsereturnf;}/*递归调用*/voidfib(intn,int*s){intf1,f2;if(n==15、6、n==2)*s=1;else{fib(n-1,&f1);fib(n-2,&f2);*s=f1+f2;}}3.素数的判断:/*方法一*/for(t7、=1,i=2;i8、}if(t)printf("%disprime",n);4.反序数:/*求反序数*/longfan(longn){longk;for(k=0;n>0;n/=10)k=10*k+n%10;returnk;}/*求回文数*/intf(longn){longk,m=n;for(k=0;n>0;n/=10)k=10*k+n%10;if(m==k)return1;return0;}/*求整数位数*/intf(longn){intcount;for(count=0;n>0;n/=10)count++;returncou9、nt;}5.求最大公约数:/*方法一*/intgcd(intx,inty){intz;z=x10、11、y%z*/z--;returnz;}/*方法二*/intgcd(intx,inty){intr;while((r=x%y)!=0){x=y;y=r;}returny;}/*方法三*/intgcd(inta,intb){intr;if((r=a%b)==0)returnb;elsereturngcd(b,r);}6.数组常用算法:查找:/*线性12、查找*/intfind(intnum,intx[],intkey){inti,m=-1;for(i=0;ikey)high=mid-1;e13、lselow=mid+1;}returnm;}/*折半查找(递归)*/intb_search(intx[],intlow,inthigh,intkey){intmid;mid=(low+high)/2;if(x[mid]==key)returnmid;if(low>=high)return-1;elseif(key14、*s1,char*s2){inti,k=0;while(s1[i]==s2[i])i++;if(s2[i]==0)returnk;s1++;k++;return-1;}分词:/*方法一*/voidfen(chars[][10],charstr){inti,j,k;for(i=0,j=0,k=0;str[i]!=0;i++)if(isalpha(a[i]))s[j][k++]=str[i];else{s[j][k]
2、+=h;}return(t);}2.生成斐波那契数列:/*直接计算*/intfib(intn){inti,f1=1,f2=1,f;for(i=3;i<=n;i++){f=f1+f2;f1=f2;f2=f;}if(n==1
3、
4、n==2)return1;elsereturnf;}/*递归调用*/voidfib(intn,int*s){intf1,f2;if(n==1
5、
6、n==2)*s=1;else{fib(n-1,&f1);fib(n-2,&f2);*s=f1+f2;}}3.素数的判断:/*方法一*/for(t
7、=1,i=2;i8、}if(t)printf("%disprime",n);4.反序数:/*求反序数*/longfan(longn){longk;for(k=0;n>0;n/=10)k=10*k+n%10;returnk;}/*求回文数*/intf(longn){longk,m=n;for(k=0;n>0;n/=10)k=10*k+n%10;if(m==k)return1;return0;}/*求整数位数*/intf(longn){intcount;for(count=0;n>0;n/=10)count++;returncou9、nt;}5.求最大公约数:/*方法一*/intgcd(intx,inty){intz;z=x10、11、y%z*/z--;returnz;}/*方法二*/intgcd(intx,inty){intr;while((r=x%y)!=0){x=y;y=r;}returny;}/*方法三*/intgcd(inta,intb){intr;if((r=a%b)==0)returnb;elsereturngcd(b,r);}6.数组常用算法:查找:/*线性12、查找*/intfind(intnum,intx[],intkey){inti,m=-1;for(i=0;ikey)high=mid-1;e13、lselow=mid+1;}returnm;}/*折半查找(递归)*/intb_search(intx[],intlow,inthigh,intkey){intmid;mid=(low+high)/2;if(x[mid]==key)returnmid;if(low>=high)return-1;elseif(key14、*s1,char*s2){inti,k=0;while(s1[i]==s2[i])i++;if(s2[i]==0)returnk;s1++;k++;return-1;}分词:/*方法一*/voidfen(chars[][10],charstr){inti,j,k;for(i=0,j=0,k=0;str[i]!=0;i++)if(isalpha(a[i]))s[j][k++]=str[i];else{s[j][k]
8、}if(t)printf("%disprime",n);4.反序数:/*求反序数*/longfan(longn){longk;for(k=0;n>0;n/=10)k=10*k+n%10;returnk;}/*求回文数*/intf(longn){longk,m=n;for(k=0;n>0;n/=10)k=10*k+n%10;if(m==k)return1;return0;}/*求整数位数*/intf(longn){intcount;for(count=0;n>0;n/=10)count++;returncou
9、nt;}5.求最大公约数:/*方法一*/intgcd(intx,inty){intz;z=x10、11、y%z*/z--;returnz;}/*方法二*/intgcd(intx,inty){intr;while((r=x%y)!=0){x=y;y=r;}returny;}/*方法三*/intgcd(inta,intb){intr;if((r=a%b)==0)returnb;elsereturngcd(b,r);}6.数组常用算法:查找:/*线性12、查找*/intfind(intnum,intx[],intkey){inti,m=-1;for(i=0;ikey)high=mid-1;e13、lselow=mid+1;}returnm;}/*折半查找(递归)*/intb_search(intx[],intlow,inthigh,intkey){intmid;mid=(low+high)/2;if(x[mid]==key)returnmid;if(low>=high)return-1;elseif(key14、*s1,char*s2){inti,k=0;while(s1[i]==s2[i])i++;if(s2[i]==0)returnk;s1++;k++;return-1;}分词:/*方法一*/voidfen(chars[][10],charstr){inti,j,k;for(i=0,j=0,k=0;str[i]!=0;i++)if(isalpha(a[i]))s[j][k++]=str[i];else{s[j][k]
10、
11、y%z*/z--;returnz;}/*方法二*/intgcd(intx,inty){intr;while((r=x%y)!=0){x=y;y=r;}returny;}/*方法三*/intgcd(inta,intb){intr;if((r=a%b)==0)returnb;elsereturngcd(b,r);}6.数组常用算法:查找:/*线性
12、查找*/intfind(intnum,intx[],intkey){inti,m=-1;for(i=0;ikey)high=mid-1;e
13、lselow=mid+1;}returnm;}/*折半查找(递归)*/intb_search(intx[],intlow,inthigh,intkey){intmid;mid=(low+high)/2;if(x[mid]==key)returnmid;if(low>=high)return-1;elseif(key14、*s1,char*s2){inti,k=0;while(s1[i]==s2[i])i++;if(s2[i]==0)returnk;s1++;k++;return-1;}分词:/*方法一*/voidfen(chars[][10],charstr){inti,j,k;for(i=0,j=0,k=0;str[i]!=0;i++)if(isalpha(a[i]))s[j][k++]=str[i];else{s[j][k]
14、*s1,char*s2){inti,k=0;while(s1[i]==s2[i])i++;if(s2[i]==0)returnk;s1++;k++;return-1;}分词:/*方法一*/voidfen(chars[][10],charstr){inti,j,k;for(i=0,j=0,k=0;str[i]!=0;i++)if(isalpha(a[i]))s[j][k++]=str[i];else{s[j][k]
此文档下载收益归作者所有