백준 온라인 코딩 문제풀이
Baekjoon Online Judge
Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.
www.acmicpc.net
코드 참고
wny0320 (박 상운)
Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
replit.com
10818, 2562, 3052번 중 일부에 대해서 다룸
리스트에 array보다 좀 더 유용한 기능이 많아 List로 작성함
2562
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void Main(string[] args)
{
List<int> num = new List<int>();
List<int> sort_num = new List<int>();
for(int i = 0; i < 9; i++)
{
num.Add(Int32.Parse(Console.ReadLine()));
}
sort_num = num.ToList();
sort_num.Sort();
int find_num = sort_num[sort_num.Count - 1];
Console.WriteLine(find_num);
Console.WriteLine((num.IndexOf(find_num))+1);
}
}
2562번은 크게 어려운 부분은 없었지만 계속 값에 대한 오류가 떠서 무엇인가 찾아봄
원인을 찾으려고 for문을 통해 sort_num과 num 모두 값을 출력해봄
sort_num = num으로 처음에 코드를 작성하였더니 sort_num이 얕은 복사를 하여 num의 값도 sort가 되는 것을 확인
sort_num을 깊은 복사를 하기 위해 sort_num = num_ToList로 코드를 작성하여 문제를 해결함
3052
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void Main(string[] args)
{
List<int> num = new List<int>();
List<int> div_num = new List<int>();
for(int i = 0; i < 10; i++)
{
num.Add(Int32.Parse(Console.ReadLine()));
}
for (int i = 0; i < 10; i++)
{
div_num.Add(num[i]%42);
}
div_num = div_num.Distinct().ToList();
Console.WriteLine(div_num.Count);
}
}
3052번은 입력값을 받은 리스트의 인덱스를 참조하여 다른 리스트에 42로 나눈 값을 저장하였음
-지금 보니 입력을 받자마자 42로 나누어 리스트에 저장하는 것이 경제성이 좋아보임
그리고 리스트 내에 있는 42로 나눈 나머지가 겹쳐서 들어 있는 것을 div_num.Distinct().ToList()를 사용
값이 중복되는 것을 삭제 후 Count를 통하여 리스트의 갯수를 출력하여 문제를 풀음
'BaekJoon[C#]' 카테고리의 다른 글
함수 1065 (0) | 2022.09.25 |
---|---|
1차원 배열 4344 (0) | 2022.09.24 |
반복문 1110 (0) | 2022.09.20 |
반복문 2439 (0) | 2022.09.19 |
반복문 15552 (0) | 2022.09.18 |