728x90
반응형

https://www.acmicpc.net/problem/2798

 

2798번: 블랙잭

첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장

www.acmicpc.net

VERSION 2.0

import sys
import itertools


a = list(map(int, sys.stdin.readline().split()))

N = a[0]
M = a[1]

# 합이 M을 넘지 않는 카드 3장을 찾는다.

card = list(map(int,sys.stdin.readline().split()))

b=  list(itertools.permutations(card ,3))
maxe=0
for i in b:
    idx = sum(i)
    if idx<=M and maxe<idx:
        maxe = idx
print(maxe)

VERSION 1.0

import sys

def BruteForce(n,p, A):
    # A=[5,6,7,13,20]
    w=0
    x=1
    y=2
    B = A[w]+A[x]+A[y]
    max = p
    res = 0
    for i in range(0,len(A)-2):
        for j in range(i+1,len(A)-1):
            for k in range(j+1, len(A)):
                B = A[i]+A[j]+A[k]

                print("{}_{}+{}_{}+{}_{} = {}".format(A[i] , i, A[j] , j , A[k] , k , B))
                # if B>p:
                #     continue
                # elif B==p:
                #     #print("{}_{}+{}_{}+{}_{} = {}".format(A[i] , i, A[j] , j , A[k] , k , B))
                #     res = B
                #     break
                if B<=p:
                    a = p-B
                    if max > a:
                        max = a
                        res = B
                        #print("{}_{}+{}_{}+{}_{} = {}".format(A[i] , i, A[j] , j , A[k] , k , B))

    return res

n = list(map(int, sys.stdin.readline().rstrip().split()))
C = list(map(int, sys.stdin.readline().rstrip().split()))



print(BruteForce(n[0] , n[1] , C))


#%%

입력값 : 5 21

5 6 7 8 9

==> 3중 for문의 첫번째 인자들 값 파악 하는것이 주효했다

브루투포스 ==> 원본 문자열 ==> 처음~끝까지 순회

 

1. 검색 인덱스 ==> 맨처음 인덱스(T : 원본 ,  P: 찾고자하는 문자열 패턴)

 

2. 검색 인덱스 ==> 하나씩 문자비교 => 비교문자 동일 => T,P 인덱스 한칸씩 뒤로

                                                           => 다르다 => T인덱스 뒤로 , P인덱스 처음으로

 

def BruteForce(P,T) :
	i =0 # t검색 index
    j =0 # p검색 index
    while i<len(t) and j<len(p):
    	if t[i] != p[j]:
        	i = i-j
            j =-1
        
        i+=1
        j+=1
   	if j==len(p):
    	return i-len(p)
    else:
    	return -1

==> 브루트 포스 알고리즘 중 ==> 문자열 찾기 알고리즘

728x90
반응형

+ Recent posts