프로그래머스 코딩테스트 연습 - x만큼 간격이 있는 n개의 숫자 (Level 1)
PS/Programmers

프로그래머스 코딩테스트 연습 - x만큼 간격이 있는 n개의 숫자 (Level 1)

programmers

 

문제

프로그래머스 코딩테스트 연습 - x만큼 간격이 있는 n개의 숫자 (Level 1)

https://programmers.co.kr/learn/courses/30/lessons/12954 

 

코딩테스트 연습 - x만큼 간격이 있는 n개의 숫자

함수 solution은 정수 x와 자연수 n을 입력 받아, x부터 시작해 x씩 증가하는 숫자를 n개 지니는 리스트를 리턴해야 합니다. 다음 제한 조건을 보고, 조건을 만족하는 함수, solution을 완성해주세요.

programmers.co.kr


사용 알고리즘

- Brute force

 

풀이

Brute force

 

나의 코드

#include <string>
#include <vector>

using namespace std;

vector<long long> solution(int x, int n) {
    vector<long long> answer;
    for(int i=1,j=x;i<=n;i++,j+=x) answer.push_back(j);
    return answer;
}

 

남의 코드(좋아요 최다 코드)

#include <string>
#include <vector>

using namespace std;

vector<long long> solution(int x, int n) {
    vector<long long> answer(n, x);

    for (int i = 1; i < n; i++)
        answer[i] = answer[i - 1] + x;

    return answer;
}

 

728x90