牛骨文教育服务平台(让学习变的简单)
博文笔记

字符串统计

创建时间:2017-03-15 投稿人: 浏览次数:252

输出m个字符串,要求输出重复n次的字符串有几个

import java.util.Arrays;
import java.util.Scanner;

public class countReStr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int j;
        int m;
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] Str = new String[n];
        int[] count = new int[n];
        // int[] count2=new int[n];
        in.nextLine();
        for (int i = 0; i < n; i++) {
            Str[i] = in.nextLine();
        }
        Arrays.sort(Str);
        for (int i = 0; i < n; i++) {
            count[i] = 1;
        }
        for (int i = 0; i < n;) {
            for (j = i + 1; j < n; j++) {
                if (Str[i].equals(Str[j])) {
                    count[i]++;
                    count[j]=count[i];
                } else
                    continue;
            }
            i = i + j - 1;
        }
        Arrays.sort(count);       
         int count2=1; 
        for(int i=0;i<n;){                  
            for( m=i+1;m<n;m++){
            if(count[i]==count[m]){
                count2++;
            }else{              
                continue;
            }           
            }System.out.println(count[i]+" "+count2);           
            i = i + m-1 ; 
            count2=1;
        }
    }
}

import java.util.Arrays;
import java.util.Scanner;

/*
 * 统计出字符串数组中重复出现的字符串的个数
 */
public class countReStr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int j;
        int m;
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] Str = new String[n];
        int[] count = new int[n];
        in.nextLine();
        for (int i = 0; i < n; i++) {
            Str[i] = in.nextLine();
        }
        Arrays.sort(Str);
        for (int i = 0; i < n; i++) {
            count[i] = 1;
        }       
        System.out.println();
        for(int i=0;i<n;){
            for(j=i+1;j<n;j++){
                if(Str[i].equals(Str[j])){
                    count[i]++; 
                    for(int k=i;k<=j;k++){
                     count[k]=count[i];                 
                     }                  
                }else break;

            }           
            i=j;        
        }
        Arrays.sort(count);
        int count2=1;
        for(int i=0;i<n;){
            for( m=i+1;m<n;m++){
                if(count[i]==count[m]){
                    count2++;
                }else break;

            }System.out.println(count[i]+" "+count2);
                count2=1;
                i=m;
        }

    }
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。