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.


【题解】

每次找小数位最靠左的且大于等于5的位置就可以了。

每次进完位可能新出现5.但是新出现的5肯定是进位后出现的。

看看那些进位后的数有哪个是5就记录一下。因为是从右往左进位。所以找到的肯定是最左边的5了。

这很顺利;

但是我竟然把有没有小数部分的条件改成小数第一位是不是0;

3.03有小数部分啊!!!!

然后我是把小数和整数部分倒过来做的。(做高精度的那种思想);

然后小数部分如果可以进位到整数部分,进的位就在小数部分的l+1的位置(因为是倒过来的);

所以最后正数部分加上小数部分[l+1];

然后在正数部分再进位。

最后再从整数开始逆序输出整数的数组;

根据小数部分的最后的位置判读有没有小数部分(-_-)!;

然后输出点号,逆序输出小数部分。

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib> const int MAXN = 209000; char s[MAXN];
int xiaoshu[MAXN], zhengshu[MAXN];
int len, l = 0, pos5 = 0, t, ma_x, pre, pos, ll = 0; void input_data()
{
scanf("%d%d", &len, &t);
scanf("%s", s);
for (int i = len - 1; i >= 0; i--)
if (s[i] != '.')
{
l++;
xiaoshu[l] = s[i] - '0';
}
else
{
pos = i;
break;
}
for (int i = pos - 1; i >= 0; i--)
{
ll++;
zhengshu[ll] = s[i] - '0';
}
} void output_ans()
{
for (int i = 1; i <= pre; i++)//把该置0的置0
xiaoshu[i] = 0;
zhengshu[1] += xiaoshu[l + 1];//加上进到整数的部分
for (int i = 1; i <= ll; i++)//正数部分再尝试进位
{
zhengshu[i + 1] += (zhengshu[i] / 10);
zhengshu[i] = zhengshu[i] % 10;
}
if (zhengshu[ll + 1] > 0)
ll++;
for (int i = ll; i >= 1; i--)
printf("%d", zhengshu[i]);
if (pre + 1 <= l)//不要写成xiaoshu[l]!=0 .....
{
printf(".");
for (int i = l; i >= pre + 1; i--)
printf("%d", xiaoshu[i]);
}
printf("\n");
} //89.2343
//xioshu[1..l]={3,4,3,2}
//zhengshu[1..2] = {9,8};
void get_ans()
{
pre = 0;
ma_x = 1;
for (int i = l; i >= 1; i--)
if (xiaoshu[i] >= 5)
{
pos5 = i;
break;
}
if (pos5 == 0)//如果没有能让结果更大的情况出现就结束
{
printf("%s\n", s);
exit(0);
}
pre = pos5;//pre表示1..pre都置0
while (t)
{
xiaoshu[pos5 + 1]++;
int tempj = pos5 + 1;
int x = xiaoshu[tempj] / 10;
xiaoshu[tempj] %= 10;
pos5 = 0;
if (xiaoshu[tempj] >= 5)//只有进位之后才可能出现新的5
pos5 = tempj;
while (x)//进位
{
tempj++;
xiaoshu[tempj] += x;
x = xiaoshu[tempj] / 10;
xiaoshu[tempj] %= 10;
if (xiaoshu[tempj] >= 5)//最靠近左边的5
pos5 = tempj;
}
t--;
if (pos5 == 0)//如果没有新的5了就退出
{
output_ans();
exit(0);
}
else
{
if (t)
pre = pos5;
}
}
output_ans();
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_data();
get_ans();
return 0;
}

