需要用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 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd
- ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
- ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
- ¥15 Windows11, backspace, enter, space键失灵