1. 二分查找
2. 归并排序
#include <stdio.h> #include <string.h> #include <stdlib.h> void merge(int arr[],int left,int mid,int right,int temp[]); void sort(int a[],int left,int right,int t[]); void main(){ int a[10]={2,3,7,8,4,6,5,9,1,10}; int b[10],i; sort(a,0,9,b); for(i=0;i<10;i++){ printf("%d ",a[i]); } printf(" "); } //将arr[left,mid]和arr[mid+1,right]合并 //图解:https://www.cnblogs.com/chengxiao/p/6194356.html void merge(int arr[],int left,int mid,int right,int temp[]){ int i=left,j=mid+1,t=left; while(i<=mid && j<=right){ if(arr[i]<arr[j]){ temp[t++]=arr[i++]; }else{ temp[t++]=arr[j++]; } } while(i<=mid){ temp[t++]=arr[i++]; } while(j<=right){ temp[t++]=arr[j++]; } //将排序好的序列复制回原数组 for(i=left;i<=right;i++){ arr[i]=temp[i]; } } void sort(int a[],int left,int right,int t[]){ if(left<right){ int mid=(left+right)/2; sort(a,left,mid,t); sort(a,mid+1,right,t); merge(a,left,mid,right,t); } }
3. 大数相乘
#include <stdio.h> #include <string.h> #include <stdlib.h> void bigmulti(char *a,char *b,int tmp[]); void main(){ char a[100],b[100]; scanf("%s",&a); scanf("%s",&b); int t[strlen(a)+strlen(b)]; memset(t,0,sizeof(t)); bigmulti(a,b,t); printf(" "); } void bigmulti(char *a,char *b,int tmp[]){ int alen=strlen(a); int blen=strlen(b); int i,j; for(i=0;i<alen;i++){ for(j=0;j<blen;j++){ tmp[i+j+1] += ((a[i]-'0')*(b[j]-'0'));//此处设为i+j+1方便下面的进位 } } for(i=alen+blen-1;i>0;i--){ if(tmp[i]>10){ tmp[i-1]+=(tmp[i]/10); tmp[i]=tmp[i]%10; } } for(i=0;i<alen+blen;i++){ if(tmp[i]==0) continue; printf("%d",tmp[i]); } }
参考:http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741370.html