New Ket net Baidu 2017 spring recruit the written question programming question collection (altogether 5 questions) __ Water question

Source: Internet
Author: User
Tags array length time limit cmath
1. [Programming questions] Buy hats

Time limit: 1 seconds

Space limit: 32768K degree bear wants to go to the mall to buy a hat, there are n hats in the mall, some hats may be the same price. The degree bear wants to buy a third-cheapest hat and ask the price of the third-cheapest hat.
Enter a description:

First enter a positive integer n (n <= 50), and then enter the number of N to indicate the price of each hat (the price is a positive integer and less than or equal to 1000)


Output Description:
If there is a third cheap hat, please output this price is how much, otherwise output-1

Enter an example:
10 10 10 10 20 20 30 30 40 40

Output Example:
30
Analysis: Compress the array, save it with the new array, and determine whether the new array length is greater than or equal to 3
The AC code is as follows:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=50+1;

int A[MAXN],B[MAXN];

int main ()
{
    int n,num=0;
    cin>>n;
    for (int i=0;i<n;i++) cin>>a[i];
    Sort (a,a+n);
    for (int i=0;i<n;i++)
    {
        b[num]=a[i];
        while (I+1<n && b[num]==a[i+1])
        {
            i++;
        }
        num++;
    }
    if (num>=3)
        cout<<b[2]<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


2, [programming questions] degrees Bear Home

Time limit: 1 seconds

Space limit: 32768K A number of a number on a number of points, the first point of the coordinates is the degree of the bear now position, the N-1 point is the degree of Bear's home. 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
Parse: Enumerate the ignored points and mark them, save them with the new array, and do the appropriate operation.
The AC code is as follows:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=50+1;

int fun (int s[],int t[],int N)
{
    int sum=0,num=0;
    int C[MAXN];
    for (int i=0;i<n;i++)
    {
        if (!t[i]) {
            c[num]=s[i];
            num++
        }
    }
    for (int i=1;i<num;i++)
    {
        sum+=abs (c[i]-c[i-1]);

    }
    return sum;
}

int main ()
{
    int a[maxn];
    int B[MAXN];
    int n,mini=999999;
    cin>>n;
    for (int i=0;i<n;i++) cin>>a[i];
    Memset (b,0,sizeof (b));
    for (int i=1;i<n-1;i++)
    {
        b[i]=1;
        Mini=min (Mini,fun (a,b,n));
        b[i]=0;
    }
    cout<<mini<<endl;
    return 0;
}
3. [Programming questions] looking for triangles

Time limit: 1 seconds

Space limit: 32768K three-dimensional space with n points, each point may be one of three colors, three colors are red, green and blue, respectively, ' R ', ' G ', ' B '.
Now it's time to figure out three points and make a triangle that's the largest area of the triangle.
But the triangle must be satisfied: The three-point color is either all the same or all different.
Enter a description:

First, enter the number of points in a positive integer n three-dimensional coordinate system. (N <=) 

Next n rows, each row is entered with a C x y z,c as one of the ' R ', ' G ', ' B '. The x,y,z is the coordinates of the point. (coordinates are integers from 0 to 999)


Output Description:
Output a number that represents the largest triangular area and retains 5 decimal places.

Enter an example:
5
r 0 0 0
R 0 4 0
R 0 0 3
G EUR 7
G 12 16 + 8

Output Example:
6.00000
Analysis: Because to judge a number of conditions, so separate write function implementation, to find the area with Helen Formula
The AC code is as follows:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std;

const int maxn=50+1;
    struct point {char color;
    int x;
    int y;
int z;

}PO[MAXN];
    Double Juli (Point A,point b) {double sum;
    Sum=sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y) + (a.z-b.z) * (A.Z-B.Z));
return sum;
    Double area (double a,double b,double C)//size {double p= (a+b+c)/2.0;
    Double S=sqrt (p* (p-a) * (p-b) * (p-c));
return s; BOOL IsColor (Point a,point b,point C)//Judgement Color {if (a.color==b.color && b.color==c.color && a.color=
    =c.color) return true;
    if (a.color!=b.color && b.color!=c.color && A.color!=c.color) return true;
return false;
    Double ISK (Point a,point b,point C)/is a triangular {double kx,ky,kz;
    Kx=juli (A,B);
    Ky=juli (A,C);
    Kz=juli (B,C);
    if (kx<ky+kz && ky<kx+kz && kz<ky+kx) return true;
return false; } inT main () {int n;
    Double maxn=-1.0;
    cin>>n;
    for (int i=0;i<n;i++) {cin>>po[i].color>>po[i].x>>po[i].y>>po[i].z;
                for (int i=0;i<n;i++) for (int j=0;j<n;j++) for (int k=0;k<n;k++) {
                    if (i!=j && j!=k && i!=k) {if (IsColor (po[i],po[j],po[k))
                            {if (ISK (Po[i],po[j],po[k])) {
                        Maxn=max (Maxn,area (Juli (PO[I],PO[J)), Juli (Po[i],po[k]), Juli (Po[j],po[k)));
    }} printf ("%.5lf\n", MAXN);
return 0;
 }


4, [programming questions] interesting sort

Time limit: 1 seconds

Space limit: 32768K degree Bear has an array of n numbers, and he wants to order the array from large to small, but the degree of the eruption of the bear will only be the following operation:
Take a number from the array and place it in the last position of the array.
Ask how many times the minimum operation can make the array small to large order.
Enter a description:

First enter a positive integer n, and the next line enters n integers. (N <= 50, the absolute value of each number is less than or equal to 1000)


Output Description:
Output An integer that represents the minimum number of operations.

Enter an example:
4
19 7 8 25

Output Example:
2
Analysis: With the original sequence and sorting (from small to large) after the sequence comparison, from beginning to end, see how many elements have been relatively sorted well, that is, do not need to move, the rest is the need to move
The AC code is as follows:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=50+1;

int A[MAXN],SORTA[MAXN];

int main ()
{
    int n;
    int count=0;
    cin>>n;
    for (int i=0;i<n;i++) {
           cin>>sorta[i];
           A[i]=sorta[i];
    }
    Sort (sorta,sorta+n);
    for (int i=0,j=0;i<n;i++)
    {
        if (A[i]==sorta[j])
        {
            count++;
            j + +;
        }
    }
    cout<<n-count<<endl;
    return 0;
}

5, [Programming question] inequality series

Time limit: 1 seconds

Space limitations: 32768K degrees bears have recently been particularly interested in the whole arrangement, for an arrangement of 1 to N, the degree bears find that the appropriate greater than and less than symbols (i.e. ' > ' and ' < ') can be inserted into a legal inequality sequence in the middle according to the size relationship. But now in the hands of a bear only K is less than the symbol (' < ') and N-k-1 is greater than the symbol (that is, ' > '), the degree bears want to know how many permutations of the 1 to n arbitrary arrangement can use these symbols to make it a valid inequality sequence.
Enter a description:

The input includes a line containing two integers n and K (K < n≤1000)


Output Description:
Output satisfies the conditions of the number of permutations, the answer to 2017 modulo.

Enter an example:
5 2

Output Example:
66
Analysis: Bo main ability is too weak this problem does not ah, should use dynamic planning

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.