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; int old = c;
c = getchar(); c = getchar();
if (!(c == EOF || !isdigit(c))) { if (isdigit(c)) {
continue; continue;
} }
if (c == EOF) {
putchar(old);
break;
}
putchar(old); putchar(old);
} else if (isdigit(c)) { } else if (isdigit(c)) {
is_num = true; is_num = true;
@@ -28,9 +33,5 @@ int main() {
c = getchar(); c = getchar();
} }
if (c != '\n') {
putchar('\n');
}
return 0; 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;
}
+7 -4
View File
@@ -1,9 +1,11 @@
#include <cmath> #include <cmath>
#include <string> #include <string>
#include <sstream>
namespace numbers { namespace numbers {
class complex { class complex {
double c_re, c_im; double c_re = 0;
double c_im = 0;
public: public:
complex(double r = 0, double i = 0) : c_re(r), c_im(i) {} complex(double r = 0, double i = 0) : c_re(r), c_im(i) {}
explicit complex(const std::string& s) { explicit complex(const std::string& s) {
@@ -22,10 +24,11 @@ namespace numbers {
return sqrt(abs2()); return sqrt(abs2());
} }
std::string to_string() const { std::string to_string() const {
char buf[100]; std::stringstream ss;
sprintf(buf, "(%.10g,%.10g)", c_re, c_im); ss.precision(10);
ss << "(" << c_re << "," << c_im << ")";
return std::string(buf); return ss.str();
} }
complex& operator+=(const complex& other) { complex& operator+=(const complex& other) {
c_re += other.c_re; c_re += other.c_re;
+2 -2
View File
@@ -15,8 +15,8 @@ void process(const std::vector<int>& v1, std::vector<int>& v2) {
iter = v2.erase(iter); iter = v2.erase(iter);
} }
num--; --num;
iter--; --iter;
} }
return; return;
View File
+12 -18
View File
@@ -12,17 +12,17 @@ struct Date {
int M; int M;
int D; int D;
Date(std::string s) { Date(std::string& s) {
std::stringstream ss; std::stringstream ss;
ss << s; ss << s;
char tmp; char tmp;
ss >> Y >> tmp >> M >> tmp >> D; ss >> Y >> tmp >> M >> tmp >> D;
} }
Date(int Y, int M, int D) : Y(Y), M(M), D(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 true;
if (Y > d.Y) return false; if (Y > d.Y) return false;
@@ -30,7 +30,7 @@ struct Date {
if (M > d.M) return false; if (M > d.M) return false;
if (D < d.D) return true; if (D < d.D) return true;
return false; return false;
} }
@@ -40,18 +40,18 @@ struct Student {
std::map<Date, int> scores; std::map<Date, int> scores;
std::string name; std::string name;
Student(std::string name) : name(name) {} Student(std::string& name) : name(name) {}
Student(const Student& s) { Student(const Student& s) {
scores = s.scores; scores = s.scores;
name = s.name; name = s.name;
} }
bool operator==(const std::string n) { bool operator==(const std::string& n) {
return n == name; return n == name;
} }
bool operator<(const Student st) const { bool operator<(const Student& st) const {
return name < st.name; return name < st.name;
} }
}; };
@@ -65,7 +65,7 @@ int main() {
while (std::cin >> name >> date >> grade) { while (std::cin >> name >> date >> grade) {
std::set<Student>::iterator iter = studs.find(name); std::set<Student>::iterator iter = studs.find(name);
Date d(date); Date d(date);
if (iter == studs.end()) { if (iter == studs.end()) {
Student s(name); Student s(name);
s.scores[d] = grade; s.scores[d] = grade;
@@ -80,25 +80,19 @@ int main() {
dates.insert(d); 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"; std::cout << '.' << "\t";
for (auto& date : dates) { for (auto& date : dates) {
std::cout << std::setfill('0') std::cout << std::setfill('0')
<< std::setw(4) << date.Y << '/' << std::setw(4) << date.Y << '/'
<< std::setw(2) << date.M << '/' << std::setw(2) << date.M << '/'
<< std::setw(2) << date.D << '\t'; << std::setw(2) << date.D << '\t';
} }
std::cout << std::endl; std::cout << std::endl;
for (auto st : studs) { for (auto st : studs) {
std::cout << st.name << '\t'; std::cout << st.name << '\t';
for (auto date : dates) { for (auto& date : dates) {
if (st.scores.find(date) == st.scores.end()) { if (st.scores.find(date) == st.scores.end()) {
std::cout << '.' << '\t'; std::cout << '.' << '\t';
} else { } else {
+26
View File
@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <functional>
template <typename It, typename F>
void myapply(It first, It last, F f) {
for (It it = first; it != last; ++it) {
f(*it);
}
return;
}
template <typename It, typename Pred>
std::vector<std::reference_wrapper<typename std::iterator_traits<It>::value_type>> myfilter2(It first, It last, Pred f) {
std::vector<std::reference_wrapper<typename std::iterator_traits<It>::value_type>> result;
for (It it = first; it != last; ++it) {
if (f(*it)) {
result.push_back(std::ref(*it));
}
}
return result;
}
+16
View File
@@ -0,0 +1,16 @@
#include <set>
template <typename It1, typename It2>
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;
}
+7 -7
View File
@@ -9,14 +9,14 @@
// }; // };
class Rectangle : Figure { class Rectangle : Figure {
double a; double a = 0;
double b; double b = 0;
public: public:
Rectangle(double a = 0, double b = 0) : a(a), b(b) {} Rectangle(double a = 0, double b = 0) : a(a), b(b) {}
double get_square() const { double get_square() const {
return a * b; return a * b;
} }
static Rectangle* make(std::string s) { static Rectangle* make(const std::string& s) {
std::istringstream ss(s); std::istringstream ss(s);
double a, b; double a, b;
ss >> a >> b; ss >> a >> b;
@@ -26,13 +26,13 @@ public:
}; };
class Square : Figure { class Square : Figure {
double a; double a = 0;
public: public:
Square(double a = 0) : a(a) {} Square(double a = 0) : a(a) {}
double get_square() const { double get_square() const {
return a * a; return a * a;
} }
static Square* make(std::string s) { static Square* make(const std::string& s) {
std::istringstream ss(s); std::istringstream ss(s);
double a; double a;
ss >> a; ss >> a;
@@ -42,13 +42,13 @@ public:
}; };
class Circle : Figure { class Circle : Figure {
double r; double r = 0;
public: public:
Circle(double r = 0) : r(r) {} Circle(double r = 0) : r(r) {}
double get_square() const { double get_square() const {
return M_PI * r * r; return M_PI * r * r;
} }
static Circle* make(std::string s) { static Circle* make(const std::string& s) {
std::istringstream ss(s); std::istringstream ss(s);
double r; double r;
ss >> r; ss >> r;
-18
View File
@@ -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 <iostream> #include <iostream>
#include <string> #include <string>