codeforces 719C. 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.
题意:你有n次操作,可以让给定的这个数的小数位四舍五入,求这个数经过t次操作后最大是多少
题解:小数位如果第一个小数位是>=5的情况下,整数位的个位就要+1,如果整数位是999这种情况就要变成1000
为了使得经过变换后的数最大,我们先从小数位的最前面开始进位
代码如下:
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
char str[];
char a[];
char b[];
int main(){
#ifndef ONLINE_JUDGE
FIN
#endif
int n,t;
scanf("%d%d",&n,&t);
cin>>str;
int pos=;
//整数
for(pos = ;; pos++) {
if(str[pos] == '.') break;
else a[pos] = str[pos];
}
pos++;
for(int i=;str[i];i++,pos++){
b[i]=str[pos];
}
pos=-;
for(int i=;b[i];i++){
if(b[i]>=''){
pos=i;
break;
}
}
int flag=;
for(int i=pos;i>=&&t>;i--){
if(i!=&&b[i]>=''){
b[i-]++;
b[i]=;
}else if(i==&&b[i]>=''){
flag=;
}else break;
t--;
}
if(!flag) printf("%s.%s\n",a,b);
else{
int len=strlen(a);
int num=;
int i;
for(i=len-;i>=;i--){
if(a[i]!=''){
a[i]++;
break;
}else{
a[i]='';
num++;
}
}
if(num==len) cout<<"";
cout<<a<<endl;
}
return ;
}
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
int n,t;
scanf("%d%d",&n,&t);
cin>>s;
int i=;
while(s[i]!='.') i++; //整数部分的长度
while(i<n&&s[i]<'') i++; //不能进位的长度
if(i==n){
//如果全部不能四舍五入就直接输出
cout<<s<<endl;
return ;
}
i--;
int len=;
while(t>){
if(s[i]!='.') s[i]++;
else{ i--;
len=i;
while(i>=&&s[i]=='') s[i--]=''; //如果当前位是9,那么进位时注意将当前位改为0
if(i==-) cout<<''; //如果是9999的情况,就变成10000;
else s[i]++;
break;
}
if(s[i]<''){
len=i;
break;
}else{
len=i;
i--;
}
t--;
}
for(int i=;i<=len;i++){
cout<<s[i];
}
cout<<endl;
}
codeforces 719C. Efim and Strange Grade的更多相关文章
- 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的,然后进行四舍五入,完成后再看看是不是还 ...
- 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 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次在任意位进 ...
- 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 ...
- 【22.17%】【codeforces718B】 Efim and Strange Grade
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- codeforces 719C (复杂模拟-四舍五入-贪心)
题目链接:http://codeforces.com/problemset/problem/719/C 题目大意: 留坑...
随机推荐
- backtrace函数
1.函数原型 #include <execinfo.h> int backtrace(void **buffer, int size); 该函数获取当前线程的调用堆栈,获取的信息将会被存放 ...
- node获取URL数据
req.method -->GET req.hostname -->127.0.0.1 req.originalUrl -->/test/test/test?name=wang ...
- Linux中的目录功能(Red Hat 7)
目录的基本功能: /bin:存放普通用户使用的命令 /sbin:存放管理员可以执行的命令 /home:存放普通的家目录 如张三家目录为/home/zhangsan /root:管理员的家目录 /etc ...
- SPOJ SUBLEX
SUBLEX - Lexicographical Substring Search 链接 题意 求第k小的子串.相同的算一个. 分析 建立后缀自动机,在后缀自动机上从一个点经过trans,到另一个点, ...
- LeetCode:9. Palindromic Number(Medium)
原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...
- python自动化之BDD框架之lettuce初识问题集
最近在学习虫师老师编写的python自动化的书.其中讲到了BDD结构lettuce入门一章. 因为是小白,按部就班地进行操作,先不谈执行操作如何,先来讲讲遇到的几个坑,和怎么解决的: 第一坑:pyth ...
- 树莓派3_win10下使用"远程桌面连接"与树莓派通信(使用VNC实现连接后)
-----------------------------------------------------------学无止境------------------------------------- ...
- 杜绝网上压根没测过就乱写之 《oracle mybatis 返回自增主键 》
面试过好多人,包括自己也属于这么一个情况: 遇到问题直接去网上查,一般都可以查到解决方案.其中也包括一些基本的面试资料的答案. 其实有很多答案也都是正确的,但是还是存在一些压根就是胡乱抄来的答案,也不 ...
- ardupilot_gazebo仿真(三)
ardupilot_gazebo仿真(三) 标签(空格分隔): 未分类 创建ROS node 实现对无人机的控制(软件在环) MAVROS MAVROS是ROS中的一个能够连接支持MAVLink地面站 ...
- OpenPAI:大规模人工智能集群管理平台介绍及任务提交指南
产品渊源: 随着人工智能技术的快速发展,各种深度学习框架层出不穷,为了提高效率,更好地让人工智能快速落地,很多企业都很关注深度学习训练的平台化问题.例如,如何提升GPU等硬件资源的利用率?如何节省硬件 ...