https://www.acmicpc.net/problem/1152
1152๋ฒ: ๋จ์ด์ ๊ฐ์
์ฒซ ์ค์ ์์ด ๋์๋ฌธ์์ ๊ณต๋ฐฑ์ผ๋ก ์ด๋ฃจ์ด์ง ๋ฌธ์์ด์ด ์ฃผ์ด์ง๋ค. ์ด ๋ฌธ์์ด์ ๊ธธ์ด๋ 1,000,000์ ๋์ง ์๋๋ค. ๋จ์ด๋ ๊ณต๋ฐฑ ํ ๊ฐ๋ก ๊ตฌ๋ถ๋๋ฉฐ, ๊ณต๋ฐฑ์ด ์ฐ์ํด์ ๋์ค๋ ๊ฒฝ์ฐ๋ ์๋ค. ๋ํ ๋ฌธ์์ด
www.acmicpc.net
My Solution
matches - ๋ฌธ์์ด์์ ๋์ผํ ๋ฌธ์์ ๊ฐ์๋ฅผ ๊ณ์ฐํ๋ค
- using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
MatchCollection matches = Regex.Matches(s, " ");
int cnt = matches.Count + 1;
char[] chars = s.ToCharArray();
cnt = (chars[0] != ' ') ? cnt : cnt-1;
cnt = (chars[chars.Length-1] != ' ') ? cnt : cnt-1;
Console.WriteLine(cnt);
}
}
}
Solution
๋ค๋ฅธ ๋ฐฉ๋ฒ Trim / IsNullOrEmpty
using System;
using static System.Console;
class Program
{
static void Main(string[] args)
{
// ์๋ค ๊ณต๋ฐฑ์ ์ ๊ฑฐํ ์
๋ ฅ๊ฐ์ ๋ฐ๋๋ค.
string inputWord = ReadLine().Trim();
// ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๋จ์ด๋ง ๋ฐฐ์ด๋ก ๋ฃ๋๋ค.
string[] words = inputWord.Split();
// ๋์ด์ฐ๊ธฐ ์ฆ ๊ณต๋ฐฑ๋ถ๋ถ์ ํ๋จํ ๋ก์ง
// - ๊ณต๋ฐฑ์ ์๋ฅผ ์ฒดํฌํ count ๋ณ์
int count = 0;
for (int i = 0; i < words.Length; i++)
{
if (String.IsNullOrEmpty(words[i]))
{
count++;
}
}
// ๋จ์ด์ ๊ณต๋ฐฑ์ผ๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด - ๊ณต๋ฐฑ์ ์ = ๋จ์ด ์
WriteLine($"{words.Length - count}");
}
}
'๐ฉโ๐ปProgramming > Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] [BOJ#5622] ๋ค์ด์ผ (0) | 2022.08.08 |
---|---|
[C#] [BOJ#2941] ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (0) | 2022.08.08 |
[C#] [BOJ#1157] ๋จ์ด ๊ณต๋ถ (0) | 2022.07.30 |
[C#] [BOJ#2475] ๊ฒ์ฆ์ (0) | 2022.07.30 |
[C#] [BOJ#11654] ์์คํค ์ฝ๋ (0) | 2022.07.30 |
๋๊ธ