
C/C++
Помогите решить задачу

#include <algorithm>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <numeric>
#include <set>
using namespace std;
struct Box {
int max;
int min;
int diff;
Box() : max(0), min(0), diff(0) {}
void setbox(int a, int b) {
max = a > b? a : b;
min = a < b? a : b;
diff = max - min;
}
friend bool operator<(const Box& a, const Box& b) {
return a.diff < b.diff;
}
};
int maxsum(const string& path) {
static const auto x = 3;
ifstream file(path);
if (file.is_open()) {
int length;
file >> length;
vector<Box> box;
Box next;
int a, b;
while (file >> a >> b) {
next.setbox(a, b);
box.emplace_back(next);
}
file.close();
auto lambda = [](int s, const Box& b) { return s + b.max; };
auto smx = accumulate(box.begin(), box.end(), 0, lambda);
if (smx % x) return smx;
set<int> diff;
for (const auto& [mx, mn, dx] : box) diff.insert(dx);
auto mx = *max_element(diff.begin(), diff.end());
for (int i = 1; i < mx; ++i) {
if (diff.count(i)) {
auto n = smx - i;
if (n % x) return n;
}
}
} else {
puts("The specified path was not found!");
}
return 0;
}
int main() {
const auto x = 3;
auto a = maxsum("a.txt");
auto b = maxsum("b.txt");
cout << a << ' ' << b << endl;
system("pause > nul");
}
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <numeric>
#include <set>
using namespace std;
struct Box {
int max;
int min;
int diff;
Box() : max(0), min(0), diff(0) {}
void setbox(int a, int b) {
max = a > b? a : b;
min = a < b? a : b;
diff = max - min;
}
friend bool operator<(const Box& a, const Box& b) {
return a.diff < b.diff;
}
};
int maxsum(const string& path) {
static const auto x = 3;
ifstream file(path);
if (file.is_open()) {
int length;
file >> length;
vector<Box> box;
Box next;
int a, b;
while (file >> a >> b) {
next.setbox(a, b);
box.emplace_back(next);
}
file.close();
auto lambda = [](int s, const Box& b) { return s + b.max; };
auto smx = accumulate(box.begin(), box.end(), 0, lambda);
if (smx % x) return smx;
set<int> diff;
for (const auto& [mx, mn, dx] : box) diff.insert(dx);
auto mx = *max_element(diff.begin(), diff.end());
for (int i = 1; i < mx; ++i) {
if (diff.count(i)) {
auto n = smx - i;
if (n % x) return n;
}
}
} else {
puts("The specified path was not found!");
}
return 0;
}
int main() {
const auto x = 3;
auto a = maxsum("a.txt");
auto b = maxsum("b.txt");
cout << a << ' ' << b << endl;
system("pause > nul");
}
Похожие вопросы
- Помогите решить задачу по программированию на C++
- Помогите решить задачу по C++!
- Помогите решить задачу по программированию
- Помогите решить задачу пожалуйста, в C++
- Помогите решить задачу на c++
- Помогите решить задачу на С++ (мне не совсем ясен смысл задания)
- Помогите решить задачу на С++, используя статические массивы
- Товарищи программисты,помогите решить задачу для 1 курса .
- Помогите решить задачу в С++
- Помогите решить задачу на С++