c++ - 打印要选择的两个元素的功率总和
问题描述
我有一份失去权力的元素的清单。我需要选择 2 并将它们混合,结果是第一个元素减去第二个元素的幂。Thr 结果应该尽可能接近我们在做任何事情之前选择的值。
我正在尝试获得最接近的派,如果有很多选项,我需要选择一个使它们的权力总和最小化的选项。
示例输入:
5
8
2
1
4
2
3
2
2
6
输出:
7
第一行输入是我们想要的结果。其次是表示没有元素的整数。然后每一行都包含权力。
输出应该是要选择的元素的总和。
我尝试了多种方法,但不知何故没有选择最接近的对。这是我用来查找最近对的一种方法。
Begin
Declare function Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2) to the double datatype.
Declare Minimum to the double datatype.
Initialize Minimum = dist.
for (int i = 0; i < s; ++i)
for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j)
if (Distance(stp[i],stp[j]) < Minimum) then
Minimum = Distance(stp[i], stp[j]).
pnt1.poi1 = stp[i].poi1, pnt1.poi2 = stp[i].poi2.
pnt2.poi1 = stp[j].poi1, pnt2.poi2 = stp[j].poi2.
Return Minimum.
End.
解决方案
推荐阅读
- swift - 为 x 数量的按钮添加目标并返回到 tabBarController
- macos - 无法将 Omnetpp 安装到 M1 Big Surr
- jmeter - NoHttpResponseException 但未设置响应超时
- c++ - 如何继承 WinAPI ListView 以修改其绘图
- youtube-api - 更新直播标题
- mysql - Mysql table Pivot to compare sales from current and previous year from the same table
- python - 在 Raspberry Pi 上构建 Python 时出现致命错误
- angular - 当没有数据匹配时,如何在 Angular 中为 KendoUI 自动完成设置“noDataTemplate”文本
- javascript - Why Are My Variables Coming Back As Undefined From an External Javascript Page?
- r - 从另一个行创建新数据框