# Comparison# Input Output# Conditional Statements

d143 - 11172 - Relational Operators

🔗 前往 ZeroJudge 原題

題目描述

題目要求讀取兩整數 a 和 b,並比較它們的大小關係,輸出 ">" (a > b), "<" (a < b), 或 "=" (a == b)。輸入包含多組測試資料,第一行表示測試資料組數。

解題思路

此題為簡單的條件判斷題。程式讀取每組測試資料的兩個整數 a 和 b,使用 if-else if-else 結構比較 a 和 b 的大小,並根據比較結果輸出相應的關係符號。為了加快輸入輸出速度,使用了 std::ios::sync_with_stdio(false);std::cin.tie(0);

複雜度分析

  • 時間複雜度: O(n) (n 為測試資料組數)
  • 空間複雜度: O(1)

程式碼

#include <iostream>
using namespace std;
int main(){
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int a,b;
    cin >> a;
    while(cin >> a >> b){
    	if(a>b){
    		cout << ">" << endl;
		}
		else if(a<b){
			cout << "<" << endl;
		}
		else{
			cout << "=" << endl;
		}
	}
}

Discussion