Algorithm (4th edition)-1.1 exercises (partial)

Source: Internet
Author: User

1.1.3

Command-line method of taking arguments:

1.stdin.readint (): Java XX, run, input parameters, separated by spaces or line breaks;

2.integer.parseint (Args[0]): Java XX parameter, separated by a space, run.

1.1.6

 for (int i = 0; I <=; i++) {    stdout.println (f);     = f + g;     = f- g;}

Note that when running to G = f-g, this line, F has changed.

1.1.8

System.out.println (' B ' + ' C ');

Answer: 197

"" Represents a string, ' ' represents a char.

1.1.15

// Https://github.com/aistrate/AlgorithmsSedgewick Public Static int[] Histogram (int[] A,intM) {    int[] h =New int[M]; intN =a.length;  for(inti = 0; i < N; i++)        if(A[i] <M) H[a[i]]++; returnh;}

Understanding test instructions, cleverly solved, do not have to write double loops.

1.1.19 *

// Https://github.com/aistrate/AlgorithmsSedgewick Public Static LongFib (intN) {    Long[] f =New Long[N+1]; returnFib (N, f);} Public Static LongFib (intNLong[] f) {    if(F[n] = = 0)    {        if(N = = 1) F[n]= 1; Else if(N > 1) F[n]= Fib (N-1, F) + fib (N-2, F); }        returnf[n];}

Use an array to save the calculated values to better implement F (N).

The measurement proves that the original method output to more than 40 minutes, and the back is more and more slow (according to test instructions 1 hours are not all output), the improved method is almost the second output. Performance varies enormously.

Algorithm (4th edition)-1.1 exercises (partial)

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.