Weird Rounding (Codeforces-779B)
来源:互联网 发布:网络签约写手的收入 编辑:程序博客网 时间:2024/05/20 01:10
题目链接:
http://codeforces.com/problemset/problem/779/B
Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k.
In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000that is divisible by 103 = 1000.
Write a program that prints the minimum number of digits to be deleted from the given integer number n, so that the result is divisible by 10k. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit).
It is guaranteed that the answer exists.
The only line of the input contains two integer numbers n and k (0 ≤ n ≤ 2 000 000 000, 1 ≤ k ≤ 9).
It is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros.
Print w — the required minimal number of digits to erase. After removing the appropriate w digits from the number n, the result should have a value that is divisible by 10k. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0).
30020 3
1
100 9
2
10203049 2
3
题目大意:
输入一个n和k(没有前导0),判断去掉n的几位数字后能够被10^k整除。比如第一组测试数据:30020 3,去掉以‘2’后n变成30000,能够被10^3整除。即去掉一位数字后技能满足题意,所以输出‘1’。另外注意:题目保证有答案。
解题思路:
需要分几种情况:
记 len 为数字n的位数
1:len=1,而题目说一定有解,说明n一定等于0,因为0能够被任何数整除。不用去掉数字,输出0即可;
2:n的位数<=k,那么只有把n变为0,才满足题意,输出 len-1 (即只剩数字0);
3:n直接能够被10^k整除,输出0;
4:去掉数字后才能内10^k整除,从字符串的最后一位开始,先把第一个不为‘0’的变为‘0’(这里注意:题目说是去掉数字,而我是:把非0数字改为0,并且让k++。效果一样),然后判断是否满足题意,不满足的话继续改非0数字为0......
具体怎么实现请看代码:
代码:
#include<iostream>using namespace std;int sum0(string s,int len) //计算并返回n从最后一位开始‘0’的个数{ int sum=0; for(int i=len-1;i>=0;i--) { if(s[i]=='0') sum++; else break; } return sum;}int main(){ string s; int k; while(cin>>s>>k) { int len=s.size(); if(len==1) //题目说保证有解,说明n的位数为1时,它必定是‘0’,而0能够被任何数整除,所以不用去掉任何数字,输出0 cout<<0<<endl; else if(len<=k) //n的位数<=k,说明只有把n变成0才满足题意,而结果没有前导0,所以输出 长度-1 (即剩下一个0即可) cout<<len-1<<endl; else if(sum0(s,len)>=k) //如果从n的最后一位起,‘0’的个数>=k,说明能够被10^k整除 cout<<0<<endl; else: { int sum=0; while(1) { for(int i=len-1;i>=0;i--) //从最后一位起,先把一个非0的数字变成0(这里注意,题目是说去掉数字,而我非0数字变为0,同时让k++,达到了省时的目的,效果一样) { if(s[i]!='0') { s[i]='0'; sum++; k++; //把非0的数字变为0,另外k++,效果一样 break; } } if(sum0(s,len)>=k) //说明改变后的数字满足题意 { cout<<sum<<endl; break; } if(len==k) //说明只有把这个数字变为0,才能够被10^k整除 { cout<<len-1<<endl; break; } } } } return 0;}
- 【codeforces 779B】Weird Rounding
- codeforces 779B Weird Rounding
- Codeforces 779B-Weird Rounding
- CodeForces 779B Weird Rounding
- codeforces 779B Weird Rounding
- Weird Rounding (Codeforces-779B)
- 【CodeForces 779B】Weird Rounding(模拟)
- Codeforces 778B. Weird Rounding
- cf 779B Weird Rounding
- Codeforces #402 (Div. 2) B. Weird Rounding
- (贪心)Codeforces Round #402 B. Weird Rounding
- CF 779B Weird Rounding 贪心
- B. Weird Rounding
- cf402 B. Weird Rounding
- B. Weird Rounding
- B. Weird Rounding
- B. Weird Rounding
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- 敌兵布阵 【hdu-1166】【树状数组】
- open-falcon 前端安装笔记及问题。
- Android xUtils3源码解析之注解模块
- Java Effective 总结(0)
- 百度地图显示多个点
- Weird Rounding (Codeforces-779B)
- 为独立服务器安装 Grid Infrastructure
- 解决方法 [ubuntu phpmyadmin 错误提示: 缺少 mcrypt 扩展,请检查 php 配置]
- Safe Or Unsafe(哈夫曼树)
- jQuery关于对表单进行验证和验证码的实现
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- WEB前端开发人员须知的常见浏览器兼容问题及解决技巧
- Python for循环之查找下一个记录
- Android xUtils3源码解析之数据库模块