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 11 decimal places. For example, Ran's average score is \frac{48}{5} \approx 9.6 5 48 ≈9.6 and Moca's average score is \frac{29}{3} \approx 9.6 3 29 ≈9.6, but apparently Moca's average score is higher. Can you help Moca calculate her average score rounding down to kk decimal places? Input The first line contains two integers n, kn,k (1 \le n, k \le 10^5)(1≤n,k≤10 5 ) – the number of courses and the number of decimal places. The second line contains nn integers a_1, a_2, \dots, a_na 1 ,a 2 ,…,a n (0 \le a_i \le 100)(0≤a i ≤100) – the score of each course. Output Print one real number in a line that denotes Moca's average score. Notice that your answer must contain kk decimal places. Sample 1 Inputcopy Outputcopy 3 10 94 100 99 97.6666666666
swjtu—春季集训 - Virtual Judge (vjudge.net)
思路:
- 除法运算利用 /,%运算就行了
#include <bits/stdc++.h> using namespace std; #define ri register int #define M 200005 template <class G> void read(G &x) { x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return ; } int val[M]; int n,m; int main(){ read(n);read(m); long long ans=0; for(ri i=1;i<=n;i++) { int a;read(a); ans+=a; } long long tmp=ans%n; ans=ans/n; for(ri i=1;i<=m;i++) { tmp*=10; val[i]=tmp/n; tmp%=n; } printf("%lld.",ans); for(ri i=1;i<=m;i++) printf("%d",val[i]); }