코딩해요/C++

[프로그래머스] PCCE 기출, 특수문자 출력

yenas0 2024. 11. 12. 21:22
반응형

PCCE 기출 6번 / 물 부족

#include <string>
#include <vector>

using namespace std;

int solution(int storage, int usage, vector<int> change) {
    int total_usage = 0;
    for(int i=0; i<change.size(); i++){
        usage = usage+ usage * change[i] / 100;
        total_usage += usage;
        if(total_usage > storage){
            return i;
        }
    }
    return -1;
}

 

 

PCCE 기출 7번 / 버스

#include <string>
#include <vector>

using namespace std;

int func1(int num){
    if(0 > num){
        return 0;
    }
    else{
        return num;
    }
}

int func2(int num){
    if(num > 0){
        return 0;
    }
    else{
        return num;
    }
}

int func3(vector<string> station){
    int num = 0;
    for(int i=0; i<station.size(); i++){
        if(station[i] == "Off"){
            num += 1;
        }
    }
    return num;
}

int func4(vector<string> station){
    int num = 0;
    for(int i=0; i<station.size(); i++){
        if(station[i] == "On"){
            num += 1;
        }
    }
    return num;
}

int solution(int seat, vector<vector<string>> passengers) {
    int num_passenger = 0;
    for(int i=0; i<passengers.size(); i++){
        num_passenger += func4(passengers[i]);
        num_passenger -= func3(passengers[i]);
    }
    int answer = func1(seat - num_passenger);
    return answer;
}

 

 

PCCE 기출 8번 / 닉네임규칙

#include <string>
#include <vector>

using namespace std;

string solution(string nickname) {
    string answer = "";
    for(int i=0; i<nickname.size(); i++){
        if(nickname[i] == 'l'){
            answer += "I";
        }
        else if(nickname[i] == 'w'){
            answer += "vv";
        }
        else if(nickname[i] == 'W'){
            answer += "VV";
        }
        else if(nickname[i] == 'O'){
            answer += "0";
        }
        else{
            answer += nickname[i];
        }
    }
    while(answer.size() < 4){
        answer += "o";
    }
    if(answer.size() > 8){
        answer = answer.substr(0,8);
    }
    return answer;
}

 

 


[PCCE 기출문제] 9번 / 지폐 접기

#include <iostream> // Added this header for cout
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> wallet, vector<int> bill) {
    int answer = 0;
    while (min(bill[0], bill[1]) > min(wallet[0], wallet[1]) || max(bill[0], bill[1]) > max(wallet[0], wallet[1])) {
        if (bill[0] > bill[1]) {
            bill[0] /= 2;
        } else {
            bill[1] /= 2;
        }
        answer++;
    }
    
    return answer;
}

int main() {
    vector<int> bill = {10, 20};
    vector<int> wallet = {5, 15};
    
    int result = solution(wallet, bill);
    cout << result << endl;
    
    return 0;
}

 

 

특수문자 출력하기

#include <iostream>

using namespace std;

int main() {
    cout << "!@#$%^&*(\\'\"<>?:;" << endl;
    return 0;
}
반응형