C++/코딩테스트

백준 10808번 풀이 및 정리(C++)

Janny_ 2022. 11. 7. 17:44

- 10808번 풀이

#include <bits/stdc++.h>
using namespace std;

int arr[26];
int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin >> s;
    for (const auto &c : s)
        arr[c-'a']++;
    for (int i =0; i<26; i++)
        cout << arr[i] << ' ';
}

 

각 알파벳 문자의 등장 횟수를 저장하는 arr[26] 배열을 하나 만들어서 처리

 s를 입력받은 후 범위 기반 for문으로 string s를 돌며 arr[c-'a']++;을 통해 알파벳 등장횟수 증가

 

https://step-forward.tistory.com/26

 

(C++) 범위 기반 for문과 auto 키워드

범위 기반 for문(range-based for statement) 예시 #include using namespace std; int main(){ int arr[]={1,2,3,4,5}; for(int number : arr) cout

step-forward.tistory.com

 

 

 

 

 

 

 

<참고>

https://blog.encrypted.gg/927?category=773649