A struct keeps the two coordinates of a point together.
What to build
A program that measures the distance between pairs of points.
Requirements
- Define a
Pointstruct with fieldsXandY. - Read n, then n lines
x1 y1 x2 y2. - For each line print
squared D, manhattan Mwhere D is the squared Euclidean distance and M is the Manhattan distance (|dx| + |dy|).
Input
Line 1: n. Then n lines with four whole numbers.
Output
n lines.
Constraints
1 ≤ n ≤ 30; coordinates are between -1000 and 1000.
Example
Input:
2
0 0 3 4
1 1 1 1
Output:
squared 25, manhattan 7
squared 0, manhattan 0
Why it works
Each pair is stored in two Point values, which is easier to read than four loose numbers. The squared distance avoids a square root and stays a whole number.
Examples
Example 1 · Input
2 0 0 3 4 1 1 1 1
Expected output
squared 25, manhattan 7 squared 0, manhattan 0
Example 2 · Input
1 -2 5 4 -3
Expected output
squared 100, manhattan 14
Hints
Hint 1
Declare type Point struct { X, Y int } and build one Point for each end.
Hint 2
The squared distance is dx*dx + dy*dy, and the Manhattan distance is |dx| + |dy|.
Solution
Show one correct solution (try it yourself first)
package main
import "fmt"
type Point struct {
X, Y int
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func main() {
var n int
fmt.Scan(&n)
for i := 0; i < n; i++ {
var a, b Point
fmt.Scan(&a.X, &a.Y, &b.X, &b.Y)
dx := a.X - b.X
dy := a.Y - b.Y
fmt.Printf("squared %d, manhattan %d\n", dx*dx+dy*dy, abs(dx)+abs(dy))
}
}