네이버 문제 풀이 - 최대공약수
https://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=315414192
# include <stdio.h>
# include <stdlib.h>
typedef struct {
int numOne;
int numTwo;
int numGcd; // 최대공약수
}NUM;
// 함수 원형 ====================================
void _init_(NUM* P); // func (1)
void stepOne(NUM* P); // func (2)
// =============================================
int main(void) { // main function
NUM node;
_init_(&node); // 초기화
stepOne(&node);
return 0;
} // end of main function
// =============================================
void _init_(NUM* P) { // func (1)
P->numOne = 0X0;
P->numTwo = 0X0;
P->numGcd = 0X0;
} // end of _init_ function
// =============================================
void stepOne(NUM* P) { // func (2)
int tmpLarge = 0x0;
int tmpSmall = 0x0;
int tmp = 0x0;
int tmpQ = 0x0; // 몫
int tmpR = 0x0; // 나머지
while (1) {
scanf("%d %d", &P->numOne, &P->numTwo);
if (P->numOne == 0 || P->numTwo == 0) {
exit(1);
}
else {
// P->numOne != 0 && P->numTwo != 0
if (P->numOne < 0 || P->numTwo < 0) {
exit(1);
}
else {
//P->numOne > 0 && P->numTwo > 0
tmpLarge = P->numOne;
tmpSmall = P->numTwo;
if (tmpLarge == tmpSmall) {
P->numGcd = tmpLarge;
}
else { // tmpLarge != tmpSmall
if (tmpLarge < tmpSmall) {
tmp = tmpLarge;
tmpLarge = tmpSmall;
tmpSmall = tmp;
}
else {
// tmpLarge > tmpSmall
if (tmpSmall == 1) {
P->numGcd = 1;
}
else {
// tmpSmall != 1
while (1) { // 유클리드 호제법
tmpR = tmpLarge % tmpSmall;
if (tmpR == 0) { break; }
tmpLarge = tmpSmall;
tmpSmall = tmpR;
} // end while
P->numGcd = tmpSmall;
} // end else
} // end else
} // end else
} // end else
printf("%d와 %d의 최대공약수(Greatest Common Diviosr) : %d \n", P->numOne, P->numTwo, P->numGcd);
} // end else
} // end while
} // end of stepOne function
'언어 > c언어' 카테고리의 다른 글
c언어 네이버 풀이 중첩 for문을 사용해서 3을 입력하면 (0) | 2019.05.06 |
---|---|
네이버 풀이 (0) | 2018.12.01 |
네이버 지식이 풀이 (0) | 2018.11.28 |
더블릿 문제 (0) | 2018.11.27 |
네이버 지식인 풀이 (c) (0) | 2018.11.24 |