常考算法总结_算法复习总结
常考算法总结由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“算法复习总结”。
-------------------------void insertsort(int list[],int n)//直接插入排序 { int i,j,temp;for(i=1;i
for(j=i-1;j>=0;j--)
if(temp
list[j+1]=list[j];
else
break;
list[j+1]=temp;} }-------------------------void incrsort(int list[],int n,int h)//shell排序 { int i,j,temp;for(i=h;i
for(j=i-h;j>=0;j-=h)
if(temp
list[j+h]=list[j];
else
break;
list[j+h]=temp;} }
void shellsort(int list[],int n)//shell排序 { int i,incr=n;do{ incr=incr/3+1;for(i=0;i
incrsort(list,n,incr);}while(incr>1);}-------------------------void bubblesort(int list[],int n)//冒泡排序 { int i,j,temp;for(i=0;i
for(j=i+1;j
{
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
} }-------------------------void swap2(int &a,int &b)//引用传值 { int temp;temp=a;a=b;b=temp;} void swap1(int a,int b)//值传值 { int temp;temp=a;a=b;b=temp;} void swap(int *a,int *b)//指针传值 { int temp;temp=*a;*a=*b;*b=temp;} int partition(int list[],int low,int high)//快速排序 { int i=low+1,j=high,temp1;temp1=list[low];do {
while(temp1>list[i])i++;
while(temp1
if(i
{
swap(&list[i],&list[j]);
}
}while(i
swap(&list[low],&list[j]);
return j;}
void quicksort(int list[],int low,int high)//快速排序 { int k;if(low
k=partition(list,low,high);
quicksort(list,low,k-1);
quicksort(list,k+1,high);} }-------------------------void merge(int list[],int *temp,int a,int b,int c,int d,int *k)//两路归并过程 { int i=a,j=b;while((i
{temp[(*k)++]=list[j++];} } while(i
void mergesort(int list[],int n)//归并排序 { int *temp=(int*)malloc(sizeof(int)*100);int a,b,c,d,i,k,h=1;while(h
a=0;k=0;
while(a+h
{
c=a+h;
b=c-1;
if(c+h-1>n-1)d=n-1;
else d=c+h-1;
merge(list,temp,a,b,c,d,&k);
a=d+1;
}
for(i=0;i
{list[i]=temp[i];}
h*=2;} }-------------------------void selectsort(int list[],int n)//简单选择排序 { int i,j,small;for(i=0;i
small=i;
for(j=i+1;j
if(list[j]
small=j;
swap(&list[i],&list[small]);} }-------------------------char * nizhi(char *str)//字符串逆置 { char *p=str;int len=strlen(str);int i,j;char temp;
for(i=0,j=len-1;i
*(p+len)=' ';
return p;}-------------------------int x,y;//判断回文数
void judge(int * data,int len)//判断是否回文 { int i,j,f=0;for(i=0,j=len-1;i
void separate(int *data,int n)//将数字个十位分开 存入data { int j,k,t;y=0;while(n!=0){ *(data+y)=n%10;n=n/10;y++;} *(data+y)=' ';
for(j=0,k=y-1;j
node* nizhi(node* head){ node *p1,*p2,*p3;p1=head;p2=p1->next;while(p2){ p3=p2->next;p2->next=p1;p1=p2;p2=p3;} }-------------------------//统计字符串中不含有重复字符的最大子串长度 int search(char *text){
int lastpos[256], lmax=0, curmax=0;
int i;
for(i=0;i
lastpos[i]=-1;
for(i=0;text[i];i++)
{
if(i-lastpos[ text[i] ] > curmax)
{
curmax++;
lmax =(lmax>=curmax)?lmax:curmax;
}
else
curmax = i-lastpos[ text[i] ];
lastpos[ text[i] ] = i;
}
return lmax;}-------------------------//整数转换成字符串 法一:
while(num){ temp[i]=num%10+’0’;i++;num=num/10;} 再将temp逆序 法二:
itoa(number,string,10);
//字符串转换成整数 法一:
while(temp[i]){ sum=sum*10+(temp[i]-‘0’);i++;} 法二:
int atoi(const char *nptr);-------------------------//字符串循环右移n个
例如
abcdefghijkl n=2 结果为
klabcdefghij 实现:
Void loopmove(char *str,int n){ Char temp[maxsize];Int step=strlen(str)-n;Strcpy(temp,str+step);Strcpy(temp+n,str);*(temp+strlen(str))=’ ’;Strcpy(str,temp);}-------------------------//单链表排序(简单选择排序)void selectSort(Node *node){
Node *p;/*当前节点*/
Node *q;/*遍历未排序节点*/
Node *small;/*指向未排序节点中最小节点*/
int temp;
for(p = node->next;p->next->next!= 0;p = p->next)
{
small = p;
for(q = p->next;q->next!= 0;q = q->next)
if(small->value > q->value)
small = q;
temp = p->value;
p->value = small->value;
small->value = temp;
} }
-------------------------
//打印一字符串,数字正常打印,小写正常打印,大写转换成小写打印,其他字符不打印 for(i=0;i=48&&(int)str[i]
if((int)str[i]>=65&&(int)str[i]
if((int)str[i]>=97&&(int)str[i]
}-------------------------//二分法查找
int halfsearch(int list[],int low,int high,int k){ int i,j,mid;i=low;j=high;if(i
else return halfsearch(list,low,mid-1,k);} } else return 0;}-------------------------//单链表中已知一个指针p指向一个结点,p非头结点也非尾结点,如何删除p指向的结点
p->data=p->next->data;p->next=p->next->next;-------------------------//计算str中子串sub出现的次数,str为原字符串,sub为子串 //判断两字符串是否相等,相等返回1,不等返回0 int Judge(char *movePt,char *tempPt){
int i;
for(i = 0;i
{
if(*movePt!= tempPt[i])
return 0;
}
return 1;} //计算子串出现的次数,str为原字符串,sub为子串 int StrCount(char *str,char *sub){
int count = 0;
char *move = str;
if(strlen(str)
{
return 0;
}
else
{
while(strlen(move)>= strlen(sub))
{
if(Judge(move, sub))
{
count++;
}
move++;
}
}
return count;}
-------------------------补充:
12.单例模式
public cla MyBean {
private static MyBean instance = null;
private MyBean(){}
public static synchronized MyBean getInstance(){
if(instance == null){instance = new MyBean();}
return instance;
}
} 13.回调函数
回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。14.句柄
句柄,是整个windows编程的基础,一个句柄是指使用的一个唯一的整数值,是指一个四字节长的数值,用于标志应用程序中的不同对象和同类对象中的不同的实例,诸如,一个窗口,按钮,图标,滚动条,输出设备,控件或者文件等,应用程序能够通过句柄访问相应的对象的信息。但是,句柄不是一个指针,程序不能利用它句柄来直接阅读文件中的信息。如果句柄不用在I/O文件中,它是毫无用处的。句柄是windows用来标志应用程序中建立的或是使用的唯一整数,windows使用了大量的句柄来标志很多对象。
// 八皇后问题(采用回溯法求解)#include #include using namespace std;
bool place(int,int,int *);void NQueens(int,int, int*);void NQueens(int,int *);static int sum=0;
int main(int argc, char* argv[]){ int x[8];NQueens(8,x);cout
}
bool place(int k,int i,int* x){
//判定两皇后是不是交叉
for(int j=0;j
if((x[j]==i)||(abs(x[j]-i)==abs(j-k)))
return false;
return true;}
void NQueens(int k,int n,int* x){ for(int i=0;i
if(place(k,i,x)){
x[k]=i;
if(k==n-1){
for(i=0;i
cout
cout
++sum;
}
else NQueens(k+1,n,x);
} } }
void NQueens(int n,int* x){ NQueens(0,n,x);}