• codeforces 719C. Efim and Strange Grade


    C. Efim and Strange Grade
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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
    Copy
    6 1
    10.245
    output
    Copy
    10.25
    input
    Copy
    6 2
    10.245
    output
    Copy
    10.3
    input
    Copy
    3 100
    9.2
    output
    Copy
    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.

    题意:你有n次操作,可以让给定的这个数的小数位四舍五入,求这个数经过t次操作后最大是多少

    题解:小数位如果第一个小数位是>=5的情况下,整数位的个位就要+1,如果整数位是999这种情况就要变成1000

    为了使得经过变换后的数最大,我们先从小数位的最前面开始进位

    代码如下:

    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define fuck(x) cout<<"["<<x<<"]";
    #define FIN freopen("input.txt","r",stdin);
    #define FOUT freopen("output.txt","w+",stdout);
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    char str[200005];
    char a[200005];
    char b[200005];
    int main(){
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        int n,t;
        scanf("%d%d",&n,&t);
        cin>>str;
        int pos=0;
        //整数
        for(pos = 0;; pos++) {
            if(str[pos] == '.') break;
            else a[pos] = str[pos];
        }
        pos++;
        for(int i=0;str[i];i++,pos++){
            b[i]=str[pos];
        }
        pos=-1;
        for(int i=0;b[i];i++){
            if(b[i]>='5'){
                pos=i;
                break;
            }
        }
        int flag=0;
        for(int i=pos;i>=0&&t>0;i--){
            if(i!=0&&b[i]>='5'){
                b[i-1]++;
                b[i]=0;
            }else if(i==0&&b[i]>='5'){
                flag=1;
            }else break;
            t--;
        }
        if(!flag) printf("%s.%s
    ",a,b);
        else{
            int len=strlen(a);
            int num=0;
            int i;
            for(i=len-1;i>=0;i--){
                if(a[i]!='9'){
                    a[i]++;
                    break;
                }else{
                    a[i]='0';
                    num++;
                }
            }
            if(num==len) cout<<"1";
            cout<<a<<endl;
        }
        return 0;
    }
    View Code
    #include<bits/stdc++.h>
    using namespace std;
    string s;
    int main(){
        int n,t;
        scanf("%d%d",&n,&t);
        cin>>s;
        int i=0;
        while(s[i]!='.') i++;  //整数部分的长度
        while(i<n&&s[i]<'5') i++;  //不能进位的长度
        if(i==n){
            //如果全部不能四舍五入就直接输出
            cout<<s<<endl;
            return 0;
        }
        i--; 
        int len=0;
        while(t>0){
            if(s[i]!='.') s[i]++;
            else{
                
                i--;
                len=i;
                while(i>=0&&s[i]=='9') s[i--]='0';  //如果当前位是9,那么进位时注意将当前位改为0
                if(i==-1) cout<<'1';           //如果是9999的情况,就变成10000;
                else s[i]++;
                break;
            }
            if(s[i]<'5'){
                len=i;
                break;
            }else{
                len=i;
                i--;
            }
            t--;
        }
        for(int i=0;i<=len;i++){
            cout<<s[i];
        }
        cout<<endl;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    【情人节礼物】纯js脚本打造精美3D玫瑰
    程序员成长系列(一):手里必须有一套自己的框架
    从灌篮高手谈项目团队组成
    【java下午茶】12306的双人票
    从怀孕编程谈公司管理
    程序员的灵光一闪
    【java下午茶系列】java三重奏之封装
    程序员的每周工作40小时之路
    程序员真适合自由的环境吗
    程序员真的需要升职吗?
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9432717.html
Copyright © 2020-2023  润新知