7-4求一批整数中出现最多的个位数字(20分)

在这里插入图片描述

参考代码

public class Main {
    
    
	public static void main(String[] args) {
    
    
		int [] a = new int[10]; // 用于记录数字出现的次数
		int numMax = 0;
		
		Scanner cin = new Scanner(System.in);
		
		int n = cin.nextInt();
		
		for( int i = 0; i < a.length; i ++ ) {
    
     // 清零
			a[i] = 0;
		}
		
		for(int i = 0; i < n; i ++ ) {
    
    
			int t = cin.nextInt();
			
			if( t == 0 ) {
    
    
				a[t] ++;
			}
			else {
    
    
				while( t > 0 ) {
    
    
					a[t % 10 ] ++;
					t /= 10;
				}
			}
		}
		
		for(int i = 0; i < a.length; i ++ ) {
    
     // 找到数字出现最多的次数
			if( a[i] > numMax ) {
    
    
				numMax = a[i];
			}
		}
		
		System.out.print(numMax + ":");
		for( int i = 0; i < a.length; i ++ ) {
    
    
			if( a[i] == numMax ) {
    
    
				System.out.print(" " + i);
			}
		}
		System.out.println();
		
		cin.close();
	}
}

猜你喜欢

转载自blog.csdn.net/wct3344142/article/details/105370636
今日推荐