[프로그래머스/C++]Lv.0 - 암호 해독

YH J·2023년 4월 17일
0

프로그래머스

목록 보기
21/168

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/120892

내 풀이

for문을 code만큼 ++하면서 돌림

내 코드

#include <string>
#include <vector>

using namespace std;

string solution(string cipher, int code) {
    string answer = "";
    
    for(int i = code - 1; i < cipher.length(); i += code)
    {
        answer += cipher[i];
    }
    
    return answer;
}

다른 사람의 풀이

#include <string>
#include <vector>

using namespace std;

string solution(string cipher, int code) {
    string answer = "";
    for(int i = 0; i<cipher.size(); i++) {
        if((i+1)%code == 0) {
            answer.push_back(cipher[i]);
        }
    }
    return answer;
}

다른 사람의 풀이 해석

for문을 i++로 돌리면서 code에 맞는 인덱스마다 추가하는듯? 탐색 횟수가 더 많은 것 같다.

profile
게임 개발자 지망생

0개의 댓글