~cpp #include <stdio.h> int search_middleNum(int *, int *, int *); void main() { int a, b, c, middleNum; while(1){ printf("멈추려면 '-999 0 0' 입력\n"); printf("비교할 A, B, C 값을 차례로 입력 : "); scanf("%d %d %d", &a, &b, &c); if(a == -999){ break; } middleNum = search_middleNum(&a, &b, &c); printf("중위수 = %d", middleNum); } } int search_middleNum(int *a, int *b, int *c) { int middleNum_1, middleNum_2, i_1=0, i_2=0; middleNum_1=middleNum_2=(*a+*b+*c)/3; while (1){ middleNum_1++; i_1++; if (middleNum_1==*a||middleNum_1==*b||middleNum_1==*c) break; } while (1){ middleNum_2--; i_2++; if (middleNum_2==*a||middleNum_2==*b||middleNum_2==*c) break; } if (i_1<i_2) return middleNum_1; else return middleNum_2; }
~cpp #include <stdio.h> int search_middleNum(int *, int *, int *); void main() { int a, b, c, middleNum; while(1){ printf("멈추려면 '-999 0 0' 입력\n"); printf("비교할 A, B, C 값을 차례로 입력 : "); scanf("%d %d %d", &a, &b, &c); if(a == -999){ break; } middleNum = search_middleNum(&a, &b, &c); printf("중위수 = %d", middleNum); } } int search_middleNum(int *a, int *b, int *c) { int middleNum_1, middleNum_2; middleNum_1=middleNum_2=(*a+*b+*c)/3; while (1){ if (middleNum_1==*a||middleNum_1==*b||middleNum_1==*c) return middleNum_1; if (middleNum_2==*a||middleNum_2==*b||middleNum_2==*c) return middleNum_2; middleNum_1++; middleNum_2--; } }
과정 | a | b | c | middleNum_1 | middleNum_2 | middleNum |
middleNum_1=middleNum_2=(*a+*b+*c)/3 | 45 | 14 | 38 | 32 | 32 | ? |
middleNum_1++; middleNum_2--; | 45 | 14 | 38 | 38 | 26 | ? |
return middleNum_1; | 45 | 14 | 38 | 38 | 26 | 38 |