프로그래머스 레벨 2 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
string solution(string s) 
{
    string answer = "";
    vector<int> vecAnswer;
 
    string str = "";
    for (int i = 0; i < s.size(); i++)
    {
 
        if (!isspace(s[i]))
            str += s[i];
 
        if (isspace(s[i]))
        {
            int iCheck = stoi(str);
            vecAnswer.emplace_back(iCheck);
 
            str = "";
        }
 
        if (i == s.size() - 1)
        {
            int iCheck = stoi(str);
            vecAnswer.emplace_back(iCheck);
        }
 
    }
 
    sort(vecAnswer.begin(), vecAnswer.end());
 
    answer += to_string(vecAnswer.front());
    answer += " ";
    answer += to_string(vecAnswer.back());
 
    return answer;
}
 

isspace(문자) 문자가 공백인지 확인해주는 함수

 

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <vector>
 
using namespace std;
 
int solution(int n) {
    int answer = n;
    
    for(int i = 1 ; i <= (n / 2)  ; i++)
    {
        if(n % i == 0)
            answer += i;
    }
    
    return answer;
}
 

 

약수에 항상 자기 자신이 포함되기 때문에 정답에 자신을 포함하고 입력값의 절반만 for문을 돌리면 더욱 빨리된다.

프로그래머스 레벨 1 테스트

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s, int n) {
    string answer = "";
    
    for(int i = 0 ; i < s.size() ; i++)
    {
        //공백 처리
        if(s[i] == ' ')
        {
            answer += " ";
            continue;
        }
        
        //아스키코드 변환
        int ascii = s[i];
        
        for(int j = 0 ; j < n ; j++)
        {
            ascii++;
            if(ascii == 'z' +1 || ascii == 'Z' +1)
                ascii -= 26;
        }
        
        answer += ascii;
    }
    
    return answer;
}
 

문자를 ascii 코드로 변환해 코드의 값을 비교해서 문제 해결

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <string>
#include <vector>
 
using namespace std;
 
int solution(string s) {
    int answer = 0;
    
    answer = atoi(s.c_str());
    
    return answer;
}
 

C 스타일의 풀이법

 

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <vector>
 
using namespace std;
 
int solution(string s) {
    int answer = 0;
    
    answer = stoi(s);
    
    return answer;
}

C++ 스타일의 풀이법

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
 
 
#include <string>
#include <vector>
 
using namespace std;
 
bool PrimeNumb(int a)
{
 
    //i*i = 제곱근은 소수가 아님
    //3 * 3 = 9    //홀수 곱하기 홀수 = 홀수 [1, 3, 9 소수 아님]
    // 3 * 4 = 12    // 홀수 곱하기 짝수 = 짝수
    // 4 * 4 = 16    // 짝수 곱하기 짝수 = 짝수
    for (int i = 3; i * i <= a; i += 2)
    {
//// 나머지가 0이면 소수가 아님
        if (a % i == 0)
            return false;
    }
 
    return true;
}
 
int solution(int n)
{
    //1은 소수가 아니며, 2는 무조건 소수이다.
    int answer = 1;
 
    for (int i = 3; i <= n; i += 2)    //2를 제외한 2의 배수는 모두 소수가 아니다.
    {
        if (PrimeNumb(i))
            answer++;
    }
 
    return answer;
}
 
 
void main()
{
    int n = 100;
 
    solution(n);
 
}
 

 

전제를 세운다.

1. 1은 소수가 아니다.

2. 2는 무조건 소수이다.

3. 2를 제외한 2의 배수는 무조건 소수가 아니다.

 

참고 :  https://wilybear.tistory.com/64

프로그래머스 레벨 1 테스트

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
bool Sort(int a, int b)
{
    return a > b; //내림차순 정렬
//return a < b; //오름차순 정렬
}    
 
string solution(string s) 
{
    sort(s.begin(), s.end(), Sort);
    
    return s;
}
 

 

문자열을 아스키 코드로 변환하면 대문자 A는 '65' 소문자 a는 '97'이기 때문에 내림차순 정렬만해주면 된다.

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <vector>
 
using namespace std;
 
bool solution(string s) 
{    
    
    if(s.size() == 4 || s.size() == 6)
    {
        for(int i = 0 ; i < s.size() ; i++)
        {
            if(isalpha(s[i]))
                return false;
        }
        return true;
    }
 
    return false;
}
 

isdigit() : 문자가 숫자인지 판별해주는 레거시 매크로

 -> 인자를 int 형으로 받는데 이는 아스키 코드값으로 비교하기 때문

 -> isdigit(3)과 isdigit('3')의 결과는 틀리다. '3' = 83으로 변환되어 체크함. 뒤의 사용법이 맞는사용법

 

isalpha() : 문자가 문자(알파뱃)인지 판별해주는 레거시 매크로

 

// 유용한 스트링 관련 매크로

 

isupper() : 문자가 대문자인지 확인

islower() : 문자가 소문자인지 확인

 

toupper() : 소문자로 들어온 문자를 대문자로 변환하여 내보냄. 다른 문자가 들어오면 들어온대로 리턴

tolower() : 대문자로 들어온 문자를 소문자로 변환하여 내보냄. 다른 문자로 들어오면 들어온대로 리턴

 

isalnum() : 숫자 또는 문자(알파벳)인가 확인해준다.

isxdigit() : 16진수인가?

isspace() : 공백 문자인가?

isascii() : 아스키 코드인가?

toascii() : 문자를 아스키코드로 변환

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>
using namespace std;
 
bool solution(string s)
{
    int p = 0;
    int y = 0;
 
    for(int i = 0 ; i < s.length() ; i++)
    {
        string strTmp;
        strTmp = s[i];
        if(strTmp.compare("p"== 0 || strTmp.compare("P"== 0)
            p++;
        else if(strTmp.compare("y"== 0 || strTmp.compare("Y"== 0)
            y++;
    }
    
    return (p == y) ? true : false;
}
 

 

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> arr, int divisor) {
    vector<int> answer;
    
    for(int i = 0 ; i < arr.size() ; i++)
    {
        if((arr[i] % divisor) == 0)
            answer.emplace_back(arr[i]);
        else
            continue;
    }
    
    if(answer.empty())
        answer.emplace_back(-1);
    else
        sort(answer.begin(), answer.end());
    
    return answer;
}
 

프로그래머스 레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <vector>
 
using namespace std;
 
bool solution(int x) 
{
    int iSum = 0;
 
    for(int i = 1; ; i *= 10 )
    {   
        iSum += (x / i) % 10;
        
        if(x / i == 0)
            return (x % iSum == 0) ? true : false;
    }
    
}
 

 

+ Recent posts