赞
踩
题目描述:
两个数组,每个数组的长度均为3,输出合并排序后的数组示例:
输入:
2 4 6
1 3 5
输出:
1 2 3 4 5 6
#include <iostream>
using namespace std;
int main() {
int a[3], b[3], c[6];
for(int i = 0; i < 3; i++) {
cin >> a[i];
}
for(int i = 0; i < 3; i++) {
cin >> b[i];
}
int i = 0, j = 0, index = 0;
while(i < 3 && j < 3) {
if(a[i] < b[j]) {
c[index++] = a[i++];
}else {
c[index++] = b[j++];
}
}
while(i < 3) {
c[index++] = a[i++];
}
while(j < 3) {
c[index++] = b[j++];
}
for(int i = 0; i < 5; i++) {
cout << c[i] << " ";
}
cout << c[5] << endl;
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。