[프로그래머스] 120923 연속된 수의 합 / 문제분석 / Java, Python3 정답

programmers

쉬운 문제라도 문제를 푸는 방법은 다양합니다.

더 명확하고 깔끔한 문제 해결 방법을 아신다면 자유롭게 말씀해주세요.

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

프로그래머스 저작권을 침해하지 않기 위해

문제 보시고 싶으시면 버튼을 눌러주세요.

답변 형식
프로그래머스 120923 자바
프로그래머스 120923 파이썬

배열 총 합과 배열 원소 수를 받아서 연속된 숫자 배열을 찾으면 됩니다.

풀이 조건

1. 배열의 중간에 위치하는 수(연속된 수의 중위값)를 구해주세요.

 total과 num으로 쉽게 구할 수 있습니다.

2. 1번의 중위값과 num으로 배열의 시작 값을 구할 수 있습니다.

3. 반복문을 통해 배열을 완성해주세요.

 연속된 수이기 때문에 반복문이 쉽습니다.


숫자 올림 또는 내림 함수를 사용하신다면 더 간결한 코드를 작성하실 수 있습니다.


혹시 설명이 부족하시다면 프로그래머스의 테스트케이스를 다음의 방법에 따라 “종이에 손으로” 풀어보시는 것을 권장드립니다.

class Solution {
    public int[] solution(int num, int total) {
        int[] answer = new int[num];
       
        int start;

        if(num%2==0){
            start = total/num-(num2)/2;            
        } else {
            start = total/num-(num1)/2;
        }

        for(int i=0; i<num; i++){
            answer[i] = start;
            start++;
        }  

        return answer;
    }
}
class Solution {
    public int[] solution(int num, int total) {
        int[] answer = new int[num];
        // int 배열 할당

        int start;

        if(num%2==0){
            start = total/num-(num-2)/2;            
        } else {
            start = total/num-(num-1)/2;
        }
        // 시작값 = 중위값 – 시작값에서 중위값까지 숫자 갯수

        for(int i=0; i<num; i++){
            answer[i] = start;
            start++;
        }  
        // for문을 통해 숫자 배열 채우기
       
        return answer;
    }
}
def solution(num, total):
    answer = []    

    if num%2 == 0 :
        start = total//num-(num-2)/2
    else :
        start = total//num-(num-1)/2    

    for i in range(num) :
        answer.append(start)
        start = start+1
   
    return answer
def solution(num, total):
    answer = []    

    if num%2 == 0 :
        start = total//num-(num2)/2
    else :
        start = total//num-(num1)/2
    # 시작값 = 중위값 – 시작값에서 중위값까지 숫자 갯수
       

    for i in range(num) :
        answer.append(start)
        start = start+1
    # for문을 통해 숫자 배열 만들기

    return answer
import math

def solution(num, total):
    answer = []    

    start = total//nummath.floor(((num1)/2))

    for i in range(num) :
        answer.append(start)
        start = start+1
   
    return answer

“[프로그래머스] 120923 연속된 수의 합 / 문제분석 / Java, Python3 정답”의 3개의 댓글

  1. It’s fascinating how online platforms are building communities around gaming now. Not just about the wins, but the shared experience! I’ve seen nunstar casino really emphasize that social aspect, which is a smart move for player retention. It’s about connection, not just chance.

  2. Excellent breakdown of the consecutive sum problem! The approach demonstrates how mathematical elegance meets programming efficiency. This problem-solving mindset translates beautifully across industries. At Super PH, we apply similar systematic thinking to optimize user experience strategies. The Java and Python implementations showcase how different languages can achieve the same elegant solution through different syntactic paths.

댓글 달기

이메일 주소는 공개되지 않습니다.