【22.17%】【codeforces718B】 Efim and Strange Grade
1 second
256 megabytes
standard input
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.
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.
Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.
6 1
10.245
10.25
6 2
10.245
10.3
3 100
9.2
9.2
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的更多相关文章
- 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 ...
- Efim and Strange Grade
Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 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 ...
- 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 ...
- 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 ...
- codeforces 373 A - Efim and Strange Grade(算数模拟)
codeforces 373 A - Efim and Strange Grade(算数模拟) 原题:Efim and Strange Grade 题意:给出一个n位的实型数,你可以选择t次在任意位进 ...
- Codeforces 718A Efim and Strange Grade 程序分析
Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...
- CodeForces 718A Efim and Strange Grade (贪心)
题意:给定一个浮点数,让你在时间 t 内,变成一个最大的数,操作只有把某个小数位进行四舍五入,每秒可进行一次. 析:贪心策略就是从小数点开始找第一个大于等于5的,然后进行四舍五入,完成后再看看是不是还 ...
- 剑指Offer:链表中倒数第k个结点【22】
剑指Offer:链表中倒数第k个结点[22] 题目描述 输入一个链表,输出该链表中倒数第k个结点. 解题思考 我们定义两个指针L和R,R事先移动K-1个位置,然后两者同时往后移动直到遇到R的下个节点为 ...
随机推荐
- uiautomator——第一个例子:打开浏览器,输入网址
1.在sdk安装目录:E:\Test_Tools\auto_test\app\adt-bundle-windows-x86-20131030\sdk\tools下启动uiautomatorviewer ...
- ACM 中JAVA的应用
原文地址:http://www.cppblog.com/vontroy/archive/2010/05/24/116233.html 先说一下Java对于ACM的一些优点吧: (1) 对于熟悉C/C+ ...
- bzoj3786星系探索(splay维护dfs序)
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- (F) linux sort,uniq,cut,wc命令详解
F:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html sort sort 命令对 File 参数指定的文件中的行排序,并 ...
- 三、Docker镜像的相关操作
原文:三.Docker镜像的相关操作 一.查看本地镜像: docker images 二.使用某个镜像来运行容器: docker run -t -i xxxx(镜像名):xx.xx(版本,不带即最新) ...
- 用实力让情怀落地!阅兵前线指挥车同款电视TCL H8800受捧
近期.一则重磅消息刷爆了平面媒体.微博.朋友圈等各个传播渠道:TCL曲面电视H8800正式入驻大阅兵前线指挥车以及国旗护卫队荣誉室.宣告代表眼下中国彩电业最高技术水准的曲面电视,正式走上大阅兵 ...
- subline Text3 安装及汉化
因为自己的subline 有问题 所以决心重新改一下了. 三步: http://www.sublimetext.com/3 官网下载subline3 https://www.imjeff. ...
- 使用jmeter监控服务器性能指标
先下载jmeter-ServerAgent Windows下载和Linux下载 https://jmeter-plugins.org/wiki/PerfMon/ 找到ServerAgent的下载链接 ...
- Java Web学习总结(7)——HttpServletRequest对象
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- 洛谷 P1192 台阶问题
P1192 台阶问题 题目描述 有N级的台阶,你一开始在底部,每次可以向上迈最多K级台阶(最少1级),问到达第N级台阶有多少种不同方式. 输入输出格式 输入格式: 输入文件的仅包含两个正整数N,K. ...