본문 바로가기

Algorithm

(402)
[프로그래머스] 스택/큐 - 기능개발 문제 풀이 1 #include #include #include using namespace std; vector solution(vector progresses, vector speeds) { vector answer; vector days; for (int i = 0; i = days[i]) { count++; } else { answer.push_back(count); count = 1..
[프로그래머스] 124 나라의 숫자 / insert 문제 풀이 #include #include using namespace std; string solution(int n) { //3진법 string answer = ""; vector nara = {"1","2","4"}; while(n>0){ n = n-1; answer.insert(0,nara[n%3]); n = n/3; } return answer; } 숫자가 1, 2, 4 뿐이므로 3진법과 동일한 방법이라 생각하고 풀었다. 3진수랑 비교했을 때 각 자리다 1씩 차이가 생기기 때문에 -1을 해주었다. 다른 사람 풀이 #include #include using namespace std; string change124(int no) { string answer = ""; int a; while(no >..
[프로그래머스] 멀쩡한 사각형 / 최대공약수, 유클리드 호재법 문제 풀이 #include #include #include using namespace std; long long solution(int w,int h) { long long answer = 1; long long a = min(w,h);// 최대공약수 (모서리 모서리로 이어지는 네모의 개수) for(; a>0; a--){ if(w%a == 0 && h%a == 0) break; } long long ws = w/a, hs = h/a; return answer = (long long)w*h - (ws + hs - 1)*a; } 처음엔 최대 공약수를 int값으로 구했기 때문에 오류가 발생했다. 범위 오류가 생길 수 있기 때문에 long long 인지 int인지 잘 보고 구할 것! 최대공약수를 사용하는 이유..
[프로그래머스] 카카오프렌즈 컬러링북 / DFS과 BFS, memset 문제 풀이 #include #include // memset using namespace std; bool visit[100][100]; int num , cnt, N, M; int dx[4]; int dy[4]; //깊이탐색 //DFS : 깊이우선탐색 void DFS(int x, int y, vector picture, int num) { visit[x][y] = true; cnt++; for (int i = 0; i = M || ny >= N) continue; if (picture[nx][ny] == num && !visit[nx][ny]) { DFS(nx,..
[프로그래머스] 오픈채팅방 / stringstream 문제 풀이 1 #include #include #include #include using namespace std; vector solution(vector record) { //map, vector unordered_map map; vector user; vector answer; for(int i=0; i> state >> id >> name; if(state != "Change"){ vec.push_back(state); vec.push_back(id); user.push_back(vec); } if(state != "Leave") map[id] = name; } for(auto a : user){ string s; s += map[a[1]] + "님이 "; a[0] == "Enter" ? s+= ..
[프로그래머스] 문자열 압축 / substr 문제 풀이 1 #include #include using namespace std; int solution(string s) { int answer = s.length(); int len = 0, count = 1; string a, before; for(int i=1; i 1){ string count_s = to_string(count); len += count_s.length(); count = 1; } if(answer > len) answer = len; } return answer; } 정답만을 구하는 것이 목적이었기 때문에(int 값) 정답인 문자열을 구할 수 없었다. 풀이 2 #include #include using namespace std; int solution(string s) { ..
[프로그래머스] 하샤드 수 문제 풀이 #include #include using namespace std; bool solution(int x) { string s = to_string(x); int num = 0; for(int i=0; i
[프로그래머스] 핸드폰 번호 가리기 / replace 문제 프로그래머스 모바일은 개인정보 보호를 위해 고지서를 보낼 때 고객들의 전화번호의 일부를 가립니다. 전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자를 전부 *으로 가린 문자열을 리턴하는 함수, solution을 완성해주세요. 풀이 #include #include using namespace std; string solution(string phone_number) { string answer = phone_number; answer.replace(answer.begin(), answer.end()-4, answer.size()-4, '*'); return answer; } replace replace는 용도에 따라 다양한 형태의 오버 로딩이 존재한다..