"Algorithmic Competition Primer Classic" "chapter three" after-school exercises (Part I)

Source: Internet
Author: User

After-school exercises the third wave came, to the third chapter after the code and the like a little bit longer, so I put this chapter of the answer into a few parts. This chapter focuses on the processing of strings, for string problems, usually as long as careful there is no problem, the following few said directly on the detailed.

Exercise 3-1 Score Statistics (STAT)

Task 1: This is straightforward on the code:

#include <stdlib.h> #include <stdio.h> #include <string.h> #define MAXN 10int cmp (const VOID*A,CON St Void*b) {return * (int*) A-* (int*) b;} int main () {int i,j,n,max,p;int stu[maxn],a[maxn]; memset (A,0,sizeof (a)); scanf ("%d", &n); for (i = 0; i < n; i++) {SCA NF ("%d", &p); a[p]++;} Max=a[0];for (i = 1; i < MAXN; i++) {if (A[i]>=max) {max=a[i];}} for (i = 0, j = 0; i < MAXN; i++) {if (A[i]==max) {stu[j++] = i;}} Qsort (Stu,j,sizeof (stu[0]), CMP), for (i = 0; i < J; i++) {if (i) printf ("");p rintf ("%d", Stu[i]);} printf ("\ n"); return 0;}


Task 2: This relative task 1 more difficult, one of the questions, in the use of the Qsort function, there is a small detail to note, that is, when the return, for double type to use the ternary operator, if the previous processing method will cause errors, the error from the floating point number error. My approach to this problem is similar to the first one, but I have to make a little sacrifice to be similar to the first integer, but I sacrificed the memory. The code is as follows:

#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXN 10000 + 100int cmp (const void*a, Const VOID*B) {return * (double*) a>* (double*) b?1:-1;} int main () {int i,j,a[maxn],n,max,temp;double p,stu[101];memset (a,0,sizeof (a)); scanf ("%d", &n); for (i = 0; i < n; i + +) {scanf ("%lf", &p); temp = 100*p;a[temp]++;} Max=a[0];for (i = 1; i < MAXN; i++) {if (A[i] > max) {max = A[i];}} for (i = 0, j = 0; i < MAXN; i++) {if (A[i]==max) {stu[j++] = (double) i/100;}} Qsort (Stu,j,sizeof (stu[0]), CMP), for (i = 0; i < J; i++) {if (i) printf ("");p rintf ("%.2lf", Stu[i]);} printf ("\ n"); return 0;}

Exercise 3-2 Word Length (word)

This problem is relatively simple, only need to pay attention to the number of words in the time to be careful of the better

#include <stdio.h> #include <ctype.h> #define MAXN 10000 + 100char Str[maxn];int main () {int i,sum = 0,count = 0; Count is used to record the number of spaces, sum is used to record the total number of letters of gets (STR), if (str[0]!= ') count = 1;for (i = 0; str[i]!= '; i++) {if (str[i]== ' && Isalpha (str[i+1])) count++;if (Isalpha (Str[i])) sum++;} printf ("count =%d,sum =%d\n", count,sum);p rintf ("%.2lf\n", 1.0*sum/count);    return 0;}


Exercise 3-3 the last three bits (product) of the product

In two steps, the first step is to discard the string containing the uppercase and lowercase letters, and the second part converts the string of pure numbers into numbers. The problem is clear and the code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h>int isnum (char *a,int len) {int i;for (i = 0; I & Lt Len i++) {if (a[i]< ' 0 ' | | A[i]> ' 9 ') return 0;} return 1;} int main () {char Str[20];int i,n,p,tmp;//n represents the number of input integers, p represents the after three bits of the integer product (scanf ("%d", &n) = = 1) {p = 1;for (i = 0; i < n; i++) {scanf ("%s", str); GetChar (); if (Isnum (Str,strlen (str))) {TMP = atoi (str);p *= tmp;p%= 1000;}} printf ("%03d\n", p);}    return 0;}

For the other exercises in chapter three, the following article will continue

(If there is an error, please correct it)



"Algorithmic Competition Primer Classic" "chapter three" after-school exercises (Part I)

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.