搜集一些常见的C算法

1 冒泡排序

#define SWAP(x, y) {Type t;(t)=(x);(x)=(y);(y)=(t);} //恨不得整个程序都用宏  
typedef int Type;       //一切都为了可移植性  

void BubbleSort(Type *, int); 

void BubbleSort(Type *p, int n)  //传统的冒泡排序  
{ 
                int i, j, flag;  //标记?一切都为了效率  
                 
                for(i=1; i<n; i++) 
                { 
                         flag=0; 
                         for(j=0; j<n-i; j++) 
                            if(p[j]>p[j+1]) 
                            { 
                                           SWAP(p[j], p[j+1]); 
                                           flag=1; 
                            } 
                         if(!flag) break; 
                } 
}

#include<iostream.h> int *insert(int*aa,int n)//插入排序 {  for(int i=0;i<10;i++)  {   for(int j=i;j>0;j--)   {    if(aa[j]<aa[j-1])    {     int temp;     temp=aa[j-1];     aa[j-1]=aa[j];     aa[j]=temp;    }   }  }  return aa; }

 

 

int*bubble(int *aa,int n)//冒泡排序 {  int temp;  for(int i=1;i<10;i++)                  //供比较n-1轮  {   for(int j=0;j<10-i;j++)               //比较一轮:最大的数到数组最后。   {    if(aa[j]<aa[j+1])               //一次交换: 将较小的数调换到前面    {     temp=aa[j];     aa[j]=aa[j+1];     aa[j+1]=temp;      }   }  }

      return aa;

}

 

 

int *choice(int *aa,int n)//选择排序法 {  int temp,max;  for(int i=n;i>0;i--) //最大的数依次被放在aa[n],aa[n-1].....aa[0]  {   max=0;   for(int j=0;j<=i;j++)   {    if(aa[max]<aa[j])     max=j;   }   temp=aa[max];    //将aa[0]到aa[i]之间的最大的数放在aa[i]的位置上。   aa[max]=aa[i];   aa[i]=temp;    }  return aa;

}

void main() {  int a[10]={1,2,4,9,6,5,3,10,8,7};  int *p=bubble(a,10);

 for(int i=0;i<10;i++)  cout<<*p++<<" ";

}

 

 

2 给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。

anSwer  记住,这种题目往往就是考你对边界的考虑情况。编程除了技术上的熟练以外,细心也是非常重要的。其实很多编程的大师可能并不是有特别的技术,往往就是他们非常的耐心和细心,记住:编程是计算机科学中最基本的工作,它是最容易去掌握的,耐心点,细心点你一定能够学好它。代码看下面:

char* myStrcpy(char* s,char* a,char* b,char n)

{

int aLen = strlen(a),bLen = strlen(b);

         if(n > aLen || n > bLen)

                   return NULL; // Error 

         for(int i = 0;i < aLen + bLen - n;i++)

                   if(i < aLen - n) s[i] = a[i];

                   else s[i] = b[i - aLen + n];

                   s[i] = '\0';

                   return s;

}

 

3.请编写能直接实现strstr()函数功能的代码

ANSWER 首先要知道strstr()这个函数是干什么的,自己去查查C语言的书,一般附录后面会给出C语言标准库的。这个题目实际上也是一类重要的算法门类,叫做“字符串的模式匹配”。它有很多的现成算法,其中最简单的要数朴素的匹配算法,还有KMP,BM这些高级算法,笔试估计是来不及写的。下面给出朴素的匹配算法。

int stringMatching(char* pattern,char* text)

{

         int pLen = strlen(pattern),tLen = strlen(text);

         for(int i = 0;i <= tLen - pLen;i++){

      for(int j = 0; pattern[j] == text[i + j];j++);

                   if(j == pLen) return i;

         }

         return -1; // Not found

}

 

 树的非递归排序

 

1.先序遍历非递归算法
#define maxsize 100
typedef struct
{
 Bitree Elem[maxsize];
 int top;
}SqStack; 
void PreOrderUnrec(Bitree t)
{
 SqStack s;
 StackInit(s);
 p=t;
 
 while (p!=null || !StackEmpty(s))
 {
   while (p!=null)       //遍历左子树
   {
     visite(p->data);
     push(s,p);
     p=p->lchild;    
   }//endwhile
   
   if (!StackEmpty(s))     //通过下一次循环中的内嵌while实现右子树遍历
   {
     p=pop(s);
     p=p->rchild;    
   }//endif
       
 }//endwhile 
 
}//PreOrderUnrec 

2.中序遍历非递归算法
#define maxsize 100
typedef struct
{
 Bitree Elem[maxsize];
 int top;
}SqStack; 

