如果您发现解答中出现错误、对于没有给出解答的题目你有好的答案或者解答有改进的地方,只要您觉得有问题的地方,您可以通过xiezhenjiang@foxmail.com和我联系 下面开始正题了:
2.1-1这题可以参照书上17自己给出过程,这里就略去了。
2.1-2 先给出书上insertion-sort的C源代码吧,然后再给出按照非升序的代码:
课本中(非降序的)insertion-sort代码:
- void insertion_sort(int *A, int n)
- {
- int i,j;
- int key;
- for(i = 1; i < n; i++)
- {
- j = i - 1;
- key = A[i];
- while(j >= 0 && A[j] > key)
- {
- A[j+1] = A[j];
- j = j - 1;
- }
- A[j+1] = key;
- }
- }
在这题中,只要讲非降序改成非升序排序,所以改后代码如下:
- void insertion_sort(int *A, int n)
- {
- int i,j;
- int key;
- for(i = 1; i < n; i++)
- {
- j = i - 1;
- key = A[i];
- while(j >= 0 && A[j] < key)
- {
- A[j+1] = A[j];
- j = j - 1;
- }
- A[j+1] = key;
- }
- }
2.1-3这题给出伪代码:
- int find(int *A , int n, int v)
- {
- int i = 0;
- for( ; i < n; i++)
- {
- if(v == A[i])
- return i;
- }
- return NIL;
- }
2.1-4直接给出代码:
- /*在A[]和B[]中,数组的最低位对应与二进制的高位,即如果一个二进制数是011100,用数组表示就是A[] = {0,1,1,1,0,0}*/
- void add(int *A ,int *B, int *C, int n)
- {
- int i, a, c = 0;
- int s;
- for(i = n - 1; i >= 0 ; i--)
- {
- s = A[i] + B[i];
- C[i+1] = (s + c) % 2;
- c = (s + c) / 2;
- }
- C[0] = c;
- }
转载于:https://blog.51cto.com/charlesxie/791083