백준 온라인 코딩 문제풀이
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
10926, 18108, 3003, 10430, 2588, 10171, 10172, 25083번에 해당하는 출력 중 일부 풀이만 다룸.
해당하는 코드를 보고 싶다면 위 replit 이용
3003
using System;
class Program
{
public static void Main(string[] args)
{
string[] chess_input = Console.ReadLine().Split(' ');
int[] chess_num = new int[chess_input.Length];
for (int index = 0; index < chess_input.Length; index++)
{
chess_num[index] = Int32.Parse(chess_input[index]);
if (index <= 1)
{
chess_num[index] = 1 - chess_num[index];
}
else if (index <= 4)
{
chess_num[index] = 2 - chess_num[index];
}
else if (index <= 5)
{
chess_num[index] = 8 - chess_num[index];
}
Console.Write(chess_num[index] + " ");
}
}
}
해당 문제 풀이를 하기 위해 사용한 솔루션은 String에 있는 Split() 메소드를 이용하여 ' '을 기준으로 단어를 슬라이싱 하여 chess_input 배열에 추가
해당 배열의 크기만큼 int 배열을 만들고 for문을 통해 차례로 int형으로 형 변환하였음
이후 for 문을 통해 문제에서 사용하는 체스의 말 갯수 1 1 2 2 2 8 에서 입력받은 체스 말의 갯수 chess_num을 빼서 출력
2588
using System;
class Program
{
public static void Main(string[] args)
{
string num1 = Console.ReadLine();
int num1_int = Int32.Parse(num1);
string num2 = Console.ReadLine();
int[] slice_num2 = new int[3];
int[] result = new int[3];
for (int index = 0; index < 3; index++)
{
slice_num2[index] = (int)Char.GetNumericValue(num2[index]);
}
for (int index = 2; index >= 0; index--)
{
result[2 - index] = num1_int * slice_num2[index];
Console.WriteLine(result[2 - index]);
}
Console.WriteLine(result[0] + result[1] * 10 + result[2] * 100);
}
}
이 문제는 3자리수끼리의 곱셈인데 ReadLine(); 메소드를 이용하여 받은 형식이 String.
String에 인덱스 번호를 사용하면 문자 하나씩 출력 가능
num2에 저장한 입력값을 인덱스를 사용하여 데이터를 빼내어 Char.GetNumericValue를 이용하여 Char에 들어있는 문자가 숫자일 경우 그 숫자의 값을 받아오는 메소드를 활용하여 int형태로 변환하여 저장
만약 Char.GetNumericValue를 사용하지 않고 int로 형변환을 시킬 경우 아스키 코드 값으로 변경되어 저장이 됨
'BaekJoon[C#]' 카테고리의 다른 글
반복문 2439 (0) | 2022.09.19 |
---|---|
반복문 15552 (0) | 2022.09.18 |
반복문 10950 (0) | 2022.09.17 |
조건문 2480 (0) | 2022.09.16 |
입출력과 사칙연산 10869 (0) | 2022.09.14 |