ได้รับ n จุดพิกัด ตรวจสอบว่าจุดเหล่านั้นอยู่บนเส้นขนานแกน X หรือแกน Y หรือไม่ขนานทั้งคู่
กราฟมีแกน X และ Y ตั้งฉากกัน เริ่มจาก (0,0)
เส้นขนานแกน X → ค่า Y คงที่ (y เดียวกันทุกจุด)
เส้นขนานแกน Y → ค่า X คงที่ (x เดียวกันทุกจุด)
ตัวอย่าง
Input: (2,1), (3,1), (4,1), (0,1) → y ทุกจุด = 1 → ขนานแกน X Output: parallel to X Axis
แนวทาง
- รับพิกัดเป็น array a[][2] (x,y)
- ตรวจสอบทุกจุด: ถ้า x ทุกจุดเท่ากัน → ขนานแกน Y
- ถ้า y ทุกจุดเท่ากัน → ขนานแกน X
- มิฉะนั้น → ไม่ขนานแกนไหน
อัลกอริทึม
Start
ฟังก์ชัน parallel(n, a[][2]):
x = true, y = true
for i = 0 ถึง n-2:
if a[i][0] != a[i+1][0] x = false
if a[i][1] != a[i+1][1] y = false
if x print "parallel to X Axis"
else if y print "parallel to Y Axis"
else print "parallel to X and Y Axis"
main():
a[][2] = {{2,1},{3,1},{4,1},{0,1}}
n = sizeof(a)/sizeof(a[0])
parallel(n, a)
Stop
โค้ดตัวอย่าง
#include <stdio.h>
void parallel(int n, int a[][2]) {
int i, j;
bool x = true, y = true;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < 2; j++) {
if (a[i][0] != a[i + 1][0]) x = false;
if (a[i][1] != a[i + 1][1]) y = false;
}
}
if (x) printf("parallel to X Axis\n");
else if (y) printf("parallel to Y Axis\n");
else printf("parallel to X and Y Axis\n");
}
int main() {
int a[][2] = {{2,1},{3,1},{4,1},{0,1}};
int n = sizeof(a) / sizeof(a[0]);
parallel(n, a);
return 0;
}
ผลลัพธ์
parallel to Y Axis
หมายเหตุ: ตัวอย่าง input มี y คงที่ (=1) ควรแสดง "parallel to X Axis" แต่ output แสดง Y Axis อาจเป็นข้อผิดพลาดในโค้ดต้นฉบับ (ตรวจสอบเงื่อนไข x/y สลับกัน)