Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

맛만 볼게요

프로그래머스 - 숫자 카드 나누기 - c 본문

c/프로그래머스

프로그래머스 - 숫자 카드 나누기 - c

여기우리집 2022. 11. 15. 16:52
프로그래머스 - 숫자 카드 나누기
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(int arrayA[], size_t arrayA_len, int arrayB[], size_t arrayB_len) {
	int answer = 0;
	int* a = malloc(sizeof(int) * arrayA_len);
	int* b = malloc(sizeof(int) * arrayB_len);
	int a_idx = 0, b_idx = 0;
	
	// a b 공약수들
	for (int i = 2; i <= arrayA[0]; i++) {
		for (int j = 0; j < (int)arrayA_len; j++) {
			if (arrayA[j] % i != 0) {
				break;
			}
			else if (j == (int)arrayA_len - 1) {
				a[a_idx++] = i;
			}
		}
	}

	for (int i = 2; i <= arrayB[0]; i++) {
		for (int j = 0; j < (int)arrayB_len; j++) {
			if (arrayB[j] % i != 0) {
				break;
			}
			else if (j == (int)arrayB_len - 1) {
				b[b_idx++] = i;
			}
		}
	}

	// 조건 만족?
	for (int i = 0; i < a_idx; i++) {
		for (int j = 0; j < (int)arrayB_len; j++) {
			if (arrayB[j] % a[i] == 0) {
				break;
			}
			else if (j == (int)arrayB_len - 1) {
				if (a[i] > answer) {
					answer = a[i];
				}
			}
		}
	}

	for (int i = 0; i < b_idx; i++) {
		for (int j = 0; j < (int)arrayA_len; j++) {
			if (arrayA[j] % b[i] == 0) {
				break;
			}
			else if (j == (int)arrayA_len - 1) {
				if (b[i] > answer) {
					answer = b[i];
				}
			}
		}
	}

	return answer;
}

a, b 배열을 아무 생각 없이

int a[(int)arrayA_len] = ....

이렇게 하려니 배열 크기에 상수만 넣으라더라...

 

그래서 역시 아무 생각 없이

arrayA_len으로 동적할당 때려버림.

 

테스트 케이스 완.벽.

 

+

대충 몇 개 테스트 케이스 생각하고 대충 넣었더니 완.벽.

 

당연히 정답일 줄 알았음.

 

근데 돌렸더니 

통과.

통과.

통과.

...

실패 (signal: aborted (core dumped))

 

실패가 4개나...

 

뭐가 문제인지 찾아 보려고 눈 빠지게 봤으나

아무리 봐도 로직상의 문제는 없는 듯 하고..(물론 있을 가능성 매우 큼)

셀프 테스트로는 너무나 완벽했음.

그렇게 몇 시간 동안 숨만 쉬고 눈만 깜빡이며 보다가 도저히 모르겠어서

친구 헬프 때림.

 

친구가 존나 현명한 게 실패 문구를 검색해 봤다 함.

찾아보니 메모리 범위? 같은 걸 벗어나서 강제 종료됐다는 오류라카더라.

그러면서 공약수가 제시된 배열 길이보다 많을 수도 있지 않냐더라....

 

와...

 

걔 ㄹㅇ 천재인 줄 알았음.

 

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

 

동적할당을 정적마냥 때려 박아 놓은 게 문제였던 거임.

 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(int arrayA[], size_t arrayA_len, int arrayB[], size_t arrayB_len) {
    int answer = 0;
    int* a = malloc(sizeof(int));
    int* b = malloc(sizeof(int));
    int a_idx = 0, b_idx = 0;

    // a b 공약수들
    for (int i = 2; i <= arrayA[0]; i++) {
        for (int j = 0; j < (int)arrayA_len; j++) {
            if (arrayA[j] % i != 0) {
                break;
            }
            else if (j == (int)arrayA_len - 1) {
                a_idx++;
                a = realloc(a, a_idx * sizeof(int));
                a[a_idx - 1] = i;
            }
        }
    }

    for (int i = 2; i <= arrayB[0]; i++) {
        for (int j = 0; j < (int)arrayB_len; j++) {
            if (arrayB[j] % i != 0) {
                break;
            }
            else if (j == (int)arrayB_len - 1) {
                b_idx++;
                b = realloc(b, b_idx * sizeof(int));
                b[b_idx - 1] = i;
            }
        }
    }

    // 조건 만족?
    for (int i = 0; i < a_idx; i++) {
        for (int j = 0; j < (int)arrayB_len; j++) {
            if (arrayB[j] % a[i] == 0) {
                break;
            }
            else if (j == (int)arrayB_len - 1) {
                if (a[i] > answer) {
                    answer = a[i];
                }
            }
        }
    }

    for (int i = 0; i < b_idx; i++) {
        for (int j = 0; j < (int)arrayA_len; j++) {
            if (arrayA[j] % b[i] == 0) {
                break;
            }
            else if (j == (int)arrayA_len - 1) {
                if (b[i] > answer) {
                    answer = b[i];
                }
            }
        }
    }

    return answer;
}

 

고치고 나니 너무 잘 됨.

 

간신히 통과..!

'c > 프로그래머스' 카테고리의 다른 글

프로그래머스 - 등대 - c  (1) 2022.11.20
프로그래머스 - 과일 장수 - c  (0) 2022.11.15
Comments