C++ 笔试汇总(9)
作者:互联网
暴力求解一个数组的所有组合
例:输入 a b c d
输出:a b c
a b d
a c d
b c d
若求解2种组合,则修改循环即可
#include<iostream>
#include<string>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
string n;
vector<string>ShuZu;
while (cin >> n)
{
ShuZu.push_back(n);
}
vector<string>::iterator ptr = ShuZu.begin();
for (vector<string>::size_type i = 0; i < ShuZu.size() - 2; i++)
{
for (vector<string>::size_type j = i + 1; j < ShuZu.size() - 1; j++)
{
for (vector<string>::size_type k = j + 1; k < ShuZu.size(); k++)
{
cout << *(ptr + i) << " " << *(ptr + j) << " " << *(ptr + k) << endl ;
}
}
}
system("pause");
}
标签:ShuZu,笔试,汇总,C++,++,vector,include,type,size 来源: https://blog.csdn.net/qq_41679776/article/details/100933054