대외활동/starters 부트캠프 feat.웅진씽크빅

유데미 스타터스 취업 부트캠프 유니티 1기 8주차 학습 일지

Heeyeon Choi 2022. 8. 14. 22:59
728x90
728x90

이번주는 백준 공부를 위주로 했습니다ㅎㅎ

저희 스타터스 1기 분들이 백준을 함께 공부했으면 하는 마음에 

그룹을 만들었습니다. 다른 분들이 푼 문제도 볼 수 있고, 랭킹도 확인하며 경쟁할 수 있습니다.

 

푼 문제중에 제일 인상깊었던 문제는 최대공약수 최소공배수를 구하는 문제였습니다. 

초등학교 때 배운 부분이라 간단히 생각해서 풀었는데 테스트케이스는 맞지만 계속 틀렸다고 나왔습니다.

 

많은 시도 끝에 맞추었습니다.

반례로 18 36을 넣었더니, 9, 36이 나오는 것을 확인한 후 코드를 수정하였습니다.

 

<오답 코드>

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project2
{
    class Class1
    {
        static void Main(string[] args)
        {

            //[1]Input
            StreamWriter writer = new StreamWriter(Console.OpenStandardOutput());
            StreamReader reader = new StreamReader(Console.OpenStandardInput());
            StringBuilder sb = new StringBuilder();

            string[] str = reader.ReadLine().Split(' ');
            int[] intArray = new int[2];
            List<int> first = new List<int>();
            List<int> second = new List<int>();
            int leastAns = 0; //최소공배수
            int biggestAns = 0; //최대공약수
            for(int i=0; i<2; i++)
            {
                intArray[i] = int.Parse(str[i]);
            }
            int j = 1;
            while (j<=intArray[0])
            {
                
                if (intArray[0] % j == 0)
                {
                    first.Add(j);
                    
                }
                j++;

            }
            j = 1;
            while (j <= intArray[1])
            {
                
                if (intArray[1] % j == 0)
                {
                    second.Add(j);
                   
                }
                j++;

            }
			// 틀린부분
            for(int i=0; i<(first.Count> second.Count? second.Count: first.Count); i++)
            {
                if (first.Contains(second[i]))
                {
                    biggestAns = second[i];
                   
                }
            }

           
            leastAns = (intArray[0] / biggestAns) * (intArray[1] / biggestAns) * biggestAns;

            writer.WriteLine(biggestAns);
            writer.WriteLine(leastAns);


            writer.Close();
            reader.Close();

        }

    }
}

 


            for(int i=0; i<(first.Count> second.Count? second.Count: first.Count); i++)
            {
                if (first.Contains(second[i]))
                {
                    biggestAns = second[i];
                   
                }
            }

 

 

이 부분을 보시면, first의 count보다 second의 count가 작을 경우, second의 모든 요소를 검사하지 않는 다는 것을 깨달았습니다.

 

<수정 후 코드>

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project2
{
    class Class1
    {
        static void Main(string[] args)
        {
            //[1]Input
            StreamWriter writer = new StreamWriter(Console.OpenStandardOutput());
            StreamReader reader = new StreamReader(Console.OpenStandardInput());
            StringBuilder sb = new StringBuilder();
            string[] str = reader.ReadLine().Split(' ');
            int[] intArray = new int[2];
            List<int> first = new List<int>();
            List<int> second = new List<int>();
            int leastAns = 0; //최소공배수
            int biggestAns = 0; //최대공약수
            for (int i = 0; i < 2; i++)
            {
                intArray[i] = int.Parse(str[i]);
            }
            int j = 1;
            while (j <= intArray[0])
            {
                if (intArray[0] % j == 0)
                {
                    first.Add(j);
                }
                j++;
            }
            j = 1;
            while (j <= intArray[1])
            {
                if (intArray[1] % j == 0)
                {
                    second.Add(j);
                }
                j++;
            }
            
            //수정한 부분
            int whatIs = first.Count > second.Count ? second.Count : first.Count;
            for (int i = 0; i < whatIs; i++)
            {
                if(whatIs == second.Count)
                {
                    if (first.Contains(second[i]))
                    {
                        biggestAns = second[i];
                    }
                }
                else
                {
                    if (second.Contains(first[i]))
                    {
                        biggestAns = first[i];
                    }

                }
               
            }
            leastAns = (intArray[0] / biggestAns) * (intArray[1] / biggestAns) * biggestAns;
            writer.WriteLine(biggestAns);
            writer.WriteLine(leastAns);
            writer.Close();
            reader.Close();
        }
    }
}

 

 int whatIs = first.Count > second.Count ? second.Count : first.Count;
            for (int i = 0; i < whatIs; i++)
            {
                if(whatIs == second.Count)
                {
                    if (first.Contains(second[i]))
                    {
                        biggestAns = second[i];
                    }
                }
                else
                {
                    if (second.Contains(first[i]))
                    {
                        biggestAns = first[i];
                    }

                }

 

경우의 수를 나누어 주어, 모든 요소를 검사할 수 있도록 수정한 뒤 답을 맞았습니다 ㅎㅎ

 

현재

 

17명의 라이벌 중에 6등 정도이기 때문에 더 열심히 알고리즘 공부를 할 것 입니다 ㅎㅎ 

 

 

 

 

——————————————————————————

유데미코리아 바로가기 : https://bit.ly/3b8JGeD

본 포스팅은 유데미-웅진씽크빅 취업 부트캠프 유니티 1기 과정 후기로 작성되었습니다.

728x90