1 条题解
-
0
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; }
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
- 2665
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者