Algorithm training the most frequently occurring integers

Source: Internet
Author: User

integer out of int range, changed to character type judgment was over.              algorithm Training the most frequently occurring integersTime 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; Next there are n rows, each line 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 and Output sample sample input 5
100
150
150
200
250 Sample output 150
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intMain () {Chara[ -][ -]; intn,ans,max,i,sum; Max=0; scanf ("%d",&N);  for(i=0; i<n;i++) scanf ("%s",&A[i]); Sum=1;  for(i=0; i<n;i++)       {           if(strcmp (a[i],a[i+1])==0) {sum++; }           Else           {               if(max<sum) {Max=sum; Ans=i; } Sum=1; }} printf ("%s\n", A[ans]); return 0;}

Algorithm training the most frequently occurring 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.