1 条题解

  • 0
    @ 2023-6-11 12:22:24

    C++ :

    
    #include <bits/stdc++.h>
    using namespace std;
    
    int a[20],i,x,ma,n;
    
    int main() {
    	cin>>n;
    	for(i = 1;i <= n;i++){
    		cin>>x;
    		a[x]++;
    	}
    	
    	ma = 0;
    	for(i = 1;i <= 10;i++){
    		if(a[i] > a[ma]){
    			ma = i;
    		}
    	}
    	cout<<ma;
    	return 0;
    }
    
    

    Java :

    import java.util.Scanner;
    public class Main{
    public static void main (String[] args){
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int[] a = new int[n];
    	int[] b = new int[11];
    	int m = 0;
    	for(int i = 0; i < n; i++) {
    		a[i] = sc.nextInt();
    		b[a[i]]++;
    	}
    	for(int i = 1 ; i < 11; i++ ) {
    		if(b[i] > b[m]){
    			m = i;
    		}
    	}
    	System.out.print(m);
    	
    	
    
    }
    }
    

    Python :

    n = int(input())
    a = input().split()
    b = [0] * 11
    x = 0
    # 类型转换
    for i in range(0, n):
        a[i] = int(a[i])
        k = a[i]
        b[k] += 1
    x = max(b)
    for i in range(1, 11):
        if b[i] > 0 and b[i] == x:
            print(i)
    
    • 1

    【入门】求n个数中出现次数最多的数

    信息

    ID
    2753
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者