列表

详情


NC221176. GradePointAverage

描述

As soon as Moca entered the university last year as a freshman, she was told that the competition is fierce and everyone was desperately trying to improve their grades. As a result, in the last academic term, Moca studied just as hard as she did in high school.

The college will award scholarships based on the average score of the previous academic term. Unfortunately, someone has the same average score as Moca because the average score is calculated rounding down to only 1 decimal places. For example, Ran's average score is and Moca's average score is , but apparently Moca's average score is higher.

Can you help Moca calculate her average score rounding down to k decimal places?

输入描述

The first line contains two integers n,k  -- the number of courses and the number of decimal places.

The second line contains  integers   -- the score of each course.

输出描述

Print one real number in a line that denotes Moca's average score. Notice that your answer must contain k decimal places.

示例1

输入:

3 10
94 100 99

输出:

97.6666666666

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 28ms, 内存消耗: 548K, 提交时间: 2021-08-01 14:45:15

#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n,k,s=0,a,i;
cin>>n>>k;
for(i=0;i<n;i++){cin>>a;s=s+a;}
printf("%d.",s/n);
s=s%n;
for(i=0;i<k;i++){s=s*10;
printf("%d",s/n);
s=s%n;}
}

Python3(3.9) 解法, 执行用时: 252ms, 内存消耗: 11384K, 提交时间: 2021-05-10 11:11:19

n,k=map(int, input().split())
a = sum([int(n) for n in (input().split())])
a=a*pow(10,k)//n
a=str(a)
while len(a)<=k:
    a="0"+a
i=len(a)-k
print(a[0:i],".",a[i:],sep="")

上一题