永发信息网

用C语言编写一个对给定的两个数组进行重新排列的程序

答案:3  悬赏:0  手机版
解决时间 2021-01-28 15:27
  • 提问者网友:遮云壑
  • 2021-01-28 00:00
#include "stdio.h"
main()
{
int a[]={1,2,3,4,5},b[]={3,5,7,9,10,11},c[10]={0},i=0,j=0,k=0;
clrscr();
while(i<5&&j<6)
{
if(a[i]>b[j]) {c[k]=b[j];k++;j++;}
if(a[i]==b[j]) break;
else {c[k]=a[i];k++;i++;}
}
while(j<6)
{c[k+1]=b[j];k++;j++;}

for(k=0;k<10;k++)
printf("%d ",c[k]);
}

这样写错在哪里了?
最佳答案
  • 五星知识达人网友:归鹤鸣
  • 2021-01-28 00:25
#include "conio.h" //要用clrscr();必要要有这个头文件

#include "stdio.h"
main()
{
int a[]={1,2,3,4,5},b[]={3,5,7,9,10,11},c[10]={0},i=0,j=0,k=0;
clrscr();
while(i<5&&j<6)
{
if(a[i]>b[j]) {c[k]=b[j];k++;j++;}
if(a[i]==b[j]) break; //这里应该是else if(a[i]==b[j]) {c[k]=b[j];k++;j++;i++} 不能break;
else {c[k]=a[i];k++;i++;}
}
while(j<6)
{c[k+1]=b[j];k++;j++;} //这里应该是{c[k]=b[j];k++;j++;} 因为k已经在之前自加过了

for(k=0;k<10;k++)
printf("%d ",c[k]);
}
全部回答
  • 1楼网友:过活
  • 2021-01-28 01:00
#include #include #define na 5 #define nb 5void sort(int a[], int nlen) { for(int i = 0; i < nlen - 1; i++) { int n = i; int p = a[i]; for(int j = i + 1; j < nlen; j++) { if(a[j] < p) { p = a[j]; n = j; } } int t = a[i]; a[i] = a[n]; a[n] = t; } }int* merge(int a[], int nlena, int b[], int nlenb) { int* c = (int*)malloc((nlena + nlenb)*sizeof(int)); int i = 0; int j = 0; int n =0; while (i < nlena && j < nlenb) { if (a[i] <= b[j]) { c[n++] = a[i++]; } else { c[n++] = b[j++]; } } if (i < nlena) { for (; i < nlena; i++) { c[n++] = a[i]; } } if (j < nlenb) { for (; j < nlenb; j++) { c[n++] = b[j]; } } return c; }void main() { int a[na] = {0}; int b[nb] = {0}; printf("input %d number for a[]: ", na); for (int i = 0; i < na; i++) { scanf("%d", &a[i]); } printf("input %d number for b[]: ", nb); for (i = 0; i < nb; i++) { scanf("%d", &b[i]); } sort(a, na); sort(b, nb); printf("a[] after sort: "); for (i = 0; i < na; i++) { printf("%d ", a[i]); } printf("\nb[] after sort: "); for (i = 0; i < nb; i++) { printf("%d ", b[i]); } int *c = merge(a, na, b, nb); printf("\na[] b[] after merge: "); for (i = 0; i < na + nb; i++) { printf("%d ", c[i]); } printf("\n"); }
  • 2楼网友:西风乍起
  • 2021-01-28 00:46
while(i<5 && j<6) { if(a[i]>b[j]) {c[k]=b[j];k++;j++;} else if(a[i]==b[j]) {c[k]=b[j];k++;j++;i++;} else {c[k]=a[i];k++;i++;} } while(j<6) {c[k]=b[j];k++;j++;}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