1、行程编码压缩算法
1 #include<stdio.h> 2 #include <string.h> 3 #define N 100 4 5 int main() 6 { 7 char s[N] = "", t[N] = "", ch; 8 gets(s); 9 10 int count = 0, index = 0; 11 for(int i=0; s[i]; ++i) 12 { 13 if(!count) 14 ch = s[i]; 15 count++; 16 if(ch!=s[i+1] || count==9){ 17 t[index++] = count+'0'; 18 t[index++] = ch; 19 count=0; 20 } 21 } 22 printf("%s",t); 23 return 0; 24 }
2、创建与遍历职工链表
1 #include <stdio.h> 2 #include <malloc.h> 3 #include <string.h> 4 #define N 100 5 6 typedef struct node EMPLOYEE,*LIST; 7 struct node{ 8 int num; 9 LIST next; 10 }; 11 12 void Insert(LIST head,int num) 13 { 14 LIST p = head, s; 15 s = (LIST)malloc( sizeof(EMPLOYEE) ); 16 s->num = num; //给p赋值 17 18 while(p->next != NULL && s->num >= p->next->num) 19 p = p->next; 20 //连接 21 s->next = p->next; //若s最大, p->next=NULL 22 p->next = s; 23 } 24 25 LIST Create() 26 { 27 //创建头结点 28 LIST head = (LIST)malloc(sizeof (EMPLOYEE)); 29 head->next = NULL; 30 31 int n,num; 32 scanf("%d",&n); 33 //插入n个职工号 34 while(n--) 35 { 36 scanf("%d",&num); 37 Insert(head,num); 38 } 39 return head; 40 } 41 42 void Print(LIST head) 43 { 44 LIST p = head; 45 while(p->next != NULL) 46 { 47 p = p->next; 48 printf("%d ",p->num); 49 } 50 } 51 52 int main() 53 { 54 LIST head = Create(); 55 Print(head); 56 return 0; 57 }
3、毕业设计论文打印
1 #include <stdio.h> 2 #include <string.h> 3 #define N 100 4 5 int main() 6 { 7 int arr[N] = {0}, n, index, count=0; 8 scanf("%d%d",&n,&index); 9 10 for(int i=0; i<n; ++i) 11 scanf("%d",&arr[i]); 12 13 for(int j=0; j<n; ++j){ 14 if(arr[j]>arr[index]) 15 count++; 16 } 17 printf("%d",++count); 18 return 0; 19 }
1 #include <stdio.h> 2 #include <malloc.h> 3 #include <string.h> 4 #define N 100 5 6 typedef struct QNode *Queue; 7 typedef int Position,ElementType; 8 struct QNode { 9 ElementType *Data; /* 存储元素的数组 */ 10 Position Front, Rear; /* 队列的头、尾指针 */ 11 int MaxSize; /* 队列最大容量 */ 12 }; 13 /* 循环链表队列 */ 14 Queue CreateQueue( int MaxSize ) 15 { 16 /* 1.创建一个队列Q */ 17 Queue Q = (Queue)malloc(sizeof(struct QNode)); 18 /* 2.队列数据赋初值 */ 19 Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); 20 Q->Front = Q->Rear = 0;/* 队列指针 */ 21 Q->MaxSize = MaxSize;/* 队列最大容量 */ 22 /* 3.返回空队列 */ 23 return Q; 24 } 25 26 int main() 27 { 28 int n, index, count=0; 29 scanf("%d%d",&n,&index); 30 /* 31 n个元素,最少需要n+1个空间 32 Q->Front 指针占据一个空间 33 Q->Front == Q->Rear 队列空 34 Q->Front == (Q->Rear+1)%Q->MaxSize 队列满 35 */ 36 Queue Q = CreateQueue(n+1); 37 index++;/* 因为开始下标0给指针占了,所以index所在的位置+1 */ 38 39 for(int i=0; i<n; ++i){ 40 Q->Rear = (Q->Rear+1)%Q->MaxSize;/* 尾指针后移 */ 41 scanf("%d",&Q->Data[Q->Rear]);/* 尾指针指向的位置输入元素 */ 42 } 43 44 for(int j=0; j<n; ++j)//遍历 45 { 46 if(Q->Data[(Q->Front+1)%Q->MaxSize]<=Q->Data[index]) 47 { 48 Q->Rear = (Q->Rear+1)%Q->MaxSize; 49 Q->Data[Q->Rear] = Q->Data[(Q->Front+1)%Q->MaxSize]; //入队尾 50 if(Q->Data[index]==Q->Data[(Q->Front+1)%Q->MaxSize]){ 51 index = Q->Rear;/*指定的打印位置到了队尾*/ 52 } 53 } 54 else{ 55 count++; /*指定的打印元素优先级高的++*/ 56 } 57 Q->Front =(Q->Front+1)%Q->MaxSize;//出队 58 } 59 60 printf("%d", ++count);//本身打印1小时 61 return 0; 62 }
4、火车站
1 #include <stdio.h> 2 #include <stdbool.h> 3 #include <malloc.h> 4 #define ERROR -1 5 #define N 100 6 7 typedef int ElementType; 8 typedef int Position; 9 struct SNode { 10 ElementType *Data; /* 存储元素的数组 */ 11 Position Top; /* 栈顶指针 */ 12 int MaxSize; /* 堆栈最大容量 */ 13 }; 14 typedef struct SNode *Stack;/* 栈指针 */ 15 16 /* 创建空栈 */ 17 Stack CreateStack( int MaxSize ) 18 { 19 Stack S = (Stack)malloc(sizeof(struct SNode)); 20 S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); 21 S->Top = -1; 22 S->MaxSize = MaxSize; 23 return S; 24 } 25 26 /* 栈满 */ 27 bool IsFull( Stack S ) 28 { 29 return (S->Top == S->MaxSize-1); 30 } 31 32 /* 入栈 */ 33 bool Push( Stack S, ElementType X ) 34 { 35 if ( IsFull(S) ) { 36 //printf("堆栈满"); 37 return false; 38 } 39 else { 40 S->Data[++(S->Top)] = X; 41 return true; 42 } 43 } 44 45 /* 栈空 */ 46 bool IsEmpty( Stack S ) 47 { 48 return (S->Top == -1); 49 } 50 51 /* 出栈 */ 52 ElementType Pop( Stack S ) 53 { 54 if ( IsEmpty(S) ) { 55 //printf("堆栈空"); 56 return ERROR; /* ERROR是ElementType的特殊值,标志错误 */ 57 } 58 else 59 return ( S->Data[(S->Top)--] ); 60 } 61 62 int main() 63 { 64 int n,a; 65 int arr[N] = {0}, i, index=0; 66 /* 1.A方向驶来的火车车厢n节 */ 67 scanf("%d",&n); 68 /* 2.车站至少可以停放n节车厢(任意多) */ 69 Stack s = CreateStack(n); 70 /* 3.出站顺序( 数组arr存放 ) */ 71 while(scanf("%d",&a) && a) 72 arr[index++] = a; 73 /* 4.A方向驶来的火车车厢顺序从1开始(in=1) */ 74 int in = 1; 75 /* 5.依次出站 */ 76 for(i=0; i<index; ++i) 77 { /*如果出站顺序号与车站车厢顺序号不一致 78 且A方向还有车厢,依次进站*/ 79 while(arr[i]!=s->Data[s->Top] && in<=n && Push(s,in)) 80 in++; 81 /* A方向没有车厢 82 且出站顺序号与车站车厢顺序号不一致 83 编排顺序不可能, 输出No, 退出*/ 84 if(in>n && arr[i]!=s->Data[s->Top]) 85 { 86 printf("No"); 87 break; 88 } 89 /* 车站有车厢, 出站 */ 90 if(!IsEmpty(s)) 91 Pop(s); 92 } 93 /* 6.上面的循环非break中断, 编排顺序正确, 输出Yes */ 94 if(i==index) 95 printf("Yes"); 96 return 0; 97 }
5、大数减法
1 #include<stdio.h> 2 #include<string.h> 3 int x[100]={0},y[100]={0},z[105]={0};//将数组元素全部初始化为0 4 void sub(int x[],int y[],int len) 5 { 6 int i,j; 7 for(i=0;i<len;i++) 8 { 9 if(x[i]>=y[i])//如果x[i]>=y[i],不用向前一位借1,可直接减 10 z[i]=x[i]-y[i]; 11 else //如果x[i]<y[i],向前一位借1,同时前一位应减1 12 { 13 z[i]=x[i]+10-y[i]; 14 x[i+1]=x[i+1]-1; 15 } 16 } 17 for(i=len-1;i>0;i--)//删除前缀0 18 { 19 if(z[i]==0) 20 len--; 21 else 22 break; 23 } 24 for(i=len-1;i>=0;i--) //倒序输出数组 25 printf("%d",z[i]); 26 printf(" "); 27 } 28 int main() 29 { 30 char a[100],b[100];//通过字符串对大数进行输入并储存 31 int len1,len2; 32 scanf("%s %s",a,b); 33 34 int i,j=0,k=0; 35 len1=strlen(a); 36 len2=strlen(b); 37 /*将两个字符串中的字符转化为数字,并倒序储存到数组中, 38 即字符串为123456,则数组为654321*/ 39 for(i=len1-1,j=0;i>=0;i--) 40 x[j++]=a[i]-'0'; 41 for(i=len2-1,k=0;i>=0;i--) 42 y[k++]=b[i]-'0'; 43 //若减数长度 > 被减数,正常减 44 if(len1>len2) 45 sub(x,y,len1); 46 else if(len1<len2) //若减数长度 < 被减数,被减数 减 减数 47 { 48 printf("-"); 49 sub(y,x,len2); 50 } 51 else //若减数长度 == 被减数,判断两个数的大小 52 { 53 for(i=len1-1;i>=0;i--)//判断每一位两个数的大小 54 { 55 if(x[i]==y[i]) 56 continue; 57 if(x[i]>y[i])//即减数大 58 { 59 sub(x,y,len1); 60 break; 61 } 62 if(x[i]<y[i])//即被减数大 63 { 64 printf("-"); 65 sub(y,x,len1); 66 break; 67 } 68 } 69 } 70 return 0; 71 }
6、精确乘幂
1 #include <stdio.h> 2 #include <string.h> 3 #define N 200 4 int main() 5 { 6 char numStr[10];/* 定义数字字符串 */ 7 /* 定义字符串长度幂int数组及下标int数字小数点后位数 */ 8 int i,j,len,n,numArray[N],index,numInt,digit; 9 memset(numArray, 0, sizeof(numArray)); 10 numInt = index = digit = 0; 11 12 /* 1.输入数字字符串幂次 */ 13 scanf("%s %d",numStr,&n); 14 len = strlen(numStr); 15 16 int flag = 1, zero = 0;//判断最后的无效'0',定义标记 17 /* 2.字符串倒序转int数组num */ 18 for(i = len-1;i >= 0;i--) 19 { 20 if(numStr[i] == '0' && flag) 21 { 22 zero++;//无效的'0' 23 continue; 24 } 25 else 26 { 27 flag = 0; 28 if(numStr[i] == '.') 29 { 30 digit = len-zero-i-1;//有效的小数点后位数 31 continue;//去掉小数点 32 } 33 } 34 numArray[index++] = numStr[i] - '0'; 35 } 36 37 /* 3.int数组转int */ 38 for(i = index-1;i >= 0;i--) 39 numInt = numInt*10 + numArray[i]; 40 41 /* 4.求幂 */ 42 for(i = 1; i < n; i++)//幂次(本身一次) 43 { 44 int carry = 0; 45 for(j = 0; j < N; j++)//int数字与int数组数字相乘 46 { 47 int tmp = numInt*numArray[j] + carry; 48 numArray[j] = tmp%10; 49 carry = tmp/10; 50 } 51 } 52 53 /* 5.去掉数组中无效的'0'(小数位控制) */ 54 for(i = N-1; numArray[i]==0 && i>n*digit; i--); 55 56 /* 6.打印结果 */ 57 for(j = i; j >= 0; j--) 58 { 59 if(j == n*digit-1)//小数位之前打印小数点, 无小数位-1 60 printf("."); 61 printf("%d",numArray[j]); 62 } 63 return 0; 64 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 char *strrev(char* p) 5 { 6 char *p1=p; 7 char *p2=p; 8 char ch; 9 while(*p1++); 10 p1--; 11 p1--; 12 13 while(p2 < p1) 14 { 15 ch = *p2; 16 *p2++ = *p1; 17 *p1-- = ch; 18 } 19 20 return(p); 21 } 22 23 //change the string to the big-int and save them in array 24 //返回值:小数点的位数 25 //src: the String 26 //number: the array 27 //length: string's length 28 int string2int(char * src, int * number, int length); 29 30 int string2int(char * src, int * number, int length) 31 { 32 int flag=0; 33 //define the buffer to store the data 34 char * str = (char *)malloc(sizeof(char)*(length+1)); 35 memset(str, '