博文

c/c++基礎習題解決(2)--因式分解問題(2007-07-27 21:24:00)

摘要:习题 12:因子分解★ 输入n(1 <= n <= 1e9),有多组测试数据:61627输出:616 = 2^3 * 7 * 1127 = 3^3(注意输出空格,但行末不要有空格)难度:for beginner                                                                             --習題來源:飛燕之家c/c++學習論壇   解決代碼: //因式分解 #include <stdio.h>#include <wchar.h>#define   PRIME_MAXCOUNT      500#define   RESULT_MAXCOUNT   10#define   FIRST_NUM                    2 //foward declarationint    GetAllPrimeNumber(int iNumber,int a......

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

顺序和二分查找(2005-07-04 12:53:00)

摘要:#include<stdio.h> #include<stdlib.h> #define N 10 typedef struct { int *elem; int length; }list; list L; int *head; void initial_list() { L.length=N; L.elem=NULL; } void create_list() { int i=0; head=L.elem=(int *)malloc(N*sizeof(int));    if(!L.elem)     {       printf("overflow!");       exit(0);     } printf("please input the sequential list(%d):\n",N); while(i<N)    {      printf("\nthe %dth elem:",i+1);     scanf("%d",&(L.elem[  i ]));    i++;    } getchar(); } int issequential_list() { int i=0; L.elem=head; while(i<N-1) {   if(L.elem[  i ]>L.elem[  i+1 ])      return 0; i++; } return 1; } void search_key() { int key,i; char ch; while(1) { i=0; printf......

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

学生成绩管理系统(2005-06-26 13:30:00)

摘要:/*第三方函数库: SGCHN3.0*/ /*该库函数的的一些函数说明 /*  C Head File.     SGCHN30.h     Definitions for SGCHN3.0 Run time Library Function.     Copyright (c) 2004, 2005 SGPRO     All Rights Reserved.     Autor: YinShengge 作者: 殷圣鸽     Version: 3.0     Build Time: 2004 - 4     Maintain Time: 2005-5-14     Maintain Time: 2005-5-19   */ extern void  SGCHNinit(); extern char  *getsgchnversion();   /*获得SGCHN版本*/   extern int   scanformat(char *formats, ...);    /*控制台格式输入  与scanf类似*/ extern int   print(char *formats,...);          /*控制台格式输出  与printf类似*/ extern int   *getsforcon(char *string);      &n......

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

学生信息管理系统(2005-06-26 13:28:00)

摘要:/*第三方函数库 SGCHN3.0*/ /*该函数库一些函数说明 /*  C Head File.     SGCHN30.h     Definitions for SGCHN3.0 Run time Library Function.     Copyright (c) 2004, 2005 SGPRO     All Rights Reserved.     Autor: YinShengge 作者: 殷圣鸽     Version: 3.0     Build Time: 2004 - 4     Maintain Time: 2005-5-14     Maintain Time: 2005-5-19   */ extern void  SGCHNinit(); extern char  *getsgchnversion();   /*获得SGCHN版本*/   extern int   scanformat(char *formats, ...);    /*控制台格式输入  与scanf类似*/ extern int   print(char *formats,...);          /*控制台格式输出  与printf类似*/ extern int   *getsforcon(char *string);      ......

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

哈希表创建伪随机处理冲突(2005-06-13 14:07:00)

摘要:#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 30 typedef char elemtype[19]; typedef struct node { elemtype name; int num; }tablenode;/*定义结点类型*/ tablenode *L;    void create_hash()/*伪随机法构造哈希表*/     {       int i,sum;       char string[19];       L=(tablenode *)malloc(N*sizeof(tablenode));       if(!L)       {        printf("\noverflow!");        exit(0);       }      for(i=0;i<N;i++)      {       L[i].name[0] ='\0';       L[i].num=i;       }     for(i=0;i<N;i++)     {  &nb......

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

图的深度优先搜索和广度优先搜索(2005-05-27 10:14:00)

摘要:#include<stdio.h> #include<stdlib.h> #define Max_vertex 20 typedef int elemtype;             /*最大顶点数*/ typedef struct QNode        /* 队列元素类型*/ { elemtype data; struct QNode *next; }QNode,*QNodeptr; typedef struct QueueNode { QNodeptr front ; QNodeptr rear; }Queue;                          /*队列类型*/ Queue *L;         /*建立队列*/ typedef enum { FALSE, TRUE }Boolean; Boolean visited[Max_vertex];/*联合体类型数组*/ typedef char vertextype[Max_vertex];/*顶点类型*/ typedef struct edgenode { int adjvex; struct edgenode *next; }EdgeNode;    /*边类型*/ typedef struct vertexnode { vertextype vertex; EdgeNode *firstedge; }VertexNode;/*链表顶点类型*/ typedef VertexNode AdjList[Max_vertex]; typedef struct ALGraph { A......

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

