C语言(数据结构,DEVFORGE学编程社区)

Posted on 2019-10-10 17:09  金色的省略号  阅读(554)  评论(0编辑  收藏  举报

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("\n");  
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, '\0', length+1);
 36     memcpy(str, src, length);
 37     /*  puts(str);*/
 38     strrev(str);
 39     /*  puts(str);*/
 40     //transfer string to int
 41     //int temp = *number; //原有的数据不为零时,也需要考虑
 42     int i, j;
 43     for (i=0, j=0; i<length; i++)
 44     {
 45         //temp *= 10;
 46         //原有数据为字符'A',顾需要减去'0'
 47         if (str[i]!='.')
 48         {
 49             number[j++] += (int)(str[i] - '0');
 50         }
 51         else
 52         {
 53             flag = j;
 54         }
 55 
 56     }
 57     //return the transfer number
 58     //*number = temp;
 59     //clear the dynamic memory
 60     free(str);
 61     str = NULL;
 62     return flag;
 63 }
 64 
 65 //大数相乘
 66 //乘数:numsA,numsB
 67 //长度:lenA, lenB
 68 int BigNumberMultiplication(int * numsA, int lenA, int * numsB, int lenB, int *
 69                             results);
 70 int BigNumberMultiplication(int * numsA, int lenA, int * numsB, int lenB, int * results)
 71 {
 72     int i, j;
 73     //multi procedure
 74     for (i=0; i<lenB; i++)
 75     {
 76         for (j=0; j<lenA; j++)
 77         {
 78             //the i,j place number multi, and the result is on the i+j place
 79             results[i+j] += numsA[j] * numsB[i];
 80         }
 81     }
 82     //process the result
 83     for (i=0; (i<lenA+lenB); i++)
 84     {
 85         if (results[i]>=10)  //the low place to the high place, if the low number is >=10       
 86         {
 87             results[i+1] += results[i] /10;
 88             results[i] %= 10;
 89         }
 90     }
 91     //calcute lenA
 92     for (i=lenA+lenB-1; i>=0; i--)
 93     {
 94         if (results[i] != 0)
 95         {
 96             break;
 97         }
 98     }
 99     return i+1;
100 }
101 
102 int main()
103 {
104     char str[7] = {'\0'};
105     int numsB[5] = {0};
106     int n;
107     scanf("%s %d", str, &n);
108     int length = strlen(str);
109     //获取小数点的位数
110     int PointPlace = string2int(str, numsB, length);
111     if (PointPlace!=0) // reduce the . nums
112     {
113         length--;
114     }
115 
116     int * pNumsA = (int *)malloc(length*n*sizeof(int));
117     int * pResults = (int *)malloc(length*n*sizeof(int));
118     memset(pNumsA, 0, length*n*sizeof(int));
119     memset(pResults, 0, length*n*sizeof(int));
120     memcpy(pNumsA, numsB, length*sizeof(int));
121     // int i;
122     // for (i=0; i<length; i++)
123     // {
124     //      printf("%d", pNumsA[i]);
125     // }
126     // printf("-%d\n", PointPlace);
127     int lenA = length;
128     /*printf("%d", lenA);*/
129     int i, j;
130     int bIsBegin = 0;
131     char * pString = (char *)malloc(sizeof(char)*length*n+2);
132     memset(pString, '\0', sizeof(char)*length*n+2);
133     if (n==1)
134     {
135         printf("%s", str);
136     }
137     else
138     {
139         //求幂
140         for (i=1; i<n; i++)
141         {
142             lenA = BigNumberMultiplication(pNumsA, lenA, numsB, length,
143                                            pResults);
144             memcpy(pNumsA, pResults, lenA*sizeof(int));
145             memset(pResults, 0, lenA*sizeof(int));
146             //printf("LenA---%d\n", lenA);
147         }
148 
149         //将结果由数组转化为字符串并添加小数点
150         i=0;
151         j = 0;
152         while (i<length*n)
153         {
154             if (pNumsA[i]==0)
155             {
156                 if (1==bIsBegin)
157                 {
158                     if ( i==(PointPlace*n) )
159                     {
160                         if (i!=0)
161                             pString[j++] = '.';
162                     }
163                     pString[j++] = pNumsA[i++] + '0';
164                 }
165                 else
166                     i++;
167 
168             }
169             else
170             {
171                 bIsBegin = 1; ////begin from the first non-zero value
172 
173                 if ( i==(PointPlace*n) )
174                 {
175                     if (i!=0)
176                     {
177                         pString[j++] = '.';
178                     }
179 
180                 }
181 
182                 pString[j++] = pNumsA[i++] + '0';
183             }
184         }
185 
186         free(pNumsA);
187         free(pResults);
188         //反转字符串:之前的字符串一直是倒叙的
189         strrev(pString);
190 
191         //输出结果:主要是处理小数点前0的个数
192         if (PointPlace==0)
193         {
194             for (i=0; i<length*n; i++)
195             {
196                 if (pString[i]!='0')
197                     break;
198             }
199             printf("%s", &pString[i]);
200         }
201         else
202         {
203             for (i=0; i<length*n; i++)
204             {
205                 if (pString[i]!='0')
206                 {
207                     if (pString[i]=='.')
208                     {
209                         i--;
210                         break;
211                     }
212                     else
213                     {
214                         break;
215                     }
216 
217                 }
218 
219             }
220             printf("%s", &pString[i]);
221         }
222     }
223 
224     free(pString);
225 
226     return 0;
227 }

