https://www.hackerrank.com/challenges/weather-observation-station-11/problem

 

Weather Observation Station 10 | HackerRank

Query a list of CITY names not ending in vowels.

www.hackerrank.com

Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

a,e,i,o,u로 시작하지 않거나 a,e,i,o,u로 끝나지 않는 CITY를 찾는 문제

 

https://www.hackerrank.com/challenges/weather-observation-station-12/problem

 

Weather Observation Station 12 | HackerRank

Query an alphabetically ordered list of CITY names not starting and ending with vowels.

www.hackerrank.com

a,e,i,o,u로 시작하지 않고 a,e,i,o,u로 끝나지 않는 CITY를 찾는 문제

반응형

https://www.hackerrank.com/challenges/weather-observation-station-10/problem

 

Weather Observation Station 9 | HackerRank

Query an alphabetically ordered list of CITY names not starting with vowels.

www.hackerrank.com

Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

 

a,e,i,o,u 로 끝나지 않는 CITY 찾기

반응형

https://www.hackerrank.com/challenges/weather-observation-station-9/problem

 

Weather Observation Station 6 | HackerRank

Query a list of CITY names beginning with vowels (a, e, i, o, u).

www.hackerrank.com

Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

a,e,i,o,u 로 시작하지 않는 CITY를 찾는 문제 

반응형

https://www.hackerrank.com/challenges/weather-observation-station-8/problem

 

Weather Observation Station 8 | HackerRank

Query CITY names that start AND end with vowels.

www.hackerrank.com

Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

 

STATION 테이블에서 a,e,i,o,u로 시작하고 a,e,i,o,u로 끝나는 CITY를 찾는 문제

반응형

https://www.hackerrank.com/challenges/weather-observation-station-7/problem

 

Weather Observation Station 7 | HackerRank

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION.

www.hackerrank.com

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

a,e,i,o,u 로 끝나는 CITY 중복없이 찾는 문제 

마찬가지로 OR로 조건을 나누거나 정규표현식으로 해결

반응형

https://www.hackerrank.com/challenges/weather-observation-station-5/problem

 

Weather Observation Station 5 | HackerRank

Write a query to print the shortest and longest length city name along with the length of the city names.

www.hackerrank.com

 

Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
The STATION table is described as follows:

 

CITY 이름중에 가장 짧은 것과 가장 긴 것을 각각 찾고, 길이가 같은 것이 있을 경우 알파벳 순으로 가장 빠른 것을 찾는 문제 

반응형

이동 평균 필터란?

->지정된 n개의 데이터만 가지고 평균을 구하는 방식

->새로운 데이터가 들어오면 가장 오래된 데이터는 버리고 n개로 평균을 구하는 방식

 

이번에도 굳이 구조체를 만들 필요는 없지만 구조체로 만들어보았다. 

만약 평균을 구할 데이터의 갯수 n이 정해져 있지 않고 사용자가 마음대로 초기화 할 수 있다고 가정하여, 

n이 정해지면 해당 갯수만큼 데이터를 담을 배열은 malloc을 사용하였다. 

#include <stdio.h>
#include <stdlib.h>
typedef struct 
{
    int cnt;
    int num;
    int *data;
    float preAvg;
    float nowAvg;
    int idx;
}mvAvg;

int init_mvAvgFilter(mvAvg *mvAvg, int num){
    mvAvg->cnt=0;
    mvAvg->num=num;
    mvAvg->data =(int*)malloc(sizeof(int)*num);
    mvAvg->preAvg=0;
    mvAvg->idx=0;
}

float mvAvgFilter(mvAvg *mvAvg,int data){
    if (mvAvg->cnt < mvAvg->num){
        mvAvg->data[mvAvg->cnt]=data;
        mvAvg->cnt++;
        
        int i=0;
        int sum=0;
        for (i=0;i<mvAvg->cnt;i++){
            sum+=mvAvg->data[i];
        }
        mvAvg->nowAvg=(float)sum/mvAvg->cnt;
        mvAvg->preAvg=mvAvg->nowAvg;
        return mvAvg->nowAvg;
    }

    mvAvg->nowAvg=(data-mvAvg->data[mvAvg->idx])/mvAvg->num+mvAvg->preAvg;
    mvAvg->data[mvAvg->idx]=data;
    mvAvg->idx++;
    if(mvAvg->idx == mvAvg->num){
        mvAvg->idx=0;
    }
    mvAvg->preAvg=mvAvg->nowAvg;
    return mvAvg->nowAvg;
}

int main(){
    mvAvg mvAvg;
    init_mvAvgFilter(&mvAvg,4);
    int i=0;
    for (i=1;i<=10;i++){
        printf("%d : %f\r\n",i,mvAvgFilter(&mvAvg,i));
    }
}

잘 되는 것 같은데.. 테스트가 부족하다.. 

 

반응형

'지식저장소 > C' 카테고리의 다른 글

[C] AverageFilter (평균 필터)  (0) 2022.04.28
[C] Ring Buffer / 링버퍼 구현  (0) 2022.03.29
[C/BOJ] 9012 괄호 - Stack이용  (0) 2022.02.16
[C]구조체를 이용한 Stack 구현  (0) 2022.02.16
[C/STM32] Uart로 printf 대신하기  (0) 2022.01.14

데이터들이 계속 들어올 때마다 평균을 갱신하는 AverageFilter 소스 

 

데이터의 수와 이전 평균만 전역 변수나 static 변수로 저장해도 되지만

구조체로 만들어보았다.

#include <stdio.h>

typedef struct 
{
    int dataCnt;
    float preAvg;
    float nowAvg;
}Avg;

float avgFilter(Avg *Avg,int data){
    if(Avg->dataCnt==0){
        Avg->dataCnt++;
        Avg->nowAvg=data;
        Avg->preAvg=data;
        return Avg->nowAvg;
    }
    Avg->preAvg=Avg->nowAvg;
    Avg->dataCnt++;
    Avg->nowAvg=(1 - (float)1/(Avg->dataCnt))*(Avg->preAvg)+((float)1/Avg->dataCnt)*data;
    return Avg->nowAvg;
}

int main(){
    Avg avg;
    avg.dataCnt = 0;
    avg.nowAvg = 0;
    avg.preAvg = 0;

    printf("%f\r\n", avgFilter(&avg,10));
    printf("%f\r\n", avgFilter(&avg,20));
    printf("%f\r\n", avgFilter(&avg,30));
    printf("%f\r\n", avgFilter(&avg,40));
    printf("%f\r\n", avgFilter(&avg,50));
    printf("%f\r\n", avgFilter(&avg,60));
    printf("%f %f\r\n",avg.preAvg,avg.nowAvg);
}

반응형

+ Recent posts