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. node 内存溢出

    遇到这个问题的人可以更快解决 再复制写一篇 利于百度搜索 坑爹的node 内存溢出 react开发项目  安装一个插件依赖 ,然后就报错了 报错如下(自己的没有截图出来 这是从别人的截图---报错基本 ...

  2. JAVA MessageDigest(MD5加密等)

    转自http://blog.csdn.net/hudashi/article/details/8394158 一.概述 java.security.MessageDigest类用于为应用程序提供信息摘 ...

  3. (F) linux sort,uniq,cut,wc命令详解

    F:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html sort sort 命令对 File 参数指定的文件中的行排序,并 ...

  4. numpy_basic2

    # 六.numpy的常用函数 1. 读取文件 逻辑上可被解释为二维数组的文本文件: 数据项1<分隔符>数据项2<分隔符>...<分隔符>数据项n numpy.loa ...

  5. vim学习2

    进入插入模式: 在插入模式下删除: 寄存器

  6. 1.1 Introduction中 Kafka as a Messaging System官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Messaging System kafka作为一个消息系统 ...

  7. idea 配置文件导出,导入

    俗话说的好,磨刀不误砍柴工.配置好自己的工具,这样撸码就会更爽. 来来来,傻瓜式配图开始. 点击后会出现有一个导出设置框默认为全部导出 点击...处 可设置导出的settings.jar包的位置 在新 ...

  8. Flask项目之手机端租房网站的实战开发(五)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  9. 11.4 Android显示系统框架_APP与SurfaceFlinger内部机制分析

    4.1 APP跟SurfaceFlinger之间的重要数据结构 一个应用程序有一个或者多个surface(一般只有一个),一个surface有一个或者多个buffer,这些buffer需要应用向sur ...

  10. 定制表格头, 学习Core Graphic 的第二部分, 阴影与玻璃效果.

    //定制表格头, 学习Core Graphic 的第二部分, 阴影与玻璃效果. https://github.com/comcuter/testsnippets/commit/e96f62d115ef ...