posted by 코딩 공부중 2020. 3. 20. 12:33

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

  • x는 -10000000 이상, 10000000 이하인 정수입니다.
  • n은 1000 이하인 자연수입니다.

입출력 예

xnanswer

2 5 [2,4,6,8,10]
4 3 [4,8,12]
-4 2 [-4, -8]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
  public long[] solution(int x, int n) {
      int count = 0;
      long sum = x;
      
      ArrayList<Long> result = new ArrayList<>();
        while(count<n) {
            result.add(sum);
            sum = sum+x;
            count++;
        }
      long[] answer = new long[result.size()];
        for(int i=0; i<answer.length; i++) {
            answer[i] = result.get(i).longValue();
            System.out.print(answer[i]);
            
        }
      return answer;
  }
}
cs

'java' 카테고리의 다른 글

(프로그래머스)하샤드 수  (0) 2020.03.20
(프로그래머스)콜라츠 추측  (0) 2020.03.20
(프로그래머스)예산  (0) 2020.03.20
(프로그래머스)이상한 문자 만들기  (0) 2020.03.20
(프로그래머스)시저암호  (0) 2020.03.20