๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[PGS] Lv.0 (์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ž…๋ฌธ) 8์ผ์ฐจ ๋ฌธ์ œ

728x90

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค C++

 

๋ฐฐ์—ด ์ž๋ฅด๊ธฐ

vector<int> solution(vector<int> numbers, int num1, int num2) {
    vector<int> answer;
    for(num1; num1<=num2; ++num1){
        answer.push_back(numbers[num1]);
    }
    return answer;
}

์™ธ๊ณ„ํ–‰์„ฑ์˜ ๋‚˜์ด

string solution(int age) {
    string alpha = "abcdefghij";
    string answer = "";
    int tmp = age;
    if(tmp==1000) return "baaa";
    if(tmp>99) {
        answer.push_back(alpha[age / 100]);
        age -= 100*(age/100);
    }
    if(tmp>9) answer.push_back(alpha[age / 10]);
    answer.push_back(alpha[age % 10]);
    return answer;
}

→ ์ƒˆ๋กœ์šด ํ’€์ด

string answer = to_string(age);
for(auto& v : answer) v += 'a'-'0';
return answer;
// for(๋ฐ˜๋ณต๋  ์ง€์—ญ๋ณ€์ˆ˜ : ๋ฐฐ์—ด) -> ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต๋จ
// ๊ทผ๋ฐ ์ด ํ’€์ด ์ดํ•ด๊ฐ€ ์•ˆ๋œ๋‹ค!!

 

์ง„๋ฃŒ์ˆœ์„œ ์ •ํ•˜๊ธฐ

vector<int> solution(vector<int> emergency) {
    vector<int> answer;
    int seq[10] = {0,};
    for(int i=0; i<emergency.size(); ++i){
        int max_index = max_element(emergency.begin(), emergency.end()) - emergency.begin();
        seq[max_index] = i+1;
        emergency[max_index] = 0;
    }
    for(int i : seq){
        if(i!=0) answer.push_back(i);
    }
    return answer;
}

์ˆœ์„œ์Œ์˜ ๊ฐœ์ˆ˜

int solution(int n) {
    int answer = 0;
    for(int i=1; i<=n; ++i){
        if(n%i == 0) answer++;
    }
    return answer;
}


โœ๐Ÿป ๋ฐ˜๋ณต๋ฌธ ์งง๊ฒŒ ์“ฐ๊ธฐ for(๋ฐ˜๋ณต๋  ์ง€์—ญ๋ณ€์ˆ˜ : ๋ฐฐ์—ด) -> ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ๋ฐฐ์—ด ๋‚ด์šฉ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ˜๋ณต๋จ

 

โœ๐Ÿป ๋ฒกํ„ฐ์˜ ์ตœ๋Œ“๊ฐ’ → int max = *max_element(v.begin(), v.end()); โœ๐Ÿป ๋ฒกํ„ฐ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ฐ€์ง„ ์ธ๋ฑ์Šค → int max_index = max_element(v.begin(), v.end()) - v.begin();

728x90