题目描述
题目:输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分,奇数部分降序排列输出,偶数部分升序输出。
输入
输入:
8 13 25 18 56 11 77 98
输出
输出:
77 25 13 11 8 18 56 98
样例输入
8 13 25 18 56 11 77 98
样例输出
77 25 13 11 8 18 56 98
参考代码
#include <stdio.h>
#include <stdlib.h>
#define Stack_Size 100
#define MAX 500
typedef struct
{
int *top;
int *base;
int stacklength;
}
Sqstack,*Pstack;
void InitStack(Pstack S)
{
S->base=(int *)malloc( Stack_Size*sizeof(int));
if(!S->base)
exit(1);
S->top=S->base;
S->stacklength=Stack_Size;
}
void Push(Pstack S,int ch)
{
if(S->top-S->base>=Stack_Size)
exit(1);
*(S->top++)=ch;
}
void Pop(Pstack S)
{
if(S->base==S->top)
exit(1);
S->top--;
}
void Clear(Pstack S)
{
S->base=S->top;
}
int main()
{
int i,j,ch,k,s1,s2,flagx=0,flagy=0;
int x,y,z;
char t;
Sqstack S;
int *p;
int Array[MAX],B[MAX]={0},C[MAX]={0};
InitStack(&S);
scanf("%d",&ch);
Push(&S,ch);
while((t=getchar())!=EOF)
{
scanf("%d",&ch);
Push(&S,ch);
}
for (p=S.base,i=0;p<S.top;p++,i++)
Array[i]=*p;
for (j=0;j<i;j++)
{
if(Array[j]%2==1)
{
B[flagx]=Array[j];
flagx++;
} else
{
C[flagy]=Array[j];
flagy++;
}
}
for (x=0;x<flagx-1;x++)
for (y=0;y<flagx-x-1;y++)
if(B[y]<B[y+1])
{
z=B[y];
B[y]=B[y+1];
B[y+1]=z;
}
for (x=0;x<flagy-1;x++)
for (y=0;y<flagy-x-1;y++)
if(C[y]>C[y+1])
{
z=C[y];
C[y]=C[y+1];
C[y+1]=z;
}
for (x=0;x<=flagx-1;x++)
printf("%d ",B[x]);
for (x=0;x<=flagy-1;x++)
printf("%d ",C[x]);
printf("n");
return 0;
}
解析
暂无