1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
template<typename T, size_t Sz> size_t SizeOf(T(&)[Sz]) { return Sz; }
struct Record {
int i, j, ai, bj, sum;
Record(int i, int j, int ai, int bj, int sum)
: i(i), j(j), ai(ai), bj(bj), sum(sum) {}
};
std::ostream& operator<<(std::ostream& os, const Record& o) {
auto W = std::setw;
return os << W(5) << o.i << W(5) << o.j
<< W(5) << o.ai << W(5) << o.bj
<< W(6) << o.sum;
}
int main() {
int A[] { 3, 5, 13, 14, 22, 23};
int B[] { 1, 2, 3, 6, 9, 10, 11, 13, 15, 18};
std::vector<Record> v;
for (int i = 0; i < (int)SizeOf(A); ++i)
for (int j = i; j < (int)SizeOf(B); ++j)
v.push_back(Record(i, j, A[i], B[j], A[i] + B[j]));
std::sort(v.begin(), v.end(),
[](const Record& a, const Record& b){
if (a.sum == b.sum) return a.i < b.i;
return a.sum < b.sum;
});
std::cout << " k i j A[i] B[j] sum\n";
int k = 0;
for (const auto& o: v)
std::cout << std::setw(3) << ++k << o << '\n';
std::cout << '\n';
}
|