웅재의 코딩세상

[프로그래머스] 문자열, 반복문, 출력, 배열, 조건문 본문

코딩테스트/프로그래머스 - LV 0

[프로그래머스] 문자열, 반복문, 출력, 배열, 조건문

웅드 2024. 1. 4. 14:33
  • 문자열 뒤집기
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string my_string) {
    string answer = "";
    reverse(my_string.begin(),my_string.end());
    return my_string;
}
  • 직각삼각형 출력하기
#include <iostream>

using namespace std;

int main(void) {
    int n;
    string s ="";
    cin >> n;
    for(int i=1; i<n+1; i++){
        s.append(1,'*');
        cout << s << endl;
    }
    
    return 0;
}
  • 짝수 홀수 개수
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> num_list) {
    vector<int> answer(2,0);
    for(int i=0; i<num_list.size();i++){
        if(num_list[i] % 2 == 0) answer[0]++;
        else answer[1]++;
    }
    return answer;
}
  • 문자 반복 출력하기
#include <string>
#include <vector>

using namespace std;

string solution(string my_string, int n) {
    string answer = "";
    for(int i=0; i<my_string.length(); i++){
        answer.append(n,my_string[i]);
    }
    return answer;
}
반응형