L1-2. Likes (the number of the most frequently seen in multiple sets of data)

Source: Internet
Author: User
Topics l1-2. Likes
There is a "like" feature on Weibo, and you can support your favorite blog post with a few likes. Each blog post has a label that describes its characteristics, and the type of post you like is also an indirect depiction of your features. The subject asks you to write a program that analyzes the character of a person by counting the records of one's liking. 
input Format: The 
    input gives a positive integer n (<=1000) on the first line, which is the number of posts that the user likes. Then n rows, each line gives an attribute description of the post it likes, in the form "K F1 ... FK ", where 1<=k<=10,fi (I=1, ..., K) is the number of the attribute tag, we numbered all the feature labels from 1 to 1000. The numbers are separated by a space. 

    output format:

    counts the most common feature tag in all the posts that were liked, outputting its number and number of occurrences in a row, with a number interval of 1 spaces. If there is a tie, the output number is the largest one. 

    Input Sample:

    4
    3 889 233 2
    5 3 233 2
    4 3 889 2 2
    233 123

    Output Example:

    233 3
Code parsing
Import Java.util.Scanner;
        public class Main {public static void main (string[] args) {Scanner io=new Scanner (system.in);
        The array is used to hold the number of occurrences of 1000 labels, indexed as label int flag[] =new int[1001];
        Record occurrence maximum number of times int count=0;
        Record the maximum number of occurrences of the tag number int value=0;

        Number of lines entered int n=io.nextint (); /* * Nextint () will only accept int values, but each time you press ENTER, a \ n * Next line of nextline () will be read at the end of the previous number, resulting in a direct skip to the data you want to read * Hence the NE

        Xtdouble,nextint and so on and nextline-connected place with nextline, Swallow line-break, */Io.nextline ();
        for (int i=1;i<=n;i++) {//line read into Data String str=io.nextline ();
            Obtain a string array that contains the data for the row, noting that the first element of the array refers to the number of attribute labels for the row, and does not need to traverse string[] Sz=str.split ("") when traversing; for (int j=1;j<sz.length;j++) {int temp=integer.parseint (sz[j]);//Read each tag number flag[temp

            ]++;
   Compare with current maximum if (Flag[temp]>count) {count=flag[temp];             Value=temp;
            }//If it is equal to the current maximum value, take the maximum if (flag[temp]==count&&temp>value) value=temp;

    }} System.out.println (value+ "" +count);
 }

}

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.