Ali 2017 Summer Intern Recruitment Technology Post Programming questions 2--java to achieve __ programming

Source: Internet
Author: User
First, the problem description:

Xiao Zhang 51 home, parents arranged for him many blind Date girl, set the number of girls is N.

The father is responsible for arranging appointments, and each time a random selection of the object of a blind date, the mother is responsible for recording which girls have dated. The blind Date will not end until after a date with all the girls.

Parents are arguing these days and there is no verbal communication between them. So the father didn't know that the girls had already dated. So the next date is likely to be the one you've already dated.

Excuse me, if Xiao Zhang is going to date all the girls, how many girls will be on average. second, the request

Input:
A total of 1 rows, giving the value of N, a positive integer.

Output:
A total of one line, that is, the average number of appointments, retaining 4 decimal places.

Enter an example:
100

Output Example:
512.0000 three, solve

programming Languages: Java

idea:

n Girl, serial number is 1~n, set up an array of length n, representing the girl's dating status, 0 represents not and Xiao Zhang about the meeting, 1 represents has been and small Zhang about, the array element of the initial value set to 0.

Set a variable to record the number of appointments.

Each random generation of a 1~n between the number of M, representing the upcoming and serial number M girl date, and then the girl's appointment status from 0 to 1;
And then the sum of all the girls ' state attributes, if and for N, shows that Xiao Zhang has dated all the girls and made a note of the number of appointments at this time.

The

loops through the above operation 100 times, and then according to each cycle of the number of Zhang's appointment, to find the mean, that is, as the answer to the question.

Package test;
Import Java.awt.Point;
Import java.io.*;

Import Java.util.Scanner; public class Datewithgirls {/** * @param args * @throws ioexception/public static void main ( String[] args) throws IOException {//TODO auto-generated method stub Scanner Scanner = new Scanner (syste

        m.in);

        The number of girls used to accept the input of the console N int n=0;
            while (Scanner.hasnext ()) {N = Scanner.nextint ();

            Int[] statusofgirls= new Int[n];
            Set the number of experiments.
            int times = 100;

            This array stores the number of appointments per experiment record int[] Allcount = new Int[times]; Start experiment for (int t=0;t<times;t++) {int count = 0;//count the number of appointments int sum = 0;//to all girls
                Appointment status to sum//Set the girl's initial appointment status is 0.
                for (int k=0;k<m;k++) {Statusofgirls[k] = 0;
                Boolean iscontinue = true;
                    while (iscontinue) {sum = 0;
                    Randomly generate a girl to date.
                    int ss = (int) Math.floor (math.random () *m);
                    count+=1;//Appointment number plus 1//Set the girl's appointment status to 1.

                    STATUSOFGIRLS[SS] = 1;
                    Plus the total girl's dating status.
                    for (int j=0;j<m;j++) {sum+=statusofgirls[j]; } if (sum==m) {//If all girls have an appointment status of 1 then terminate the loop Iscontinue = Fals
                    E
                }//system.out.println (ss);
            } Allcount[t] = count;//The number of appointments into array} int totalcount=0;
            for (int j=0;j<times;j++) {totalcount+=allcount[j];
            Double meancount = totalcount/times;

        System.out.printf ("%.4f", meancount);//Format output.
 }
    }   
}
Related Article

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.