Top.Mail.Ru
Ответы

Написать код C++

Вариант 8 пожалуйста

По дате
По рейтингу
Аватар пользователя
Новичок
123456789101112131415161718192021222324252627282930313233
 #include <iostream> 
using namespace std; 
struct Dot { 
    double x; 
    double y; 
    double length(const Dot& d)const { 
        return sqrt(pow(d.x - x, 2) + pow(d.y - y, 2)); 
    } 
    friend ostream& operator<<(ostream& out, const Dot& dot) { 
        return out << "(" << dot.x << "; " << dot.y << ")"; 
    } 
}; 
int main() { 
    const size_t n = 10; 
    double dots[n]{}; 
    for (auto& coord : dots) cin >> coord; 
    Dot a{ dots[0], dots[1]}, b{ dots[2], dots[3]}; 
    auto length = a.length(b); 
    for (size_t x = 4, y = 5; y < n; x += 2, y += 2) { 
        const Dot c{ dots[x], dots[y] }; 
        const auto ac = a.length(c); 
        const auto bc = b.length(c); 
        if (ac > bc && ac > length) { 
            length = ac; 
            b = c; 
        } 
        if (bc > ac && bc > length) { 
            length = bc; 
            a = c; 
        } 
    } 
    cout << "{" << a << ", " << b << "} => length: " << length << '\n'; 
} 
Аватар пользователя
Мудрец

#include <iostream>
#include <vector>
typedef const double& dbl;

double dist(dbl x1, dbl y1, dbl x2, dbl y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

auto find_dist(const std::vector<double>& arr)
{
std::pair<std::vector<double>::const_iterator, std::vector<double>::const_iterator> res;
size_t it1{}, it2{};
double point_distance{}, tmp;
for (it1 = 0; it1 < arr.size() - 2; it1 += 2)
for (it2 = it1 + 2; it2 < arr.size(); it2 += 2)
{
tmp = dist(arr[it1], arr[it1 + 1], arr[it2], arr[it2 + 1]);
if (tmp > point_distance)
{
point_distance = tmp;
res.first = arr.begin() + it1;
res.second = arr.begin() + it2;
}
}
return res;
}

int main()
{
constexpr const size_t size = 10;
std::vector<double> coords(size);
std::cout << "Enter x and y position (x5):";
for (auto& i : coords) std::cin >> i;
auto res = find_dist(coords);
std::cout << "Far distance points at coordinates: { " << *res.first << " , " << *(res.first + 1) << " } and { "
<< *res.second << " , " << *(res.second + 1) << " }";
}



Видео по теме