From 1924ca48dba6032592dcb856d9c6c969acacb665 Mon Sep 17 00:00:00 2001 From: Krosh Date: Sat, 10 May 2025 16:10:00 +0300 Subject: [PATCH] some more solves --- contests/1contest/3.cpp | 11 +++--- contests/1contest/6.cpp | 77 ++++++++++++++++++++++++++++++++++++++++ contests/1contest/6t.cpp | 66 ++++++++++++++++++++++++++++++++++ contests/3contest/1.cpp | 11 +++--- contests/4contest/3.cpp | 4 +-- contests/4contest/4.cpp | 0 contests/5contest/3.cpp | 30 +++++++--------- contests/6contest/3.cpp | 26 ++++++++++++++ contests/6contest/4.cpp | 16 +++++++++ contests/7contest/2.cpp | 14 ++++---- contests/8contest/2.cpp | 18 ---------- 11 files changed, 219 insertions(+), 54 deletions(-) create mode 100644 contests/1contest/6.cpp create mode 100644 contests/1contest/6t.cpp create mode 100644 contests/4contest/4.cpp create mode 100644 contests/6contest/4.cpp diff --git a/contests/1contest/3.cpp b/contests/1contest/3.cpp index 9f5ee07..0acd754 100755 --- a/contests/1contest/3.cpp +++ b/contests/1contest/3.cpp @@ -13,10 +13,15 @@ int main() { int old = c; c = getchar(); - if (!(c == EOF || !isdigit(c))) { + if (isdigit(c)) { continue; } + if (c == EOF) { + putchar(old); + break; + } + putchar(old); } else if (isdigit(c)) { is_num = true; @@ -28,9 +33,5 @@ int main() { c = getchar(); } - if (c != '\n') { - putchar('\n'); - } - return 0; } \ No newline at end of file diff --git a/contests/1contest/6.cpp b/contests/1contest/6.cpp new file mode 100644 index 0000000..7fbc956 --- /dev/null +++ b/contests/1contest/6.cpp @@ -0,0 +1,77 @@ +#include +#include + +/* +На плоскости даны две прямые. Каждая прямая задается парой точек, через которые она проходит. Требуется установить, пересекаются ли эти прямые, и найти координаты точки пересечения. +Input format + +Вводятся сначала координаты двух различных точек, через которые проходит первая прямая, а затем - координаты еще двух различных (но, быть может, совпадающих с первыми двумя) точек, через которые проходит вторая прямая. Координаты каждой точки - целые числа, по модулю не превышающие 1000. +Output format + +Если прямые не пересекаются, выведите одно число 0. Если прямые совпадают, выведите 2. Если прямые пересекаются ровно в одной точке, то выведите сначала число 1, а затем два вещественных числа - координаты точки пересечения с точностью не менее 5 знаков после десятичной точки. +Notes + +Пишите переиспользуемый код - у вас должны появится классы точки, прямой, функция получение точки пересечения. +*/ + +class Point { +public: + long double x, y; + + Point(long double x = 0, long double y = 0) : x(x), y(y) {} +}; + +class Line { +public: + long double a, b, c; + + Line(const Point& p1, const Point& p2) { + a = p1.y - p2.y; + b = p2.x - p1.x; + c = p1.x * p2.y - p2.x * p1.y; + } + + bool isParallel(const Line& other) const { + return std::abs(a * other.b - b * other.a) < 1e-9; + } + + bool isCoincide(const Line& other) const { + return isParallel(other) && std::abs(c - other.c) < 1e-9; + } + + Point intersection(const Line& other) const { + long double det = b * other.a - a * other.b; + + long double x = (a * other.c - c * other.a) / det; + long double y = (c * other.b - b * other.c) / det; + + return Point(x, y); + } + +}; + +int main() { + Point* p = new Point[4]; + for (int i = 0; i < 4; ++i) { + std::cin >> p[i].x >> p[i].y; + } + + Line line1(p[0], p[1]); + Line line2(p[2], p[3]); + + delete[] p; + + if (line1.isCoincide(line2)) { + std::cout << 2; + } else if (line1.isParallel(line2)) { + std::cout << 0; + } else { + Point intersection = line1.intersection(line2); + std::cout << std::fixed << std::setprecision(5); + std::cout << 1 << " " << intersection.x << " " << intersection.y; + } + + std::cout << std::endl; + + return 0; +} diff --git a/contests/1contest/6t.cpp b/contests/1contest/6t.cpp new file mode 100644 index 0000000..0023e76 --- /dev/null +++ b/contests/1contest/6t.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +using std::cout, std::cin, std::endl; + +class Point +{ +public: + double x; + double y; + Point(const double a = 0, const double b = 0) : x{ a }, y{ b } {} +}; +class Line +{ +public: + // m * x + n * y + k = 0; + double n; + double m; + double k; + Line(const Point& a, const Point& b) { + m = a.y - b.y; + n = b.x - a.x; + k = a.x * b.y - b.x * a.y; + } +}; + +Point* crossPoint(const Line& l_1, const Line& l_2) { + double det = l_1.m * l_2.n - l_2.m * l_1.n; + if (abs(det) < 1e-9) { + return nullptr; + } + + double x = (l_1.n * l_2.k - l_2.n * l_1.k) / det; + double y = (l_2.m * l_1.k - l_1.m * l_2.k) / det; + + return new Point(x, y); +} + +int main() { + double x1, x2, y1, y2; + std::cin >> x1 >> y1; + std::cin >> x2 >> y2; + Line l_1(Point(x1, y1), Point(x2, y2)); + + std::cin >> x1 >> y1; + std::cin >> x2 >> y2; + Line l_2(Point(x1, y1), Point(x2, y2)); + + Point* pnt = crossPoint(l_1, l_2); + if (pnt != nullptr) { + std::cout << std::fixed << std::setprecision(5); + std::cout << 1 << " " << pnt->x << " " << pnt->y << std::endl; + delete pnt; + return 0; + } + + double det1 = l_1.n * l_2.k - l_2.n * l_1.k; + double det2 = l_1.m * l_2.k - l_2.m * l_1.k; + + if (abs(det1) < 1e-9 && abs(det2) < 1e-9) { + std::cout << 2 << std::endl; + return 0; + } + std::cout << 0 << std::endl; +} diff --git a/contests/3contest/1.cpp b/contests/3contest/1.cpp index d29e0ca..e37fef2 100644 --- a/contests/3contest/1.cpp +++ b/contests/3contest/1.cpp @@ -1,9 +1,11 @@ #include #include +#include namespace numbers { class complex { - double c_re, c_im; + double c_re = 0; + double c_im = 0; public: complex(double r = 0, double i = 0) : c_re(r), c_im(i) {} explicit complex(const std::string& s) { @@ -22,10 +24,11 @@ namespace numbers { return sqrt(abs2()); } std::string to_string() const { - char buf[100]; - sprintf(buf, "(%.10g,%.10g)", c_re, c_im); + std::stringstream ss; + ss.precision(10); + ss << "(" << c_re << "," << c_im << ")"; - return std::string(buf); + return ss.str(); } complex& operator+=(const complex& other) { c_re += other.c_re; diff --git a/contests/4contest/3.cpp b/contests/4contest/3.cpp index d355271..3245e04 100644 --- a/contests/4contest/3.cpp +++ b/contests/4contest/3.cpp @@ -15,8 +15,8 @@ void process(const std::vector& v1, std::vector& v2) { iter = v2.erase(iter); } - num--; - iter--; + --num; + --iter; } return; diff --git a/contests/4contest/4.cpp b/contests/4contest/4.cpp new file mode 100644 index 0000000..e69de29 diff --git a/contests/5contest/3.cpp b/contests/5contest/3.cpp index 93fad66..4860956 100644 --- a/contests/5contest/3.cpp +++ b/contests/5contest/3.cpp @@ -12,17 +12,17 @@ struct Date { int M; int D; - Date(std::string s) { + Date(std::string& s) { std::stringstream ss; ss << s; - + char tmp; ss >> Y >> tmp >> M >> tmp >> D; } Date(int Y, int M, int D) : Y(Y), M(M), D(D) {} - bool operator<(const Date d) const { + bool operator<(const Date& d) const { if (Y < d.Y) return true; if (Y > d.Y) return false; @@ -30,7 +30,7 @@ struct Date { if (M > d.M) return false; if (D < d.D) return true; - + return false; } @@ -40,18 +40,18 @@ struct Student { std::map scores; std::string name; - Student(std::string name) : name(name) {} + Student(std::string& name) : name(name) {} Student(const Student& s) { scores = s.scores; name = s.name; } - bool operator==(const std::string n) { + bool operator==(const std::string& n) { return n == name; } - bool operator<(const Student st) const { + bool operator<(const Student& st) const { return name < st.name; } }; @@ -65,7 +65,7 @@ int main() { while (std::cin >> name >> date >> grade) { std::set::iterator iter = studs.find(name); Date d(date); - + if (iter == studs.end()) { Student s(name); s.scores[d] = grade; @@ -80,25 +80,19 @@ int main() { dates.insert(d); } - // std::cout << studs.size() << std::endl; - // std::cout << studs[0].name << studs[0].scores.size() << std::endl; - - // std::sort(dates.begin(), dates.end()); - // std::sort(studs.begin(), studs.end()); - std::cout << '.' << "\t"; for (auto& date : dates) { std::cout << std::setfill('0') - << std::setw(4) << date.Y << '/' - << std::setw(2) << date.M << '/' - << std::setw(2) << date.D << '\t'; + << std::setw(4) << date.Y << '/' + << std::setw(2) << date.M << '/' + << std::setw(2) << date.D << '\t'; } std::cout << std::endl; for (auto st : studs) { std::cout << st.name << '\t'; - for (auto date : dates) { + for (auto& date : dates) { if (st.scores.find(date) == st.scores.end()) { std::cout << '.' << '\t'; } else { diff --git a/contests/6contest/3.cpp b/contests/6contest/3.cpp index e69de29..b120e30 100644 --- a/contests/6contest/3.cpp +++ b/contests/6contest/3.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + + +template +void myapply(It first, It last, F f) { + for (It it = first; it != last; ++it) { + f(*it); + } + + return; +} + +template +std::vector::value_type>> myfilter2(It first, It last, Pred f) { + std::vector::value_type>> result; + + for (It it = first; it != last; ++it) { + if (f(*it)) { + result.push_back(std::ref(*it)); + } + } + + return result; +} diff --git a/contests/6contest/4.cpp b/contests/6contest/4.cpp new file mode 100644 index 0000000..cd55f07 --- /dev/null +++ b/contests/6contest/4.cpp @@ -0,0 +1,16 @@ +#include + +template +It2 myremove(It1 first1, It1 last1, It2 first2, It2 last2) { + std::set to_remove(first1, last1); + It2 result = first2; + + for (It2 it = first2; it != last2; ++it) { + if (to_remove.find(std::distance(first2, it)) == to_remove.end()) { + std::iter_swap(result, it); + ++result; + } + } + + return result; +} diff --git a/contests/7contest/2.cpp b/contests/7contest/2.cpp index 0179c63..0576003 100644 --- a/contests/7contest/2.cpp +++ b/contests/7contest/2.cpp @@ -9,14 +9,14 @@ // }; class Rectangle : Figure { - double a; - double b; + double a = 0; + double b = 0; public: Rectangle(double a = 0, double b = 0) : a(a), b(b) {} double get_square() const { return a * b; } - static Rectangle* make(std::string s) { + static Rectangle* make(const std::string& s) { std::istringstream ss(s); double a, b; ss >> a >> b; @@ -26,13 +26,13 @@ public: }; class Square : Figure { - double a; + double a = 0; public: Square(double a = 0) : a(a) {} double get_square() const { return a * a; } - static Square* make(std::string s) { + static Square* make(const std::string& s) { std::istringstream ss(s); double a; ss >> a; @@ -42,13 +42,13 @@ public: }; class Circle : Figure { - double r; + double r = 0; public: Circle(double r = 0) : r(r) {} double get_square() const { return M_PI * r * r; } - static Circle* make(std::string s) { + static Circle* make(const std::string& s) { std::istringstream ss(s); double r; ss >> r; diff --git a/contests/8contest/2.cpp b/contests/8contest/2.cpp index fbc0305..38cc85e 100644 --- a/contests/8contest/2.cpp +++ b/contests/8contest/2.cpp @@ -1,21 +1,3 @@ -/* -Некоторая рекурсивная функция func от трех целых аргументов a, b, k (a, b >= 1, k >= 0) определена следующим образом: - - func(a, b, k) == a + b при k == 0 - func(a, b, k) == a при k > 0, b == 1 - func(a, b, k) == f(a, f(a, b - 1, k), k - 1) при k > 0, b > 1 - -На стандартном потоке ввода подаются тройки чисел: два 64-битных знаковых целых положительных числа a, b и 32-битное целое неотрицательное число k. Для каждой тройки чисел на стандартный поток вывода напечатайте значение описанной выше функции. - -Параметры на входе будут таковы, что вычисление завершится за разумное время. Для вычислений достаточно 64-битного целого знакового типа. - -Для возврата значения из рекурсии используйте исключения. Выход из рекурсивной функции с помощью явного или неявного return запрещен. - -Напишите свой класс (например, Result) для передачи результата вычислений вместе с исключением. Не используйте выбрасывание исключений базовых (например, int) типов. - -В комментарии в начале программы опишите, что из себя представляет эта рекурсивная функция. -*/ - #include #include