본문 바로가기

Algorithm/Programers - C++

(121)
[프로그래머스] 콜라 문제 Level. 1 문제 콜라 빈 병 2개를 가져다주면 콜라 1병을 주는 마트가 있다. 빈 병 20개를 가져다주면 몇 병을 받을 수 있는가? 단, 보유 중인 빈 병이 2개 미만이면, 콜라를 받을 수 없다. 콜라를 받기 위해 마트에 주어야 하는 병 수 - a 빈 병 a개를 가져다주면 마트가 주는 콜라 병 수 - b 상빈이가 가지고 있는 빈 병의 개수 - n 이 매개변수로 주어집니다. 상빈이가 받을 수 있는 콜라의 병 수를 return 하도록 solution 함수를 작성해 주세요. 풀이 #include #include using namespace std; int solution(int a, int b, int n) { int answer = 0; while(n>=a){ answer += (n/a * b); n ..
[프로그래머스] 영어 끝말잇기 Level.2 문제 구해야 하는 값 : [가장먼저 탈락하는 사람의 번호, 자신의 몇 번째 차례에 탈락하는지] 풀이 #include #include #include #include using namespace std; vector solution(int n, vector words) { vector answer = {0,0}; map map; string word = words[0]; map[word]++; int index; for(index=1; index 1){ break; } } else break; } if(index!=words.size()){ answer[0] = index%n+1; answer[1] = index/n+1; } return answer; } n : 게임에 참가한 사람 수 wor..
[프로그래머스] 문자열 나누기 Level. 1 문제 풀이 #include #include #include using namespace std; int solution(string s) { int answer = 0; char c = s[0]; int scount = 1, ocount = 0; cout
[프로그래머스] 푸드 파이트 대회 - reverse() Level. 1 문제 풀이 #include #include #include #include using namespace std; string solution(vector food) { string answer = ""; string str1 = ""; string str2; for(int i=1; i
[프로그래머스] 숫자짝꿍 - map Level. 1 문제 풀이 #include #include #include using namespace std; string solution(string X, string Y) { string answer = ""; map Xmap; map Ymap; for(int i=0; i 0){ minVal--; answer += to_string(i); } } if(answer == "") return "-1"; if(answer[0] == '0') return "0"; return answer; } 해결과정 자료구조 map을 사용해 key값으로 0~9를 주고 문자열에 들어있는 숫자의 개수를 각각 세어주었다. answer는 겹치는 숫자중 가장 큰 값이어야 하므로, 9부터 0까지 공통된 숫자를 탐색한다. minVa..
[프로그래머스] 점프와 순간 이동 - 비트연산 Level.2 문제 풀이 #include using namespace std; int solution(int n) { int ans = 0; while(n>0){ if(n%2==0){ n/=2; } else { int num = n%2; ans += num; n -= num; } } return ans; } 다른 사람 풀이 1. #include using namespace std; int solution(int n) { int ans = 0; while(n >0) { ans += n%2; n /=2; } return ans; } 다른 사람 풀이 2. #include using namespace std; int solution(int n) { int ans=0; for(int i=0;i
[프로그래머스] JadenCase 문자열 만들기 / isspace Level.2 문제 풀이 #include #include #include // isspace using namespace std; string solution(string s) { string answer = s; answer[0] = toupper(answer[0]); for(int i=1; i
[프로그래머스] 행렬의 곱셈 Level.2 문제 풀이 #include #include using namespace std; vector solution(vector arr1, vector arr2) { vector answer; int Arow = arr1.size(); int Acol = arr1[0].size(); int Bcol = arr2[0].size(); for(int a=0; a