C#/문제해결

백준 4344번

버스창문 2022. 6. 15. 13:06
using System;

namespace study00
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] scores;
            int[][] total;
            int check = 0;
            int a;
            Console.WriteLine("반 개수 입력");
            int cls = Convert.ToInt32(Console.ReadLine());
            scores = new int[cls];
            total = new int[cls][];

            for (int i = 0; i < scores.Length; i++)
            {
                do
                {
                    check = 0;
                    Console.WriteLine("{0}반 학생수 입력",i+1);
                    a = Convert.ToInt32(Console.ReadLine());
                    if(a > 1000 || a < 1)
                    {
                        Console.WriteLine("1이상 1000이하 정수만");
                        check++;
                    }
                } while (check != 0);


                total[i] = new int[a];

                for(int j = 0; j < total[i].Length; j++)
                {
                    do
                    {
                        check = 0;
                        Console.WriteLine("{0}반의 {1}번 학생 점수 입력", i + 1, j+1);
                        total[i][j] = Convert.ToInt32(Console.ReadLine());
                        if(total[i][j] > 100 || total[i][j] < 0)
                        {
                            Console.WriteLine("0이상 100이하 정수만");
                            check++;
                        }
                    } while (check != 0);
                }
            }
            double[] avg = new double[cls];
            for (int i = 0; i < avg.Length; i++)
            {
                avg[i] = Avg(total[i]);
            }
            foreach(double v in avg)
            {
                Console.WriteLine("{0:00.00}", v*100);
            }
        }
        static double Avg(int[] arr)
        {
            double sum = 0;
            double cnt = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
            for(int i = 0; i < arr.Length; i++)
            {
                if(arr[i]>sum / arr.Length)
                {
                    cnt += 1;
                }
            }
            
            
            return cnt/arr.Length;
        }
    }
}

런타임 에러
readline에 split이란게 있다는걸 알게됨
https://jaeho0613.tistory.com/74

 

백준 BOJ C# 4344 평균은 넘겠지

백준 BOJ C# [4344] [평균은 넘겠지] 출제 링크 : https://www.acmicpc.net/problem/4344 4344번: 평균은 넘겠지 문제 대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈..

jaeho0613.tistory.com

 

 

using System;

namespace study00
{
    internal class Program
    {
        static void Main(string[] args)
        {
            
            string[] input;
            input = Console.ReadLine().Split(' ');
            foreach (string z in input)
                Console.WriteLine(z);

            
        }
        
    }
}

테스트

using System;

namespace study00
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] scores;
            int[][] total;
            int check = 0;
            int a;
            int cls = Convert.ToInt32(Console.ReadLine());
            scores = new int[cls];
            total = new int[cls][];
            string[] input;

            for (int i = 0; i < scores.Length; i++)
            {
                input = Console.ReadLine().Split(' ');


                total[i] = new int[Convert.ToInt32(input[0])];
                

                for (int j = 0; j < total[i].Length; j++)
                {
                    total[i][j] = Convert.ToInt32(input[j+1]);
                }
            }
            double[] avg = new double[cls];
            for (int i = 0; i < avg.Length; i++)
            {
                avg[i] = Avg(total[i]);
            }
            foreach (double v in avg)
            {
                Console.WriteLine("{0:00.00}%", v * 100);
            }
        }
        static double Avg(int[] arr)
        {
            double sum = 0;
            double cnt = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
            for(int i = 0; i < arr.Length; i++)
            {
                if(arr[i]>sum / arr.Length)
                {
                    cnt += 1;
                }
            }
            
            
            return cnt/arr.Length;
        }
    }
}

작동확인

하지만 숫자 제한(반의 인원수가 1~1000이라거나 점수가 0~100이라거나)은 적용되지않음

using System;

namespace study00
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] scores;
            int[][] total;
            int cls = Convert.ToInt32(Console.ReadLine());
            scores = new int[cls];
            total = new int[cls][];
            string[] input;

            for (int i = 0; i < scores.Length; i++)
            {

                input = Console.ReadLine().Split(' ');
                int stu = Convert.ToInt32(input[0]); // 반의 학생수
                if (stu >1000 || stu < 1)
                {
                    Console.WriteLine("에러 :학생수는 1~1000사이의 정수입니다.");
                    return;
                }
                total[i] = new int[Convert.ToInt32(input[0])];
                

                for (int j = 0; j < total[i].Length; j++)
                {
                    total[i][j] = Convert.ToInt32(input[j+1]);
                    if (total[i][j] > 100 || total[i][j] < 0)
                    {
                        Console.WriteLine("에러 : 점수는 0~100사이의 정수입니다.");
                        return;
                    }
                }
            }
            double[] avg = new double[cls];
            for (int i = 0; i < avg.Length; i++)
            {
                avg[i] = Avg(total[i]);
            }
            foreach (double v in avg)
            {
                Console.WriteLine("{0:00.000}%", v * 100);
            }
        }
        static double Avg(int[] arr)
        {
            double sum = 0;
            double cnt = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
            for(int i = 0; i < arr.Length; i++)
            {
                if(arr[i]>sum / arr.Length)
                {
                    cnt += 1;
                }
            }
            
            
            return cnt/arr.Length;
        }
    }
}

에러 뜰시 그냥 강제종료 되게 설정
근데 오답임
쓰래기게임 그냥

'C# > 문제해결' 카테고리의 다른 글

0719 Canvas가 여러개일때  (0) 2022.07.19
0712 총알 발사 방향 바꾸기  (0) 2022.07.12
0701 시간  (0) 2022.07.01
0620 오  (2) 2022.06.20
0616 액션안에 액션  (0) 2022.06.16