๋ฐ์ํ
x๋งํผ ๊ฐ๊ฒฉ์ด ์๋ n๊ฐ์ ์ซ์
ํจ์ solution์ ์ ์ x์ ์์ฐ์ n์ ์ ๋ ฅ ๋ฐ์, x๋ถํฐ ์์ํด x์ฉ ์ฆ๊ฐํ๋ ์ซ์๋ฅผ n๊ฐ ์ง๋๋ ๋ฆฌ์คํธ๋ฅผ ๋ฆฌํดํด์ผ ํฉ๋๋ค. ๋ค์ ์ ํ ์กฐ๊ฑด์ ๋ณด๊ณ , ์กฐ๊ฑด์ ๋ง์กฑํ๋ ํจ์, solution์ ์์ฑํด์ฃผ์ธ์.
์ ํ ์กฐ๊ฑด
* x๋ -10000000 ์ด์, 10000000 ์ดํ์ธ ์ ์์
๋๋ค.
* n์ 1000 ์ดํ์ธ ์์ฐ์์
๋๋ค.
์ ์ถ๋ ฅ ์
x | n | answer |
---|---|---|
2 | 5 | [2,4,6,8,10] |
4 | 3 | [4,8,12] |
-4 | 2 | [-4, -8] |
๋ฌธ์ ํ์ด
i๋ฅผ n๊ณผ ๊ฐ์์ง๊ธฐ ์ ๊น์ง ์ฆ๊ฐ์ํค๊ณ answer[i]์ answer[i-1]+x๋ฅผ ์ ์ฅ์ํจ๋ค. ๊ทธ๋ ๊ฒ ํ๋ฉด x์ (x * n)์ ๋ต ์ฌ์ด์ ์ซ์๋ค์ด x๋งํผ์ ๊ฐ๊ฒฉ์ผ๋ก answer์ ์ถ๋ ฅ๋๋ค.
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;
}
๋ค๋ฅธ ํ์ด ๋ฐฉ์
์ถ์ฒ : ํ๋ก๊ทธ๋๋จธ์ค ์ค์ฟจ
for๋ฌธ์ ์ด์ฉํ์ฌ answer์ push_back((i+1)*x); ํด์ฃผ๋ ๋ฐฉ์๋ ์๋ค.
vector<long long> solution(int x, int n) {
vector<long long> answer;
for(int i=0; i < n; i++){
answer.push_back((i+1)*x);
}
return answer;
}
๋ฐ์ํ
๋๊ธ