需要用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 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示
- ¥15 求三国群英传pl国战时间的修改方法
- ¥15 matlab代码代写,需写出详细代码,代价私
- ¥15 ROS系统搭建请教(跨境电商用途)