'Project > Bus Traffic Light Detection' 카테고리의 다른 글
Presentation(1) (0) | 2019.05.20 |
---|---|
19/04/15 진행 상황 (0) | 2019.04.16 |
19/04/10 진행 상황 (0) | 2019.04.10 |
19/04/01 진행 상황 (0) | 2019.04.01 |
프로젝트 로드맵 (0) | 2019.03.28 |
Presentation(1) (0) | 2019.05.20 |
---|---|
19/04/15 진행 상황 (0) | 2019.04.16 |
19/04/10 진행 상황 (0) | 2019.04.10 |
19/04/01 진행 상황 (0) | 2019.04.01 |
프로젝트 로드맵 (0) | 2019.03.28 |
시간 복잡도가 O(N2)인 정렬 방법을 정리합니다.
가장 큰 원소를 맨 뒤로 보내고 그 다음 원소를 그 앞, ... 을 반복한다.
void Bubble_sort(int ary[], int length) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - 1 - i; j++) {
if (ary[j] > ary[j + 1])
swap(ary[j], ary[j + 1]);
}
}
}
개선된 버블 정렬 알고리즘이 있다. 두번째 for문에서 모두 정렬된 상태면 더 이상 살피지 않고 끝낸다.
void Bubble_sort_improved(int ary[], int length) {
bool sorted = false;
for (int i = 0; i < length; i++) {
// 정렬된 상태
if (sorted)
break;
sorted = true;
for (int j = 0; j < length - 1 - i; j++) {
if (ary[j] > ary[j + 1]) {
// 정렬되지 않았으니 False 처리.
sorted = false;
swap(ary[j], ary[j + 1]);
}
}
}
}
가장 작은 원소를 맨 앞으로 보내고, 그 다음 원소를 그 다음으로 보낸다. 가장 작은 원소의 index를 저장해뒀다가 한번 훑은 뒤 swap을 하기 때문에 버블 정렬보다는 빠르게 가능하다.
void Selection_sort(int ary[], int length) {
int min_index;
for (int i = 0; i < length - 1; i++) {
min_index = i;
for (int j = i + 1; j < length; j++) {
if (ary[min_index] > ary[j]) {![Image](https://i.imgur.com/jmdyU4f.png)
min_index = j;
}
}
swap(ary[i], ary[min_index]);
}
}
두번째 원소부터 시작해서 왼쪽에 있는 원소와 비교한다. 왼쪽의 원소가 기준값보다 더 크면 오른쪽으로 보낸다. 왼쪽의 원소가 더 작거나(올바른 순서로 되어있거나) 배열의 맨 앞까지 오면 멈춘다.
데이터가 이미 어느정도 정리되어 있는 경우 유리하다.
루프 중간에 멈춰도 검사하지 않은 기존 데이터의 순서가 유지되어 안정적이다.
void Insertion_sort(int ary[], int length) {
int i, j, current;
for (i = 1; i < length; i++) {
current = ary[i];
for (j = i - 1; j >= 0 && ary[j] > current; j--) {
ary[j + 1] = ary[j];
}
ary[j + 1] = current;
}
}
[정렬] 퀵 정렬, 병합 정렬, 힙 정렬 (0) | 2019.03.29 |
---|
주석에 설명 적었습니다.
O(N^3)으로 풀수 있다는데 이해가 직관적이지 않아서 재귀를 써서 풀어봤습니다.
16719.cpp
#include <bits/stdc++.h>
using namespace std;
#define INF 260
bool visited[101];
string s;
int sSize;
void solve(int, int);
char getSmallIndex(int, int);
int main() {
cin >> s;
sSize = s.size();
solve(0, sSize - 1);
}
void solve(int l, int r) {
int index = getSmallIndex(l, r); // 현재 고르지 않은 문자 중 가장 작은 값의 Index
if (index == -1) return; // 모든 문자를 골랐을 경우 탈출
visited[index] = true; // 고른 문자 재사용 하는 일 없게 true
// true인 문자는 순서대로 출력
for (int i = 0; i < sSize; i++) {
if (visited[i])
cout << s[i];
}
cout << '\n';
solve(index + 1, r); // 가장 작은 값을 골랐기 때문에 우측, 좌측 순으로 호출한다.
solve(l, index - 1);
}
char getSmallIndex(int l, int r) {
int small = 999;
int index = -1;
for (int i = l; i < r + 1; i++) {
if (s[i] < small && !visited[i]) {
small = s[i];
index = i;
}
}
return index;
}
백준 5430번 : AC C++ (0) | 2019.07.17 |
---|---|
백준 1021번 회전하는 큐: C++ (0) | 2019.07.17 |
백준 2503번 : 숫자 야구 C++ (0) | 2019.01.28 |
백준 1213번 : 팰린드롬 만들기 C++ (0) | 2019.01.28 |
백준 1072번 : 게임 C++ (0) | 2019.01.28 |
코딩 테스트 및 면접 준비 목적으로 글 쓸 계획입니다.
정리 해야할 주제를 여기에 적고, 하나씩 정리하면서 목록에서 지우거나 포스트 링크를 넣을 생각입니다.
최종 수정 : 2019. 04. 04
[Vi] 내 vimrc 설정 (0) | 2018.11.13 |
---|
코드에 주석을 달았다. 서로 다른 숫자 세 개로 구성된 점과 0을 제외한 1-9 사이의 숫자임에 유의해서 풀면 된다.
2503.cpp
#include <bits/stdc++.h>
using namespace std;
#define NUMBERSIZE 3
int result;
int test;
struct question {
string num;
int strike;
int ball;
};
vector<question> questionV;
int main() {
int n;
question input;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input.num >> input.strike >> input.ball;
questionV.push_back(input);
}
for (int i = 123; i <= 987; i++) {
string target = to_string(i);
test = 0;
// 숫자가 0 일 경우 continue, 중복 숫자가 있을 경우 continue.
if (target[0] == '0' || target[1] == '0' || target[2] == '0' || target[0] == target[1] || target[1] == target[2] || target[2] == target[0]) {
continue;
}
for (int j = 0; j < n; j++) {
int targetStrike, targetBall;
targetStrike = targetBall = 0;
for (int k = 0; k < NUMBERSIZE; k++) {
// 같은 자리일때 Strike를 증가
if (target[k] == questionV[j].num[k])
targetStrike++;
// 다른 자리의 숫자일때 Ball을 증가
if (target[k] == questionV[j].num[(k + 1) % NUMBERSIZE] || target[k] == questionV[j].num[(k + 2) % NUMBERSIZE])
targetBall++;
}
// Strike, Ball의 수가 같을 때 test의 값을 증가.
if (questionV[j].strike == targetStrike && questionV[j].ball == targetBall) {
test++;
}
}
// 모든 경우에 대해 가능한 숫자일 경우 결괏값을 증가.
if (test == n) {
result++;
}
}
cout << result;
}
백준 1021번 회전하는 큐: C++ (0) | 2019.07.17 |
---|---|
백준 16719번 : ZOAC C++ (0) | 2019.03.26 |
백준 1213번 : 팰린드롬 만들기 C++ (0) | 2019.01.28 |
백준 1072번 : 게임 C++ (0) | 2019.01.28 |
백준 10539번 : 수빈이와 수열 C++ (0) | 2019.01.15 |
코드에 주석을 달았다. 홀수번 등장하는 알파벳이 두 개 이상이면 팰린드롬을 만들 수 없다.
1213.cpp
#include <bits/stdc++.h>
#define ALPHABET 26
using namespace std;
int alphabet[ALPHABET];
string result;
int main() {
string input;
cin >> input;
int input_size = input.size();
for (int i = 0; i < input_size; i++) {
alphabet[input[i] - 'A']++;
}
int odd_index, odd;
odd_index = -1;
odd = 0;
for (int i = 0; i < ALPHABET; i++) {
if (alphabet[i] % 2 == 1) {
odd++;
odd_index = i;
}
}
// 홀수번 등장한 알파벳이 2개 이상이면 만들 수 없다.
if (odd > 1) {
result = "I\'m Sorry Hansoo";
}
// 홀수번 등장한 알파벳이 1번이면
// AAABBBA -> AABBBAA
else if (odd == 1) {
char odd_char = odd_index + 'A';
// temp : 홀수번 등장한 알파벳을 제외한 string 저장.
// AAABBBA -> AAABBA, odd_char = 'B'
string temp = "";
bool do_once = true;
for (int i = 0; i < input_size; i++) {
if (do_once && input[i] == odd_char) {
do_once = false;
continue;
}
temp += input[i];
}
// temp = AAAABB
// half = AAB
sort(temp.begin(), temp.end());
string half = "";
int temp_size = temp.size();
for (int i = 0; i < temp_size; i += 2) {
half += temp[i];
}
result = half + odd_char;
// half = BAA
reverse(half.begin(), half.end());
result += half;
}
// 모두 짝수번 등장.
// ABAABA -> AABBAA
else {
sort(input.begin(), input.end());
string half = "";
for (int i = 0; i < input_size; i += 2) {
half += input[i];
}
result += half;
reverse(half.begin(), half.end());
result += half;
}
cout << result;
}
백준 16719번 : ZOAC C++ (0) | 2019.03.26 |
---|---|
백준 2503번 : 숫자 야구 C++ (0) | 2019.01.28 |
백준 1072번 : 게임 C++ (0) | 2019.01.28 |
백준 10539번 : 수빈이와 수열 C++ (0) | 2019.01.15 |
백준 2953번 : 나는 요리사다 C++ (0) | 2019.01.15 |
이분탐색으로 풀면 된다. 99%일때 예외 경우를 따로 해줘야한다. 1패라도 있으면 승률이 100%가 될 수 없다.
1072.cpp
#include <bits/stdc++.h>
#define MAX_ 1000000000
using namespace std;
typedef long long ll;
int main() {
ll x, y, z, low, high, mid, test_z;
cin >> x >> y;
z = 100 * y / x;
if (z >= 99) {
cout << -1;
return 0;
}
low = 0;
high = MAX_;
while (low <= high) {
mid = (low + high) / 2;
test_z = 100 * (y + mid) / (x + mid);
if (z < test_z) {
high = mid - 1;
} else {
low = mid + 1;
}
}
cout << low;
}
백준 2503번 : 숫자 야구 C++ (0) | 2019.01.28 |
---|---|
백준 1213번 : 팰린드롬 만들기 C++ (0) | 2019.01.28 |
백준 10539번 : 수빈이와 수열 C++ (0) | 2019.01.15 |
백준 2953번 : 나는 요리사다 C++ (0) | 2019.01.15 |
백준 1193번 : 분수찾기 C++ (0) | 2019.01.15 |