user-0 提问: 请教一个OJ题，大佬请看看

回复:
user: user-1
content: 希望对你有帮助：https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助：https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-2
content: 供参考：#include <stdio.h> int main() {     int n;     scanf("%d",&n);     //int a[n+10]; //数组的定义非法，会报错。     //int o[n];     //int ji[n];     int i;     int j;     int k,l=2;     if(n%2==0)l=0;     //k=l=0;      int *a = new int[n];//动态数组     memset(a,0,sizeof(int)*n);//数组初始化      for (i=0;i<n;i++){     //输入数据          scanf("%d",&a[i]);     }      for (i=0;i<n;i+=2){   //下标奇数位排序，升序          for (j=0;j<n-i-2;j+=2){               if (a[j]>a[j+2]){                        k = a[j];                     a[j] = a[j+2];                    a[j+2]= k;               }          }     }     for (i=1;i<n;i+=2){     //下标偶数位排序，降序          for (j=1;j<n-i-l;j+=2){                 if (a[j]<a[j+2]){                        k = a[j];                     a[j] = a[j+2];                    a[j+2]= k;               }          }     }     for (i=0;i<n;i++){        //输出          printf("%d%c",a[i],i<n-1?' ':'\n');     }      delete []a;          return 0;      /*while(scanf("%d",&a[i])!=EOF)          {            if(a[i]==0)              {                 break;              }            if((i+1)%2==0)              {                o[k]=a[i];                k++;              }            if((i+1)%2!=0)              {                ji[l]=a[i];                l++;              }            i++;          }     int g;     for(i=0;i<k;i++)        {         for(j=i;j<k;j++)            {              g=o[i];              if(g<o[j])                {                  o[i]=o[j];                  o[j]=g;                }            }        }     int g1;     for(i=0;i<l;i++)        {          for(j=i;j<l;j++)             {               g1=ji[i];               if(g1>ji[i])                 {                   ji[i]=ji[j];                   ji[j]=g1;                 }             }        }     int u=l;     int p=0;     while(j>0&&k>0)        {           printf("%d %d ",ji[p],o[p]);           p++;           j--;           k--;        }     if(n%2!=0)        {           printf("%d",ji[u-1]);        } */ }  //8 //2 6 7 1 3 8 9 4 //2 8 3 6 7 4 9 1 //请按任意键继续. . .  //7 //1 2 3 4 5 6 7 //1 6 3 4 5 2 7 //请按任意键继续. . .  //6 //1 2 3 4 5 6 //1 6 3 4 5 2 //请按任意键继续. . .

user: user-0
content: 引用 1 楼 qzjhjxj 的回复：供参考：#include <stdio.h> int main() {     int n;     scanf("%d",&n);     //int a[n+10]; //数组的定义非法，会报错。     //int o[n];     //int ji[n];     int i;     int j;     int k,l=2;     if(n%2==0)l=0;     //k=l=0;      int *a = new int[n];//动态数组     memset(a,0,sizeof(int)*n);//数组初始化      for (i=0;i<n;i++){     //输入数据          scanf("%d",&a[i]);     }      for (i=0;i<n;i+=2){   //下标奇数位排序，升序          for (j=0;j<n-i-2;j+=2){               if (a[j]>a[j+2]){                        k = a[j];                     a[j] = a[j+2];                    a[j+2]= k;               }          }     }     for (i=1;i<n;i+=2){     //下标偶数位排序，降序          for (j=1;j<n-i-l;j+=2){                 if (a[j]<a[j+2]){                        k = a[j];                     a[j] = a[j+2];                    a[j+2]= k;               }          }     }     for (i=0;i<n;i++){        //输出          printf("%d%c",a[i],i<n-1?' ':'\n');     }      delete []a;          return 0;      /*while(scanf("%d",&a[i])!=EOF)          {            if(a[i]==0)              {                 break;              }            if((i+1)%2==0)              {                o[k]=a[i];                k++;              }            if((i+1)%2!=0)              {                ji[l]=a[i];                l++;              }            i++;          }     int g;     for(i=0;i<k;i++)        {         for(j=i;j<k;j++)            {              g=o[i];              if(g<o[j])                {                  o[i]=o[j];                  o[j]=g;                }            }        }     int g1;     for(i=0;i<l;i++)        {          for(j=i;j<l;j++)             {               g1=ji[i];               if(g1>ji[i])                 {                   ji[i]=ji[j];                   ji[j]=g1;                 }             }        }     int u=l;     int p=0;     while(j>0&&k>0)        {           printf("%d %d ",ji[p],o[p]);           p++;           j--;           k--;        }     if(n%2!=0)        {           printf("%d",ji[u-1]);        } */ }  //8 //2 6 7 1 3 8 9 4 //2 8 3 6 7 4 9 1 //请按任意键继续. . .  //7 //1 2 3 4 5 6 7 //1 6 3 4 5 2 7 //请按任意键继续. . .  //6 //1 2 3 4 5 6 //1 6 3 4 5 2 //请按任意键继续. . .谢谢指教

