#include <stdio.h>
#include <stdlib.h>
//정렬
int compare(const void *a,const void *b) {
    int A= *(int *)a;
    int B= *(int *)b;
    return A-B;
}
//하한
int find_low(int *arr, int size, int m) {
    //시작
    int low=0;
    //끝
    int high=size-1;
    //시작이 끝보다 크기전까지 다함
    //반복문이 끝나면 low>high 인 상태 -> low가 m 이상의 값 중 가장 작음
    //high: m보다 작은 값들 중 가장 오른쪽에 위치함.
    while (low<=high) {
        //중간값
        int mid=(low+high)/2;
        //하한값 찾기 위해서 찾는수가 더 작으면 끝 범위를 줄임
        if(arr[mid]>=m)  {
            high=mid-1;
        }
            //찾는수가 크면 시작값 높임
        else if(arr[mid]<m) {
            low=mid+1;
        }
    }
    return low;
}
//상한
int find_high(int *arr, int size, int m) {
    int low=0;
    int high=size-1;
    while (low<=high) {
        int mid=(low+high)/2;
        if(arr[mid]<=m)  {
            low=mid+1;
        }
        else if(arr[mid]>m) {
            high=mid-1;
        }
    }
    return low;
}

int main() {
    int n,m;
    scanf("%d",&n);
    int num_n[n];
    for(int i=0; i<n; i++) {
        scanf("%d",&num_n[i]);
    }
    qsort(num_n,n,sizeof(int),compare);
    scanf("%d",&m);
    int num_m[m];
    for(int i=0; i<m; i++) {
        scanf("%d",&num_m[i]);
    }  
    for(int i=0; i<m; i++) {
        int l=find_low(num_n,n,num_m[i]);
        int h=find_high(num_n,n,num_m[i]);
        printf("%d ",h-l);
    }
    
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: