浙江财经大学
信息管理与工程学院

Codeforces Round #373 (Div. 2) C – Efim and Strange Grade

本文由 Ocrosoft 于 2016-09-24 23:17:13 发表

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.

Solution

题意:先输入len,k,再给出一个小数,对其四舍五入k次,求能得到的最大数。
思路:先从小数点开始往后找,如果没有大于5的,显然不能五入,最大的数就是原来的。
如果找到的大于5的,就从这位开始往前进位。(第一个大于等于5的,后面的进位肯定不如这一位大)。
如果进位跨过了小数点,要注意像999.999这样,前面要补零。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
//#include "Osort.h"
#define ms(a) memset(a,0,sizeof(a))
typedef long long LL;
const LL LINF = 1e17;
const int INF = INT_MAX / 2;
const int MAXN = 150 + 10;
const int MOD = 100000;
int gcd(int a, int b)
{
	if (!b)return a;
	return gcd(b, a%b);
}
using namespace std;
//or2..or2..or2..or2..//
char s[10000000];
int main()
{
	int n, m, dot = 0, i; cin >> n >> m >> s;
	for (i = 0; i < n; i++)
	{
		if (s[i] == '.')dot = i;
		if (!dot)continue;
		if (s[i] >= '5') break;;
	}
	int sav = i;
	while (m--)
	{
		if (i >= dot&&s[i] >= '5')
		{
			i--;
			while (s[i] == '.' || (i >= 0 && s[i] == '9'))
			{
				if (s[i] == '.')s[i--] = '\0';
				else s[i--] = '0';
			}
			if (i != -1) s[i]++;
			else printf("1");
		}
		//else if(i<dot)i = dot - 1;
		else break;
	}
	if (i > dot)s[i + 1] = '\0';
	cout << s << endl;
	return 0;
}

欢迎分享与转载,请保留链接与出处。Ocrosoft » Codeforces Round #373 (Div. 2) C – Efim and Strange Grade

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址