zoukankan      html  css  js  c++  java
  • 【Codeforces】Codeforces Round #373 (Div. 2) -C

    C. 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.

    Examples
    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.

    *******************************************************************

    Solution:

      Here is a simple solution for this implementation problem. Just reducing unused functions and mining processes to avoid TLE. Even it's third problem but it is really simple for stronger me!

      For the trick , Just recognizing that there is no need loop the carrying position for every second t, because if u carried in pos i , then u can insure that from 0 to i-1 has no element greater than '4'! So the next carry pos may be i -1, if not thus can conclude that carrying work is done! 

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <stdlib.h>
    
    using namespace std;
    
    int n, t;
    string grade;
    
    void numPlusOne(string & integer)
    {
        int car = 1;
        for(int i = integer.length() - 1; i >= 0; i --){
            if(integer[i] == '9'){
                integer[i] = '0';
            }else{
                integer[i] ++;
                car = 0;
                break;
            }
        }
        if(car){
            integer = "1" + integer;
        }
    }
    int main()
    {
        cin>>n>>t;
        cin>>grade;
        int pos = 0;
        for(int i = 0; i < grade.length(); i ++){
            if(grade[i] == '.'){
                pos = i;
                break;
            }
        }
        string integer =grade.substr(0, pos);
        string dec = grade.substr(pos + 1, grade.length() - pos - 1);
        bool integerPlusOne = false;
        int len = dec.length();
        int carPos =  - 1;
        for(int i = 0; i < len; i ++){
            if(dec[i] >= '5'){
                carPos = i;
                break;
            }
        }
        bool haveDec = true;
        while(t --){
            if(carPos == -1){
                break;
            }
            else if(carPos == 0){
                integerPlusOne = true;
                haveDec = false;
                break;
            }else{
                dec[carPos - 1] ++;
                len = carPos;
                if(dec[carPos - 1] < '5') carPos = -1;
                else carPos --;
            }
        }
        if(integerPlusOne){
            numPlusOne(integer);
        }
        if(!haveDec){
            cout<<integer<<endl;
        }else{
            cout<<integer<<".";
            for(int i = 0; i < len; i ++)
                printf("%c", dec[i]);
        }
        return 0;
    }
  • 相关阅读:
    好用的绘图工具推荐-processon
    前台获取到的后台json对象取值时undefined的解决方法
    axios怎么引用?
    postman发送post请求,后端无法接收到参数
    bcrypt加密算法
    mongoose创建model名字时,为什么集合名会自动加s?
    《遇见未知的自己》 张德芬
    《把时间当做朋友》笔记摘要
    精英与普通人的区别
    金刚经全文
  • 原文地址:https://www.cnblogs.com/luntai/p/5903961.html
Copyright © 2011-2022 走看看