void InOrderUnrec(Bitree t)
{
 SqStack s;
 StackInit(s);
 p=t;
 while (p!=null || !StackEmpty(s))
 {
   while (p!=null)       //遍历左子树
   {
     push(s,p);
     p=p->lchild;
   }//endwhile
   
   if (!StackEmpty(s))
   {
     p=pop(s);
     visite(p->data);    //访问根结点
     p=p->rchild;      //通过下一次循环实现右子树遍历
   }//endif  
 
 }//endwhile 

}//InOrderUnrec 

 

链表基本操作 

 

#include<iostream> 
#include<string> 
using namespace std; 

struct List 
{ 
int num; 
List *next; 
}; 

List *head=NULL; 

////***************创建一个单链表**********************///
List* CreateList() 
{ 
List *pL;                     //节点指针
List *pEnd;                   //链尾指针,用于在其后插入节点 


pL=new List;                 //动态分配内存==新建一个节点,准备插入到链表中

pEnd=pL; 
cout<<"请输入节点的数据,以 0 结束"<<endl; //给节点赋值
cin>>pL->num; 

while(pL->num!=0)        //如果pL是第一个节点,head=pL;若不是:pEnd->next=pL
{ 
 if(NULL==head)
  head=pL;
 else
  pEnd->next=pL;


pEnd=pL; // 相当于pEnd=pEnd->next.
pL=new List; 
cin>>pL->num; 
} 

delete pL; 
pEnd->next=NULL; 
return head; 
} 


//*******************顺序显示一个单链表******************************//
void ShowList(List *head) 
{ 
cout<<endl; 
cout<<"链表节点如下:"<<endl; 
while(head) 
{ 
cout<<head->num<<endl; 
head=head->next; 
} 
} 


//***********************删除数据是num的链表节点****************************//
void DeleteList(List *head, int num) //在程序中是对全局变量head进行修改。所以用不着返回head。
{ 

List *l; 
if(!head)
{
 cout<<"\nlist null~!\n";
 return;                   //空链表。未作删除

}
if(head->num==num)  //要删除的节点在链首
{ 
l=head;             //l用于删除节点
head=head->next; 
::head=head;        //令全局变量::head等于局部变量head
delete l; 
return ; 
} 

for(List *pGuard=head;pGuard->next;pGuard=pGuard->next)
{
 if(pGuard->next->num==num)
 {
  l=pGuard->next;
  pGuard->next=l->next;
  delete l;
  return;
 }
}
cout<<"not found!\n";

}


//*******************获取链表节点的个数*********************//
int GetListNum(List *head) 
{ 
int num=0; 
while(head) 
{ 
num++; 
head=head->next; 
} 
return num ; 
} 

 //**************************在链表末尾插入一个链表节点**************************//
void InsertList(List *head,int num) 
{ //这里的head是局部变量。不会影响全局变量head.

List *list =new List; 
List *l; 
while(head) 
{ 
l=head; 
head=head->next; 
} 
list->num=num; 
list->next=NULL; 
l->next=list; 
} 


void  main()
{
 head=CreateList();
 ShowList(head);
 InsertList(head,4);
 ShowList(head);
 DeleteList(head,1);
 ShowList(head);
 
}

 

判断素数

 

判断n是不是素数,n只需要被根号n之间的整数除就行了.如果都除不尽,则n为素数.

举个例子:

判断17是不是整数..
(17^1/2)=4.x
将17被2,3,4除,都不能整除,所以17是素数

同样判断40是不是素数
(40^1/2)=6.x
用40被2,3,4,5,6整除,2,4,5都可以整除40,所以40不是素数.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

编程求101~1000内素数:
#include "Stdio.h"
#include "math.h"

int main(void)
{
int m,i,k,n=0;
for(m=101;m<=1000;m=m+2)
{
   k=sqrt(m);
  for(i=2;i<=k;i=i+2)

  if(m%i==0)
  break;
  if(i>=k+1)
  {printf("%d ",m);
  n=n+1;}
  if(n%10==0 )
  printf("\n");

}

} 

 

 

汉诺塔 

 

*********************用c解决************************

#include <stdio.h>
void move(char x,char y)
{ printf("%c-->%c\n",x,y);
}

void hanoi(int n,char a, char b, char c)//将n个盘从a座借助b座,移到c座
{ if(n==1)
  move(a,c);
else
{
  hanoi(n-1,a,c,b);         //将n-1个盘从a座借助c座移到b座
  move(a,c);                //将剩下的第n个盘移到c座
  hanoi(n-1,b,a,c);         //将n-1个盘从b座借助a座移到c座
}
}

int main()
{ int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("The step to moving %3d diskes:\n",m);
hanoi(m,'A','B','C');
return 0;
}

n=3时输出是:A-->C A-->B C-->B A-->C B-->A B-->C A-->C

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值