Exercise 3-1 Score Statistics
Enter some student's score, which score appears the most times? If there are multiple parallel, from small to large output.
Task 1: Non-negative integers with fractions not exceeding 100
Task 2: The score does not exceed 100 of the nonnegative real number, but retains a maximum of two decimal places.
Task 1
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 101 + 10int a[maxn];int Main (int ARGC, Char *argv[]) { int n, max = 0, I; memset (A, 0, sizeof (a)); while (scanf ("%d", &n) = = 1) { a[n]++; if (A[n] > max) max = A[n]; } for (i = 0; I <=; i++) { if (a[i] = = max) printf ("%d", I); } printf ("\ n"); memset (A, 0, sizeof (a)); System ("PAUSE"); return 0;}
Task 2
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAXN 10001 + 10int a[maxn];int Main (int argc, char *argv[]) { double degree, m; int max = 0, I; memset (A, 0, sizeof (a)); while (scanf ("%lf", °ree) = = 1) { double m = degree*100; int n; n = Floor (m+0.5); a[n]++; if (A[n] > max) max = A[n]; } for (i = 0; I <= 10000; i++) { if (a[i] = = max) printf ("%.2lf", i*0.01); } printf ("\ n"); System ("PAUSE"); return 0;}
Summary: Task 2 degree set to double, then to int, with floor (m+0.5), previously encountered, remember
Exercise 3-2 The length of the word
The average length of their output. Words contain only uppercase and lowercase letters, separated by one or more spaces.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 100char a[maxn];int Main (int ARGC, Char *argv[]) { int count = 0; float sum = 0; while (scanf ("%s", a) = = 1) { count++; Sum + = strlen (a); } printf ("%.3lf\n", sum/count); System ("PAUSE"); return 0;}
Summary: Do not know the other answers on the Internet why write so many code, there is time to take a closer look
Algorithmic competition Getting Started classic exercise 3-1 score statistics Exercise 3-2 the length of a word