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 题目大意: 留坑...
随机推荐
- Servlet生命周期与线程安全
上一篇介绍了Servlet初始化,以及如何处理HTTP请求,实际上在这两个过程中,都伴随着Servlet的生命周期,都是Servlet生命周期的一部分.同时,由于Tomcat容器默认是采用单实例多线程 ...
- HTML5 + JS 调取摄像头拍照下载
<video id="video" width="640" height="480" autoplay></video&g ...
- 完整的vue+vuex+api-router+database请求流程
- Django admin操作
无名小妖 昵称:无名小妖园龄:1年6个月粉丝:22关注:1 +加关注 搜索 常用链接 我的随笔 我的评论 我的参与 最新评论 我的标签 我的标签 Python(1) python3 ...
- 深入理解计算机系统(1)--hello world程序的生命周期
第一篇笔记的主题是讨论Hello World程序的生命周期,程序是最简单的hello world程序,使用高级C语言编写. 先介绍整个生命周期中涉及到的几个部分以及相应的概念,然后总结整个生命周期,最 ...
- android中接入twitter进行第三方登录
在应用中接入Twitter进行第三方登录时,开发人员遇到了一点问题,主要是概念有点混乱,这里把经验记录一下,帮助遇到同样问题的朋友. 一.注册应用并配置登录权限 这一步比较简单,就不多说了,直接去官网 ...
- jmeter设置全局变量的方法
需求: 同一个线程组内有两个http请求A.B,A请求的后置处理器中存储的有值,B请求中添加用户变量Va先要引用该值,然后B请求的前置处理器再引用用户变量va. 第一种方式: 1.A请求后置处理添加如 ...
- jmeter使用beanshell构造参数化
1.先在本地写一个java类,用来随机生成一个数字,如: package com.jmeter.test; public class BeanShellTest { public int getRan ...
- NetBeans集成SVN代码管理实例
最近给银行做一个小工具,要求用Java做一个C端带界面的小工具,想来想去用NetBeans最合适,因为Eclipse,MyEclipse,IDEA这些做界面得要额外的UI插件,比较麻烦. 我跟同事两个 ...
- 数据库学习(三) sql语句中添加函数 to_char,round,连接符||
** to char 是把日期或数字转换为字符串 to date 是把字符串转换为数据库中得日期类型 参考资料:https://www.cnblogs.com/hllnj2008/p/533296 ...