반응형

#include <iostream>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a,b,c;
int arr[100] = {};
cin >> a;
for (int i=0; i<a; i++)
{
cin >> b;
arr[i] = b;
}
cin >> c;
int cnt = 0;
for (int i=0; i<a; i++)
{
if (arr[i] == c) cnt++;
}
cout << cnt;
return 0;
}
N이 1 ~ 100까지므로 배열을 100의 크기로 선언해주고
배열을 돌면서 주어진 수와 일치하는 지 판단하고
일치하면 개수를 카운트해주는 방식으로 하는 간단한 문제이다
중간에 cnt값을 0으로 초기화해주지 않으면
쓰레기값이 들어가고
전역변수로 설정한다면 0으로 자동초기화되므로 굳이 초기화하지 않아도 된다
반응형
'algorithm > 배열' 카테고리의 다른 글
| [algorithm] 백준 11328번 Strfry (0) | 2026.01.10 |
|---|---|
| [algorithm] 백준 13300번 방 배정 (0) | 2026.01.10 |
| [algorithm] 백준 1475번 방 번호 (0) | 2026.01.10 |
| [algorithm] 백준 2577번 숫자의 개수 (0) | 2026.01.10 |
| [algorithm] 백준 10808번 알파벳 개수 (0) | 2026.01.10 |
