공부하기/백준

[Java] 백준 풀기 10769 - 행복한지 슬픈지

XEV 2023. 12. 22. 23:19

자바 백준 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");
}
}
}