1 条题解

  • 0
    @ 2023-6-11 12:21:10

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    
    
    int main() {
    	int n,a[110],c1=0,c2=0;
    	cin>>n;
    	int i;
    	for(i = 0; i < n; i++) {
    		cin>>a[i];
    	}
    	sort(a,a+n);
    	for(i = 0;i < n;i++){
    		if(a[i] == a[n-1]){
    			c1++;
    		}else if(a[i] == a[0]){
    			c2++;
    		}
    	}
    	
    	cout<<c1<<' '<<c2;
    }
    
    

    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 num2 = scanner.nextInt();
            int[] a = new int[num1];
            int x = 0,y = 0;
            for (int i = 0; i < num1; i++) {
                a[i] = scanner.nextInt();
            }
            int max = MAX(a);
            int min = MIN(a);
            for (int i = 0; i < num1; i++) {
                if(max == a[i])x++;
                else if(min == a[i])y++;
            }
            System.out.println(x + " " + y);
        }
    
        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 paiXu(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;
        }
    }
    
    

    Python :

    n=int(input())
    a=input().split()
    a=list(map(int,a))
    da=max(a)
    xiao=min(a)
    n1=a.count(da)
    n2=a.count(xiao)
    print(n1,n2)
    
    • 1

    【入门】最贵商品和最便宜商品分别有几个

    信息

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