반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- compose
- 매개변수탐색
- 물채우기
- Kafka
- 기술면접
- Docker
- 오퍼레터
- BFS
- 백트래킹
- @P0
- 퇴사통보
- 처우협의
- 13908
- 백준
- 연결요소
- OFFSET
- boj #19237 #어른 상어
- upper_bound
- incr
- BOJ
- 성적평가
- 소프티어
- 처우산정
- 6987
- 이분탐색
- 파라메트릭
- msSQL
- dfs
- 경력
- softeer
Archives
- Today
- Total
기술 블로그
1107번 리모컨 본문
728x90
반응형
https://www.acmicpc.net/problem/1107
아마 제한시간이 있었더라면, 틀렸을 것이다.
막상 풀어보니 바로 풀리길래 제출하였더니 '시간 초과'가 난 것이다.
그래서 생각해보니, 81 ~ 90번 째 코드들을 놓쳤다.
하지만 어떤 테이스 케이스들이 있나 생각하여 입력을 해보았더니
102
0
3
이렇게 뜨는 것이다.
원래 답은 2이다.
그래서 else if 조건문 2개를 추가해줬다. 차이가 1, 2일 때.
번외로, else if 조건문을 추가하지 않은 정답 코드(첫 번째 코드)와 예시 올렸다.
첫 번째 코드 : else if 조건문 사용 X
두 번째 코드 : else if 조건문 사용 O
500000
10
0 1 2 3 4 5 6 7 8 9
답 : 499900
1555
8
0 1 3 4 5 6 7 9
답 : 670
10
9
1 2 3 4 5 6 7 8 9
답 : 11
103
10
0 1 2 3 4 5 6 7 8 9
답 : 3
6
9
0 2 3 4 5 6 7 8 9
답 : 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; int start = 100, goal = 0; vector<int> error; bool Chk(string s) { for (int i = 0; i < s.length(); i++) { for (int j = 0; j < error.size(); j++) { if (s.at(i) - '0' == error.at(j)) { return false; } } } return true; } int main(void) { int M = 0, num = 0, result = 87654321; scanf("%d", &goal); scanf("%d", &M); for (int i = 0; i < M; i++) { scanf("%d", &num); error.push_back(num); } int direct = abs(start - goal); if (start == goal) { printf("0\n"); return 0; } if (abs(start - goal) >= 1) { int left = goal; int right = goal; while(1) { if (left < 0) left = 0; if (Chk(to_string(left))) { result = to_string(left).length(); result += abs(goal - left); break; } if (Chk(to_string(right))) { result = to_string(right).length(); result += abs(goal - right); break; } --left; ++right; if (left == start) { result = abs(left - goal); break; } else if (right == start) { result = abs(right - goal); break; } } printf("%d\n", min(result, direct)); } return 0; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; int start = 100, goal = 0; vector<int> error; bool Chk(string s) { for (int i = 0; i < s.length(); i++) { for (int j = 0; j < error.size(); j++) { if (s.at(i) - '0' == error.at(j)) { return false; } } } return true; } int main(void) { int M = 0, num = 0, result = 87654321; scanf("%d", &goal); scanf("%d", &M); for (int i = 0; i < M; i++) { scanf("%d", &num); error.push_back(num); } if (start == goal) { printf("0\n"); return 0; } if (abs(start - goal) > 2) { int left = goal; int right = goal; while(1) { if (left < 0) left = 0; if (Chk(to_string(left))) { result = to_string(left).length(); result += abs(goal - left); break; } if (Chk(to_string(right))) { result = to_string(right).length(); result += abs(goal - right); break; } --left; ++right; if (left == start) { result = abs(left - goal); break; } else if (right == start) { result = abs(right - goal); break; } } printf("%d\n", result); } else if (abs(start - goal) == 1) { printf("1\n"); } else if (abs(start - goal) == 2) { printf("2\n"); } return 0; } | cs |
728x90
반응형
'알고리즘 문제 > BOJ' 카테고리의 다른 글
1213번 팰린드롬 만들기 (0) | 2018.12.29 |
---|---|
1038번 감소하는 수 (0) | 2018.12.28 |
11657번 타임머신 (0) | 2018.12.26 |
2577번 숫자의 개수 (0) | 2018.12.25 |
5567번 결혼식 (0) | 2018.12.24 |