차이점

  1. 'cin'은 'white space' 전까지만, 'getline'은 'white space' 까지 입력을 받는다. (둘다 '\n(엔터)' 만나면 종료)
  2. 'cin'은 처음으로 들어오는 'white space' 입력은 무시하고, 이후의 입력을 받는다.
  3. 'getline'은 처음으로 들어오는 'white space' 입력을 받는다. (입력을 받지만 '\n'(엔터) 같은 경우에는 저장 X)

 

Ex)

입력이  a  b  c  d  '\n'  e  f  '\n'  g  '\n'  이라고 하면,

 

cin >> first;       

// first에  a  b  c d 저장  ->  '\n'  e  f  '\n'  g  '\n' 남음 -> 1번 조건에 의해서 '\n' 입력 무시

 

cin >> second;   

// second에  e  f 저장  ->  '\n'  g  '\n' 남음  ->  2번 조건에 의해서 맨 앞의 '\n' 입력 무시

 

getline(cin, third); 

// third가  '\n'  입력 받지만 저장 X  ->  g  '\n' 남음  ->  3번 조건에 의해서 맨 앞의 '\n' 입력 받음

 

해결법

  1. 'cin'과 'getline' 사이에 cin.ignore()를 선언하여 \n 제거
  2. 'cin'과 'getline' 사이에 string buffer, getline(cin, buffer) 를 선언하여 \n 제거

 

#include <string>
#include <vector>
using namespace std;
 
long long solution(int N) 
{
    long long answer = 0;
    long long dp[85= {0};
    
    dp[1= 1;
    dp[2= 1;
     
    if(N == 1)
    {
        return 1;
    }
    else if(N == 2)
    {
        return 6;
    }
    else
    {
        for(int i = 3; i <= N; i++)
        {
            dp[i] = dp[i-1+ dp[i-2];
        }
    }
    
    answer = dp[N]*2 + (dp[N]+dp[N-1])*2;
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
 
int solution(vector<vector<int>> triangle) 
{
    int answer = 0;
    int Max[510][510= {0};
    
    Max[0][0= triangle[0][0];
    for(int i = 1; i < triangle.size(); i++)
    {
        for(int j = 0; j < triangle[i].size(); j++)
        {
            if(j == 0)
            {
                Max[i][j] = Max[i-1][j] + triangle[i][j];
            }
            else if(j == triangle[i].size()-1)
            {
                Max[i][j] = Max[i-1][triangle[i-1].size()-1+ triangle[i][j];
            }
            else
            {
                Max[i][j] = max(Max[i-1][j-1], Max[i-1][j]) + triangle[i][j];
            }
        }
    }
    
    for(int i = 0; i < triangle.size(); i++)
    {
        if(answer < Max[triangle.size()-1][i])
        {
            answer = Max[triangle.size()-1][i];
        }
    }
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
int solution(vector<int> weight) 
{
    sort(weight.begin(), weight.end());
    
    if(weight[0!= 1)
    {
        return 1;
    }
    else
    {
        int sum = weight[0];
        
        for(int i = 1; i < weight.size(); i++)
        {
            // 다음수가 (누적합+1)이하면 만들수있음
            if(sum+1 < weight[i])
            {
                break;
            }
            else
            {
                sum += weight[i];
            }
        }
        
        return sum+1;
    }
}
cs
#include <string>
#include <vector>
#include <queue>
using namespace std;
 
vector<pair<intint>> map[110];
int visited[110];
int answer;
 
// MST - 프림알고리즘
void prim(int start)
{
    visited[start] = 1;
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    
    for(int i = 0; i < map[start].size(); i++)
    {
        int next = map[start][i].first;
        int nextCost = map[start][i].second;
        
        pq.push({nextCost, next});
    }
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(visited[now] == 1)
        {
            continue;
        }
        
        visited[now] = 1;
        answer += nowCost;
        
        for(int i = 0; i < map[now].size(); i++)
        {
            int next = map[now][i].first;
            int nextCost = map[now][i].second;
            
            pq.push({nextCost, next});
        }
    }
}
 
int solution(int n, vector<vector<int>> costs) 
{
    for(int i = 0; i < costs.size(); i++)
    {
        map[costs[i][0]].push_back({costs[i][1], costs[i][2]});
        map[costs[i][1]].push_back({costs[i][0], costs[i][2]});
    }
    
    prim(1);
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
 
int solution(vector<vector<int>> routes) 
{
    int answer = 0;
    
    sort(routes.begin(), routes.end());
    
    int s, e;
    int cnt = 0;
    for(int i = 0; i < routes.size();)
    {
        int s = routes[i][0];
        int e = routes[i][1];
        int cnt = 0;
                
        for(int j = i; j < routes.size(); j++)
        {
            if(s <= routes[j][0&& routes[j][0<= e)
            {
                s = routes[j][0];
                e = min(e, routes[j][1]);
                cnt++;
            }
            else
            {
                break;
            }
        }
        
        i += cnt;
        answer++;
    }
 
    return answer;
}
cs
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
 
int solution(vector<vector<int>> jobs) 
{   
    int answer = 0;
    
    vector<pair<intint>> list;
    
    // 소요시간, 요청시간 순으로 삽입
    for(int i = 0; i < jobs.size(); i++)
    {
        list.push_back({jobs[i][1], jobs[i][0]});
    }
    
    // 작업 소요시간 순으로 정렬
    sort(list.begin(), list.end());
    
    int time = 0// 현재시간
    while(list.size() > 0)
    {
        bool find = false;
        
        for(int i = 0; i < list.size();)
        {
            if(list[i].second <= time)
            {
                find = true;
                
                time += list[i].first;
                answer += time - list[i].second;
                
                list.erase(list.begin()+i);
                break;
            }
            else
            {
                i++;
            }
        }
        
        if(find == false)
        {
            time++;
        }
    }
    
    return answer / jobs.size();
}
cs
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
 
int solution(string name) 
{
    int answer = 0;
    string start;
    
    for(int i = 0; i < name.size(); i++)
    {
        start += "A";
    }
    
    int nowIdx = 0;
    while(1)
    {   
        answer += min(name[nowIdx] - 'A''Z' - name[nowIdx] + 1);
        start[nowIdx] = name[nowIdx];
        
        if(start == name)
        {
            return answer;
        }
        
        // 1. 왼쪽 이동 횟수 구하기
        int leftIdx = nowIdx;
        int leftCnt = 0;
        
        while(start[leftIdx] == name[leftIdx])
        {
            leftIdx--;
            leftCnt++;
            
            // 왼쪽으로 이동시에 범위를 벗어나는 경우
            if(leftIdx == -1)
            {
                leftIdx = name.size()-1;
            }
        }
        
        // 2. 오른쪽 이동 횟수 구하기
        int rightIdx = nowIdx;
        int rightCnt = 0;
        
        while(start[rightIdx] == name[rightIdx])
        {
            rightIdx++;
            rightCnt++;
            
            // 오른쪽으로 이동시에 범위를 벗어나는 경우
            if(rightIdx == name.size())
            {
                rightIdx = 0;
            }      
        }
        
        // 3. 왼쪽, 오른쪽 중 횟수가 최소인 방향을 선택
        // 왼쪽 선택
        if(leftCnt < rightCnt)
        {
            answer += leftCnt;
            nowIdx = leftIdx;
        }
        // 오른쪽 선택
        else
        {
            answer += rightCnt;
            nowIdx = rightIdx;
        }
    }
}
cs

+ Recent posts