Algorithm(CodeTree, C++)/1차원 배열
[코드트리] 나눗셈의 나머지 C
by kurooru
2024. 5. 18.
메모리를 동적으로 할당 (입력받은 값 만큼 할당)하려면, calloc함수 또는 malloc 함수를 사용해야 함.
(사용할 배열의 형태 *)calloc(할당할 크기, 요소의 크기)
malloc과 calloc의 차이는 malloc은 0으로 초기화를 진행하지 않는다는 점.
#include <stdio.h>
#include <stdlib.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
int *modList = (int *)calloc(b, sizeof(int));
while (a > 1) {
int idx = a % b;
modList[idx]++;
a /= b;
}
int ans = 0;
for (int i = 0; i < b; i++) {
ans += modList[i] * modList[i];
}
printf("%d", ans);
free(modList);
return 0;
}