JAVAIARY

프로그래머스) 연속된 수의 합 본문

examplePractice

프로그래머스) 연속된 수의 합

shiherlis 2023. 2. 28. 22:38

문제: https://school.programmers.co.kr/learn/courses/30/lessons/120923

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

class Solution {
    public int[] solution(int num, int total) {
        int sum =0;
        for(int i = 0; i< num; i++){
            sum += i;
        }
        int start = (total-sum)/num;
        int[] answer = new int[num];
      
        for(int i =0; i < num; i++){
            answer[i] = start + i;
        }
        return answer;
    }
}
  • total = num *초항 + 1 ~ (num - 1) 까지의 합으로 일반식을 써서 
    초항을 구한 다음 등차수열을 합함
  • 등차수열을 알고 있어서 후다닥 풀어버림

'examplePractice' 카테고리의 다른 글

프로그래머스) H-Index  (0) 2023.03.04
프로그래머스) 약수 구하기  (0) 2023.03.01
프로그래머스) 구명보트  (0) 2023.02.26
프로그래머스) 둘만의 암호  (0) 2023.02.25
프로그래머스) 종이자르기  (0) 2023.02.24