文章
47
粉丝
109
获赞
8
访问
29.6k
尝试不用链表,感觉要简单点
#include<stdio.h>
#include<string.h>
#include<math.h>
int main() {
int n1;
scanf("%d", &n1);
int s1[105];
for (int i = 0; i < n1; i++) {
scanf("%d", &s1[i]);
}
int n2;
scanf("%d", &n2);
int s2[105];
for (int i = 0; i < n2; i++) {
scanf("%d", &s2[i]);
}
int sum[10010];//存储所有的结果
//双指针
int i = 0;//第一个链表
int j = 0;//第二个链表
int k = 0;//总链表
while (i < n1 && j < n2) {
if (s1[i] < s2[j]) {
sum[k++] = s1[i];
i++;
}
else {
sum[k++] = s2[j];
j++;
}
}
while (i < n1) {
sum[k++] = s1[i];//把剩下的所有存入总的
i++;
}
while (j < n2) {
sum[k++] = s2[j];//把剩下的所有存入总的
j++;
}
for (int i = 0; i < n1 + n2; i++) {
if (i != (n1 + n2 - 1)) {
printf("%d ", sum[i]);
}
else {
printf("%d", sum[i]);
}
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发