코딩해요/C++

[프로그래머스/C++] PCCE 기출문제 1-5번

yenas0 2024. 8. 27. 00:17
반응형

 

1번/출력

 

#include <iostream>

using namespace std;

int main(void) {
    string msg = "Spring is beginning";
    int val1 = 3;
    string val2 = "3";
    
    cout << msg << endl;
    cout << val1 + 10 << endl;
    cout << val2 + "10" << endl;
    
    return 0;
}

 

 

2번/피타고라스의 정리

#include <iostream>

using namespace std;

int main(void) {
    int a;
    int c;
    cin >> a >> c;
    
    int b_square = (c - a)*(c + a);
    cout << b_square << endl;
    return 0;
}

 

 

3번/나이 계산

#include <iostream>

using namespace std;

int main(void) {
    int year, answer;
    string age_type;
    cin >> year >> age_type;

    if (age_type == "Korea") {
        answer = 2030-year+1;
    }
    else if (age_type == "Year") {
        answer = 2030-year;
    }

    cout << answer << endl;
    return 0;
}

 

 

4번/저축

#include <iostream>

using namespace std;

int main(void) {
    int start;
    int before;
    int after;
    cin >> start >> before >> after;

    int money = start;
    int month = 1;

    while (money < 70) {
        money += before;
        month++;
    }
    while (money < 100) {
        money += after;
        month++;
    }
    cout << month << endl;
    return 0;
}

 

 

5번/산책

#include <string>
#include <vector>

using namespace std;

vector<int> solution(string route) {
    int east = 0;
    int north = 0;
    vector<int> answer(2);
    for(int i=0; i<route.length(); i++){
        switch(route[i]){
            case 'N':
                north++;
                break;
            case 'S':
                north--;
                break;
            case 'E':
                east++;
                break;
            case 'W':
                east--;
                break;
        }
    }
    answer[0] = east;
    answer[1] = north;
    return answer;
}

 

 

vector 기억 안나서 찾아봤는데 동적 배열 할 때쓰는거라고 함.

C 때는 배열선언 int arr[10];이런식으로 하고 동적배열은 int* arr = (int*)malloc(10 * sizeof(int)); 이런식으로 했는데 얘는라이브러리로 한다고 함 

C++에서도 그냥 배열 선언은 동일하게 int arr[10];해도 되고,.. 동적배열만
vector<int> arr;
arr.push_back(5);
arr.push_back(10);

이렇게 하면 5, 10 저장되는 식으로 되는거. 그래서 vector<int> answer(2)하면 크기 2인 배열 만든거..

 


자바하다가 넘 하기싫어서 다시 C언어 하고싶었지만... 다른것도 해야하니 cpp로 갈아탔습니다...ㅜㅜ 너무 오랜만에 봐서기억나는게 하나도 없는 C++.. 

반응형