69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
int main(void)
{
int a[] = {1,2,4,7,8,9,10,12,16,18};
int b[] = {1,2,6,7,8,9,10,17,19,29};
int c[20] = {0};
int ACount = 0; //a数组中的位置
int BCount = 0; //b数组中的位置
int CCount = 0;
for(int k = 0; k < 20; k++)
{
if(a[ACount] < b[BCount])
{
c[CCount++] = a[ACount++];
continue;
}
else if(a[ACount] == b[BCount])
{
c[CCount++] = a[ACount++];
BCount++;
continue;
}
else
{
c[CCount++] = b[BCount++];
continue;
}
}
for(int i = 0; i < 20; i++)
printf("%d ",c[i]);
return 0;
}
#include <stdio.h>
#define M 10
#define N 10
void main(void)
{
int a[M] = {1,2,4,7,8,9,10,12,16,18};
int b[N] = {1,2,6,7,8,9,10,17,19,29};
int c[50];
int i=0,j=0,k=0;
while(i<M||j<N)
if(i<M&&j<N)
c[k++]=a[i]<b[j]?a[i++]:a[i]==b[j]?j++,a[i++]:b[j++];
else if(i>=M)
c[k++]=b[j++];
else
c[k++]=a[i++];
for(i=0;i<k;i++)
printf("%d ",c[i]);
}
int main(void)
{
int a[] = {1,2,4,7,8,9,10,12,16,18};
int b[] = {1,2,6,7,8,9,10,17,19,29};
int c[sizeof(a) / sizeof(a[0]) + sizeof(b) / sizeof(b[0])];
int ACount = 0; //a数组中的位置
int BCount = 0; //b数组中的位置
int CCount = 0;
while (ACount != sizeof(a) / sizeof(a[0]) && BCount != sizeof(b) / sizeof(b[0]))
{
if(a[ACount] < b[BCount])
{
c[CCount++] = a[ACount++];
}
else if(a[ACount] == b[BCount])
{
c[CCount++] = a[ACount++];
BCount++;
}
else
{
c[CCount++] = b[BCount++];
}
}
while (ACount != sizeof(a) / sizeof(a[0]))
{
c[CCount++] = a[ACount++];
}
while (BCount != sizeof(b) / sizeof(b[0]))
{
c[CCount++] = b[BCount++];
}
for(int i = 0; i < CCount; i++)
printf("%d ",c[i]);
system("pause");
return 0;
}