QianYi Ke? 2019-10-19 15:03 采纳率: 100%
浏览 264
已采纳

这是一道本科的编程问题,有点复杂,因为我刚学一年还不大清楚,请问有人愿意解答吗?谢谢

需要用C++语言,谢谢

图片说明
图片说明

  • 写回答

2条回答 默认 最新

  • soar3033 2019-10-19 19:15
    关注

    代码:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        while (true)
        {
            int n = 0;
            int count = 0;
            cin >> n;
            int* nodes = (int*)malloc(sizeof(int)*n);
            for (int i = 1; i < n; i++)
            {
                int k = 0;
                cin >> k;
                cin >> k;
                cin >> nodes[k - 1];
            }
            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    int max = 0;
                    int i_tmp = i;
                    int j_tmp = j;
                    while (true)
                    {
                        if (i_tmp == j_tmp)
                        {
                            count += max;
                            break;
                        }
                        else
                        {
                            if (i_tmp >= 2 * j_tmp)
                            {
                                if (max < nodes[i_tmp])
                                {
                                    max = nodes[i_tmp];
                                }
                                if (i_tmp % 2 == 1)
                                {
                                    i_tmp = (i_tmp - 1) / 2;
                                }
                                else
                                {
                                    i_tmp /= 2;
                                }
                            }
                            else if (2 * i_tmp <= j_tmp)
                            {
                                if (max < nodes[j_tmp])
                                {
                                    max = nodes[j_tmp];
                                }
                                if (j_tmp % 2 == 1)
                                {
                                    j_tmp = (j_tmp - 1) / 2;
                                }
                                else
                                {
                                    j_tmp /= 2;
                                }
                            }
                            else
                            {
                                if (max < nodes[i_tmp])
                                {
                                    max = nodes[i_tmp];
                                }
                                if (i_tmp % 2 == 1)
                                {
                                    i_tmp = (i_tmp - 1) / 2;
                                }
                                else
                                {
                                    i_tmp /= 2;
                                }
    
                                if (max < nodes[j_tmp])
                                {
                                    max = nodes[j_tmp];
                                }
                                if (j_tmp % 2 == 1)
                                {
                                    j_tmp = (j_tmp - 1) / 2;
                                }
                                else
                                {
                                    j_tmp /= 2;
                                }
                            }
                        }
                    }
                }
            }
            cout << "sum is" << count << "\n\n";
        }
        return 0;
    }
    
    

    运行结果
    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了