leetcode_sum4

#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <vector>

using namespace std;


int main()
{
    int tar;
    cin >> tar;
    int a1[5] = { 1,2,3,-1,0 };
    vector<int>num(a1, a1 + 5);
    vector<vector<int> > res;
    sort(num.begin(), num.end());
    unordered_map<int, vector<pair<int, int> > >cace;
    for(int a=0;a<num.size();++a)
        for (int b = a + 1; b < num.size(); ++b)
        {
            cace[num[a] + num[b]].push_back(pair<int,int>(a, b));
        }
    for(int c=0;c<num.size();++c)
        for (int d = c + 1; d < num.size(); ++d)
        {
            int tem = tar - num[c] - num[d];
            auto k = cace.find(tem);
            if (k == cace.end())continue;
            else {
                const auto vec = cace[tem];
                for (int z = 0; z < vec.size(); ++z)
                {
                    if (c <= vec[z].second)continue;
                    res.push_back({ num[vec[z].first],num[vec[z].second],
                    num[c],num[d] });
                }
            }
        }
    for (auto i : res)
    {
        for (auto j : i)
        {
            cout << j << " ";
        }
        cout << endl;
    }
        
    system("pause");
    return 0;
}

 

posted @ 2017-05-28 22:33  babyking1  阅读(129)  评论(0编辑  收藏  举报