반응형
반응형

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

문제 요약

연속된 n개의 수의 합이 total이 되는 수들의 집합을 반환하는 함수 만들기

 

 

 

풀이
using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int num, int total) {
        int sum = 0;
        int x = 0;
        List<int> list = new List<int>();

        for (int i = 1; i < num; i++) sum += i;
        
        x = (total - sum) / num;

        for (int j = 0; j < num; j++) list.Add(x + j);

        return list.ToArray();
    }
}

오늘 엄청 부지런하게 보냈음!!

반응형

+ Recent posts