codeforces 373 A - Efim and Strange Grade(算数模拟)
codeforces 373 A - Efim and Strange Grade(算数模拟)
题意:给出一个n位的实型数,你可以选择t次在任意位进行四舍五入的进位,求最大结果。
解法:这道题一定不能忽略数位计算时本身带来的进位,如果我们要改变这个数的大小,一定是在最先的那个出现5以上的数字进行四舍五入,之后的t次允许我们多次四舍五入,如果自然进位则不消耗t。
最后一点,整数位的进位也是需要考虑的
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=2e5+10;
char a[N];
int main()
{
int n,t;
while(~scanf("%d%d",&n,&t))
{
scanf("%s",a);
int len=strlen(a),i,j,st,v,flag=0;
for(i=0; i<len&&a[i]!='.'; i++);
for(j=i+1; j<len&&a[j]<'5'; j++);
st=(j==len?0:1);
t-=1;
a[j]='\0';
for(int k=j-1; k>=0; k--)
{
if(a[k]=='.'){
flag=1;continue;
}
v=a[k]-'0'+st;
a[k]=v%10+'0';
st=v/10;
if(!st){
if(!flag&&v>=5&&t>0){
t--;
st=1;
a[k]='\0';
}else break;
}
}
if(st==1) putchar('1');
for(j=strlen(a)-1; j>i&&a[j]=='0'; j--);
if(a[j]!='.') j++;
a[j]='\0';
printf("%s",a);
puts("");
}
return 0;
}
codeforces 373 A - Efim and Strange Grade(算数模拟)的更多相关文章
- 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 718A Efim and Strange Grade 程序分析
Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...
- codeforces 719C. Efim and Strange Grade
C. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 718A Efim and Strange Grade (贪心)
题意:给定一个浮点数,让你在时间 t 内,变成一个最大的数,操作只有把某个小数位进行四舍五入,每秒可进行一次. 析:贪心策略就是从小数点开始找第一个大于等于5的,然后进行四舍五入,完成后再看看是不是还 ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
随机推荐
- MySQL:目录
ylbtech-MySQL:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech ...
- Android网络相关代码
/** * 获取本机的Ip * * @return */ public String getLocalIpAddress() { try { for (Enumeration<NetworkIn ...
- .NET Runtime version 2.0.50727.8762 - 执行引擎错误(7969097A) (80131506)
VS2010调试IIS发布的web工程提示:无法连接到 Visual Studio 开发服务器 .NET Runtime version 2.0.50727.8762 - 执行引擎错误(7969097 ...
- shell判断文件,目录是否存在或者具有权限 (转载)
转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...
- Juicer.js模板引擎问题
由于jsp中的EL表达式语法和jquery.tmpl十分类似,,所以单纯的使用${name},数据是渲染不上tmpl的. SO.. 要加上转义: ${'${'}amount} 或者 \${amount ...
- thinkphp 5 常用的助手函数
load_trait:快速导入Traits,PHP5.5以上无需调用 /** * 快速导入Traits PHP5.5以上无需调用 * @param string $class t ...
- [转]c++中的string常用函数用法总结
标准c++中string类函数介绍 注意不是CString之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为 ...
- Redis基础---链接管理
Redis连接 Redis AUTH命令是用来向服务器验证给定的密码. 如果密码与在配置文件中的口令相匹配,则服务器会返回OK状态码,并开始接受命令.否则,将返回一个错误,并且客户需要尝试新的密码. ...
- [ Luogu 4626 ] 一道水题 II
\(\\\) \(Description\) 求一个能被\([1,n]\) 内所有数整除的最小数字,并对 \(100000007\) 取模 \(N\in [1,10^8]\) \(\\\) \(Sol ...
- STL容器迭代过程中删除元素技巧(转)
1.连续内存序列容器(vector,string,deque) 序列容器的erase方法返回值是指向紧接在被删除元素之后的元素的有效迭代器,可以根据这个返回值来安全删除元素. vector<in ...