61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
// LeetCode 3516: Find Closest Person
|
|
// 難度: Easy
|
|
// 日期: 2025-09-04
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public class Solution {
|
|
public int FindClosest(int x, int y, int z)
|
|
{
|
|
var distanceX = Math.Abs(x - z);
|
|
var distanceY = Math.Abs(y - z);
|
|
|
|
return distanceX == distanceY ? 0 : distanceX > distanceY ? 2 : 1;
|
|
}
|
|
}
|
|
|
|
// 測試程式
|
|
public class Program
|
|
{
|
|
public static void Main()
|
|
{
|
|
TestCase1();
|
|
TestCase2();
|
|
TestCase3();
|
|
}
|
|
|
|
// 基本測試案例 - Example 1
|
|
static void TestCase1()
|
|
{
|
|
var solution = new Solution();
|
|
// Input: x = 2, y = 7, z = 4
|
|
// Expected: 1 (Person 1 距離較近: |2-4| = 2 < |7-4| = 3)
|
|
var actual = solution.FindClosest(2, 7, 4);
|
|
var expected = 1;
|
|
Console.WriteLine($"Test 1: Input(2, 7, 4) Expected: {expected}, Actual: {actual}, Result: {(actual == expected ? "PASS" : "FAIL")}");
|
|
}
|
|
|
|
// 基本測試案例 - Example 2
|
|
static void TestCase2()
|
|
{
|
|
var solution = new Solution();
|
|
// Input: x = 2, y = 5, z = 6
|
|
// Expected: 2 (Person 2 距離較近: |5-6| = 1 < |2-6| = 4)
|
|
var actual = solution.FindClosest(2, 5, 6);
|
|
var expected = 2;
|
|
Console.WriteLine($"Test 2: Input(2, 5, 6) Expected: {expected}, Actual: {actual}, Result: {(actual == expected ? "PASS" : "FAIL")}");
|
|
}
|
|
|
|
// 基本測試案例 - Example 3
|
|
static void TestCase3() {
|
|
var solution = new Solution();
|
|
// Input: x = 1, y = 5, z = 3
|
|
// Expected: 0 (距離相等: |1-3| = 2 = |5-3| = 2)
|
|
var actual = solution.FindClosest(1, 5, 3);
|
|
var expected = 0;
|
|
Console.WriteLine($"Test 3: Input(1, 5, 3) Expected: {expected}, Actual: {actual}, Result: {(actual == expected ? "PASS" : "FAIL")}");
|
|
}
|
|
}
|