题目链接:http://codeforces.com/problemset/problem/719/C

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

题解:

1.整段数字被分成两部分, 小数部分和正数部分。 先对小数部分进行进位。

2.首先找到小数点后第一个大于等于5的位置,对这个位置进行四舍五入。进位时,先用个while循环处理完'999……'的进位,然后再对当前位+1。如果当前位<5,就可以马上停止了, 因为前面的数都不可能>=5。 如果当前位>=5, 则继续下一次四舍五入(一定是当前位,所以不必每次都遍历找第一个>=5的数),最多t次。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <algorithm>
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 2e5+; char s[maxn];
int n, t, dot; int main()
{
scanf("%d%d%s",&n, &t, s+);
for(dot = ; dot<=n; dot++)
if(s[dot]=='.') break; int i;
for(i = dot+; i<=n; i++)
if(s[i]>='') break; if(i>n)
{
puts(s+);
return ;
} while(t--)
{
s[i--] = ;
while(s[i]!='.' && s[i]=='')
s[i--] = ;
if(s[i]!='.') s[i]++; if(s[i]=='.' || s[i]<'') break;
} if(s[dot+]==)
{
s[dot] = ;
i = dot-; while(i>= && s[i]=='')
s[i--] = '';
if(i>=) s[i]++;
else putchar('');
}
puts(s+);
}

Codeforces Round #373 (Div. 2) C. Efim and Strange Grade —— 贪心 + 字符串处理的更多相关文章

  1. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题

    C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...

  2. Codeforces Round #373 (Div. 1)

    Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...

  3. Codeforces Round #373 (Div. 2)A B

    Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...

  4. Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. Codeforces Round #373 (Div. 2) A , B , C

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. 【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 sc ...

  7. Codeforces Round #373 (Div. 2)

    A,B,C傻逼题,就不说了. E题: #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  8. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  9. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

随机推荐

  1. 洛谷——P1220 关路灯

    P1220 关路灯 题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉 ...

  2. java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page pagehelper报错无法类型转化。

    报错信息: 严重: Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request ...

  3. mysql function

    mysql 自定义函数的使用 先查看函数功能是否开启:show variables like '%func%'; 若是未开启则:SET GLOBAL log_bin_trust_function_cr ...

  4. Android View 测量流程(Measure)完全解析

    前言 上一篇文章,笔者主要讲述了DecorView以及ViewRootImpl相关的作用,这里回顾一下上一章所说的内容:DecorView是视图的顶级View,我们添加的布局文件是它的一个子布局,而V ...

  5. Zxing二维码精简(竖屏、拉伸处理、扫描框大小和扫描线移动)

    本帖最后由 levil_ad 于 2013-12-30 13:55 编辑 最近没事做了下二维码扫描,用的是ZXing的开源代码,官方源码地址:http://code.google.com/p/zxin ...

  6. 手把手教你如何利用Meterpreter渗透Windows系统

    在这篇文章中,我们将跟大家介绍如何使用Meterpreter来收集目标Windows系统中的信息,获取用户凭证,创建我们自己的账号,启用远程桌面,进行屏幕截图,以及获取用户键盘记录等等. 相关Payl ...

  7. DICOM医学图像处理:深入剖析Orthanc的SQLite,了解WADO & RESTful API

    背景: 上一篇博文简单翻译了Orthanc官网给出的CodeProject上“利用Orthanc Plugin SDK开发WADO插件”的博文,其中提到了Orthanc从0.8.0版本之后支持快速查询 ...

  8. SSH 原理和公匙私匙

    先主要介绍了Telnet.SSH 的通信原理,分析了其通信时的工作流程. Telnet 无论Telnet协议连接的是什么类型终端,都会转换为NVT(Net Virtual Terminal)格式进行通 ...

  9. VC2012编译protobuf出错处理

    近来要学习protobuf的协议生成.须要从网上下载它的代码,从这个SVN地址下载: 个.因此编译提示上面的出错.仅仅须要把std;;tuple里的个数定义为10个就可以.,因此不支持5个以上的參数输 ...

  10. 视图交互--表视图(UITableView)的cell交互析略

    在表视图UITableView的cell上经常有一些交互,根据项目开发中的情况,需要对此进行一些规范.总结出了几种交互方法,这些方法在其他视图的交互上同样可以适用.用一个简单的例子来举例说明一下,其他 ...