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. C#截取指定长度中英文字符串方法 (修改)

    public static string GetFirstString(string stringToSub, int length) { Regex regex = new Regex(" ...

  2. codeforces 1037E. Trips(倒叙)

    题目传送门: 解题思路: 正着搞好像有点恶心. 反着搞. 一边删一边搞,从崩坏的地方开始,入度--. 最后dfs崩坏,更新答案. 注意要把边删掉防止重复崩坏. 代码: #include<cstd ...

  3. Http请求连接池 - HttpClient 的 PoolingHttpClientConnectionManager

    两个主机建立连接的过程是非常复杂的一个过程,涉及到多个数据包的交换,而且也非常耗时间.Http连接须要的三次握手开销非常大,这一开销对于比較小的http消息来说更大.但是假设我们直接使用已经建立好的h ...

  4. Moodle 中文 API 之 文件管理API

    File API  文件管理 文件夹 1. 概述 2. 文件域 2.1 命名文件域 3. 提供文件给用户 4. 从用户那获取文件 5. 样例 5.1 浏览文件 5.2 移动文件 5.3 文件列表 5. ...

  5. CentOS经常使用文件操作命令[百度博客搬家]

    路径操作的CentOS经常使用命令 如今整理例如以下(百度博客搬家)   cd pwd  NO1. 显示当前路径  [root@rehat root]# pwd  NO2. 返回用户主文件夹  [ro ...

  6. 存储过程和SQL语句比较

    做为SQL存储过程和.NET的新手,下面的指导还是很有用的,自己这一段刚刚接触这些东西,搜集了一些相关的东西,能使新手较容易上手,当然啦,要精通和熟练应用,还是要看更多更深的资料的,高手请不要见笑.以 ...

  7. python3怎样画二维点图

    引用自:http://www.cnblogs.com/super-zhang-828/p/4792206.html import matplotlib.pyplot as pltplt.plot([1 ...

  8. MWPhotoBrowser 属性详解 和代理解释

    --------0.MWPhoto简单属性解释---------------- MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString: ...

  9. 《ECMAScript6入门》笔记——Generator函数

    今天在看<ECMAScript6入门>的第17章——Generator函数的语法.理解起来还是有点费劲,几段代码看了很多遍.总算有点点理解了. 示例代码如下:(摘自阮一峰<ECMAS ...

  10. 自定义npm包的创建、发布、更新和撤销

    大纲 1.准备2.自定义npm包3.发布自定义npm包4.引用npm包5.更新npm包6.撤销发布的npm包 简书原文 https://www.jianshu.com/p/d737bc5df5b7 1 ...