수학 에피소드 - 귀속법 최소치 찾기
5777 단어 귀속
1. 제목
하나의 정수 서열에서 가장 작은 원소를 찾아내 귀속적인 방법으로 실현하다
2. 원본 코드(2분 서열 귀속) 1: #include <stdio.h>
2:
3: int getMin(int array[], int n)
4: {
5: int val1, val2, val3;
6:
7: if(n == 1) return array[0];
8:
9: if(n % 2 == 0)
10: {
11: val1 = getMin(array, n / 2);
12: val2 = getMin(array + n / 2, n / 2);
13:
14: if(val1 < val2)
15: return val1;
16: else
17: return val2;
18: }
19:
20: if(n % 2 != 0)
21: {
22: val1 = getMin(array, n / 2);
23: val2 = getMin(array + n / 2 + 1, n / 2);
24: val3 = array[n/2];
25:
26: if(val1 < val2)
27: {
28: if(val1 < val3) return val1;
29: else return val3;
30: }
31: else
32: {
33: if(val2 < val3) return val2;
34: else return val3;
35: }
36: }
37: }
38:
39: int main()
40: {
41: int array[9] = {11, 13, 23, 56, 8, 23, 11, 23, 111}, val; /* , 8 */
42: val = getMin(array, 9); /* getMin */
43: printf("%d", val);
44: printf("The minum element of this array is %d
", val);
45: return 0;
46: }
1: #include <stdio.h>
2:
3: int getMin(int array[], int n)
4: {
5: int val1, val2, val3;
6:
7: if(n == 1) return array[0];
8:
9: if(n % 2 == 0)
10: {
11: val1 = getMin(array, n / 2);
12: val2 = getMin(array + n / 2, n / 2);
13:
14: if(val1 < val2)
15: return val1;
16: else
17: return val2;
18: }
19:
20: if(n % 2 != 0)
21: {
22: val1 = getMin(array, n / 2);
23: val2 = getMin(array + n / 2 + 1, n / 2);
24: val3 = array[n/2];
25:
26: if(val1 < val2)
27: {
28: if(val1 < val3) return val1;
29: else return val3;
30: }
31: else
32: {
33: if(val2 < val3) return val2;
34: else return val3;
35: }
36: }
37: }
38:
39: int main()
40: {
41: int array[9] = {11, 13, 23, 56, 8, 23, 11, 23, 111}, val; /* , 8 */
42: val = getMin(array, 9); /* getMin */
43: printf("%d", val);
44: printf("The minum element of this array is %d
", val);
45: return 0;
46: }