반응형

백준 10797번 10부제 풀이 코드

C | C++ | Java | Python


풀이

첫 번째 들어오는 숫자를 기억하고(n) 다음으로 들어오는 5개 숫자 중 일치하는 것이 있을 때마다 count를 올려준 후 마지막에 출력합니다. 파이썬은 count() 함수로 보다 간단히 구현할 수 있습니다.

코드

#include <stdio.h>

int main(){
    int n, t, temp, count = 0;
    scanf("%d", &n);
    for(t = 0; t < 5; t++){
        scanf("%d", &temp);
        if(temp == n){
            count += 1;
        }
    }
    printf("%d", count);
    return 0;
}
#include <iostream>
using namespace std;

int main(){
    int n, temp, count = 0;
    cin>>n;
    for(int t = 0; t < 5; t++){
        cin>>temp;
        if(temp == n){
            count += 1;
        }
    }
    cout<<count;
    return 0;
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        for(int t = 0; t < 5; t++){
            if(sc.nextInt() == n){
                count++;
            }
        }
        System.out.println(count);
    }
}
n = int(input())
car = list(map(int, input().split()))
print(car.count(n))

문제 출처

https://www.acmicpc.net/problem/10797

반응형

+ Recent posts