Maximum possible intersection by moving centers of line segments in C++
-
10-09-2020 - |
Вопрос
In this tutorial, we will be discussing a program to find maximum possible intersection by moving centers of line segments
For this we will be provided with the center of three line segments and their length. Our task is to move their center by K distance to increase the length of intersection region.
Example
#include <bits/stdc++.h> using namespace std; //finding maximum intersection int max_intersection(int* center, int length, int k) { sort(center, center + 3); if (center[2] - center[0] >= 2 * k + length) { return 0; } else if (center[2] - center[0] >= 2 * k) { return (2 * k - (center[2] - center[0] - length)); } else return length; } int main() { int center[3] = { 1, 2, 3 }; int L = 1; int K = 1; cout << max_intersection(center, L, K); }
Output
1
Не связан с Tutorialspoint