웅재의 코딩세상

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

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

[프로그래머스] 문자열, 배열, 조건문

웅드 2024. 1. 13. 13:50
  • 7의 개수
#include <string>
#include <vector>

using namespace std;

int solution(vector<int> array) {
    int answer = 0;
    for(int i=0; i< array.size(); i++){
        while(array[i] != 0){
            if(array[i] % 10 == 7){
                answer++;
            }
            array[i] /=10;
        }
    }
    return answer;
}
  • 잘라서 배열로 저장하기
#include <string>
#include <vector>

using namespace std;

vector<string> solution(string my_str, int n) {
    vector<string> answer;
    string s="";
    for(int i=0; i<my_str.size(); i++){
        if((i+1)%n == 0) {
            s+= my_str[i];
            answer.push_back(s);
            s = "";
        }
        else{
            s+= my_str[i];
        }
    }
    if(!empty(s)) answer.push_back(s);
    return answer;
}
  • 중복된 숫자 개수
#include <string>
#include <vector>

using namespace std;

int solution(vector<int> array, int n) {
    int answer = 0;
    for(int i=0; i<array.size(); i++){
        if(array[i] == n) answer++;
    }
    return answer;
}
  • 머쓱이보다 키 큰 사람
#include <string>
#include <vector>

using namespace std;

int solution(vector<int> array, int height) {
    int answer = 0;
    for(int i=0; i<array.size(); i++){
        if(height < array[i]) answer++;
    }
    return answer;
}
반응형