https://www.acmicpc.net/problem/10808
문제
알파벳 소문자로만 이루어진 단어 S가 주어진다. 각 알파벳이 단어에 몇 개가 포함되어 있는지 구하는 프로그램을 작성하시오.
입력
첫째 줄에 단어 S가 주어진다. 단어의 길이는 100을 넘지 않으며, 알파벳 소문자로만 이루어져 있다.
출력
단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.
예제 입력1
baekjoon
예제 출력1
1 1 0 0 1 0 0 0 0 1 1 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0
내 풀이
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 배열_구현
{
public class BOJ10808
{
public static void Main(string[] args)
{
// 단어 S가 주어진다.
// 단어의 a - z의 개수를 출력하라.
// a = 97, z = 122, A = 65, Z = 90
int[] arr = new int[200];
string str = Console.ReadLine();
//foreach(char c in str)
//{
// arr[(int)c]++;
//}
foreach(char c in str)
{
//Console.WriteLine($"{c - 'a'}"); // 122-97 = 25
arr[c - 'a']++;
}
//for(int i = 97; i < 123; i++)
//{
// Console.Write($"{arr[i]} ");
//}
for(int i = 0; i < 26; i++)
{
Console.Write($"{arr[i]} ");
}
}
}
}
다른 풀이 (C++)
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
ios::syns_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
for(char a = 'a'; a <= 'z'; a++)
{
int cnt = 0;
for(auto c : s)
if(a == c) cnt++;
cout << cnt << ' ';
}
}
복습1 풀이 (5/24)
크기가 26인 알파벳 배열을 만들고, 알파벳의 개수를 저장하고, 알파벳 배열값을 출력해주면 된다.
코드 - 정답
using System.Text;
public class BOJ10808
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
StringBuilder sb = new StringBuilder();
int[] arr = new int[26];
string input = sr.ReadLine();
foreach(char c in input)
{
arr[c - 97]++;
}
foreach(int num in arr)
{
sb.Append($"{num} ");
}
sw.WriteLine(sb.ToString());
sw.Close();
sr.Close();
sb.Clear();
}
}'자료구조, 코딩테스트 > 배열(Array)' 카테고리의 다른 글
| [코딩테스트] Programmers - 가장 가까운 같은 글자 (성공) (0) | 2026.05.06 |
|---|---|
| [코딩테스트] BOJ 1919 - 에너그램 만들기 (성공) (0) | 2026.04.05 |
| [코딩테스트] BOJ 11328 - Strfry (실패) (0) | 2026.04.02 |
| [코딩테스트] BOJ 13300 - 방 배정 (실패) (0) | 2026.04.01 |
| [코딩테스트] BOJ 10807 - 개수 세기 (성공) (0) | 2026.03.31 |