백준

2810번: 컵홀더

bingual 2022. 2. 15. 09:47
반응형

 

 

 

 

 

 

 

풀이

 

시작시 컵홀더의 초기값은 1, 이후 S는 한개당 1, LL은 두개당 1을 카운트하므로 S의 값은 1, LL의 값은 0.5로 계산한다.

 

 

 

 

자바

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
		String seats = br.readLine();

		float cnt = 1;
		for (int i = 0; i < seats.length(); i++) {
			
			seats.charAt(i);
			if (seats.charAt(i) == 'S')
				cnt += 1;
			else if (seats.charAt(i) == 'L')
				cnt += 0.5;
		}
		
		if (cnt < n)
			System.out.print((int)cnt);
		else 
			System.out.print(n);
	}
}

 

 

 

 

 

 

파이썬

n = int(input())
seats = str(input())

arr = [1, 0.5]
type = ['S', 'L']

cnt = 1
for seat in seats:
    for i in range(len(type)):
        if seat == type[i]:
            cnt += arr[i]
if cnt < n:
    print(int(cnt))
else:           
    print(n)