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 题目大意: 留坑... 
随机推荐
- JavaScript之this解析
			1.解析器在调用函数每次都会向函数内部传递进一个隐含的参数,这个隐含的参数就是this,this指向的是一个对象,这个对象我们称为函数执行的上下文对象,根据函数的调用方式不同,this会指向不同的对象 ... 
- ts包、表、子表、section的关系
			我们经常接触到创建 DEMUX,注册 Filter 过滤数据, 通过回调过滤出 section 数据,然后我们对 section 数据做具体的解析或者其他操作. 我们这里说的 section 就是段的 ... 
- (数据科学学习手札04)Python与R在自定义函数上的异同
			自编函数是几乎每一种编程语言的基础功能,有些时候我们需要解决的问题可能没有完全一致的包中的函数来进行解决,这个时候自编函数就成了一样利器,而Python与R在这方面也有着一定的差别,下面举例说明: P ... 
- [Cracking the Coding Interview] 4.3 List of Depths
			Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth. ... 
- 10-mongodb启动错误
			1.error信息 python@ubuntu:~$ mongod --22T17:: I CONTROL [initandlisten] MongoDB starting : pid= port= ... 
- mock.js中新增测试接口无效,返回404
			项目是使用的npm+vue+mock模拟数据 我碰到的是在mock配置文件中新增接口,但是接口在测试时无效,返回404.但是在前端代码中把新接口换成配置文件中之前就有的,然后测试就正常了. 所以按问题 ... 
- centos linux 因别名问题引起的麻烦及解决技巧
			老男孩儿-19期 L005-13节中分享.自己整理后发到自己微博中留档. 原文:http://oldboy.blog.51cto.com/2561410/699046 实例:老男孩linux实战培训第 ... 
- windows系统下npm升级的正确姿势以及原理
			本文来自网易云社区 作者:陈观喜 网上关于npm升级很多方法多种多样,但是在windows系统下不是每种方法都会正确升级.其中在windows系统下主要的升级方法有以下三种: 首先最暴力的方法删掉no ... 
- 【廖雪峰老师python教程】day1
			主要内容摘要 函数参数[个人感觉难度很大,却很重要,可以先大概记一记]不要用的太复杂.戳这儿温习 递归函数:使用递归函数的优点是逻辑简单清晰,缺点是过深的调用会导致栈溢出.重点:找到递归关系和终止条件 ... 
- SSH公钥认证(码云)
			开发者向码云版本库写入最常用到的协议是 SSH 协议,因为 SSH 协议使用公钥认证,可以实现无口令访问,而若使用 HTTPS 协议每次身份认证时都需要提供口令.使用 SSH 公钥认证,就涉及到公钥的 ... 
