import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] arr = new int[N]; // N만큼의 길이를 가진 배열 arr 생성
for(int i=0; i < N; i++){
arr[i] = sc.nextInt(); // 배열에 입력한 정수들 저장
}
int v = sc.nextInt();
int count = 0; // v의 개수를 count해주는 변수
for(int j=0; j < arr.length; j++){ // 배열의 길이만큼 반복문 돌리기
if(arr[j] == v){ // 배열의 요소가 v와 일치할 시
count++; // count 변수 1씩 증가
}
}
System.out.print(count);
sc.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N]; // N만큼의 길이를 가진 배열 arr 생성
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for(int i=0; i < N; i++){
arr[i] = Integer.parseInt(st.nextToken()); // 배열에 입력한 정수들 저장
}
int v = Integer.parseInt(br.readLine());
int count = 0; // v의 개수를 count해주는 변수
for(int j=0; j < arr.length; j++){
if(arr[j] == v){ // 배열의 요소가 v와 일치할 시
count++; // count 변수 1씩 증가
}
}
System.out.print(count);
br.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N]; // N만큼의 길이를 가진 배열 arr 생성
int count = 0; // v의 개수를 count해주는 변수
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for(int i=0; i < N; i++){
arr[i] = Integer.parseInt(st.nextToken()); // 배열에 입력한 정수들 저장
}
int v = Integer.parseInt(br.readLine());
int count = 0; // v의 개수를 count해주는 변수
for(int j=0; j < N; j++){
if(arr[j] == v){ // 배열의 요소가 v와 일치할 시
count++; // count 변수 1씩 증가
}
}
br.close();
bw.write(count + "\n");
bw.flush();
bw.close();
}
}
🔎 BufferedWriter의 write 메서드는 문자열 또는 문자의 유니코드 값을 받기 때문에 정수를 바로 전달할 수 없고, 정수를 문자열로 변환하여 전달해야 함
1. bw.write(count + "\n");
2. bw.write(Integer.toString(count));
bw.write(count) (x) → 컴파일 에러 발생
const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
const N = Number(input[0]);
const arr = input[1].split(" ").map(Number);
const v = Number(input[2]);
let cnt = 0;
for(let i=0; i < N; i++){
if(arr[i] === v){
cnt++;
}
}
console.log(cnt);
N = int(input())
arr = map(int, input().split())
v = int(input())
cnt = 0
for i in arr :
if i == v :
cnt += 1
print(cnt)
💡 파이썬에는 증감연산자(++, --) 가 존재하지 않음
증감연산을 수행하려면 일반적으로 += 혹은 -= 사용
N = int(input())
N_list = list(map(int, input().split()))
v = int(input())
print(N_list.count(v))
💡 리스트 (List)
: 여러 자료들을 목록 형태로 관리하는 자료구조 (순서가 있고, 수정할 수 있음)
< 리스트 생성 방법 >
1. 대괄호 이용리스트명 = [요소1, 요소2, 요소3, ... ]
2. list() 이용
a = list()
Ex)a = [] # 비어 있는 리스트, 요소 없음 b = [1, 2, 3] # 숫자를 요솟값으로 가짐 c = ['Life', 'is', 'too', 'short'] # 문자열을 요솟값으로 가짐 d = [1, 2, 'Life', 'is'] # 숫자와 문자열을 함께 요솟값으로 가짐 e = [1, 2, ['Life', 'is']] # 리스트 자체를 요솟값으로 가짐
→ 리스트는 비어 있는 리스트([])일 수도 있고, 숫자나 문자열 또는 리스트 자체를 요솟값으로 가질 수 있다. 즉, 리스트 안에는 어떠한 자료형도 포함할 수 있다.
💡 count(x) : 리스트 안에 x가 몇 개 있는지 조사하여 그 개수를 리턴하는 함수
a = [1, 2, 3, 1] a.count(1) >> 출력결과 : 2