문제
내가 작성한 코드
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b;
scanf("%d %d", &a, &b);
int max = 1;
int small = a;
if(a>b){
small = b;
}
for(int i = 2; i<=small; i++){
if(a%i==0 && b%i==0){
max = i;
}
}
int min = (a*b)/max;
printf("%d %d\n", max, min);
return 0;
}
<최대공약수>
- a,b 중에 작은 수를 기준으로 1부터 작은 수까지 차례대로 나눈다
- a,b 둘다 나누어떨어지는 수를 max에 저장한다
<최소공배수>
- a*b의 값을 최대공약수 max로 나누어준다
'C언어 > 문제은행' 카테고리의 다른 글
[3주차] 인치, 센티미터 계산기 (0) | 2023.04.19 |
---|---|
[3주차] 사칙연산 계산기 (0) | 2023.04.19 |
[3주차] "Hello, World!" in C (0) | 2023.04.19 |
[2주차] Challenge - sorting and counting (0) | 2023.04.18 |
[2주차] 별찍기 (0) | 2023.04.18 |