반응형
1차 시도 ( 실패 )
더보기
namespace BaekJoon;
public class VowelCount
{
static char[] vowel = new char[] { 'a', 'e', 'i', 'o', 'u' };
static char[] delimiter = { '!', '\n', '?', '.' };
static List<string> inputText = new List<string>();
public static void Main()
{
while (true)
{
Input();
if (inputText.Last().Contains("#")) break;
}
foreach (string text in inputText)
{
if (string.IsNullOrEmpty(text)) continue;
Console.WriteLine(GetVowelCount(text));
}
}
public static void Input()
{
Console.WriteLine();
#nullable disable
string[] texts = Console.ReadLine().Split(delimiter);
foreach (string text in texts)
{
if (text != null)
inputText.Add(text);
}
}
public static int GetVowelCount(string text)
{
int count = 0;
foreach (char c in text.ToCharArray())
{
foreach (char cc in vowel)
if (c.Equals(cc)) count++;
}
return count;
}
}
예제로 테스트 했을 때 출력부분에 7 14가 보여서 끝났다 하고 제출을 했는데 틀렸다고 뜨길래 출력창을 보니 7 14 8 이었다.
뭐가 문제지 하며 처음에는 글자를 직접 하나씩 세어봤는데도 8개가 맞는데 왜 문제에는 9라고 뜨는지 이해가 안됐다
여러번 다시 보니 맨 앞에 대문자 I가 있는것을 보고 ToLower()를 이용해 전부 소문자로 바꾸기로 했다.
2차 시도 (실패)
더보기
소문자로 바꿔서 예제 출력과 동일하게 나왔지만 자세히 보니 마지막 줄에 0이 출력되고 있었다.
마지막 라인의 #도 모음 개수를 세고 있어
while (true)
{
Input();
if (inputText.Last().Contains("#"))
{
inputText.RemoveAt(inputText.Count -1);
break;
}
}
while 부분을 빠져나가면서 마지막 인덱스를 삭제하도록하고 제출했는데 출력 형식이 잘못되었다라는 결과가 나왔다.
성공
namespace BaekJoon;
public class VowelCount
{
static char[] vowel = new char[] { 'a', 'e', 'i', 'o', 'u' };
static char[] delimiter = { '!', '\n', '?', '.' };
public static void Main(string[] args)
{
while (true)
{
string text = Input();
if (string.IsNullOrEmpty(text)) break;
Console.WriteLine(GetVowelCount(text));
}
}
public static string Input()
{
#nullable disable
string texts = Console.ReadLine();
if (texts.Contains("#")) texts = string.Empty;
return texts;
}
public static int GetVowelCount(string text)
{
int count = 0;
foreach (char c in text.ToLower().ToCharArray())
{
foreach (char cc in vowel)
if (c.Equals(cc)) count++;
}
return count;
}
}
문제가 되던 부분은 Input()함수의 WriteLine이었다.
처음에 List<string> 변수로 입력을 받아 삭제할 코드들이 많아 보인다...
'BaekJoon' 카테고리의 다른 글
JavaFestival23번 문제 C#으로 풀어보기 (0) | 2024.04.24 |
---|---|
백준 알고리즘 10875 : 뱀 (시간초과) (2) | 2023.10.13 |
백준 알고리즘 2754 : 학점계산 (0) | 2023.09.26 |
백준 알고리즘 25206번 : 너의 평점은 (2) | 2023.09.26 |
백준 알고리즘 1330번 : 두 수 비교하기 (0) | 2023.09.11 |