欢迎来到天天文库
浏览记录
ID:29619597
大小:47.51 KB
页数:9页
时间:2018-12-21
《c语言常用算法集合》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
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;i0;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++;returncount;} 5.求最大公约数:/*方法一*/intgcd(intx,inty){intz;z=xkey)high=mid-1;elselow=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(key#includevoidmain(){inti=0,n=0;chars[80],*p;strcpy(s,"Itisabook.");for(p=s;p!=' ';p++)if(*p=='')i=0;elseif(i==0){n++;i=1;}printf("%d ",n);getch();}排序:/*插入法排序*/voidsort(inta[],intn){inti,j,t;for(i=1;i=0&&ta[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;flag=0;}if(flag)break;}}/*交换法排序2*/voidsort(inta[],intn){inti,j,t;for(i=0;ia[j]){t=a[i];a[i]=a[j];a[j]=t;}}/*选择法排序*/voidsort(inta[],intn){inti,j,point,t;for(i=0;i#includeintmain(void){doublex,x1,eps=1e-6,f,f1;/*误差为eps*/ x=1.0;/*x=1.0是初值*/do{x1=x;f=6-x1*(5-x1*(4-3*x1));/*f为f(x)函数*/f1=-5+x1*(8-9*x1);/*f1为f(x)的导函数*/x=x1-f/f1;f=6-x*(5-x*(4-3*x));}while(fabs(f)>=eps&&fabs(x-x1)>=eps);printf("x=%f",x);}/*二分法求函数跟*/#include#includedoublef(doublex){return6-x*(5-x*(4-3*x));/*f(x)函数*/}intmain(void){doublea,b,c,x,eps=1e-6;do{scanf("%lf%lf",&a,&b);}while(f(a)*f(b)>0);if(fabs(f(a))<1e-6)x=a;elseif(fabs(f(b))<1e-6)x=b;else{c=(b+a)/2;while(fabs(f(c))>eps&&fabs(b-a)>eps){if(f(a)*f(c)<0)b=c;elsea=c;c=(b+a)/2;}x=c;}printf("x=%f",x);}/*弦截法求函数跟*/c=(a*f(b)-b*f(a))/(f(b)-f(a));while(fabs(f(c))>eps){if(f(a)*f(c)<0)b=c; elsea=c;c=(a*f(b)-b*f(a))/(f(b)-f(a));}#includevoidf();intmain(void){intx,loop=0;do{for(x=1;x<5;x++){intx=2;printf("%d",x);}printf("%d",x);f();loop++;}while(loop<1);getch();}voidf(){printf("%d",x++);}8.汉诺塔:#includevoidHanoi(intn,charA,charB,charC){if(n==1)printf(" move%dfrom%cto%c",n,A,C);else{Hanoi(n-1,A,C,B);printf(" move%dfrom%cto%c",n,A,C);Hanoi(n-1,B,A,C);}}intmain(void){Hanoi(3,'A','B','C');getch();}9.建立链表:NODE*creat(void)/*void表示无参函数*/{NODE*head=NULL,*p1=NULL,*p2=NULL;longnum;unsignedscore; intn=0;do{scanf(“%ld%u”,&num,&score);if(num==0)break;n++;p1=(NODE*)malloc(sizeof(NODE));p1->data.num=num,p1->data.score=score;p1->next=NULL;if(n==1)head=p2=p1;else{p2->next=p1;p2=p1;}}while(1);returnhead;}10.级数的近似计算:#include#includeintmain(void){doubles=1,a=1,x,eps,f;intn,m;printf("inputxandeps:");scanf("%lf%lf",&x,&eps);for(n=1;fabs(a)>eps;n++){for(f=1,m=1;m<=n;m++)f*=m;a=pow(x,n)/f;s+=a;}printf("%f",s);}
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处