zoukankan      html  css  js  c++  java
  • 【22.17%】【codeforces718B】 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 0001 ≤ 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("
    ");
    }
    
    //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
    ", 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;
    }


  • 相关阅读:
    JUC-狂神笔记整理学习
    多线程-学习笔记
    Redis分布锁
    Redis
    springcloud一个简单的基本流程
    Nacos
    mysql单表查询
    mysql多表查询
    mysql数据库
    mysql详细安装教程以及1067错误代码解决方案
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632221.html
Copyright © 2011-2022 走看看