2022. 12. 28. 17:59 자기개발/코딩테스트
C++ (코딩테스트) - 우박수열 정적분(프로그래머스)
문제
https://school.programmers.co.kr/learn/courses/30/lessons/134239
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
내가 쓴 코드
#include <string>
#include <vector>
using namespace std;
vector<double> solution(int k, vector<vector<int>> ranges) {
vector<double> answer(0);
vector<float> ubak_con(0);
while (true)
{
if (k <= 1) break;
int temp = k;
k = (k % 2 == 0) ? k /= 2 : k = 3 * k + 1;
ubak_con.push_back((float)(temp + k) / 2);
}
for (int i = 0; i < ranges.size(); i++)
{
double sum = 0;
int start = ranges[i][0];
int end = ubak_con.size() + ranges[i][1];
for (int j = start; j < end; j++)
{
sum += (double)ubak_con[j];
}
if (start > end) { sum = -1; }
answer.push_back(sum);
}
return answer;
}
(signal: segmentation fault (core dumped)) 에러나서 바꿈
for (int j = ranges[i][0]; j < ubak_con.size() + ranges[i][1]; j++)
{
sum += (double)ubak_con[j];
}
//(signal: segmentation fault (core dumped)) 에러나서 아래로 바꿈
int start = ranges[i][0];
int end = ubak_con.size() + ranges[i][1];
for (int j = start; j < end; j++)
{
sum += (double)ubak_con[j];
}
'자기개발 > 코딩테스트' 카테고리의 다른 글
C++ (코딩테스트) - 회의실 배정(백준) (0) | 2023.09.19 |
---|---|
C++ (코딩테스트) - 숫자 카드 나누기(프로그래머스) (0) | 2022.12.29 |
C++ (코딩테스트) - 디펜스 게임(프로그래머스) (0) | 2022.12.28 |
C++ (코딩테스트) - 하노이의 탑(프로그래머스) (0) | 2022.11.08 |
C++ (코딩테스트) - 메뉴 리뉴얼(프로그래머스) (0) | 2022.11.01 |