user: user-3
content: 我的VS2017 Community前些天明明允许像楼主那样定义数组的，可是今天不知道为啥就说不行了，必须得用常量云云。

user: user-4
content: #include <stdio.h> #include <stdlib.h>   void bubble_sort(int *arr, int len); int main() {  #if 1     int n, *arr, i;      scanf("%d", &n);      arr = (int *)malloc(sizeof(int) * (n + 1));     if (!arr)         return -1;      for (i = 1; i < n+1; i++)         scanf("%d", &arr[i]);      bubble_sort(arr+1, n);      for (i = 1; i < n+1; i += 1)         printf("%d ", arr[i]);     putchar(10);      for (i = 1; i < n+1; i += 2)         printf("%d ", arr[i]);     putchar(10);     for (i = 2; i < n+1; i += 2)         printf("%d ", arr[i]);     putchar(10);   #else     int n;     scanf("%d",&n);     int a[n+10];     int i=0;     int j;     int o[n];     int ji[n];     int k,l;     k=l=0;     while(scanf("%d",&a[i])!=EOF)     {         if(a[i]==0)         {             break;         }         if((i+1)%2==0)         {             o[k]=a[i];             k++;         }         if((i+1)%2!=0)         {             ji[l]=a[i];             l++;         }         i++;     }     int g;     for(i=0;i<k;i++)     {         for(j=i;j<k;j++)         {             g=o[i];             if(g<o[j])             {                 o[i]=o[j];                 o[j]=g;             }         }     }     int g1;     for(i=0;i<l;i++)     {         for(j=i;j<l;j++)         {             g1=ji[i];             if(g1>ji[i])             {                 ji[i]=ji[j];                 ji[j]=g1;             }         }     }     int u=l;     int p=0;     while(j>0&&k>0)     {         printf("%d %d ",ji[p],o[p]);         p++;         j--;         k--;     }     if(n%2!=0)     {         printf("%d",ji[u-1]);     } #endif     return 0; }  void bubble_sort(int *arr, int len) {     int i, j;     int even_flag, odd_flag ,tmp;      for (i = 0; i < len; i++) {         for (j = 0; j < len; j++) {             if (j + 2 < len) {                 if (j % 2 == 0 && arr[j] > arr[j+2])                     odd_flag = 1;                 if (j % 2 == 1 && arr[j] < arr[j+2])                     even_flag = 1;                  if (odd_flag || even_flag) {                     tmp = arr[j];                     arr[j] = arr[j+2];                     arr[j+2] = tmp;                     odd_flag = even_flag = 0;                 }             }         }     } } 供参考～

user: user-5
content: #include <assert.h> #include <stdio.h> #include <stdlib.h>  static int cmp1(const void *p1, const void *p2) {     int *v1;     int *v2;      v1 = (int *)p1;     v2 = (int *)p2;     return (*v1) - (*v2); }  int cmp2(const void *p1, const void *p2) {     int *v1;     int *v2;      v1 = (int *)p1;     v2 = (int *)p2;     return (*v2) - (*v1); }  int main(void) {     int   i;     int   n;     int   a;     int   n1;     int   n2;     int   a1[500];     int   a2[500];      for (;;) {         assert(1 == scanf("%d", &n));         if (0 == n)             break;          n1 = 0;         n2 = 0;         for (i = 1; i <= n; i++) {             assert(1 == scanf("%d", &a));             if (i % 2 == 1) {                 /* 下标为奇数 */                 a1[n1++] = a;             } else {                 /* 下标为偶数 */                 a2[n2++] = a;             }         }          qsort(a1, n1, sizeof(int), cmp1);         qsort(a2, n2, sizeof(int), cmp2);          printf("%d", a1[0]);         n1 = 1;         n2 = 0;         for (i = 2; i <= n; i++) {             if (i % 2 == 1) {                 printf(" %d", a1[n1++]);             } else {                 printf(" %d", a2[n2++]);             }         }         printf("\n");     }      return 0; }