목록콤퓨타 왕왕기초 (156)
파게로그
문제 링크: 1011번 Fly me to the Alpha Centauri https://www.acmicpc.net/problem/1011 n회 이동으로 최대 거리를 이동할 때 각 경로를 표시하면 다음과 같다. n=1일 때, [1] n=2일 때, [1 1] n=3일 때, [1 2 1] n=4일 때, [1 2 2 1] n=5일 때, [1 2 3 2 1] ... 그러면 다음과 같이 수열의 일반항을 구할 수 있다. 홀수일 때는 1, 4, 9, 16, ... → a_n = n^2 짝수일 때는 2, 6, 12, 20, ... → a_n = n^2 + n → 여기서 쓸데없는 고민을 너무 많이 했다. 짝수일 때까지는 고려할 필요가 없었다! 사용할 수는 있지만, 복잡해진다. 표를 그리거나 순서도를 그려볼 때, 어떤 ..
문제 링크: 2775번 부녀회장이 될테야 https://www.acmicpc.net/problem/2775 그림을 그려보면 풀기 쉬운 문제였다! 아마 재귀적으로도 풀릴 것 같다. def test(floor, hosu): house = [[0] * (hosu+1) for i in range(floor+1)] # 0호와 0층을 만들어준다. # 연산 과정에서 0호는 무시하면 된다. for h in range(hosu+1): house[0][h] = h for f in range(floor+1): house[f][1] = 1 for f in range(1, floor+1): for h in range(2, hosu+1): house[f][h] = house[f][h-1] + house[f-1][h] return..
문제 링크: 2839번 설탕 배달 https://www.acmicpc.net/problem/2869 정상에 올라간 후에는 미끄러지지 않는다는 조건이 왜 필요한 것인지는 잘 모르겠다. day, night, tree = map(int, input().split()) line = tree - day # 이 높이까지는 낮밤과 관계 없이 정상에 도달하지 못함 oneday = day - night # 하루에 올라가는 높이 total = line // oneday if (line % oneday == 0): # line까지 정확히 도달한 상태라면 total += 1 else: # line까지도 도달하지 못한 상태라면 total += 2 print(total)
문제 링크: 2839번 설탕 배달 https://www.acmicpc.net/problem/2839 큰 것부터 하되 하나씩 줄여나간다는 생각으로. 이게 봉지의 종류가 여러 가지가 된다면 배열에 담아야 할 것 같다. 그 땐 굉장히 복잡해질 건데, 어떻게 해야 할까...? 고민해보고 한 번 풀어보자. #include #include using namespace std; int main(void) { int i; bool arr[10000]; // arr[i] is true // if n exists, false if n doesn't exist (n is gen. of d(n)) for (i = 0; i < 10000; i++) { arr[i] = false; } for (int n = 1; n < 1000..
문제 링크: 1065번 한수 https://www.acmicpc.net/problem/1065 n > 10이 아니라 n >= 10이다. 항상 기본적인 것부터 조심하자. #include using namespace std; bool checkSeq(int n) { if (n = 10) { // 여기! right = n % 10; n /= 10; left = n % 10; curSub = right - left; if (curSub != lastSub) { if (lastSub == 10) { lastSub = curSub; continue; } return false; ..
문제 링크: 4673번 셀프 넘버 https://www.acmicpc.net/problem/4673 std::to\_string과 n - '0'를 이용하여 int와 string 간의 상호 변환이 가능하다. #include #include using namespace std; int main(void) { int i; bool arr[10000]; // arr[i] is true // if n exists, false if n doesn't exist (n is gen. of d(n)) for (i = 0; i < 10000; i++) { arr[i] = false; } for (int n = 1; n < 10000; n++) { string nStr = to_string(n); int sum = n; f..
문제 링크: 10951번 A+B - 4 https://www.acmicpc.net/problem/10951 C++의 경우, 테스트 케이스의 개수가 주어지지 않는 경우, cin.eof()를 이용하여 입력을 중단시킬 수 있다. 자바는 bufferedReader.readLine( )의 반환값이 null일 때 입력받기를 중단한다. Scanner를 이용할 경우에는 .hasNext( ) 메서드를 이용할 수 있다. C++ 아래와 같이 .eof( )를 이용할 수 있다. #include using namespace std; int main(void) { while (1) { int a, b; cin >> a >> b; if (cin.eof()) break; // cin.eof() returns true if eof co..
패턴이 10번 이상 반복되면 그 자리수도 고려해야 한다는 점이 엣지 테케였던 것 같다. while 조건에서 너무 막히면, while True 걸어주고 break하는 것도 고민해보아야겠다... def solution(s): def intlength(dup): if dup==1: return 0 cnt = 0 while dup>9: dup //= 10 cnt += 1 return cnt+1 min_compressed_length = len(s) for length in range(1, len(s)//2 + 1): compressed_length = 0 idx = 0 cur_pattern = '' last_pattern = '' dup = 1 while True: cur_pattern = s[idx:idx+l..