Let the Balloon Rise

原创


Let the Balloon Rise

  题目要求从给出的N个字符串中找出出现次数最多的,所以完成统计功能存储输出就可以了;每输入一个字符串就拿当前字符串str2和

之前的字符串strx相比,相同则将统计数组加1(value[strx]++),然后再从统计数组中找出最大值,存储索引指向的字符串即可。

Java AC

 1 import java.util.*;
 2 
 3 public class HDOJ_1004 {
 4 
 5     public static void main(String[] args) {
 6         Scanner reader=new Scanner(System.in);
 7         ArrayList list=new ArrayList();
 8         int count=-1;
 9         while(reader.hasNext()) {
10             int N=reader.nextInt();
11             if(N==0) {
12                 break;
13             }
14             String str[]=new String[N];
15             int value[]=new int[N];
16             for(int i=0;i<N;i++) {
17                 str[i]=reader.next();
18                 for(int j=0;j<i;j++) {
19                     if(str[i].equals(str[j])==true) {    //判断此字符串与前面字符串是否相等
20                         value[j]++;
21                     }
22                 }
23             }
24             //寻找最大值
25             int index=0;
26             int max=-1;
27             for(int i=0;i<N;i++) {
28                 if(value[i]>max) {
29                     max=value[i];
30                     index=i;
31                 }
32             }
33             list.add(str[index]);
34             count++;
35         }
36         for(int i=0;i<=count;i++) {
37             System.out.println(list.get(i));
38         }
39     }
40 
41 }

16:36:54

2018-08-20