Pagini recente » Cod sursa (job #48428) | Cod sursa (job #1177) | Cod sursa (job #2556110) | Cod sursa (job #2536036) | Cod sursa (job #3036613)
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream f ("consecutive.in");
ofstream g ("consecutive.out");
vector<pair<int,int>> find_consecutive_sum(int N) {
vector<pair<int,int>> solutions;
int start = 1;
while (start <= N / 2) {
int end = start + 1;
int total = start + end;
while (total <= N) {
if (total == N) {
solutions.push_back(make_pair(start, end));
break;
}
end++;
total += end;
}
start++;
}
return solutions;
}
int main() {
int T;
f >> T;
while (T){
int N;
f >> N;
vector<pair<int,int>> solutions = find_consecutive_sum(N);
sort(solutions.begin(), solutions.end(), [](pair<int,int> a, pair<int,int> b) {
return b.second - b.first < a.second - a.first;
});
g << solutions.size() << endl;
for (auto it = solutions.rbegin(); it != solutions.rend(); it++) {
g << it->first << " " << it->second << endl;
}
T--;
}
return 0;
}