더블릿 풀이 c언어 pie 문제
http://59.23.150.58/30stair/pie/pie.php?pname=pie&stair=3
# include <stdio.h>
# include <stdlib.h>
# define AND &&
typedef struct {
int Fone[2]; // 0: 분자 , 1: 분모
int Ftwo[2]; // 0: 분자 , 1: 분모
int result[2];// 0: 분자 , 1: 분모
}Friends, *ptrFriends;
// function prototype ==============
ptrFriends retNode(); // func (0)
void _init_(Friends** p); // func (1)
void WriteData(Friends** p); // func (2)
void step01(Friends** p); // func (3)
void result(Friends** p); // func (4)
// =================================
int main(void) { // [MAIN FUNCTION]
ptrFriends node = NULL;
node = retNode();
_init_(&node); // 초기화
WriteData(&node);
step01(&node);
result(&node);
return 0;
} // end of main function
// =================================
ptrFriends retNode() { // func (0)
ptrFriends ret_ = NULL;
ret_ = (ptrFriends)malloc(sizeof(Friends));
return ret_;
} // end of retNode function
// =================================
void _init_(Friends** p) { // func (1)
int i; // index
for (i = 0; i < 2; i++) {
(**p).Fone[i] = 0x0;
(**p).Ftwo[i] = 0x0;
} // endfor
} // end of _init_ function
void WriteData(Friends** p) { // func (2)
int i;
scanf("%d %d", &(**p).Fone[0], &(**p).Fone[1]);
scanf("%d %d", &(**p).Ftwo[0], &(**p).Ftwo[1]);
} // end of WriteData function
void step01(Friends** p) { // func (3)
//========================================
int tmp = 0x0;
int i; // index
int divisor = 0x0;
int large; // 큰 수
int small; // 작은 수
int t1, t2, t3;
int tempOne;
int tempTWO;
//========================================
if((*p)->Fone[1] != (*p)->Ftwo[1]) { // 분모가 서로 다른 경우 통분
// (*p)->Fone[1] != (*p)->Ftwo[1]
// 통분
large = (*p)->Fone[1];
small = (*p)->Ftwo[1];
if (large < small) {
tmp = large;
large = small;
small = tmp;
}
for (i = 1; i <= small; i++) {
if (small%i == 0 AND large%i == 0) {
divisor = i;
}
} // endfor
tempOne = (*p)->Ftwo[1];
tempTWO = (*p)->Fone[1];
if (divisor == 1) { // case 1) 서로소 인 관계
// one
(*p)->Fone[1] = (*p)->Fone[1] * tempOne; // 분모
(*p)->Fone[0] = (*p)->Fone[0] * tempOne; // 분자
// two
(*p)->Ftwo[1] = (*p)->Ftwo[1] * tempTWO; // 분모
(*p)->Ftwo[0] = (*p)->Ftwo[0] * tempTWO; // 분자
}
else { // case 2) 서로소가 아닌 관계
t1 = ((*p)->Fone[1] * (*p)->Ftwo[1]) / divisor;
t2 = t1 / (*p)->Fone[1];
t3 = t1 / (*p)->Ftwo[1];
// one
(*p)->Fone[1] = t1;// 분모
(*p)->Fone[0] = (*p)->Fone[0] * t2; // 분자
// two
(*p)->Ftwo[1] = t1;// 분모
(*p)->Ftwo[0] = (*p)->Ftwo[0] * t3; // 분자
}
}
tmp = ((*p)->Fone[1] - ((*p)->Fone[0] + (*p)->Ftwo[0]));
if (tmp == 0) { // 다 먹은 경우
// case 1-1)
(*p)->result[0] = 0;
(*p)->result[1] = 0;
}
else { // 일부 남게 되는 경우
// case 1-2)
if ((*p)->Fone[1] % tmp == 0) {
// case 1-2-1) 분모가 분자로 나누어 떨어지는 경우
(*p)->result[0] = 1;
(*p)->result[1] = (int)((*p)->Fone[1] / tmp);
}
else {
// case 1-2-2) 분모가 분자로 나누어 떨어지지 않는 경우
//(*p)->Fone[1] % tmp != 0
for (i = 1; i < tmp; i++) {
if (tmp%i == 0 AND(*p)->Fone[1] % i == 0) {
divisor = i;
}
}
if (divisor == 1) {
(*p)->result[0] = tmp; // 분자
(*p)->result[1] = (*p)->Fone[1]; // 분모
}
else {
// divisor != 1
(*p)->result[0] = tmp/divisor; // 분자
(*p)->result[1] = (*p)->Fone[1] / divisor; // 분모
}
}
}
} // end of step01 function
void result(Friends** p) { // func (4)
if ((*p)->result[0] == 0) {
printf("%d\n", 0);
}
else {
// (*p)->result[0] != 0
printf("%d/%d\n", (*p)->result[0], (*p)->result[1]);
}
} // end of result function
'언어 > c언어' 카테고리의 다른 글
네이버 지식인 풀이 (0) | 2018.11.22 |
---|---|
네이버 풀이 (0) | 2018.11.19 |
선택정렬 (0) | 2018.11.18 |
버블 정렬 (0) | 2018.11.18 |
c언어 네이버 지식인 풀이 (0) | 2018.11.17 |