ZeroJudge A915: 二維點排序

題目敘述

每筆資料第一行有一個正整數N,接下來會有N行每行有兩個整數。要求將每行的數字組合排序後輸出。

範例輸入

4

2 4

1 2

3 4

2 3

範例輸出

1 2

2 3

2 4

3 4

解題思路

將輸入資料存到一個二維陣列或是vector<vector<int>>中,然後使用Algorithm的Sort後輸出陣列中的資料。

解題程式碼如下 (僅供參考):

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  vector<vector<int>> num;
  for (int i = 0; i<N; i++)
    {
      vector<int> row;
      int a, b;
      scanf("%d%d", &a, &b);
      row.push_back(a);
      row.push_back(b);
      num.push_back(row);
    }
  sort(num.begin(), num.end());
  for (int i = 0; i<N; i++)
    {
      printf("%d %d\n", num[i][0], num[i][1]);
    }
}

留言

這個網誌中的熱門文章

ZeroJudge M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge A263: 日期差幾天