BaekJoon[C#]

1차원 배열 4344

wny0320 2022. 9. 24. 20:33

백준 온라인 코딩 문제풀이

https://www.acmicpc.net/

 

Baekjoon Online Judge

Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.

www.acmicpc.net

 

코드 참고

https://replit.com/@wny0320

 

wny0320 (박 상운)

Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.

replit.com

 

1546, 8958, 4344 문제 모두를 다룸

 


1546

using System;
using System.Collections.Generic;

class Program
{
    public static void Main(string[] args)
    {
        Console.ReadLine();
        List<int> score_list = new List<int>();
        List<double> new_score_list = new List<double>();
        int max;
        double aver = 0;
        string[] score = Console.ReadLine().Split();
        for (int i = 0; i < score.Length; i++)
        {
            score_list.Add(Int32.Parse(score[i]));
        }
        score_list.Sort();
        max = score_list[score_list.Count - 1];
        for (int i = 0; i < score_list.Count; i++)
        {
            double buff = (double)score_list[i] / max * 100;
            new_score_list.Add(buff);
        }
        for (int i = 0; i < new_score_list.Count; i++)
        {
            aver += new_score_list[i];
        }
        aver /= new_score_list.Count;

        Console.WriteLine(aver);
    }
}

뭔가 되게 난잡하게 코딩이 되서 마음에 안듦

 

우선 플로우 차트는 받은 점수를 Split하고 Int 형태로 바꾸어 score_list에 Add

 

score_list.Sort로 정렬하여 최대값 max를 찾음

 

각각의 점수 score_list[index]를 max값으로 나눈 후 100을 곱하여 new_score_list에 저장

 

aver에 계산하여 평균을 출력

- 뒤에 문제에도 나오지만 new_score_list.Average()를 사용하여 평균을 구할 수도 있음

 

다음에 기회가 되면 좀 더 효율적이고 직관적인 코드를 짜보고자 함

 


8958

using System;

class Program
{
    public static void Main(string[] args)
    {
        int cnt = 0;
        int times = Int32.Parse(Console.ReadLine());
        int[] score = new int[times];
        string[] OX = new string[times];
        for(int i = 0; i < times; i++)
        {
            OX[i] = Console.ReadLine();
        }
        for(int i = 0; i < times; i++)
        {
            cnt = 0;
            for(int j = 0; j < OX[i].Length; j++)
            {
                if(OX[i][j] == 'O')
                {
                    cnt++;
                }
                else
                {
                    cnt = 0;
                }
                score[i] += cnt;
            }
        }
        for(int i = 0; i < times; i++)
        {
            Console.WriteLine(score[i]);
        }
    }
}

cnt를 두어 받은 OX string에 대해 O이면 cnt를 증가시켜 score에 추가함

 

한 줄마다 cnt를 초기화 해야하기 때문에 2중 for문 바깥쪽 for문에 cnt값 초기화를 넣어줌

 


4344

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    public static void Main(string[] args)
    {
        int times = Int32.Parse(Console.ReadLine());
        double[] pct = new double[times];
        for (int i = 0; i < times; i++)
        {
            string[] buff = Console.ReadLine().Split();
            List<int> score = new List<int>();
            int cnt = 0;
            for (int j = 1; j < buff.Length; j++)
            {
                score.Add(Int32.Parse(buff[j]));
            }
            for (int j = 0; j < score.Count; j++)
            {
                if (score[j] > score.Average())
                {
                    cnt++;
                }
            }
            pct[i] = (double)cnt / (double)score.Count * 100;
        }
        for (int i = 0; i < times; i++)
        {
            Console.WriteLine(string.Format("{0:N3}%", Math.Round(pct[i], 3)));
        }
    }
}

입력받는 한줄마다 계산하여 pct에 저장하여 한번에 출력하는 메소드

 

입력받은 값을 int로 바꾸어 score에 Add

 

score.Average()보다 각 score의 값이 크면 cnt++

 

pct값을 계산 후 출력 포맷을 string.Format을 통해 소수 3번째자리까지 출력하도록 하고 반올림으로 Math.Round 사용

'BaekJoon[C#]' 카테고리의 다른 글

문자열 단계 2675  (0) 2022.09.26
함수 1065  (0) 2022.09.25
1차원 배열 2562  (0) 2022.09.23
반복문 1110  (0) 2022.09.20
반복문 2439  (0) 2022.09.19