zoukankan      html  css  js  c++  java
  • Efim and Strange Grade

    Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer).

    There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.

    In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.

    For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.

    Input

    The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively.

    The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.

    Output

    Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.

    Example

    Input
    6 1
    10.245
    Output
    10.25
    Input
    6 2
    10.245
    Output
    10.3
    Input
    3 100
    9.2
    Output
    9.2

    Note

    In the first two samples Efim initially has grade 10.245.

    During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.

    In the third sample the optimal strategy is to not perform any rounding at all.

    给定可以进位的次数,输出可以得到的最大的结果,对小数部分进行进位,因为次数有限,所以找小数部分离小数点最近一个大于等于五的数往前检查并进位就好了,注意99.99这种可以连续进位的。。

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
        int n,t,m,r,i = 0,p,sum;
        char s[200000],ch;
        cin>>n>>t;
        p = m = n;
        r = n - 1;
        for(int i = 0;i < n;i ++)
        {
            cin>>s[i];
            if(s[i] == '.')m = i;
        }
        for(int i = m;i < n;i ++)
        if(s[i]>='5'){r = i;break;}
        for(int i = r;i > m && t;i --)
        {
            if(s[i] >= '5')
            {
                t --;
                int o = 1,q = 1;
                if(s[i - 1] == '.')i--;
                p=i;
                o += s[i - q] - '0';
                while(o == 10)
                {
                    s[i - q] = '0';
                    q ++;
                    if(i - q >= 0)
                    {
                        o = s[i - q] - '0' + 1;
                    }
                    else
                    {
                        cout<<1;
                        break;
                    }
                }
                if(i - q >= 0)s[i - q] = o + '0';
            }
        }
        for(int i = 0;i < p;i ++)
            cout<<s[i];
    }
  • 相关阅读:
    Luogu4233 射命丸文的笔记 DP、多项式求逆
    LOJ2267 SDOI2017 龙与地下城 FFT、概率密度函数、Simpson
    LOJ2882 JOISC2014 两个人的星座 计算几何
    UOJ343 清华集训2017 避难所 构造、打表
    Solution -「CTS2019」珍珠
    「珂朵莉树」学习笔记
    CSP2019-J/S 游记
    LeetCode(164)Maximum Gap
    LeetCode(165) Compare Version Numbers
    LeetCode(162) Find Peak Element
  • 原文地址:https://www.cnblogs.com/8023spz/p/7668652.html
Copyright © 2011-2022 走看看