zoukankan      html  css  js  c++  java
  • Codeforces Round #415 (Div. 2) A Straight <<A>>

    链接:http://codeforces.com/contest/810/problem/A
    A. Straight «A»
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one.

    In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8.

    For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with8.

    To graduate with «A» certificate, Noora has to have mark k.

    Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.

    Input

    The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack.

    Output

    Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.

    Examples
    input
    2 10
    8 9
    output
    4
    input
    3 5
    4 4 4
    output
    3
    Note

    Consider the first example testcase.

    Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to . Consequently, new final mark is 10. Less number of marks won't fix the situation.

    In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate.

    题意:给定一个序列,和一个k求序列的平均值的四舍五入是否达到k,要最少添加最大的数多少次才能达到k。

    题解:设置while循环知道四舍五入的平均值到达k跳出,否则一直添加最大值求平均值。

    #include<bits/stdc++.h>
    using namespace std;
    int main() 
    {
        double a[100+7]; 
        int n,k;
        cin>>n>>k;
        int ans=0; 
        double sum=0,count=0,average; 
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            sum+=a[i]; 
        }
        average=sum/n;
        if(int(average*10)%10>=5)
            average=int(average)+1;
        else average=(int)average; 
        //cout<<average<<endl; 
        while(average<k)
        {
            sum+=k;
            ans++;
            average=sum/(n+ans);
            if(int(average*10)%10>=5)
                average=int(average)+1;
            else average=(int)average; 
            //cout<<average<<endl; 
        }
        cout<<ans<<endl;
        return 0; 
    } 
    漫天星辰,繁华时下。心中冷淡,一笑奈何。
  • 相关阅读:
    Java-JUC(四):同步容器介绍
    Java-JUC(三):原子性变量与CAS算法
    Java:双向链表反转实现
    Java-JUC(二):Java内存模型可见性、原子性、有序性及volatile具有特性
    Java-JUC(一):volatile引入
    TSQL:判断某较短字符串在较长字符串中出现的次数。
    二叉树的定义与前序、中序、后序遍历
    c#:判断一个数组元素中否有重复元素
    c#:对两个字符串大小比较(不使用c#/java内部的比较函数),按升序排序
    mysql之 OPTIMIZE TABLE整理碎片
  • 原文地址:https://www.cnblogs.com/Scalpel-cold/p/7193017.html
Copyright © 2011-2022 走看看