十字链表创建图(2005-05-25 13:49:00)

摘要:#include<stdio.h> #include<stdlib.h> #include<string.h> #define Max 20 typedef char  vertextype[Max]; typedef int InfoType; typedef struct ArcBox { int tailvex,headvex; struct ArcBox *hlink,*tlink; InfoType  info; }ArcBox; typedef struct vexnode { vertextype data; ArcBox *firstin,*firstout; }vexnode; typedef struct { vexnode xlist[Max]; int vexnum,arcnum; }OLGraph; OLGraph G; int LocateVex(OLGraph *H,vertextype c) { int i; for(i=0;i<G.vexnum;i++) { if(strcmp(c,H->xlist[i].data))    return i; } return 0; } main() { vertextype a,b; InfoType intout; ArcBox *p; int i,j,k; printf("please input the number of vertex and arcnode,IncInfo(0 or 1):\n"); scanf("%d%d%d",&G.vexnum,&G.arcnum,&intout); fflush(stdin); printf("\n please input the vertex:\n"); for(i=0;i<G.vexnum;i++) { scanf("%s",&G.xlist[i].data); G.xlist[i].firstin=NULL; G.xlist[i].firstout=NULL; } printf("\n ple......

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

图的邻接表建立及深度优先搜索(2005-05-14 13:02:00)

摘要:#include<stdio.h> #include<stdlib.h> #define Max_vertex 20 typedef enum { FALSE, TRUE }Boolean; Boolean visited[Max_vertex]; typedef char vertextype; typedef struct edgenode { int adjvex; struct edgenode *next; }EdgeNode; typedef struct vertexnode { vertextype vertex; EdgeNode * firstedge; }VertexNode; typedef VertexNode AdjList[Max_vertex]; typedef struct ALGraph { AdjList adjlist; int n,e; }Graphic; void createGraphic(Graphic *G) { int i,j,k; EdgeNode *s; printf("please input the vertexs' number and Edges' number:\n"); fflush(stdin); scanf("%d%d",&G->n,&G->e); getchar(); printf("please input the vertexs' information.\n"); for(i=1;i<(G->n+1);i++) { fflush(stdin); G->adjlist[i].vertex=getchar(); G->adjlist[i].firstedge=NULL; getchar(); } printf("please input the nodes(double):\n"); for(k=1;k<(G->e+1);k++) { scanf("%d%d",&i,&j); getchar(); s=(EdgeNode *)malloc(sizeof(EdgeNode)); if(!s) ......

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

哈夫曼编码及解码(2005-05-11 14:19:00)

摘要: 统计文件中字符的次数,并且把他们当作权值,进行hufuman编码: #include<stdio.h> #include<stdlib.h> #include<alloc.h> #include<ctype.h> #define swap(x,y,t) ((t)=(x),(x)=(y),(y)=(t)) #define N 26 char string[N]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; int num[N]; typedef struct { int weight; int parent,lchild,rchild; }HTnode,*hafumantree; typedef char **hufumanode; hafumantree HT; hufumanode HC; int small(int x)/*求最小权值*/ { int i=0,flag,k=32767; for(;i<=x;i++) if(HT[i].weight<k&&HT[i].parent==0) { k=HT[i].weight; flag=i; } HT[flag].parent=1; return flag; } void select(int i,int *s1,int *s2)/*选择s1,s2,为最小的两个权值(s1<s2)*/ { int temp; *s1=small(i); *s2=small(i); if(*s1>*s2) swap(*s1,*s2,temp); } void hufumancoding(int *w,int n)/*编译码*/ { int m,i,s1,s2,f,c,start; hafumantree p; char *code; m=2*n-1; HT=(HTnode *)malloc(m*sizeof(HTnode)); if(!HT) { printf("\noverflow!"); exit(0); } p=HT; for(i=0;i<n;i++,......

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

线性表的基本操作(2005-05-08 13:47:00)

摘要:#include<stdio.h> #include<stdlib.h> typedef struct { int data; struct *next; }node,*link; link L; int  inital_linklist(link S) {link head,new; char ch; int num=1; head=NULL; S=(node *)malloc(sizeof(node)); if(!S) { printf("overflow!");   exit(0); } S->next=head; head=S; printf("please input the data:\n"); scanf("%d",&head->data); printf("continue?Y or N?\n"); scanf("%c",ch); while(ch!='N'||'n') { new=(node *)malloc(sizeof(node)); if(!new)   {   printf("overflow!");   exit(0); } new->next=head; head=new; printf("please input the data:\n"); scanf("%d",&head->data); num++; printf("continue?Y or N?\n"); scanf("%c",ch); } printf("succeed in create a list!\n"); return num; } void output_linklist(link S) { printf("the original elems in the list are:\n"); while(S->next!=NULL) {   ......

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