C#

백준 C# 4153 문제 풀이

psb08 2024. 9. 17. 14:11
728x90

문제 링크 : https://www.acmicpc.net/problem/4153

 

백준 브론즈 3 문제 입니다.

 

내 코드

 while (true)
 {
     string[] input = Console.ReadLine().Split();

     int a = int.Parse(input[0]);
     int b = int.Parse(input[1]);
     int c = int.Parse(input[2]);

     if (a == 0 && b == 0 && c == 0)
     {
         break;
     }
     if (a * a + b * b == c * c)
     {
         Console.WriteLine("right");
         continue;
     }
     if (a * a + c * c == b * b)
     {
         Console.WriteLine("right");
         continue;
     }
     if (b * b + c * c == a * a)
     {
         Console.WriteLine("right");
         continue;
     }
     Console.WriteLine("wrong");
 }

 

 

코드 풀이 해석 내용

while (true)

while (true)는 무한 루프를 생성합니다.

이 루프는 특정 조건이 충족될 때까지 계속해서 반복됩니다.

 

string[] input = Console.ReadLine().Split();

한 줄의 입력을 받습니다.

입력된 문자열은 공백을 기준으로 나눕니다. 그 다음 배열 input에 저장됩니다.

 

int a = int.Parse(input[0]);
int b = int.Parse(input[1]);
int c = int.Parse(input[2]);

배열 input의 첫 번째, 두 번째, 세 번째 요소를 각각 정수 a,b,c로 변환합니다.

 

if (a == 0 && b == 0 && c == 0)
{
    break;
}

만약 a,b,c가 모두 0이라면, break를 통해 루프를 종료합니다.

이는 입력의 종료를 나타냅니다.

 

if (a * a + b * b == c * c)
{
    Console.WriteLine("right");
    continue;
}

a와 b제곱의 합이 c의 제곱과 같으면 "right"를 출력합니다.

루프의 처음으로 돌아갑니다.

 

if (a * a + c * c == b * b)
{
    Console.WriteLine("right");
    continue;
}

a와 c제곱의 합이 b제곱과 같으면 "right"를 출력합니다.

루프의 처음으로 돌아갑니다.

 

if (b * b + c * c == a * a)
{
    Console.WriteLine("right");
    continue;
}

b와 c제곱의 합이 a제곱과 같으면 "right"를 출력합니다.

루프의 처음으로 돌아갑니다.

 

Console.WriteLine("wrong");

위의 세 가지 조건 중 어느 것도 만족하지 않으면 "wrong"을 출력합니다.

 

 

실행 결과

실행 결과

 

728x90

'C#' 카테고리의 다른 글

백준 C# 2775 문제 풀이  (4) 2024.09.18
백준 C# 30802 문제 풀이  (0) 2024.09.18
백준 C# 1181 문제 풀이  (0) 2024.09.16
백준 C# 2839 문제 풀이  (0) 2024.09.16
백준 C# 8958 문제 풀이  (0) 2024.09.15