프로그래머스 : 연속된 수의 합
2022. 12. 13. 15:00
package practice;
class Solution {
public int[] solution(int num, int total) {
int[] answer = new int[num];
int centerNum = total / num; // 4
int subNum = total % num == 0 ? num/2 : num/2-1;
int startNum = centerNum - subNum;
for (int i = 0; i < answer.length ; i++) {
answer[i] = startNum+i;
}
return answer;
}
}
public class Programmers {
public static void main(String[] args) {
int num = 5;
int total = 5;
System.out.println(new Solution().solution(num, total));
}
}
이 문제의 핵심은.. 주어진 숫자를 가지고 시작하는 숫자를 어떻게 찾아내느냐가 관건 이었던것 같다..
1일 1문제 운동 파이팅!

'java > Exam' 카테고리의 다른 글
프로그래머스 : 진료 순서 정하기 (0) | 2022.12.30 |
---|---|
프로그래머스 : 치킨 쿠폰 (0) | 2022.12.19 |
프로그래머스 : 다음에 올 숫자 (0) | 2022.12.12 |
백준 : 8958 OX퀴즈 (0) | 2022.09.28 |
프로그래머스 : 2018 KAKAO BLIND RECRUITMENT (0) | 2022.09.28 |