자바 백준 10769번
브론즈 1
https://www.acmicpc.net/problem/10769
10769번: 행복한지 슬픈지
승엽이는 자신의 감정을 표현하기 위해서 종종 문자 메시지에 이모티콘을 넣어 보내곤 한다. 승엽이가 보내는 이모티콘은 세 개의 문자가 붙어있는 구조로 이루어져 있으며, 행복한 얼굴을 나
www.acmicpc.net
문제 보기

분류: 문자열, 파싱
코드 보기
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 입력 받기 String input = sc.nextLine(); // 각 이모티콘 개수 세기 int happyCount = countEmoticons(input, ":-)"); int sadCount = countEmoticons(input, ":-("); // 결과 출력 printResult(happyCount, sadCount); } // 이모티콘 개수를 세는 함수 public static int countEmoticons(String input, String emoticon) { int count = 0; for (int i = 0; i < input.length() - emoticon.length() + 1; i++) { // 이모티콘과 일치하는 경우 개수 증가 if (input.substring(i, i + emoticon.length()).equals(emoticon)) { count++; } } return count; } // 결과를 출력하는 함수 public static void printResult(int happyCount, int sadCount) { // 각 경우에 따라 결과 출력 if (happyCount == 0 && sadCount == 0) { System.out.print("none"); } else if (happyCount == sadCount) { System.out.print("unsure"); } else if (happyCount > sadCount) { System.out.print("happy"); } else { System.out.print("sad"); } } }
'공부하기 > 백준' 카테고리의 다른 글
[Java] 백준 풀기 15786 - Send me the money (0) | 2023.12.26 |
---|---|
[Java] 백준 풀기 9946 - 단어 퍼즐 (0) | 2023.12.23 |
[Java] 백준 풀기 9366 - 삼각형 분류 (2) | 2023.12.21 |
[Java] 백준 풀기 10833 - 할로윈의 사탕 (2) | 2023.12.20 |
[Java] 백준 풀기 17249 - 태보태보 총난타 (2) | 2023.12.18 |