1 条题解

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

    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] != 0){
    			cout<<i<<" "<<a[i]<<endl;
    		}
    	}
    	return 0;
    }
    
    

    Java :

    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int num1 = scanner.nextInt();
            int[] a = new int[num1];
            int[] count = new int[100];
            double avg,x = 0;
            long s = 0;
            for (int i = 0; i < num1; i++) {
                a[i] = scanner.nextInt();
            }
            for (int i = 0; i < num1; i++) {
                count[a[i]]++;
            }
            for (int i = 0; i < 100; i++) {
                if(count[i] != 0){
                    System.out.println(i + " " + count[i]);
                }
            }
        }
    
        public static boolean judge(int i,int j){
            int a,s = 0;
            while(i != 0){
                a = i % 10;
                s = s + a;
                i /= 10;
            }
            if(s == j)return true;
            else return false;
        }
    
        public static void paiXux(int[] a){
            int temp;
            for(int i = 0;i < a.length - 1;i++){
                for(int k = 0;k < a.length - i - 1;k++){
                    if(a[k] > a[k + 1]){
                        temp = a[k];
                        a[k] = a[k + 1];
                        a[k + 1] = temp;
                    }
                }
            }
        } public static void paiXud(int[] a){
            int temp;
            for(int i = 0;i < a.length - 1;i++){
                for(int k = 0;k < a.length - i - 1;k++){
                    if(a[k] < a[k + 1]){
                        temp = a[k];
                        a[k] = a[k + 1];
                        a[k + 1] = temp;
                    }
                }
            }
        }
    
        public static int MAX(int[] a){
            int max = a[0];
            for (int i = 0; i < a.length; i++) {
                if(max < a[i]){
                    max = a[i];
                }
            }
            return max;
        }
    
        public static int MIN(int[] a){
            int min = a[0];
            for (int i = 0; i < a.length; i++) {
                if(min > a[i]){
                   min = a[i];
                }
            }
            return min;
        }
        public static int sum(int a){
            int x,s = 0;
            while(a != 0){
                x = a % 10;
                s = s + x;
                a /= 10;
            }
            return s;
        }
        public static boolean isPrime(int a){
            if(a <= 1)return false;
            else
            for(int i = 2;i <= Math.sqrt(a);i++){
                if(a % i == 0)return false;
            }return true;
        }
    
        public static int qiuYu(int i){
            int a;
            a = i % 10;
            return a;
        }
    }
    
    

    Python :

    n = int(input())
    a = input().split()
    # b[1]记录1出现的次数,b[2]记录2出现的次数...b[10]记录10出现的次数
    b = [0] * 11  # 定义一个11个都是0的列表
    # 类型转换
    for i in range(0, n):
        a[i] = int(a[i])
        # a[i]每出现一次,b[a[i]]自增1
        k = a[i]
        # 记录k出现的次数
        b[k] += 1
    # 循环b列表 1-10 b[1]~b[10]
    for i in range(1, 11):
        if b[i] > 0:
            print(i, b[i])
    
    • 1

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

    信息

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