輸入月份 (1-12) 和日期 (1-31),輸出對應中文星座名稱。關鍵難點在於**邊界日期**(如 1 月 20 日是摩羯座還是水瓶座)的準確劃分。
| 星座 | 起始日期 | 結束日期 |
|---|---|---|
| 摩羯座 | 12 月 22 日 | 1 月 19 日 |
| 水瓶座 | 1 月 20 日 | 2 月 18 日 |
| 雙魚座 | 2 月 19 日 | 3 月 20 日 |
| 白羊座 | 3 月 21 日 | 4 月 19 日 |
| 金牛座 | 4 月 20 日 | 5 月 20 日 |
| 雙子座 | 5 月 21 日 | 6 月 21 日 |
| 巨蟹座 | 6 月 22 日 | 7 月 22 日 |
| 獅子座 | 7 月 23 日 | 8 月 22 日 |
| 處女座 | 8 月 23 日 | 9 月 22 日 |
| 天秤座 | 9 月 23 日 | 10 月 23 日 |
| 天蠍座 | 10 月 24 日 | 11 月 22 日 |
| 射手座 | 11 月 23 日 | 12 月 21 日 |
/**
* 根據月份、日期返回星座名稱
* @param int $month 1-12
* @param int $day 1-31
* @return string|false 星座名或 false(參數非法)
*/
function getConstellation($month, $day) {
// 參數校驗
if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return false;
// 靜態邊界表:[月份] => [起始日 => 星座名]
// 注意:每月只存儲該月第一個星座的起始日
static $bounds = [
1 => [20 => '水瓶座'],
2 => [19 => '雙魚座'],
3 => [21 => '白羊座'],
4 => [20 => '金牛座'],
5 => [21 => '雙子座'],
6 => [22 => '巨蟹座'],
7 => [23 => '獅子座'],
8 => [23 => '處女座'],
9 => [23 => '天秤座'],
10 => [24 => '天蠍座'],
11 => [22 => '射手座'],
12 => [22 => '摩羯座'],
];
$bound = $bounds[$month];
$startDay = key($bound); // 該月第一個星座起始日
$constellation = current($bound);
// 如果日期小於起始日,則屬於上一個星座
if ($day < $startDay) {
$prevMonth = $month === 1 ? 12 : $month - 1;
$prevBound = $bounds[$prevMonth];
$constellation = current($prevBound); // 上個月的第一個星座即為上一星座
}
return $constellation;
}
$tests = [
[1, 19] => '摩羯座', [1, 20] => '水瓶座', [1, 21] => '水瓶座',
[2, 18] => '水瓶座', [2, 19] => '雙魚座', [2, 20] => '雙魚座',
[3, 20] => '雙魚座', [3, 21] => '白羊座',
[4, 19] => '白羊座', [4, 20] => '金牛座',
[5, 20] => '金牛座', [5, 21] => '雙子座',
[6, 21] => '雙子座', [6, 22] => '巨蟹座',
[7, 22] => '巨蟹座', [7, 23] => '獅子座',
[8, 22] => '獅子座', [8, 23] => '處女座',
[9, 22] => '處女座', [9, 23] => '天秤座',
[10, 23] => '天秤座', [10, 24] => '天蠍座',
[11, 22] => '天蠍座', [11, 23] => '射手座',
[12, 21] => '射手座', [12, 22] => '摩羯座', [12, 31] => '摩羯座',
];
foreach ($tests as [$m, $d] => $expected) {
$actual = getConstellation($m, $d);
$status = $actual === $expected ? '✓' : '✗';
echo "$status $m/$d => $actual (期望: $expected)\n";
}
function getConstellationInfo($month, $day) {
$name = getConstellation($month, $day);
if (!$name) return false;
$details = [
'水瓶座' => ['symbol' => '♒', 'element' => '風', 'date' => '1/20-2/18'],
'雙魚座' => ['symbol' => '♓', 'element' => '水', 'date' => '2/19-3/20'],
'白羊座' => ['symbol' => '♈', 'element' => '火', 'date' => '3/21-4/19'],
'金牛座' => ['symbol' => '♉', 'element' => '土', 'date' => '4/20-5/20'],
'雙子座' => ['symbol' => '♊', 'element' => '風', 'date' => '5/21-6/21'],
'巨蟹座' => ['symbol' => '♋', 'element' => '水', 'date' => '6/22-7/22'],
'獅子座' => ['symbol' => '♌', 'element' => '火', 'date' => '7/23-8/22'],
'處女座' => ['symbol' => '♍', 'element' => '土', 'date' => '8/23-9/22'],
'天秤座' => ['symbol' => '♎', 'element' => '風', 'date' => '9/23-10/23'],
'天蠍座' => ['symbol' => '♏', 'element' => '水', 'date' => '10/24-11/22'],
'射手座' => ['symbol' => '♐', 'element' => '火', 'date' => '11/23-12/21'],
'摩羯座' => ['symbol' => '♑', 'element' => '土', 'date' => '12/22-1/19'],
];
return array_merge(['name' => $name], $details[$name]);
}
// 使用
print_r(getConstellationInfo(4, 20));
/* 輸出:
Array (
[name] => 金牛座
[symbol] => ♉
[element] => 土
[date] => 4/20-5/20
) */
靜態邊界表 + 單次比較,避免了原代碼中 each() 依賴內部指針的副作用(PHP 7.2+ 棄用),代碼更清晰、可測試、易維護。附帶完整測試用例保證邊界正確性。