Most occurrences of integers

Source: Internet
Author: User

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 NN? £? 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

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.