some more solves

This commit is contained in:
2025-05-10 16:10:00 +03:00
parent 0cb745c9a1
commit 1924ca48db
11 changed files with 219 additions and 54 deletions
+6 -5
View File
@@ -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;
}
+77
View File
@@ -0,0 +1,77 @@
#include <iostream>
#include <iomanip>
/*
На плоскости даны две прямые. Каждая прямая задается парой точек, через которые она проходит. Требуется установить, пересекаются ли эти прямые, и найти координаты точки пересечения.
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;
}
+66
View File
@@ -0,0 +1,66 @@
#include <iostream>
#include <iomanip>
#include <cmath>
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;
}