求一道C语言题:已知数组a中有m个按升序排列的元素,数组b中有n个按降序排列的元素,编程将a与b中的所有元素按降序存入数组c中
求一道C语言题:已知数组a中有m个按升序排列的元素,数组b中有n个按降序排列的元素,编程将a与b中的所有元素按降序存入数组c中?
#include
#define M 5
#define N 4
void maoPao(int *, int, int)
void main()
{
int a[M], b[N], c[M + N], i, j
printf("请输入a的元素:
")
for (i = 0 i < M i++)
scanf("%d", &a[i])
maoPao(a, M, 1)
printf("a的元素升序后:
")
for (i = 0 i < M i++)
printf("%4d", a[i])
printf("
")
printf("请输入b的元素:
")
for (i = 0 i < N i++)
scanf("%d", &b[i])
maoPao(b, N, 0)
printf("b的元素降序后:
")
for (i = 0 i < N i++)
printf("%4d", b[i])
printf("
")
for (i = 0, j = 0 i < N + M i++)
{
if (i >= M)
c[i] = b[j++]
else
c[i] = a[i]
}
printf("C数组的元素:
")
for (i = 0 i < M + N i++)
printf("%4d", c[i])
printf("
")
maoPao(c, M + N, 0)
printf("C数组降序后:
")
for (i = 0 i < N + M i++)
printf("%4d", c[i])
getchar()
return 0
}
//zt = 0 时为降序, = 1 时升序,t 为数量
void maoPao(int *p, int t, int zt)
{
int i, j, temp
for (i = 0 i < t - 1 i++)
for (j = 0 j < t - 1 - i j++)
{
if (zt == 0)
{
if (p[j] > p[j + 1])
{
temp = p[j]
p[j] = p[j + 1]
p[j + 1] = temp
}
}
else
{
if (p[j] < p[j + 1])
{
temp = p[j]
p[j] = p[j + 1]
p[j + 1] = temp
}
}
}
}