c++ 给定n个整数要求在所有的数字m后面插入q输出插入q之后的整数序列。
以下是一种可以实现此功能的 C++ 代码:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m, q;
vector<int> nums;
// 输入 n 和原始数字序列
cin >> n;
for (int i = 0; i < n; i++) {
cin >> m;
nums.push_back(m);
}
// 输入要插入的数字 q
cin >> q;
// 在每个数字后面插入 q,并输出结果
for (int i = 0; i < n; i++) {
cout << nums[i] << " " << q << " ";
}
cout << endl;
return 0;
}
代码思路:
首先使用 vector 存储输入的数字序列,然后输入要插入的数字 q。接着遍历原始数字序列,并在每个数字后面插入 q,最后输出结果
原文地址: http://www.cveoy.top/t/topic/hgUo 著作权归作者所有。请勿转载和采集!