华为软件编程题:排序为倒锥形数组

  1 /* centre_sort.cpp
  2  * 给定一个数组input[] ,如果数组长度n为奇数,
  3  * 则将数组中最大的元素放到 output[] 数组最中间的位置,
  4  * 如果数组长度n为偶数,则将数组中最大的元素放到
  5  * output[] 数组中间两个位置偏右的那个位置上,
  6  * 然后再按从大到小的顺序,依次在第一个位置的两边,
  7  * 按照一左一右的顺序,依次存放剩下的数。
  8  */
  9 
 10 #include <stdio.h>
 11 #include <stdlib.h>
 12 
 13 void sort(int n, int input[], int output[])
 14 {
 15     if (n <= 0 || input == NULL)
 16     {
 17         printf("Date Error!\n"); 
 18         return; 
 19     }
 20 
 21     //先对输入数组从大到小排序
 22     //冒泡排序法
 23     int i, j, k; 
 24     for (i = 0; i < n - 1; i++)
 25     {
 26         for (j = 0; j < n - 1 - i; j++)
 27         {
 28             if (input[j] < input[j + 1])
 29             {
 30                 k = input[j + 1]; 
 31                 input[j + 1] = input[j]; 
 32                 input[j] = k; 
 33             }
 34         }
 35     }
 36 
 37     //找出n个数的中间位置
 38     if (n % 2 == 0)
 39     {
 40         j = (n - 1) / 2 + 1; 
 41     }
 42     else if (n % 2 == 1)
 43     {
 44         j = (n + 1) / 2 - 1; 
 45     }
 46 
 47     //将input数组中的数放到output数组中合适位置
 48     for (i = 0; i < n; i++)
 49     {
 50         if (i % 2 == 0)
 51         {
 52             j += i; 
 53         }
 54         else if (i % 2 == 1)
 55         {
 56             j -= i; 
 57         }
 58         output[j] = input[i]; 
 59     }
 60     
 61 }
 62 
 63 int main()
 64 {
 65     int n = 0; 
 66     int i = 0; 
 67     int input_flag = 0; 
 68     int MAX = 2 ^ (8 * sizeof(int) - 1); 
 69     printf("How many numbers do you want to input : "); 
 70     scanf("%d", &n); 
 71 
 72     int *input = (int *)malloc(n * sizeof(int)); 
 73     int *output = (int *)malloc(n * sizeof(int)); 
 74     printf("Please input the array : "); 
 75     for (i = 0; i < n; i++)
 76     {
 77         scanf("%d", &input[i]); 
 78         if (input[i] > MAX || input[i] < (0 - MAX -1))
 79         {
 80             printf("The number is too large!\n"); 
 81             return 0; 
 82         }
 83     }
 84 
 85     sort(n, input, output); 
 86     printf("The sorted array is : "); 
 87     for (i = 0; i < n; i++)
 88     {
 89         printf("%d ", output[i]); 
 90     }
 91     printf("\n"); 
 92     return 0; 
 93 }
 94 
 95 
 96 //以上为自己写的,欢迎指正
 97 //下面为网上流传的答案
 98 
 99 //#include <stdio.h>
100 //#include <string.h>
101 //#include <conio.h>
102 //
103 //void sort(int input[], int n, int output[])
104 //{
105 //    int i, j; 
106 //    int k = 1; 
107 //    int temp; 
108 //    int med; 
109 //    for(i = 0; i < n - 1; i++)
110 //    {
111 //        for(j = 0; j < n - 1 - i; j++)
112 //        {
113 //            if(input[j] > input[j + 1])
114 //            {
115 //                temp = input[j]; 
116 //                input[j] = input[j + 1]; 
117 //                input[j + 1] = temp; 
118 //            }
119 //        }
120 //    }
121 //    if(n % 2 != 0)
122 //    {
123 //        for(i = 0; i < n; i++)
124 //        //printf("%2d", input[i]); 
125 //        //printf("\n"); 
126 //        med = (n - 1) / 2; 
127 //        output[med] = input[n - 1]; 
128 //        for(i = 1; i <= med; i++)
129 //        {
130 //            output[med - i] = input[n - 1 - k]; 
131 //            output[med + i] = input[n - 2 - k]; 
132 //            k = k + 2; 
133 //        }
134 //    }
135 //    else
136 //    {
137 //                
138 //        for(i = 0; i < n; i++)
139 //        //printf("%2d", input[i]); 
140 //        //printf("\n"); 
141 //        med = n / 2; 
142 //        output[med] = input[n - 1]; 
143 //        for(i = 1; i <= med - 1; i++)
144 //        {
145 //            output[med - i] = input[n - 1 - k]; 
146 //            output[med + i] = input[n - 2 - k]; 
147 //            k = k + 2; 
148 //        }
149 //        output[0] = input[0]; 
150 //    }
151 //    for(i = 0; i < n; i++)
152 //    {
153 //        printf("%2d", output[i]); 
154 //    }
155 //    printf("\n"); 
156 //}
157 //
158 //int main()
159 //{
160 //    int a[6] = {3, 6, 1, 9, 7, 8}; 
161 //    int b[6] = {0}; 
162 //    for(int i = 0; i < 6; i++)
163 //    {
164 //        printf("%2d", a[i]); 
165 //    }
166 //    printf("\n"); 
167 //    sort(a, 6, b); 
168 //    return 0; 
169 //}

 

posted @ 2013-09-08 17:09  安迪Lee  阅读(532)  评论(0编辑  收藏  举报