코딩 테스트/백준

백준 - [단계별로 풀어보기 약수, 배수와 소수 1] 5086 배수와 약수

devrabbit22 2026. 3. 26. 04:13

이번 문제는 입력된 두 수의 관계를 판단하여, 약수인지 배수인지 또는 해당되지 않는지를 조건에 따라 출력하는 문제이다.
기초적인 수학 개념인 약수와 배수의 정의를 알고 있다면 쉽게 해결할 수 있는 문제였다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    internal class math1
    {
        static void Main(string[] args)
        {
            //백준 5086 배수와 약수
            while(true)
            {
                string[] input = Console.ReadLine().Split();

                int i = int.Parse(input[0]);
                int j = int.Parse(input[1]);

                if (i == 0 && j == 0)
                    break;
                else if (j % i == 0)     // b가 a의 배수라면 a는 b의 약수
                {
                    Console.WriteLine("factor");
                }
                else if(i % j == 0)     // a가 b의 배수라면 b는 a의 약수
                {
                    Console.WriteLine("multiple");
                }
                else
                {
                    Console.WriteLine("neither");
                }

            }
        }
    }
}

출력 결과