博文

快速排序算法举例(2006-07-30 02:47:00)

摘要:函数名: qsort 功  能: 使用快速排序例程进行排序 用  法: void qsort(void *base, int nelem, int width, int (*fcmp)()); 程序例: #include <stdio.h> #include <stdlib.h> #include <string.h> int sort_function( const void *a, const void *b); char list[5][4] = { "cat", "car", "cab", "cap", "can" };   int main(void) {    int  x;    qsort((void *)list, 5, sizeof(list[0]), sort_function);    for (x = 0; x < 5; x++)       printf("%s\n", list[x]);    return 0; } int sort_function( const void *a, const void *b) {    return( strcmp(a,b) ); } ......

阅读全文(3106) | 评论:0

2开平方的算法... (2006-07-30 02:46:00)

摘要:算法一:#include <stdio.h>#include <stdlib.h>double __sqrt(double n){    double  root=n/2.0;    while( fabs(root*root-n)>0.000001 )        root = (root+n/root)/2.0;    return root;  }    int main(int argc, char *argv[]){    int n;    for(n=0; n<=100; n++)        printf("sqrt(%3d) = %lf\n", n, __sqrt((double) n));     system("PAUSE");        return 0;}算法二:#include<math.h>#include<stdio.h>int main(){double x,x0;  x=1.5;  do{   x0=x;      x=x/2+1/x;  } while (fabs(x-x0)>=1e-10);printf("sqrt(2)=%1.10f\n",x);return 0;}......

阅读全文(2268) | 评论:0

汉诺塔递归算法(2006-07-30 02:45:00)

摘要:#include <iostream.h>void move(char getone,char putone){    cout<<getone<<"-->"<<putone<<endl;}void hanoi(int n,char one,char two,char three){        void move(char getone,char putone);    if(n==1)move (one ,three);    else    {        hanoi(n-1,one, three,two);        move(one ,three);        hanoi(n-1,two,one ,three);    }}void main(){    void hanoi(int n,char one,char two,char three);    int m;    cout<<"请输入汉诺塔的级数:";    cin>>m;    cout<<"移动"<<m<<"盘子的方法:"<<endl;    hanoi(m,'A','B','C');}......

阅读全文(2580) | 评论:3

一道算法题(2006-07-30 02:44:00)

摘要:例如整数121,如何实现1-10十个数字,在不重复使用的情况下,分成5组,两两一组相乘,然后等到的五个积相加,和等于121!用什么算法求出这五组组合!   -------------------------------------- rickone 的算法(8月18日) #include#includeint s[10];int v[11];void dfs(int c, int ob){    int i;    if(c == 10){        int sum = 0;        for(i = 0;i < 10;i += 2)            sum += s[i]*s[i+1];         if(sum == ob){ //输出组合            for(i = 0;i < 10; ++i)                cout<......

阅读全文(1119) | 评论:0

迷宫的算法(2006-07-30 02:37:00)

摘要:深度搜索:/*迷宫的算法*//*   先建立迷宫(外围建筑围墙),设置入口和出口。建立链表,把第一个点(入口)入舰,同时做好标记,以免重复入舰。a:判断舰顶元素的东面是否可通,若可通则将其入舰 ,同时做好标记,以免重复入舰;若不通则判断舰顶元素的南面是否可通,依此操作至北面元素。     判断新的舰顶元素是否为出口 ,若是,则报告结束,并打印出路径;否则递归操作a:。     若舰顶元素其余三面皆不通 ,则将舰顶元素其重新设定为不通,以免以后再走弯路。然后退舰。    重复a;操作。    若退回到入口,则表示迷宫走不通,报告不通。*/ /*2005-4-22 梁见斌*/ #include<stdio.h>#include<stdlib.h>#include<string.h>#define OK 1#define NO 0#define OVERFLOW 1#define M 5 #define N 5typedef struct poi{/*建立链表结构,存储每个路径点的有关数据;横坐标,纵坐标,所指方向和向下的指针*/    int x;   int y;   char *p;   struct poi *next;} Point;Point *head;char a[100][200];char FX[14]={'E','S','W','N','E','S','W', 'E','N','W','S','E','N','W',};/*存储方向*/ int MG_S[M+2][N+2], MG[M+2][N+2], In[2], Out[2], X_Y[2];/*分别存储迷宫,入,出口和当前坐标*/ int FX_Head, FX_Tail, row=0;void Build(void);/*建立迷宫(外围建筑围墙),设置入口和出口*/void B_IO(void);void Copy(void);void B_Head(......

阅读全文(2449) | 评论:0

生成不重复组合算法(2006-07-30 02:36:00)

摘要:#include <stdio.h>#define MAXM    10int m, n;int workarr[MAXM];int record[MAXM];void DFS(int l){    int i;    if (l == n)    {        for (i=0; i<n; i++)            printf("%c", 'a'+record[i]);        printf("\n");        return;     }    for (i=0; i<m; i++)        if (workarr[i] > 0)        {            workarr[i]--;            record[l] = i;            DFS(l+1);         ......

阅读全文(1979) | 评论:0

生成排列算法(2006-07-30 02:35:00)

摘要:#include <stdio.h>int n;int visited[20];int workarr[20];void DFS(int l){    int i;    if (l == n)    {        for (i=0; i<l; i++)            printf("%d ", workarr[i]+1);        printf("\n");        return;     }     for (i=0; i<n; i++)         if (!visited[i])         {             visited[i] = 1;             workarr[l] = i;             DFS(l+1);           ......

阅读全文(1225) | 评论:0

回形方阵算法(2006-07-30 02:34:00)

摘要:与下面的方阵类似的方阵称为回形方阵:1      2     3     4      5     6     724     25    26    27     28    29    823     40    41    42     43    30    922     39    48    49     44    31    1021     38    47    46     45    32    1120     37    36    35   &......

阅读全文(1748) | 评论:0

全排序的拓扑算法(2006-07-30 02:32:00)

摘要:一般的拓扑算法只有一种输出,这个可以把所有的可能都输出^)^:#include<iostream>#include<string>using namespace std;#define max 10#define arraysizemax 20int total=0;class Point                             {public:Point(char i){value=0;  used=0;  name=i;  for(int j=0;j<max;j++)     next[j]=0;}Point* operator->()                  {return this;} int size(){return used;}int value;int used;char name;Point* next[max];};                                 Point* HEAD; void Initial(){char i,j;cin>>i>>j;Point* head=new Point(i);Point* second=new Point(......

阅读全文(1001) | 评论:0

银行家算法模拟程序(2006-07-30 02:28:00)

摘要:#include #include #include #define N  5#define M  3#define ok  1#define wait -1#define error 0typedef int Status;int Allocation[N][M],Available[N][M],Need[N][M];Status SAsign(int *Request)  // 资源分配策略函数{  if(!compare(Request,Need[i]))        return error;   if(!compare(Request,Available[i]))       return wait;   substract(Available[i],Request);   add(Allocation[i],Request);   substract(Need[i],Request);   if(Safecheck())       return ok;   else{     add(Available[i],Request);     substract(Allocation[i],Request);     add(Need[i],Request);   }   return wait;}int compare(int * x,int *y)      //比较函数{  ......

阅读全文(2910) | 评论:0