Meituan 2021 School Recruitment Written Test-Programming Questions (General Programming Test Questions, Session 1) 2. Xiaomei’s Scoring Calculator

Meituan’s evaluation system for merchants is a 1-5 star evaluation system. After completing an order, users can give merchants 1/2/3/4/5 stars, but on the client side, merchants’ ratings are not necessarily integers. Instead, one digit after the decimal point is displayed. Obviously, this requires a calculator. Xiaomei has some evaluation data of merchants, and hopes to calculate the ratings displayed by merchants on the client.
The calculation of this score is very simple. It is to calculate the average of the star ratings of all customers of the merchant, and then remove the tail to display one digit after the decimal point. For example, if the average score is 3.55, it will display 3.5. For example, if a merchant gets a 1-5 star rating, the displayed rating is (1+2+3+4+5)/5=3.0.
Displays 0.0 if the business has no reviews.
Time limit: 1 second for C/C++, 2 seconds for other languages
​​Space limit: 256M for C/C++, 512M for other languages

Input description:
The input contains 5 integers, which in turn indicate the number of reviews from 1 star to 5 stars for the merchant, and the number of reviews for each type is not greater than 1000.
Output description:
The output contains only one decimal, indicating the merchant's rating displayed on the client.
Example 1
Input example:
2 2 1 1 2
Output example:
2.8

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
    
    
    int scores[5];
    double sum=0.;
    double cnt=0.;
    for(int i=0;i<5;i++){
    
    
        cin>>scores[i];
        sum=sum+(i+1)*scores[i];
        cnt=cnt+scores[i];
    }
    if(cnt==0) cout<<"0.0";
    else{
    
    
        printf("%.1lf",sum/cnt-0.05);
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

Guess you like

Origin blog.csdn.net/weixin_45184581/article/details/129464866