[알고리즘]프로그래머스 0525

정제철·2023년 5월 25일
0

알고리즘

목록 보기
3/12
post-thumbnail

1. 피자 나눠 먹기

  • 문제 설명
    머쓱이네 피자가게는 피자를 일곱 조각으로 잘라 줍니다. 피자를 나눠먹을 사람의 수 n이 주어질 때, 모든 사람이 피자를 한 조각 이상 먹기 위해 필요한 피자의 수를 return 하는 solution 함수를 완성해보세요.

  • 제한사항
    1 ≤ n ≤ 100

  • 입출력 예
    n result
    7 1
    1 1
    15 3

- 내풀이

#include <cmath>
using namespace std;

int solution(int n) {
    double x = n/7.0;
    return ceil(x);
}

ceil(), floor(), round()

- 헤더에 정의되어 있다.

  • floor(x): 주어진 실수 x를 내림한 정수를 반환합니다. floor(3.7)은 3을 반환합니다.

  • ceil(x): 주어진 실수 x를 올림한 정수를 반환합니다. ceil(3.2)는 4를 반환합니다.

  • round(x): 주어진 실수 x를 반올림한 정수를 반환합니다. round(3.7)은 4를 반환하고, round(3.2)는 3을 반환합니다.

int solution(int n)
{
    return n % 7 == 0 ? n / 7 : n / 7 + 1;
}

2. 배열 뒤집기

  • 문제 설명
    정수가 들어 있는 배열 num_list가 매개변수로 주어집니다. num_list의 원소의 순서를 거꾸로 뒤집은 배열을 return하도록 solution 함수를 완성해주세요.

  • 제한사항
    1 ≤ num_list의 길이 ≤ 1,000
    0 ≤ num_list의 원소 ≤ 1,000

  • 입출력 예
    num_list -> result
    [1, 2, 3, 4, 5][5, 4, 3, 2, 1]
    [1, 1, 1, 1, 1, 2][2, 1, 1, 1, 1, 1]
    [1, 0, 1, 1, 1, 3, 5][5, 3, 1, 1, 1, 0, 1]

- 내풀이

for(int i =0; i<num_list.size();i++)
{
    answer.push_back(num_list.back());
    num_list.pop_back();
}

for(int i =0; i<num_list.size();i++)로 반복문을 작성했는데
num_list.pop_back();의 영향으로 반복문의 끝지점에 있는 벡터의 값이 줄어들어서 5개의 값이 들어있는 배열이 출력되어야하는데 3개만 출력되는 문제를 겪었다.
그래서 문제인 부분만 주석처리를 했는데 5개의 값을 가진 배열로 출력되었다.
그렇기 때문에 그 부분이 문제라고 판단하여 풀어냈다.
그래서 조건문의 끝지점 을 따로 x라고 변수화하여 고정시켰다.

#include <vector>

using namespace std;

vector<int> solution(vector<int> num_list) {
    vector<int> answer;
    int x=num_list.size();
    for(int i =0; i<x;i++)
    {
        answer.push_back(num_list.back());
        num_list.pop_back();
    }
    return answer;
}

- 우수풀이

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> num_list) {
    vector<int> answer;
    reverse(num_list.begin(), num_list.end());

    return num_list;
}

- 정석풀이

int i = 배열의 길이-1 ; i가 = 0이 될때까지 i를 뺀다.
이는 (int i = 0; i < 배열의 길이; i++ )와 같다.
배열의 인덱스를 구해야 하기때문에 -1씩 해준다.
뒤의 값부터 answer에 담는다.

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> num_list) {
    vector<int> answer;
    for(int i = num_list.size()-1; i >= 0; i--){
        answer.push_back(num_list[i]);
    }
    return answer;
}

3. 배열 자르기

  • 문제 설명
    정수 배열 numbers와 정수 num1, num2가 매개변수로 주어질 때, numbers의 num1번 째 인덱스부터 num2번째 인덱스까지 자른 정수 배열을 return 하도록 solution 함수를 완성해보세요.

  • 제한사항
    2 ≤ numbers의 길이 ≤ 30
    0 ≤ numbers의 원소 ≤ 1,000
    0 ≤num1 < num2 < numbers의 길이

  • 입출력 예
    numbers / num1 /num2 -> result
    [1, 2, 3, 4, 5] /1/ 3 -> 2, 3, 4
    [1, 3, 5] /1 /2 -> [3, 5]

- 내풀이

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> numbers, int num1, int num2) {
    vector<int> answer;
    for(int i = num1;i<=num2;i++ )
    {
        answer.push_back(numbers.at(i));
    }
    return answer;
}

- 정석풀이

#include <vector>

using namespace std;

vector<int> solution(vector<int> numbers, int num1, int num2) {
    vector<int> answer;
    answer.assign(numbers.begin()+num1, numbers.begin()+num2+1);
    return answer;
}

4. 점의 위치 구하기

  • 문제 설명
    사분면은 한 평면을 x축과 y축을 기준으로 나눈 네 부분입니다. 사분면은 아래와 같이 1부터 4까지 번호를매깁니다.

    x 좌표와 y 좌표가 모두 양수이면 제1사분면에 속합니다.
    x 좌표가 음수, y 좌표가 양수이면 제2사분면에 속합니다.
    x 좌표와 y 좌표가 모두 음수이면 제3사분면에 속합니다.
    x 좌표가 양수, y 좌표가 음수이면 제4사분면에 속합니다.
    x 좌표 (x, y)를 차례대로 담은 정수 배열 dot이 매개변수로 주어집니다. 좌표 dot이 사분면 중 어디에 속하는지 1, 2, 3, 4 중 하나를 return 하도록 solution 함수를 완성해주세요.

  • 입출력 예
    dot : result
    [2, 4] : 1
    [-7, 9] : 2

- 내코드

using namespace std;

int solution(vector<int> dot)
{
    int answer = 0;
    if(dot[0] >0 && dot[1]>0) return 1;
    else if(dot[0] <0 && dot[1]>0) return 2;
    else if(dot[0] <0 && dot[1]<0) return 3;
    else return 4;
}
profile
성공의 반대는 실패가 아닌 도전하지 않는 것이다.

0개의 댓글