1 条题解

  • 0
    @ 2021-6-14 23:20:23

    C++ :

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
     
    using namespace std;
     
     struct scale
     {
     	int q;
     	int biao;
     }a[1500];
    int s[1500];
    
     int  comp(const scale &a,const scale &b)
     {
     	if(a.q<b.q)return 1;
     	if(a.q>b.q)return 0;
     	if(a.biao<b.biao) return 1;
     	return 0;
     }
     
    int main()
    {
        int n,ans=0;
        cin>>n;
        for(int i=1;i<=n;++i)
        {
        	cin>>a[i].q;
        	a[i].biao=i;
        }
        sort(a+1,a+n+1,comp);
        for(int i=1;i<=n;++i)
        {
        	s[i]=a[i-1].q+s[i-1];
        	ans+=s[i];
        }
        double l=ans*1.0/n;
        for(int i=1;i<=n;++i)
        {
        	if(i!=n)printf("%d ",a[i].biao);
        	else printf("%d\n",a[i].biao);
        }
        printf("%.2lf",l);
        return 0;
    }
    

    Pascal :

    var a:array[0..5000,1..2] of longint;
     n,i,j,ans1:longint;
     ans2:real;
    
    procedure qsort(l,r:longint);
     var i,j,mid,t1,t2:longint;
     begin
      i:=l;
      j:=r;
      mid:=a[(l+r) div 2,1];
      repeat
      while (a[i,1]<mid) do
      inc(i);
      while (mid<a[j,1]) do
      dec(j);
      if (i<=j) then
       begin
       t1:=a[i,1];
       a[i,1]:=a[j,1];
       a[j,1]:=t1;
       t2:=a[i,2];
       a[i,2]:=a[j,2];
       a[j,2]:=t2;
       inc(i);
       dec(j);
       end;
       until i>j;
       if l<j then
       qsort(l,j);
       if i<r then
       qsort(i,r);
     end;
    
    procedure solve;
     var i:longint;
     begin
      for i:=1 to n do ans1:=ans1+a[i,1]*(n-i);
      ans2:=ans1/n;
     end;
    
    begin
     readln(n);
     for i:=1 to n do
      begin
       read(a[i,1]);
       a[i,2]:=i;
      end;
     qsort(1,n);
     ans1:=0;
     solve;
     for i:=1 to n-1 do write(a[i,2],' ');
     writeln(a[n,2]);
     writeln(ans2:0:2);
    end.
    
    

    Java :

    import java.util.*;
    public class Main{
      public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        Map<Integer,Integer> map = new TreeMap<Integer,Integer>();
        for(int i = 0; i < n; i ++){
          int value = cin.nextInt();
          map.put(i+1,value);
        }
        List<Map.Entry<Integer, Integer>> infoIds = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
    		Collections.sort(infoIds, new Comparator<Map.Entry<Integer, Integer>>() {
    			public int compare(Map.Entry<Integer, Integer> o1,
    					Map.Entry<Integer, Integer> o2) {
    				return (o1.getValue()).compareTo(o2.getValue());
    			}
    		});
        int sum = 0;
    		for (int i = 0; i < infoIds.size(); i++) {
          if(i!=infoIds.size()-1){
            System.out.print(infoIds.get(i).getKey()+" ");
          }else{
            System.out.println(infoIds.get(i).getKey()+" ");
          }
          sum += infoIds.get(i).getValue()*(infoIds.size()-i-1);
        }
        double average = (double)sum/infoIds.size();
        String result = String.format("%.2f",average);
        System.out.println(result);
      }
    }
    
    • 1

    信息

    ID
    139
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    3
    已通过
    1
    上传者