본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 11651 좌표 정렬하기 2

by 왹져박사 2023. 10. 4.
728x90

https://www.acmicpc.net/problem/11651

 

11651번: 좌표 정렬하기 2

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

www.acmicpc.net

https://narmhye.tistory.com/entry/BOJC-11650-%EC%A2%8C%ED%91%9C-%EC%A0%95%EB%A0%AC%ED%95%98%EA%B8%B0

 

[ BOJ/C# ] 11650 좌표 정렬하기

https://www.acmicpc.net/problem/11650 11650번: 좌표 정렬하기 첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표

narmhye.tistory.com

전의 문제에서 살짝만 바뀐 문제이다. 

using System;
using System.Text;

namespace B11651
{
    class Program
    {
        class XY
        {
            public int x { get; set; }
            public int y { get; set; }
        }
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();

            int n = int.Parse(sr.ReadLine());
            XY[] array = new XY[n];
            for (int i = 0; i < n; i++)
            {
                int[] ints = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
                array[i] = new XY { x = ints[0], y = ints[1] };
            }
            Array.Sort(array, (XY a, XY b) =>
            {
                if (a.y == b.y) return a.x.CompareTo(b.x);
                else
                {
                    if (a.y > b.y) return 1;
                    else if (a.y < b.y) return -1;
                    else return 0;
                }
            });
            foreach (XY xy in array)
                sb.Append(xy.x + " " + xy.y + "\n");
            sw.Write(sb);
            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90

'알고리즘 > 백준 BOJ' 카테고리의 다른 글

[ BOJ/C# ] 9375 패션왕 신해빈  (0) 2023.10.05
[ BOJ/C# ] 11659 구간 합 구하기 4  (0) 2023.10.04
[ BOJ/C# ] 17626 Four Squares  (0) 2023.10.03
[ BOJ/C# ] 9012 괄호  (0) 2023.10.01
[ BOJ/C# ] 11866 요세푸스 문제 0  (0) 2023.09.30