최종 프로젝트

2024.03.19 내일배움캠프 58일차 TIL_Unity (최종 프로젝트, 알고리즘, 기술 면접)

mongle_0l 2024. 3. 19. 16:46

알고리즘 코드카타 41일차

K번째수

문제 설명
배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.2에서 나온 배열의 3번째 숫자는 5입니다.배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.
using System;

public class Solution {
    public int[] solution(int[] array, int[,] commands) {
        int[] answer = new int[commands.GetLongLength(0)];
        
        for(int i = 0; i < commands.GetLength(0); i++)
        {
            int start = commands[i, 0];
            int end = commands[i, 1];
            int find = commands[i, 2];
            
            int[] temp = new int[end - start + 1];
            for(int a = 0; a < temp.Length; a++) { temp[a] = array[a + start - 1]; }
            Array.Sort(temp);
            answer[i] = temp[find - 1];
        }
        return answer;
    }
}


기술면접 연습하기 10일차

제네릭이란 무엇인가요?
제네릭(Generic)은 프로그래밍 언어의 한 기능으로, 코드를 작성할 때 특정 데이터 형식을 지정하지 않고 추상적인 형식으로 유지할 수 있는 기능이다. 제네릭을 사용하면 함수나 클래스가 다양한 형식의 데이터를 다룰 수 있으며, 코드의 재사용성과 유연성을 향상시킬 수 있다.

최종 프로젝트

public class CharacterExperience : MonoBehaviour
{
    [SerializeField] private int levelMax;
    [SerializeField] private int expBase;
    [SerializeField] private int incrementalvalue;

    public int Level { get; set; }

    private float expActualTemp;
    private float ExpRequiredNextLevel;

    private void Start()
    {
        Level = 1;
        ExpRequiredNextLevel = expBase;
        UpdateBarExp();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.B))
        {
            AddExperience(2f);
        }
    }

    public void AddExperience(float expObtained)
    {
        if (expObtained > 0f)
        {
            float NewLevelRepresentative = ExpRequiredNextLevel - expActualTemp;
            if(expObtained >= NewLevelRepresentative)
            {
                expObtained -= NewLevelRepresentative;
                UpdateLevel();
                AddExperience(expObtained);
            }
            else
            {
                expActualTemp += expObtained;
                if(expActualTemp == ExpRequiredNextLevel)
                {
                    UpdateLevel();
                }
            }
        }

        UpdateBarExp();
    }

    private void UpdateLevel()
    {
        if(Level < levelMax)
        {
            Level++;
            expActualTemp = 0f;
            ExpRequiredNextLevel *= incrementalvalue;
        }
    }

    private void UpdateBarExp()
    {
        UIManager.instance.UpdateExpPersonality(expActualTemp, ExpRequiredNextLevel);
    }
}
경험치와 레벨을 관리하는 코드이다.
현재는 키보드 B을 누르면 경험치가 차며 점점 갈 수록 경험치가 늦게 차오르는 것을 확인 할 수있다!

 


작은 트러블 슈팅

    public void AddExperience(float expObtained)
    {
        if (expObtained < 0f)
        {
            float NewLevelRepresentative = ExpRequiredNextLevel - expActualTemp;
            if(expObtained >= NewLevelRepresentative)
            {
                expObtained -= NewLevelRepresentative;
                UpdateLevel();
                AddExperience(expObtained);
            }
            else
            {
                expActualTemp += expObtained;
                if(expActualTemp == ExpRequiredNextLevel)
                {
                    UpdateLevel();
                }
            }
        }

        UpdateBarExp();
    }
작은 실수가 있었다..
if (expObtained < 0f)
이 부분을 0보다 크게 한것!
아무리 B을 눌러도 경험치 변화가 없었다.
많은 시간을 소비하지 않아서 다행이였다.
if (expObtained > 0f)
이부분은 0보다 크다 라고하면 된다!