1 条题解

  • 0
    @ 2023-6-11 12:20:58

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int a[1100],n,ma = INT_MIN,mi = INT_MAX,s = 0,c_ma = 0,c_mi = 0;
    int i;
    int main() {
    	cin>>n;
    	for(i = 0;i < n;i++){
    		cin>>a[i];
    		s = s + a[i];
    		
    		if(a[i] > ma){
    			ma = a[i];
    		}
    		
    		if(a[i] < mi){
    			mi = a[i];
    		}
    	}
    	
    	for(i = 0;i < n;i++){
    		if(a[i] == ma){
    			c_ma++;
    		}
    		
    		if(a[i] == mi){
    			c_mi++;
    		}
    	}
    	
    	cout<<s - (ma * c_ma + mi * c_mi)<<endl;
    	cout<<fixed<<setprecision(1)<<(s - (ma * c_ma + mi * c_mi)) * 1.0 / (n - (c_ma + c_mi));
    }
    
    

    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];
    		for (int i = 0; i < n; i++) {
    			a[i] = sc.nextInt();
    		}
    		int t = 0;
    		for (int i = 1; i <= n - 1; i++) {
    			for (int j = 0; j <= n - i - 1; j++) {
    				if (a[j] > a[j + 1]) {
    					t = a[j];
    					a[j] = a[j + 1];
    					a[j + 1] = t;
    				}
    
    			}
    		}
    		int p = 0 , q = n-1,s = 0 ;
    		for(int i = 1 ; i < n ; i++)
    		{
    			if(a[i] == a[0])
    				p = i;
    		}
    		for(int i = n-2 ; i >=0 ; i--)
    		{
    			if(a[n-1] == a[i])
    				q = i;	
    		}
    		for(int i = p+1 ; i < q ; i++)
    		{
    			s = s + a[i];
    		}
    		double z = s*1.0/(q-p-1);
    		System.out.println(s);
    		System.out.println(String.format("%.1f", z));
    	}
    }
    

    Python :

    n=int(input())
    a=input().split()
    a=list(map(int,a))
    da=max(a)
    xiao=min(a)
    s=0
    js=0
    for i in range(n):
        if(a[i]!=da and a[i]!=xiao):
            s=s+a[i]
            js=js+1
    av=s/js
    print(s)
    print("%.1f"%av)
    
    • 1

    信息

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