内容目录
#include<iostream> #include<sstream> #include<set> #include<vector> using namespace std; struct sett { std::vector<int> vv; std::set<int> s; bool flag; }; int caljiao(sett& a, sett& b) { int num = 0; set<int>::iterator it; it = a.s.begin(); set<int>::iterator it2; for (; it != a.s.end() ; it++) { for (it2 = b.s.begin(); it2 != b.s.end(); it2++) if (*it == *it2) num++; } return num; } void Print(sett& a) { std::vector<int>::iterator it; it = a.vv.begin(); cout << *it; it++; for (; it != a.vv.end(); it++) { cout << " " << *it; } cout << endl; } std::vector<sett> v; int main() { freopen("/Users/sky/Documents/sublime project/in.txt", "r", stdin); char s[10000]; int id = 0; std::vector<int> sortRe; while (gets(s) && s[0] != 'n') { sett nset; stringstream in; in << s; int num; while (in >> num) { nset.s.insert(num); nset.vv.push_back(num); } nset.flag = false; v.push_back(nset); id++; } int pos = 1; int getpos = 1; sortRe.push_back(0); v[0].flag = true; for (int i = 1; i < id; i++) { sett &now = v[sortRe[i - 1]]; int nowmax = 0; getpos = i; for (int j = 1; j < id; j++) { int nowjiao = caljiao(now, v[j]); // printf("%d\n", nowjiao); if (v[j].flag == false && nowjiao > nowmax) { getpos = j; nowmax = nowjiao; // printf("%d %d\n", nowmax, getpos); } } if (v[getpos].flag == true) continue; v[getpos].flag = true; sortRe.push_back(getpos); } for (int j = 1; j < id; j++) { if(v[j].flag==false) sortRe.push_back(j); } for (int i = 0; i < id; i++) { Print(v[sortRe[i]]); // Print(v[i]); // printf("%d\n", sortRe[i]); } return 0; }
2 3 11
4 8
5 3
2 6 9
5 7
n
输出
2 3 11
5 3
5 7
2 6 9
4 8
具有相似元素的放一起 否则按照顺序排序
1
发发发
发发发