일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- scanf
- 엑셀
- 이분그래프
- 알고리즘 공부방법
- Django란
- string 메소드
- 백준
- c++
- iOS14
- string 함수
- getline
- 장고란
- double ended queue
- 시간복잡도
- 구조체와 클래스의 공통점 및 차이점
- correlation coefficient
- vscode
- UI한글변경
- k-eta
- 입/출력
- 매크로
- Django의 편의성
- 표준 입출력
- EOF
- 연결요소
- 프레임워크와 라이브러리의 차이
- 자료구조
- Django Nodejs 차이점
- 2557
- 입출력 패턴
Archives
- Today
- Total
Storage Gonie
(1) [C++, Java] 백준 No.10808 : 알파벳 개수 본문
반응형
문제
풀이
# C++
- strlen 을 사용하기 위해서는 #include <cstring>이 필요하다. 아니면 string 을 사용해서 size 함수 혹은 length 함수를 사용.
#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
// 알파벳의 개수를 저장할 배열 초기화
int count[26];
for(int i = 0; i < 26 ; i++)
count[i] = 0;
// 알파벳의 개수 카운트
char s[100];
cin >> s;
int len;
len = strlen(s);
for (int i = 0; i < len; i++ )
{
int index;
index = int(s[i]) - 97; // 'a'가 아스키코드로 97이기 때문에 'a' 일때 index=0, 'b' 일때 index=1,,,
count[index] += 1;
}
// 결과 출력
for (int i = 0; i < 26 ; i++)
{
if (i < 25)
cout << count[i] << " ";
else
cout << count[i] << endl;
}
}
#include <iostream>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
// 알파벳의 개수를 저장할 배열 초기화
int count[26];
for(int i = 0; i < 26 ; i++)
count[i] = 0;
// 알파벳의 개수 카운트
string s;
cin >> s;
for (int i = 0; i < s.size(); i++ )
{
int index;
index = int(s[i]) - 97; // 'a'가 아스키코드로 97이기 때문에 'a' 일때 index=0, 'b' 일때 index=1,,,
count[index] += 1;
}
// 결과 출력
for (int i = 0; i < 26 ; i++)
{
if (i < 25)
cout << count[i] << " ";
else
cout << count[i];
}
}
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
for (int i='a'; i<='z'; i++) {
cout << count(s.begin(), s.end(), i) << ' ';
}
cout << '\n';
return 0;
}
# Java
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int[] cnt = new int[26];
for (int i=0; i<s.length(); i++) {
cnt[s.charAt(i) - 'a'] += 1;
}
for (int i=0; i<26; i++) {
System.out.print(cnt[i] + " ");
}
System.out.println();
}
}
반응형
'알고리즘 > 백준풀이5. 문자열' 카테고리의 다른 글
(6) [C++, Java] 백준 No.10824 : 네 수 (0) | 2019.04.25 |
---|---|
(5) [C++, Java] 백준 No.11655 : ROT13 (0) | 2019.04.25 |
(4) [C++, Java] 백준 No.2743 : 단어 길이 재기 (0) | 2019.04.25 |
(3) [C++, Java] 백준 No.10820 : 문자열 분석 (0) | 2019.04.25 |
(2) [C++, Java] 백준 No.10809 : 알파벳 찾기 (0) | 2019.04.25 |
Comments