[Programming questions] degree bear Home __ Cattle Net

Source: Internet
Author: User
Tags abs

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.