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 题目大意: 留坑...
随机推荐
- 清华大学《C++语言程序设计基础》线上课程笔记02---类与对象
类与对象 public是类的对外访问接口: 类内初始值 在定义类时对数据成员写初始值,在创建对象的时候,会使用类内初始值初始化数据成员: class Clock { public: void show ...
- C# 实现程序开机自启动
最近在做一个自动备份文件的小工具,需要用到开机自启动 下面是代码 private void checkBox8_CheckedChanged(object sender, EventArgs e) { ...
- WebService第一天——概述与入门操作
一.概述 1.是什么 Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些 ...
- java加密用到了BASE64Decoder时报错信息:Access restriction: The type BASE64Encoder is not accessible due to restrict
在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : ...
- 2016年JD工作遇到的问题:1-5,搭建环境和熟悉项目过程中的坑
1.更新不需要权限的项目A,却提示没有权限. 先从Git上更新项目A的代码,提示没有权限. 然后从Git上更新其它项目B的代码,正常. 再更新项目A的代码,正常了. 奇葩问题! 2.Eclipse中, ...
- struts2官方 中文教程 系列七:消息资源文件
介绍 在本教程中,我们将探索使用Struts 2消息资源功能(也称为 resource bundles 资源绑定).消息资源提供了一种简单的方法,可以将文本放在一个视图页面中,通过应用程序,创建表单字 ...
- 【原创】java 获取十个工作日之前或之后的日期(算当天)-完美解决-费元星
[原创]java 获取十个工作日之后的日期(算当天)-完美解决-费元星(仅考虑星期六星期天) /** * * 根据开始日期 ,需要的工作日天数 ,计算工作截止日期,并返回截止日期 * @param s ...
- SharePoint2013修复报错
今天项目组的Sharepoint2013不小心被卸载了,本想着直接修复,谁知道在修复的时候一直报错,说找不到什么文件.报的就是类似于这样的错误: Product: ######### -- ...
- 修改npm全局安装模式的路径
由于npm全局模块的存放路径及cache的路径默认是放在C盘下,这样肯定会增加C盘的负担,那么如果需要修改其存放路径应该怎么做呢? 第一步:在nodejs安装目录(也可以指定其它目录)下创建”node ...
- SQL - SELECT COUNT用法
SQL Server数据库 COUNT() 函数返回匹配指定条件的行数. 语法 SQL COUNT(column_name) 语法 COUNT(column_name) 函数返回指定列 ...