#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(void)
{
	string s;
	cin >> s;
	int max = 0; 
	int temp = 0;
	bool flag = false;
	int arr[26] = { 0, };
	
	transform(s.begin(), s.end(), s.begin(), ::toupper); // 소문자화
	
	for (int i = 0; i < s.length(); i++)
		arr[int(s[i] - 65)] += 1;

	for (int i = 0; i < 26; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
			temp = i;
		}
	}

	for (int i = 0; i < 26; i++)
	{
		if ((max == arr[i]) and (i != temp))
			flag = true;
	}

	if (flag)
		std::cout << "?" << endl;
	else
		std::cout << char(temp+65) << endl;

	return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
	string s;
	int cnt = 1;

	getline(cin, s);
	char delim = 0x20;

	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == delim)
			cnt = cnt + 1;
	}

	if (s[0] == delim)
		cnt--;

	if (s[s.length() - 1] == delim)
		cnt--;

	cout << cnt << endl;
	return 0;
}

 

#include <iostream>
using namespace std;

int main(void)
{
	int N = 0;
	int count = 0;
	int quote = 0;
	int remain = 0;
	int sum = 0;
	int newNumber = 0;
	int tempN = 0;
	
	cin >> N;
	
	if (N>=0 and N<=99)
	{
		tempN = N;	
		while  (true)
		{
			quote = tempN / 10; //2, 6, 8, 4
			remain = tempN % 10; // 6, 8, 4, 2
			sum = quote + remain; // 8, 14, 12, 6
			
			if (sum <10)
			{
				sum = (sum * 10) + sum;
			}
			
			tempN = (remain * 10) + (sum % 10); // 8, 84, 42, 2
			count = count + 1; // 1, 2, 3, 4
			
			if (N == tempN)
			{
				cout << count << '\n';
				break;
			}	
		}
	}
	
	return 0;
}
import sys
input = sys.stdin.readline

def get_binary(X):
    if X == 0:
        return 

    Q, R = divmod(X, 2)
    binary.append(R)
    get_binary(Q)
    binary.reverse()

X = int(input())
binary = []
get_binary(X)
print (sum(binary))

 

import sys
input = sys.stdin.readline
x, y, w, h = list(map(int, input().split()))
print (min([x, w-x, h-y, y]))

최소값이 될 수 있는 경우는 크게 4가지가 있다. 경계선에 닿기만 하면 되는 최소 거리(직선)이므로 (x, y)에서 직선이 되는 경우 4가지 경우를 생각해서 최소값을 구하면 된다.

 

#include <iostream>
using namespace std;

bool hansoo(int i)
{
	if (i < 100)
		return true;
		
	int a = i / 100;
	int b = i / 10 % 10;
	int c = i % 10;
	
	if (a+c == 2*b)
	{
		return true;
	}
	
	return false;
}

int main(void)
{
	int N = 0;
	int count = 0;
	
	cin >> N;
	
	if (N>=1 and N<=1000) // Initial condition
	{
		for (int i=1; i<=N; i++)
		{
			if(hansoo(i))
				count = count + 1;
		}
		
		cout << count << endl;
	}
	
	return 0;
}
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
S = 0

A = sorted(A)
B = sorted(B, reverse=True)

for idx in range(N):
    s = A[idx] * B[idx]
    S = S + s

print (S)

파이썬 내장함수 sorted를 이용해서 풀었다. 편리하게 사용 후 지적 태만감을 느낀다. 파이썬으로 어느정도 백준 문제 풀이에 익숙해지면 C++로 다시 풀 예정이다.

네 번 중첩되는 for 문이 사용될까 싶었는데 사용된다. 앞으로 브루트포스 문제를 만날 때면 시간 복잡도가 높아지는 것에 대해서 심적 부담감을 조금 내려 놓아 봐야 겠다.

N, M = list(map(int, input().split()))
chessboard = []
counts = []

for i in range(N):
    value = input()
    if len(value) != M:
        raise ValueError('Check size of chessboard')
    chessboard.append(value)
#print (chessboard)

for x in range(0, N - 8 + 1): # Abstract
    for y in range(0, M - 8 + 1): # Abstract
        white_start, black_start = 0, 0
        for i in range(x, x + 8): # Concrete
            for j in range(y, y + 8): # Concrete
                if (i + j) % 2 == 0:
                    if chessboard[i][j] != "W":
                        white_start +=1
                    if chessboard[i][j] != "B":
                        black_start +=1

                elif (i + j) % 2 == 1:
                    if chessboard[i][j] != "B":
                        white_start +=1
                    if chessboard[i][j] != "W":
                        black_start +=1
        counts.append(white_start)
        counts.append(black_start)

print (min(counts))
#include <stdio.h>

int main(void)
{
	int A = 0;
	int B = 0;
	
	scanf("%d %d", &A, &B);
	
	if (A>0 and B<10)
	{
		printf("%.9lf", (double)A/B);
	}
	
	return 0;
}

초기 코드 (실패)

가장 기본이 되는 재귀 형식으로 구현했더니 시간 초과 발생

import sys
input = sys.stdin.readline

def fibonacci(N):
    if N == 0:
        global zero
        zero += 1
        return 0
    elif N == 1:
        global one
        one += 1
        return 1
    else:
        return fibonacci(N-2) + fibonacci(N-1)

T = int(input())
for i in range(T):
    N = int(input())
    zero, one = 0, 0
    fibonacci(N)
    print (zero, one)

 

정답 코드 (성공)

핵심은 동적 프로그래밍의 메모이제이션 기법을 사용하는 것

이미 했던 계산을 반복 하는 경우, 기존에 계산 해두었던 값을 활용

 

import sys
input = sys.stdin.readline

zero = [1, 0, 1]
one = [0, 1, 1]

def fibonacci(N):
    length = len(zero)
    if N >= length:
        for i in range(length, N+1):
            zero.append(zero[i-1] + zero[i-2])
            one.append(one[i-1] + one[i-2])
    print (f"{zero[N]} {one[N]}")

T = int(input())
for i in range(T):
    N = int(input())
    fibonacci(N)

+ Recent posts