입력받은 정수 a,b 사이에 속한 모든 수의 합을 구하는 문제이다.

나는 그냥 두 정수 사이의 차를 구한 후에, 그 차이 만큼 for문을 돌면서 값을 더했는데...

다른 사람들의 풀이를 보니까 간결하고 예쁘게 짠 게 많았다. (ex. 비트연산자를 쓰는 경우...)

공부 열심히 해야겠다...

    long long answer = 0;
    int sub = 0;

    if(a == b)
    {
        answer = a;
    }
    else if(a < b)
    {
        sub = b - a;
        for(int i = 0; i<sub; i++)
        {
            answer += a + i;
        }
        answer += b;
    }
    else if(a > b)
    {
        sub = a - b;
        for(int i = 0; i<sub; i++)
        {
            answer += b + i;
        }
        answer += a;
    }
    return answer;
}

input 값 's' 에 대해서, 'p' 와 'y'의 개수를 비교해 같으면 True, 다르면 False를 리턴하는 문제이다.

'p' 와 'y' 둘 다 없으면 무조건 True를 리턴한다.

 

s 안의 문자들을 체크할 때 대문자와 소문자 모두 있는지 확인해줘야 한다

 

#include <string>
#include <iostream>
using namespace std;

bool solution(string s)
{
    bool answer = true;
    
    int p_cnt = 0;
    int y_cnt = 0;
    int s_len = s.length();
    
    for(int i = 0; i < s_len; i++)
    {
        if(s[i] == 'p' || s[i] == 'P')
        //if(s[i] == 80 || s[i] == 112)
        {
            p_cnt++;
        }
        else if(s[i] == 'y' || s[i] == 'Y')
        //else if(s[i] == 121 || s[i] == 89)
        {
            y_cnt++;
        }
    }
    
    if(p_cnt != y_cnt)
    {
        answer = false;
    }

    return answer;
}

인풋값 s에 대해서 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하는 문제이다.

C++ 의 isdigit() 함수를 사용하면 쉽게 풀 수 있다.

isdigit 함수를 사용하지 않고, 알파벳 아스키코드 값보다 크거나 작은 지 비교하면서 풀어도 될 것 같다.


헤더 파일 : <cctype>

기본형 : int isdigit(int c);

  • intput c 가 숫자가 아니면 0을 리턴

#include <string>
#include <vector>
#include <cctype>

using namespace std;

bool solution(string s) {
    bool answer = true;

    int s_len = s.length();

    if(s_len == 4 || s_len == 6)
    {
        for(int i = 0; i < s_len; i++)
        {
            if(isdigit(s[i]) == 0)
            {
                answer = false;   
            }
        }
    }
    else
    {
        answer = false;
    }
    return answer;
}

입력값 (a,b) 를 받았을 때, 2016년 a월 b일을 구하는 문제.

2016년은 윤년. (2월 29일까지 있다.!)

a월 b일을 일단위로 변환한 후, 7로 나눈 나머지를 가지고 요일을 구한다.

 

#include <string>
#include <vector>

using namespace std;

string solution(int a, int b) {
    string answer = "";
    vector<string> week = {"THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED"};
    int month_final_day[12]  = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int a_month = 0;
    int b_day =0;
    
 	// 전달까지의 일 수 더하기
    for(int i = 0; i < a-1; i++)
    {
        a_month += month_final_day[i];
    }
    b_day = a_month + b;
    const int result = b_day%7;

    answer = week.at(result);
    return answer;
}

 

 

 

 

array 배열과 [i,j,k] 를 입력 받았을 때, array 에 대해서 i~j 까지 정렬한 후, k번째 수를 구하는 문제

임시 벡터 temp를 선언한 후, i~j까지의 값을 담아준다.

array를 1부터 세기 때문에, k 번째 수 역시 k-1번째를 반환해야 한다.

 

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    for(int i = 0; i < commands.size(); i++)
    {
        vector<int> temp;
        for(int j = commands[i][0] -1; j < commands[i][1]; j++)
        {
            temp.push_back(array[j]);
        }
        sort(temp.begin(), temp.end());
        answer.push_back(temp[commands[i][2] - 1]);
        temp.clear();
    }
    
    return answer;
}

입력받은 문자가 짝수/홀수인 지 판단한 후, substr 함수를 이용해 가운데 글자를 계산한다.

 

#include <string> 
#include <vector> 

using namespace std; 

string solution(string s) { 
    int s_len = s.length(); 
    string answer = ""; 
     
    if(s_len % 2 == 0) 
    { 
        answer = s.substr(s_len/2-1 , 2); 
    }     
    else 
    { 
        answer = s.substr(s_len/2,1); 
    } 
    return answer; 
}

+ Recent posts