1 분 소요

한 문제를 가지고 C#과 C++을 이용해 풀어 나갈 것이다. 앞으로 C++를 주력으로 나갈 것이다.

그럼 Start !

Question

2839Q


Solution

  • 5와 3의 배수로 먼저 탐색을 한다
  • 5와 3의 배수가 아니면 모두 -1이라는 값이 나온다
  • 5의 배수인지 확인하고 아니라면 3의 배수로 확인한 뒤 결과를 내면 될것같다.

Cord

[C#]

using System;

class Program
{
    static void Main()
    {
        int input = int.Parse(Console.ReadLine());
        int result = 0;

        while(input > 0)
        {
            if (input % 5 == 0)
            {
                input -= 5;
                result++;
            }
            else if (input % 3 == 0)
            {
                input -= 3;
                result++;
            }
            else if (input > 5)
            {
                input -= 5;
                result++;
            }
            else
            {
                result = -1;
                break;
            }
        }
        Console.WriteLine(result);
    }
}

[C++]

#include<iostream>

using namespace std;

int main()
{
    int input;
    int result = 0;
    
    cin >> input;
    
    while(input > 0)
    {
        if (input % 5 == 0)
        {
            input -= 5;
            result++;
        }
        else if (input % 3 == 0)
        {
            input -= 3;
            result++;
        }
        else if (input > 5)
        {
            input -= 5;
            result++;
        }
        else 
        {
            result = -1;
            break;
        }
    }
    cout << result;
    return 0;
}

Result

2839Sharp

2839

아직 알고리즘 적으로 C#과 C++에 차이는 없었다.
유니티를 하다보니 C#과 C++의 문법이 너무 헤깔린다.
다시 차근차근 알아가야 할 것 같다.