Algorithm training most occurrences of integer time limit: 1.0s memory limit: 512.0MB
Problem Description
Write a program that reads a set of integers that are arranged in order from small to large, and that the number of n is also entered by the user, up to a maximum of 20. The program then counts the array and prints the value of the array element that has the most occurrences. If there are two element values that occur the same number of times, i.e. tie, then only the smaller value is printed.
Input format: The first line is an integer
N,
N? £? 20; Then there are
NRows, each row represents an integer, and is arranged in order from small to large.
Output format: The output is only one line, that is, the most frequently occurring element value.
input/Output sampleSample Input 5
100
150
150
200
250 sample output 150 don't say much. Read the code comment;
#include <stdio.h>intMain () {intn,m,i,j,s,p,k; inta[ -];P=1; k=1; scanf ("%d",&N); scanf ("%d", &a[0]); for(i=1; i<n;i++) {scanf ("%d",&A[i]); if(a[i]==a[i-1]//For two next to the number comparison, as long as the equivalent of K plus 1; {k++; } if(a[i]!=a[i-1] As long as the unequal K is changed back to the original value. {k=1; } if(k>p) {p=k;//in order to get the most number of times;}} printf ("%d\n", p); for(i=0; i<n;i++) {m=0; for(j=0; j<n;j++) { if(a[i]==A[j]//From the first and all the comparisons, as long as equal let M plus a {m++; } } if(m==p) {printf ("%d\n", A[i]);//until M equals the maximum number of p, the output and jumps out of the loop. Break; }} getchar (); GetChar (); GetChar (); GetChar (); GetChar ();}
Most occurrences of integers