a731簡單開區間的矩形內部判定
CPE:Points in Figures: Rectangles (UVa 476)
用四個嚴格不等式列出所有包含點的矩形。
解題思路
矩形以左上 (x1,y1)、右下 (x2,y2) 表示。點 (x,y) 位於內部,恰好等價於 x1<x<x2 且 y2<y<y1。四個條件都要嚴格成立,邊界與角落一律不算。
先讀到 * 收集矩形,再逐點測試所有矩形,符合者依輸入編號輸出;一點可能同時在多個矩形內。全部檢查後若沒有符合者才輸出 not contained。遇到 9999.9 9999.9 結束,不替終止點編號或輸出,句型依題目大小寫且沒有句尾句點。
Step-by-step animation
示意:矩形 (0,4)–(4,0)
步驟 1 / 3
內部點 (2,2)
0<2<4 在兩軸均成立。
x:0<2<4
y:0<2<4
- 包含
- 1
#include <iostream>
#include <vector>
#include <array>
using namespace std;
int main() {
vector<array<double, 4>> r;
char c;
while (cin >> c && c != '*') {
array<double, 4> a;
for (double &v : a)
cin >> v;
r.push_back(a);
}
double x, y;
int p = 0;
while (cin >> x >> y && !(x == 9999.9 && y == 9999.9)) {
++p;
bool found = false;
for (size_t i = 0; i < r.size(); ++i) {
auto a = r[i];
if (a[0] < x && x < a[2] && a[3] < y && y < a[1]) {
found = true;
cout << "Point " << p << " is contained in figure " << i + 1 << '\n';
}
}
if (!found)
cout << "Point " << p << " is not contained in any figure\n";
}
}先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。