There are n points on a number axis, and the coordinates of the first point is the degree of the bear now position, the N-1 point is the home of the degree bear. Now he needs to move from number No. 0 to coordinates n-1th.
But in addition to coordinates No. 0 and N-1th, he can select a point in the remaining N-2 coordinates and ignore the point directly, asking how much distance the bear will go home at least.
Enter a description:
Enter a positive integer n, n <= 50.
The next n integers represent the coordinates, a positive number represents the positive direction of the x-axis, and a negative number represents the negative direction of the X axis. Absolute value is less than or equal to 100
Output Description:
Output an integer representing the minimum distance a bear needs to go.
Enter an example:
4
1 4-1 3
Output Example:
4
Simulation questions
#include "iostream" #include "stdlib.h" using namespace std;
int main () {int n,beg,end;
int ai[100];
int mmin=1e9;
cin>>n;
cin>>beg;
for (int i=0;i<n-2;i++) cin>>ai[i];
cin>>end;
for (int i=0;i<n-2;i++) {int Qi=beg;
int sum=0;
for (int j=0;j<n-2;j++) {if (j==i) continue;
Sum+=abs (Qi-ai[j]);
QI=AI[J];
} sum+=abs (END-QI);
if (sum<mmin) mmin=sum;
} cout<<mmin<<endl;
return 0; }