7、大数除法

  1 #include<stdio.h>
  2 #include<string.h>
  3  
  4 char a[100],b[100];//用两个字符串用来输入两个大数
  5  
  6 int x[100],y[100],z[100],m[100];//被除数  除数  商  余数
  7  
  8 int digit;  //大数的位数
  9  
 10 void sub(int x[],int y[],int len1,int len2)//大数减法
 11 {
 12     int i;
 13     for(i=0;i<len1;i++)
 14     {
 15         if(x[i]<y[i])
 16         {
 17             x[i]=x[i]+10-y[i];
 18             x[i+1]--;
 19         }
 20         else
 21             x[i]=x[i]-y[i];
 22     }
 23     for(i=len1-1;i>=0;i--)//判断减法结束之后,被除数的位数
 24     {
 25         if(x[i])
 26         {
 27             digit=i+1;
 28             break;
 29         }
 30     }
 31 }
 32 int judge(int x[],int y[],int len1,int len2)
 33 {
 34     int i;
 35     if(len1<len2)
 36         return -1;
 37     if(len1==len2)//若两个数位数相等
 38     {
 39         for(i=len1-1;i>=0;i--)
 40         {
 41             if(x[i]==y[i])//对应位的数相等
 42                 continue;
 43             if(x[i]>y[i])//被除数 大于 除数,返回值为1
 44                 return 1;
 45             if(x[i]<y[i])//被除数 小于 除数,返回值为-1
 46                 return -1;
 47         }
 48         return 0;//被除数 等于 除数,返回值为0
 49     }
 50 }
 51 int main()
 52 {
 53     int i,j=0,k=0,temp;
 54     int len1,len2,len;//len两个大数位数的差值
 55  
 56     scanf("%s %s",a,b);
 57     //while(~scanf("%s %s",a,b))
 58     {
 59         len1=strlen(a);//被除数位数
 60         len2=strlen(b);//除数位数
 61  
 62         for(i=len1-1,j=0;i>=0;i--)//将字符串中各个元素倒序储存在数组中
 63         {
 64             x[j++]=a[i]-'0';
 65         }
 66         for(i=len2-1,k=0;i>=0;i--)
 67         {
 68             y[k++]=b[i]-'0';
 69         }
 70  
 71         if(len1<len2)//当被除数位数 小于 除数位数时
 72         {
 73             printf("商是:0\n");
 74             printf("余数是:");
 75             puts(a);
 76         }
 77         else //当被除数位数 大于或者等于 除数位数时
 78         {
 79             len=len1-len2;//两个大数位数的差值
 80             for(i=len1-1;i>=0;i--)//将除数后补零,使得两个大数位数相同。
 81             {
 82  
 83                 if(i>=len)
 84                     y[i]=y[i-len];
 85                 else
 86                     y[i]=0;
 87                 
 88             }
 89             len2=len1;//将两个大数数位相同
 90  
 91             digit=len1; //将原被除数位数赋值给digit
 92  
 93             for(j=0;j<=len;j++)
 94             {
 95                 z[len-j]=0;
 96  
 97                 while(((temp=judge(x,y,len1,len2))>=0)&&digit>=k)//判断两个数的大小以及被除数位数与除数原位数的关系
 98                 {
 99                     sub(x,y,len1,len2); //大数减法函数
100  
101                     z[len-j]++;//储存商的每一位
102  
103                     len1=digit;//重新修改被除数的长度
104  
105                     if(len1<len2&&y[len2-1]==0)
106                         len2=len1;  //将len1长度赋给len2;
107                 }
108  
109                 if(temp<0)//若被除数 小于 除数,除数减小一位。
110                 {
111                     for(i=1;i<len2;i++)
112                         y[i-1]=y[i];
113                     y[i-1]=0;
114                     if(len1<len2)
115                         len2--;
116                 }
117             }
118  
119             //printf("商是:");
120             for(i=len;i>0;i--)//去掉前缀0
121             {
122                 if(z[i])
123                     break;
124             }
125             for(;i>=0;i--)
126                 printf("%d",z[i]);
127             printf("\n");
128  
129             /* printf("余数是:");
130             for(i=len1;i>0;i--)
131             {
132                 if(x[i])
133                     break;
134             }
135             for(;i>=0;i--)
136                 printf("%d",x[i]);
137             printf("\n"); */
138         }
139     }
140     return 0;
141 }