【22.17%】【codeforces718B】 Efim and Strange Grade的更多相关文章

  1. CF719C. Efim and Strange Grade[DP]

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

  2. Efim and Strange Grade

    Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input standa ...

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

  4. codeforces 719C. Efim and Strange Grade

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

  5. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade —— 贪心 + 字符串处理

    题目链接:http://codeforces.com/problemset/problem/719/C C. Efim and Strange Grade time limit per test 1 ...

  6. codeforces 373 A - Efim and Strange Grade(算数模拟)

    codeforces 373 A - Efim and Strange Grade(算数模拟) 原题:Efim and Strange Grade 题意:给出一个n位的实型数,你可以选择t次在任意位进 ...

  7. Codeforces 718A Efim and Strange Grade 程序分析

    Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...

  8. CodeForces 718A Efim and Strange Grade (贪心)

    题意:给定一个浮点数,让你在时间 t 内,变成一个最大的数,操作只有把某个小数位进行四舍五入,每秒可进行一次. 析:贪心策略就是从小数点开始找第一个大于等于5的,然后进行四舍五入,完成后再看看是不是还 ...

  9. 剑指Offer:链表中倒数第k个结点【22】

    剑指Offer:链表中倒数第k个结点[22] 题目描述 输入一个链表,输出该链表中倒数第k个结点. 解题思考 我们定义两个指针L和R,R事先移动K-1个位置,然后两者同时往后移动直到遇到R的下个节点为 ...

随机推荐

  1. 看好腾讯,鄙视百度(腾讯的核心竞争力,不是超过10亿的QQ的注册用户,也不是某一项产品、技术方面优势,而是“耐心”:懂得在合适的时间推出合适的产品。”)

    百度,自始至终只是一个低劣的模仿者,且一切向前看,完全违背了一个搜索引擎所应该遵循的基本原则.谁给的钱多就能搜着谁,这跟贩毒有什么区别? 腾讯也在模仿别人,但是,它是模仿然后超越.在中国互联网发展历史 ...

  2. vue实现多语言国际化(vue-i18n),结合element ui、vue-router、echarts以及joint等。

    老板说我们的项目要和国际接轨,于是乎,加上了多语言(vue-i18n).项目用到的UI框架是element ui ,后续echarts.joint等全都得加上多语言. 一.言归正传,i18n在vue项 ...

  3. HDU 4508 湫湫系列故事——减肥记I (2013腾讯编程马拉松初赛第一场)

    http://acm.hdu.edu.cn/showproblem.php?pid=4508 题目大意: 给定一些数据. 每组数据以一个整数n开始,表示每天的食物清单有n种食物.  接下来n行,每行两 ...

  4. 【习题 6-8 UVA - 806】Spatial Structures

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写两个dfs模拟就好. 注意每12个数字输出一个换行.. [代码] /* 1.Shoud it use long long ? 2. ...

  5. 10559 - Blocks(方块消除|DP)

    该题乍一看和矩阵链乘非常类似,但是有一个不同之处就是该题能够拼接 .   为了达到这个目的.我们不得不拓展维度d[i][j][k].用一个k表示最右边拼接了k个和a[j]同样颜色的方块. 问题的关键在 ...

  6. PHP路由技术的原理与实践

    0x00 路由实现原理 用户通过指定的URL范式对后台进行訪问.URL路由处理类进行处理后.转发到逻辑处理类,逻辑处理类将请求结果返回给用户. 约定URL范式和规则 约定一套自己喜欢的,对搜索引擎友好 ...

  7. 以流的形式接收Http请求

    IEnumerable<ReportObject> IHttpReceiveReport.OnPost(System.Web.HttpContextBase context) { //[{ ...

  8. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  9. PythonAdvanced

    PythonAdvanced function 函数 (要多使用函数,方便,少变量,好改错) 函数是可以重复执行的语句块,可以重复使用 作用: 1.用于封装语句块,提高代码的重用性 2.定义用户级别的 ...

  10. TOMCAT8009端口与AJP13协议

    Tomcat最主要的功能是提供Servlet/JSP容器,尽管它也可以作为独立的Java Web服务器,它在对静态资源(如HTML文件或图像文件)的处理速度,以及提供的Web服务器管理功能方面都不如其 ...