Python/개인 공부

[BOJ] 7568번 덩치 파이썬 풀이 & C# 사용 풀이

Bueong_E 2023. 3. 26. 01:22
반응형
SMALL

코드

#학생수 입력받기 input() 함수 사용
num_student = int(input()) 
# 학생들의 몸무게와 키를 담을 리스트 생성
student_list = []
# for 문을 돌며 (변수 없이)   num_student 숫자 만큼 반복
for _ in range(num_student):
    # 무게와 키 변수에 map함수를 사용하여 정수 타입의 숫자를 담는다
    weight, height = map(int, input().split())
    # 학생 리스트에 키와 몸무게를 넣는다.
    student_list.append((weight, height))
# for문을 돌며 학생리스트를 순회, 학생리스트의 값을 i에 담는다
for i in student_list:
    #랭크 변수 초기화
    rank = 1
#for문을 돌며 i보다  몸무게와 키가 더 큰 학생 j를 찾는다
    for j in student_list:
        if i[0] < j[0] and i[1] < j[1]:
                # 찾았다면 랭크 +1
                rank += 1
    #결과 출력 end 는 결과를 한줄에 출력하기 위한 한정자
    print(rank, end = " ")

 

출력

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace BOJ2108
{
    internal class Program
    {

        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int N = int.Parse(sr.ReadLine());
            List<int>rankList = new List<int>();
            List<Tuple<int, int>> studentList = new List<Tuple<int, int>>();

            for (int i = 0; i < N; i++)
            {
                int[] a = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
                studentList.Add(new Tuple<int, int>(a[0], a[1]));
            }

            foreach ( Tuple<int,int> i  in studentList)
            {
                int rank = 1; 
                foreach(Tuple<int, int> j in studentList)            
                {
                    if(i.Item1 < j.Item1 && i.Item2 < j.Item2)
                    {
                        rank++;
                    }                                            
                
                }
                rankList.Add(rank);
            }     

            foreach(int i in rankList){
                sw.Write(i);  
                sw.Write(' '); 
            }
                   
            sr.Close();
            sw.Close();
        }
    }
}

확실히 파이썬의 경우 코드도 훨씬 짧고 딕셔너리의 키 중복 한계 같은 문제가 없이 편하게 처리할수 있다.

C#으로도 해결할수 있을거 같아  dictionary 를 사용했지만 argument exception이 나서 tuple을 사용하니 해결할수 있었다.

 

슬슬 파이썬으로 갈아타야 겠다...

반응형
LIST