CodeForces-718A Efim and Strange Grade(模拟)
来源:互联网 发布:淘宝商家客服投诉电话 编辑:程序博客网 时间:2024/06/07 04:47
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 110.245
10.25
6 210.245
10.3
3 1009.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.
传送门:http://codeforces.com/problemset/problem/718/A
题意:给一个小数,可以在小数点后进行最多k次四舍五入(可以小于k次),要求最后得到的数最大
#include<cstdio>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>using namespace std;const int maxn = 2e5+ 5;char st[maxn];int main(){ int n,t; // freopen("in.txt","r",stdin); while(~scanf("%d%d%s",&n,&t,st)){ int index; for(int i=0;i<n;i++) if(st[i]=='.') {index=i;break;} int ed=0; for(int i=index+1;i<n;i++){ if(st[i]>='5') { st[i]='\0'; st[i-1]++; ed=i; break; } } if(ed!=0){ for(int i=ed-1;i>index;i--){ if(st[i]>'9') { st[i]='\0'; st[i-1]++; ed=i; } else break; } t--; } while(t--){ int tmp=ed; for(int i=ed-1;i>index;i--){ if(st[i]>='5') { st[i]='\0'; st[i-1]++; ed=i; break; } } if(ed==tmp) break; for(int i=ed-1;i>index;i--){ if(st[i]>'9') { st[i]='\0'; st[i-1]++; ed=i; } else break; } } if(st[index]>'.') { st[index-1]++; st[index]='\0'; for(int i=index-1;i>0;i--){ if(st[i]>'9') { st[i]='0'; st[i-1]++; } } if(st[0]>'9') { printf("1"); st[0]='0'; } } printf("%s\n",st); } return 0;}
- CodeForces-718A Efim and Strange Grade(模拟)
- Codeforces 718A A. Efim and Strange Grade
- [Codeforces #373 (Div. 2) C. Efim and Strange Grade] 模拟
- codeforces 719C Efim and Strange Grade (复杂模拟)
- codeforces 719 C. Efim and Strange Grade(模拟)
- [codeforces] C - Efim and Strange Grade 模拟+贪心
- codeforces 719 C Efim and Strange Grade (模拟)
- Codeforces 719C Efim and Strange Grade 贪心+模拟
- Codeforces Round #373 (Div. 1)A. Efim and Strange Grade【恶心模拟】
- Codeforces Round #373 (Div. 1) A.Efim and Strange Grade
- codeforces 719C. Efim and Strange Grade
- codeforces Round 373 div2 C. Efim and Strange Grade 模拟+ priority_queue
- Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 解题报告
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 贪心、实数字符串"a.b"处理
- [codeforces]A. Efim and Strange Grade——贪心+字符串处理
- Codeforces Round #373 (Div. 2) C: Efim and Strange Grade
- Codeforces 719C Efim and Strange Grade【贪心】
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade(贪心+模拟,t次进位之后的数是什么)
- C#单例模式的 几种写法
- dart资料
- 文件I/O-008.fcntl返回文件标志并解析打印标志
- Hadoop1.2.1开发环境搭建
- #ifdef #ifndef #if #include #line #pragma #define命令
- CodeForces-718A Efim and Strange Grade(模拟)
- 关于Visual Studio c#程序.netframework错误的解决记录
- SwipeRefreshLayout详解和自定义加载更多
- Google's Neural Machine Translation System
- 文件I/O-009.set_fl()的实现
- 微信小应用,又一大神,音乐播放器
- 密室问题
- Apache mina2 用户指南(四)会话
- 关于ssdb性能方面的一些思考