首页 > 解决方案 > 打印要选择的两个元素的功率总和

问题描述

我有一份失去权力的元素的清单。我需要选择 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.

标签: c++

解决方案


推荐阅读