코드카타

29. 제일 작은 수 제거하기

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;



//정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요.
//단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 - 1을 채워 리턴하세요.
//예를들어 arr이[4, 3, 2, 1]인 경우는[4, 3, 2]를 리턴 하고, [10]면[-1]을 리턴 합니다.

vector<int> solution(vector<int> arr) {
    vector<int> answer;
    if (arr.size() == 1)
    {
        answer.push_back(-1);
        return answer;
    }
    int min_index = min_element(arr.begin(), arr.end()) - arr.begin();
    arr.erase(arr.begin() + min_index);
    return arr;
}

 

이 문제를 풀며 벡터의 최소 최대값과 위치를 구하는 함수를 알게 되었다.

max_element, min_element

 

최대/최소값 구하기 : *max_element(arr.begin(), arr.end());

  • max_element는 이터레이터를 반환하기 때문에 *를 꼭 붙여줘야 한다.

 

최대/최소값의 인덱스 번호 구하기 : max_element(arr.begin(), arr.end()) - arr.begin();

  • max_element는 이터레이터를 반환하기 때문에 결과값에 시작점을 빼면 인덱스 번호를 알 수 있다.

30. 가운데 글자 가져오기

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

//단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 
//단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.

string solution(string s) {
    string answer = "";
    int index = s.length() / 2;
    if (s.length() % 2 == 1)
    {
        return answer = s.at(index);
    }
    answer = s.at(index - 1);
    answer.push_back(s.at(index));
    return answer;
}

 

string 의 substr을 써야 했는데 자주 사용을 안하다 보니 생각을 못하게 됐다..

만약에 나중에 다시 풀게 된다 면 answer에 답을 대입하는 과정을 substr()을 사용해 구현할 것 같다.


프로젝트

백그라운드 노래와 효과음 동시에 출력 테스트 구현

#include <SFML/Audio.hpp>
#include <iostream>
#include <vector>
#include <Windows.h>
#include <map>
using namespace std;

enum SFXType
{
    Select,
    LevelUp,
    Heal,

};

class AudioManager
{
public:
    AudioManager()
    {
        InitSound();
    }

    // 백그라운드 노래 재생
    void PlayBGM(const std::string& path, float volume) {
        bgm.openFromFile(path);
        bgm.setLoop(true);
        bgm.setVolume(volume);
        bgm.play();
    }


    // 노래 정지
    void StopBGM() {
        bgm.stop();
    }

    // 효과음 초기화
    void InitSound() {

        // 선택음 설정
        buffers[SFXType::Select].loadFromFile("D:/Project/Memo/SoundTest/SoundTest/SFX/snd_select.wav");
        pools[SFXType::Select].resize(1);
        for (auto& s : pools[SFXType::Select])
        {
            s.setBuffer(buffers[SFXType::Select]);
        }

        // 레벨 업
        buffers[SFXType::LevelUp].loadFromFile("D:/Project/Memo/SoundTest/SoundTest/SFX/poketmon/pokemon-Level-up-Sound.ogg");   
        pools[SFXType::LevelUp].resize(1);
        for (auto& s : pools[SFXType::LevelUp])
        {
            s.setBuffer(buffers[SFXType::LevelUp]);
        }

        // 체력 회복
        buffers[SFXType::Heal].loadFromFile("D:/Project/Memo/SoundTest/SoundTest/SFX/poketmon/Pokemon-Center.ogg");   
        pools[SFXType::Heal].resize(1);
        for (auto& s : pools[SFXType::Heal])
        {
            s.setBuffer(buffers[SFXType::Heal]);
        }
      
        
    }

    // 효과음 재생 (여러 번) 
    void PlaySFX(SFXType type, float volume) {
        auto& pool = pools[type];
        for (auto& s : pool) {
            if (s.getStatus() != sf::Sound::Playing) {
                s.setVolume(volume);
                s.play();
                break;
            }
        }
    }

private:
    // 🔥 수명 유지용 멤버들
    sf::Music bgm;

    map<SFXType, sf::SoundBuffer> buffers;
    map<SFXType, std::vector<sf::Sound>> pools;    
};

void MainMusic()
{
    
    cin.get();
        // =======================
    //// 1. 배경음 (BGM)
    //// =======================
    //if (!bgm.openFromFile("D:/Project/Memo/SoundTest/SoundTest/music/mus_shop.ogg")) {
    //    std::cout << "BGM 로드 실패\n";
    //    return;
    //}

    //bgm.setLoop(true);
    //bgm.setVolume(30.f);
    //bgm.play();   // 🔥 여기서부터 계속 재생됨

}


int main() {
    AudioManager* player = new AudioManager();
    player->PlayBGM("D:/Project/Memo/SoundTest/SoundTest/music/mus_shop.ogg", 30);

    player->PlaySFX(SFXType::Select, 70.0);
    Sleep(500);

    player->PlaySFX(SFXType::LevelUp, 20.0);
    Sleep(5000);

    player->PlaySFX(SFXType::Heal, 20.0);
    Sleep(500);

    while (1)
    {
		// 위의 구문을 타고 사운드 테스트를 위해 종료 방지
    }
    delete player;
    return 0;
}

+ Recent posts