15、奇偶数分离
有一个整型偶数n(2<= n <=10000),你要做的是:先把1到n中的所有奇数从小到大输出,再把所有的偶数从小到大输出。
- 输入 第一行有一个整数i(2<=i<30)表示有 i 组测试数据;每组有一个整型偶数n。
- 输出 第一行输出所有的奇数。第二行输出所有的偶数
1、思路
遍历输出奇数,偶数存于数组中,输完奇数输出偶数
2、具体实现
#include<string>
#include<vector>
using namespace std;
int main()
{
int i = 0, n = 0;
cin >> i;
while (i-- && cin >> n)
{
if (n % 2 != 0) return 0;
vector<int>a;
for (int j = 1; j <= n; j++)
{
if (j % 2 != 0) cout << j << " ";
else a.push_back(j);
}
cout << endl;
for (auto it = a.begin(); it != a.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
return 0;
}