理解错了,看题目好久,考完做了下 #include<iostream> #include<map> #include<vector> #include<algorithm> using namespace std; int data[10000]; void buildtree(int n) { int zhi = n - 1; int index = 1; for (int i = 0; i < zhi; i++) { index = index * 2; } int end = index * 2 - 1; for (int i = index; i <= end; i++) { data[i] = 2 * (i - index) + 1; } for (int i = index - 1; i >= 1; i--) data[i] = (data[i * 2] + data[i * 2 + 1]) / 2;   } void findroot(int m, int p, int q) { int index1 = 0; int index2 = 0; int index3 = 0; for (int i = 1; i < 10000; i++) { if (data[i] == m) index1 = i; if (data[i] == p) index2 = i; if (data[i] == q) index3 = i; } while (index1 != index2) { if (index1 > index2) index1 = index1/ 2; else if (index2 > index1) index2 = index2 / 2; }   while (index3 != index2) { if (index3 > index2) index3= index3 / 2; else if (index2 > index3) index2 = index2 / 2; }   cout << data[index3]<< endl; } int main() { int n, m, p, q; while (cin >> n >> m >> p >> q) { buildtree(n); findroot(m, p, q); } }
点赞 4

相关推荐

06-04 19:53
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务