欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sdut java 单词和字符鉴别器

程序员文章站 2022-04-12 22:47:09
import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.LinkedList;import java.util.List;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(Sy...
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		String str[]=new String[100000];
		int pos=0;
		while(true) {
			String s=cin.nextLine();
			s=s.toUpperCase();
			if(s.compareTo("0000")==0)break;
			String ss[]=s.split("\\W+");
			for(int i=0;i<ss.length;i++){
			  str[++pos]=ss[i];
		}}
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		for(int i=1;i<=pos;i++) {
			if(map.containsKey(str[i]))map.put(str[i], map.get(str[i])+1);
			else map.put(str[i], 1);
		}
		ArrayList<String> list=new ArrayList(map.keySet());
		String s="";
		String ss="zzzzzzzzzzzzzzzzzzzzz";
		int min=999999999;
		int max=-1;
		for(int i=0;i<list.size();i++) {
		  if(max<map.get(list.get(i))) {
			  s=list.get(i);
			  max=map.get(list.get(i));
		  }
		  else if(max==map.get(list.get(i))) {
			  if(s.compareTo(list.get(i))<0) {
				  s=list.get(i);
				  max=map.get(list.get(i));
			  }
		  }
		  if(min>map.get(list.get(i))) {
			  ss=list.get(i);
			  min=map.get(list.get(i));
		  }
		  else if(min==map.get(list.get(i))) {
			  if(ss.compareTo(list.get(i))>0) {
				  ss=list.get(i);
				  min=map.get(list.get(i));
			  }
		  }
		}
		if(max!=-1)
		System.out.printf("%-10s%8d\n",s, max);
		if(min!=999999999)
		System.out.printf("%-10s%8d\n",ss,min);
		int arr[]=new int [100];
		for(int i=1;i<=pos;i++) {
			for(int j=0;j<str[i].length();j++){
				arr[str[i].charAt(j)-'A']++;
			}
		}
		for(int i=0;i<26;i++) {
			System.out.printf("%-10c%8d\n",i+'A',arr[i]);
		}
	}
}

本文地址:https://blog.csdn.net/Are_you_ready/article/details/109642150