본문 바로가기

알고리즘

(23)
[프로그래머스] (C++) 큰 수 만들기 문제 문제로 가시려면 이 링크를 클릭해주세요. 힌트 앞 뒤 자리 비교 해보라 소스 코드 // [프로그래머스] 큰 수 만들기 #include #include #include using namespace std; string solution(string number, int k) { int eraseNum = 0; for (int i = 0; i < k; i++) { for (int j = 0; j < number.length() - 1; j++) { if (number[j] < number[j+1]) { number.erase(j,1); eraseNum++; break; } } } for (int i = 0; i < k - eraseNum; i++) { number.pop_back(); } return n..
[프로그래머스] (C++) 카펫 문제 문제는 이 곳에 가면 보실 수 있습니다. 힌트 테두리는 내부 블록에 의해 결정된다. 소스 코드 // [프로그래머스] // 카펫 #include #include #include using namespace std; vector solution(int brown, int red) { vector answer; int length; int width; int numBrown; int numCorner = 4; for (int i = 1; i
[프로그래머스] (C++) 단체사진 찍기 문제는 이 링크를 타고 가시면 있습니다. 힌트 이 문제는 완전탐색 문제 입니다. 이 문제는 std::next_permutation 함수를 사용하여 벡터를 ascending order 에서 descending order로 한번 한번 보낼 수 있습니다. // while(vector.next_permutation()) 사용 int형 숫자와 char형 글자를 비교하지 않도록 주의하세요. 예.) if (0 == '0') 소스 코드 #include #include #include #include #include using namespace std; int solution(int n, vector data) { int answer = 0; vector line{'A', 'C', 'F', 'J', 'M', 'N', '..
[프로그래머스] (C++) 숫자의 표현 문제는 이 링크를 타고 가시면 있어요! 이 문제는 비교적 간단하게 풀었어요 힌트: 컴퓨터는 사람이 귀찮아 하는 반복적인 수학 계산을 대신 잘해주죠. #include int solution(int n) { int answer = 0; int addNum; int total; for (int i = 1; i = n) break; addNum++; } } return answer; }
[프로그래머스] (C++) 프린터 문제는 이 링크를 타고 가시면 있습니다. 저는 이 문제를 풀면서 다음 세가지를 생각해보았습니다. 문제에서 나온 로직 자체를 프로그래밍 하면 계산 시간이 너무 오래 걸린다. 어떤 꼼수가 없을까? 굳이 번호를 뒤로 가져다 붙인다고 생각하기보다 번호를 가리키는 화살표가 있어서 한칸 씩 움직이고 끝까지 갔을 때는 다시 돌아오면 되지 않을까? 다음과 같은 코드로 문제를 해결하였습니다. #include #include #include using namespace std; bool compare(const int & a, const int & b) { return (a < b); } int solution(vector priorities, int location) { int answer = 0; vector::ite..
[프로그래머스] (C++) 체육복 문제는 이 링크를 타고 가면 있습니다. 이 문제는 정렬을 필요로 합니다. 왜일까요? #include #include #include using namespace std; int solution(int n, vector lost, vector reserve) { int answer = 0; sort(lost.begin(), lost.end()); sort(reserve.begin(), reserve.end()); vector::iterator lost_iter, res_iter; lost_iter = lost.begin(); res_iter = reserve.begin(); while ((lost_iter != lost.end()) && (res_iter != reserve.end())) { if (*lo..
[프로그래머스] (C++) 땅따먹기 문제는 이 주소로 가면 있습니다. 처음에 저는 아래와 같은 코드로 문제를 푸려고 하였습니다. #include #include using namespace std; vector gLand; int gMax; void getTotal(int cur, int last, int n, int total) { if (cur == n) { if (gMax < total) gMax = total; return; } for (int i = 0; i < 4; i++) { if (last != i) { getTotal(cur+1, i, n, total + gLand[cur][last]); } } } int solution(vector land) { gLand = land; for (int i = 0; i < 4; i++) ..