b201簡單

98 彰雲嘉區資訊學科能力競賽(2009)|座標位置判別

先處理x=y=0的原點,再依y=0、x=0處理兩軸。

解題思路

先處理x=y=0的原點,再依y=0、x=0處理兩軸。

剩下兩座標都非零,依正負號組合判第一至第四象限。

這些互斥條件涵蓋所有整數座標,照指定括號與中文字串輸出。

固定次數整數比較,時間空間依輸入位數。

Step-by-step animation

教學小例:優先處理軸線再判象限

回報動畫錯誤
步驟 1 / 3

點(2,-3)

兩座標皆非0,非軸線

兩座標皆非0,非軸線
步驟
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
int main() {
    cpp_int x, y;
    cin >> x >> y;
    string s;
    if (x == 0 && y == 0)
        s = "在原點";
    else if (y == 0)
        s = "在x軸上";
    else if (x == 0)
        s = "在y軸上";
    else
        s = "在第" + string(x > 0 ? (y > 0 ? "一" : "四") : (y > 0 ? "二" : "三")) +
            "象限內";
    cout << '(' << x << ", " << y << ')' << s << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多