본문 바로가기
728x90

전체 글247

[프로그래머스] 모스 부호(1)(C) - strtok() 문자열을 특정 문자(공백 등)으로 자르고 순서대로 접근하는 방법 ptr = strtok(str, " "); while(ptr!=NULL){ ptr = strtok(NULL, " "); } 모스 부호(1) 문제 풀이 #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. char* solution(const char* letter) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요. char* answer = (char*)malloc(sizeof(char)*1000); char* mos[] = {".-","-...","-.-.","-..",".","..-.".. 2023. 9. 21.
[프로그래머스] 특정 문자 제거하기(C) #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. char* solution(const char* my_string, const char* letter) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요. char* answer = (char*)malloc(sizeof(char)*strlen(my_string)); int index=0; for(int i=0;i 2023. 9. 21.
검색 알고리즘 선형 검색(linear search): O(n) #include #include int search(const int a[], int n, int key){ int i; for(i=0;i 2023. 9. 19.
구조체 구조체: 임의의 데이터를 다시 조합하여 만드는 자료구조 구조체 태그: struct와 함께 쓰임 구조체 멤버: 구조체를 구성하는 요소 struct xyz { int x; long y; double z; }; a.x //struct xyz형을 갖는 객체 a p->x //sturct xyz형에 대한 포인터 p typedef struct xyz XYZ; //구조체 자료형은 'struct + 구조체 태그'. 이를 typedef로 간단하게 나타냄 typedef struct { int x; long y; double z; } XYZ; 2023. 9. 19.
배열 배열 역순 정렬 #include #include #define swap(type, x, y) do {type t = x; x = y; y = t;} while(0) void ary_reverse(int a[], int n){ int i; for(i=0;i 2023. 9. 19.
기초 세 값의 최댓값 #include int max3(int a, int b, int c){ int max = a; if(b>max) max = b; if(c>max) max = c; return max; } int main(void) { int a, b, c; printf("세 정수의 최댓값 구하기\n"); printf("a의 값: "); scanf("%d", &a); printf("b의 값: "); scanf("%d", &b); printf("c의 값: "); scanf("%d", &c); printf("최댓값은 %d입니다.\n",max3(a,b,c)); } calloc #include #include int main(void) { int i; int *a; //배열 첫 요소 포인터 int na; //요소.. 2023. 9. 19.