72.假设一维数组中存放互不相同的十个整数,要求根据输入的下标值,即可直接删除.
1 #include <stdio.h> 2 3 int mydel (int *a, int n, int k) 4 { 5 int i; 6 7 for (i = k; i < n - 1; i++) 8 *(a+i) = *(a+i+1); 9 n--; 10 11 return n; 12 } 13 14 void myout (int *a, int n) 15 { 16 while (n > 0) 17 { 18 printf("%4d", *a); 19 a++,n--; 20 } 21 printf(" "); 22 } 23 24 void main (void) 25 { 26 int n, k, a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 27 printf("Input data: "); 28 scanf("%d", &k); 29 printf("Before delete: "); 30 myout(a, 10); 31 n = mydel(a, 10, k); 32 printf("After delete: "); 33 myout(a, n); 34 }
结果:
Input data: 3
Before delete: 1 2 3 4 5 6 7 8 9 10
After delete: 1 2 3 5 6 7 8 9 10
73.用选择法将10个数按由小到大的顺序进行排序.
1 #include <stdio.h> 2 3 void mysort (int *p, int n) 4 { 5 int i, j, k, t; 6 7 for (i = 0; i < n - 1; i++) 8 { 9 k = i; 10 for (j = k + 1; j < n; j++) 11 if (*(p+k) > *(p+j)) 12 k = j; 13 t = *(p+i); 14 *(p+i) = *(p+k); 15 *(p+k) = t; 16 } 17 } 18 19 void myout (int *p, int n) 20 { 21 while (n > 0) 22 { 23 printf("%4d", *p); 24 p++,n--; 25 } 26 printf(" "); 27 } 28 29 void main (void) 30 { 31 int a[10] = {10, 9, 8, 2, 5, 1, 7, 3, 4, 6}; 32 33 printf("Before sort: "); 34 myout(a, 10); 35 mysort(a, 10); 36 printf("After sort: "); 37 myout(a, 10); 38 }
结果:
Before sort: 10 9 8 2 5 1 7 3 4 6
After sort: 1 2 3 4 5 6 7 8 9 10
74.输入字符串,调用函数将字符倒置.
1 #include <stdio.h> 2 3 void myfun (char *p); 4 5 void main (void) 6 { 7 char a[50]; 8 printf("Input data: "); 9 gets(a); 10 myfun(a); 11 printf("After: "); 12 puts(a); 13 } 14 15 void myfun(char *p) 16 { 17 char *q = p, t; 18 while (*q != '