Algorithmic Competition Primer Classic (second Edition)-chapter II Study questions and exercises

Source: Internet
Author: User

Topic 1 (Amount of necessary storage)

    • Enter some number, count the number. (No array required)
    • Enter some numbers to find the maximum, minimum, and average values. (No array required)
    • Enter some number, which is the closest two number. (Array required)
    • Enter some numbers to find the second largest value. (No array required)
    • Enter some numbers to ask for their variance. (Array required)
    • Enter some number, statistics do not exceed the number of averages. (No array required)

Topic 2 (Number of statistical characters 1)

    1. MAXN settings are too large for the system to allocate such a large amount of memory.
    2. The judgment statement should be changed to if (s[i] = = ' 1 '), 1 is the value, ' 1 ' is the character.
    3. Use the Strlen function under the string.h header file, but this header file is not included.

      Compiler will give warning warning:implicit declaration of function ' strlen '

    4. The for-loop judgment expression I < strlen (s) needs to call the Strlen function each time it is judged, resulting in a loss of efficiency.

The Modified program:

1#include <stdio.h>2#include <string.h>3 #defineN 655364 intMainvoid)5 {6     CharS[n];7scanf"%s", s);8     inttot =0, Len =strlen (s);9      for(inti =0; i < Len; i++)TenTot + = ('1'= = S[i])?1:0; Oneprintf"%d\n", tot); A     return 0; -}

Exercise 3-1 Score (score)

1#include <stdio.h>2#include <string.h>3 intMainvoid)4 {5     intN;6     Chars[Bayi];7scanf"%d", &n);8      for(inti =0; I < n; i++) {9scanf"%s", s);Ten         intsum =0, flag =0, Len =strlen (s); One          for(intj =0; J < Len; J + +) { AFlag + = (S[j] = ='O') ?1: -Flag; -Sum + = (flag)? Flag:0; -         } theprintf"%d\n", sum); -     } -     return 0; -}

Exercise 3-2 Molecular weight (Molar Mass)

Algorithmic Competition Primer Classic (second Edition)-chapter II Study questions and exercises

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.