编写C++程序:设整型数组a中按序存放有以下数据:2、4、5、8、12、14、16、18、20、30。从键盘任意输入一个整数插入a数组插入后该数组仍有序。输出插入后数组a中的内容。
#include <iostream>
using namespace std;
int main() {
int a[11] = {2, 4, 5, 8, 12, 14, 16, 18, 20, 30};
int n, i, j;
cout << "请输入一个整数:";
cin >> n;
for (i = 9; i >= 0; i--) {
if (a[i] > n) {
a[i+1] = a[i];
} else {
break;
}
}
a[i+1] = n;
cout << "插入后的数组为:";
for (j = 0; j < 11; j++) {
cout << a[j] << " ";
}
cout << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/bupb 著作权归作者所有。请勿转载和采集!