8、插入链表结点

 1 #include <stdio.h> 
 2 #include <stdlib.h>
 3 #include <string.h>
 4 /* node */
 5 typedef struct staff *STAFF;
 6 struct staff
 7 {
 8     char  num[6];      //职工工号
 9     STAFF next;
10 };
11 /* makenode */
12 STAFF makenode()
13 {
14     STAFF p = (STAFF)malloc(sizeof(struct staff));
15     scanf("%s",p->num);
16     p->next = NULL;
17     return p;
18 }
19 /* create */
20 STAFF create(int m) 
21 {
22     STAFF head,p;
23     head = p = makenode();
24     while(--m)
25     {        
26         p->next = makenode();//尾插    
27         p = p->next;    
28     }
29     return head;
30 }
31 /* print */
32 void list(STAFF p)
33 {
34     while(p){
35         printf("%s ",p->num);
36         p= p->next;
37     }    
38     printf("\n");
39 }
40 /* insert */
41 STAFF insert(STAFF head,STAFF s)
42 {
43     STAFF p = head;    
44     while(p->next!=NULL&&strcmp(p->next->num,s->num)<0){
45         p=p->next;
46     } 
47     s->next=p->next;
48     p->next=s;      
49     return head;
50 }
51 int main()
52 {
53     int n;
54     char num[6];
55     scanf("%d",&n);
56     STAFF head = create(n);
57     STAFF s = makenode();
58     head = insert(head,s);   
59     list(head); //打印链表
60     return 0;
61 }

9、大数乘法

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define MAX 300
 4 
 5 unsigned an1[MAX+5] = {0};
 6 unsigned an2[MAX+5] = {0};
 7 unsigned a[2*MAX+10] = {0};
 8 char aa[MAX+10] = "";
 9 char b[MAX+10] = "";
10 int main()
11 {
12     gets(aa);//读入一个字符串 
13     gets(b);
14     int i,j;
15     int len_a = strlen(aa);
16     for(j=0,i=len_a-1;i>=0;i--)
17         an1[j++]=aa[i]-'0';//转换为10进制数字     
18     int len_b=strlen(b);    
19     for(j=0,i=len_b-1;i>=0;i--)
20         an2[j++]=b[i]-'0';
21     
22     for(i=0;i<len_b;i++){//这一步可以进行优化 但是很复杂 O(logn(1.59)) 
23         for(j=0;j<len_a;j++)
24             a[i+j]+=an2[i]*an1[j];//每一位都进行相加 m*n的复杂度 
25     }    
26     for(i=0;i<2*MAX;i++){
27         if(a[i]>=10){
28             a[i+1]+=a[i]/10;//由于进位还没有考虑进去 这样就有些数字大于10的 
29             a[i]%=10;//我们要进位 
30         }
31     }
32     bool b=false;
33     for(i=MAX*2;i>=0;i--)//下面就是输出了 一定要注意个位数字的下表更大 所以是从第一个非0数字开始的 
34         if(b)    
35             printf("%d",a[i]);
36         else if(a[i]){
37             printf("%d",a[i]);
38             b=true;
39         }
40     if(!b)//这一步太重要了 一定不要忘记 如果有一个数字是0的话 这个我就忘记了 一直wrong answer 几乎快崩溃了 
41         printf("0");
42     return 0;
43 }

10、Symmetric Sort

 1 #include<stdio.h>  
 2 #include<string.h>  
 3 #include<algorithm>  
 4 using namespace std;  
 5 struct ch{  
 6     int len; 
 7     char str[30];  
 8 }s[1000];  
 9 bool cmp(struct ch a,struct ch b){  
10     return a.len<b.len;  
11 }  
12 int main()
13 {  
14     int i,j,n;  
15     scanf("%d",&n); 
16     for(i=0;i<n;i++){  
17         scanf("%s",s[i].str);//这里s[i]前边加不加&符号都行   
18         s[i].len=strlen(s[i].str);  
19     }  
20     /* 这里还有一个stable_sort,用法和sort是一样的,
21     区别是stable_sort是稳定排序,对于相等的值次序不变   */
22     sort(s,s+n,cmp); 
23     for(i=0;i<n;i+=2)  
24          printf("%s ",s[i].str);  
25     if(i==n+1) j=n-2;//因为i变到n+1的时候,可以自己调试一下   
26     else j=n-1;  
27     for(;j>=0;j-=2)  
28         printf("%s ",s[j].str);  
29     printf("\n");
30     return 0;  
31 }