- 论坛徽章:
- 1
|
很规律的排列,献丑了。- #include <stdio.h>
- void getAncestors(int * ancestor, int layer)
- {
- static int from, to;
-
- if(*ancestor == 0){ from = to = 0; return;}
- ancestor[1] = (ancestor[0] - 1) / 3;
- getAncestors(ancestor + 1, !layer);
- from = from * 3 + 1;
- to = (to + 1) * 3;
- if(layer) *ancestor = from + to - *ancestor;
- }
- int commonAncestor(int a, int b)
- {
- int La[24] = {a}, Lb[24] = {b}, *pa, *pb;
-
- getAncestors(La, 0);
- getAncestors(Lb, 0);
- for(pa = La, pb = Lb; *pa != *pb; *pa > *pb ? pa++ : pb++);
- return *pa;
- }
- int main()
- {
- int a, b;
-
- scanf("%d %d", &a, &b);
- printf("%d\n", commonAncestor(a, b));
- return 0;
- }
复制代码 |
|