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 题目大意: 留坑...
随机推荐
- HyperLedger Fabric 1.4 超级账本起源(5.1)
至比特币开源以来,无数技术人员对其进行研究,并且对该系统经过了无数次改进,超级账本项目(Hyperledger)最初也是用来改善比特币的底层技术,最终由Linux基金会组织发展起来. 开放 ...
- js分类多选全选
效果如图: HTML代码: <div class="form-group quanxian-wrap"> <label>项目</label> & ...
- 【转】在Ubuntu 16.10 Server 上部署 Moodle
第一步 安装 Ubuntu 16.10 Server LTS Moodle 的官方文档肯定了Ubuntu Server LTS 是适合运维Moodle平台的. 1.使用纯代码交互的服务器Ubuntu更 ...
- MVC使用ajax取得JSon数据
为了在view中获取模型中的数据,用ajax异步模式读取数据,再用json返回的view中. 1.controller中: [HttpPost] public ActionResult GetAjax ...
- [Jmeter]jmeter数据库性能测试配置
学习jmeter过程中,记录一些学习过程中的点点滴滴,用于备忘.本文主要介绍的是如何创建一个简单的测试计划用户测试数据库服务器. 一.添加线程组 二.添加JDBC请求 1.在第一步里面定义并发用户以及 ...
- Java:Random函数及其种子的作用
伪随机(preundorandom):通过算法产生的随机数都是伪随机!! 只有通过真实的随机事件产生的随机数才是真随机!!比如,通过机器的硬件噪声产生随机数.通过大气噪声产生随机数 Random生成的 ...
- 台湾ML笔记--1.2 formalize the learning probelm
Basic notations input: x∈χ (customer application) output: y∈y (good/bad after approving cred ...
- CSS流布局权威指南
http://www.cnblogs.com/qieguo/p/5421252.html
- python------- IO 模型
IO模型介绍 ...
- tensorflow nmt基本配置(tf-1.4)
随着tensorflow的不断更新,直接按照nmt的教程搭建nmt环境会报错的...因此,需要一些不太好的办法来避免更多的问题出现.tensorflow看来在ubuntu和debian中运行是没有问题 ...