Codeforces Round #402 (Div. 2) B. Weird Rounding
来源:互联网 发布:vivo手机数据恢复软件 编辑:程序博客网 时间:2024/05/20 10:53
题目:
B. Weird Rounding
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 3000 that 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.
Input
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.
Output
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).
Examples
input
30020 3
output
1
input
100 9
output
2
input
10203049 2
output
3
Note
In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number.
**
题意:
给出一串长度为n的数字,问这个数字能否被10^k除尽,不能的话删除这个数字中最少几个数字能使这个数字被除尽。
**
思路:
因为题目保证结果存在
想要被10^k除尽,即统计数字n中0的个数
0的个数少于k==》数字只能删除到只剩下一个0才可能被整除
0的个数大于k==》从后往前统计删除多少个非0数字可以使连续的0的个数和k相等
**
代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char n[12];int main(){ int k; cin>>n; scanf("%d",&k); int i,len; int key=0,zero=0; len = strlen(n); for(i=0;i<len;i++){ if(n[i]=='0'){ zero++; } } if(zero<k){ key=len-1; }else{ zero=0; for(i=len-1;i>=0;i--){ if(zero==k){ break; } if(n[i]=='0'){ zero++; } if(n[i]!='0'){ key++; } } } cout<<key<<endl; return 0;}
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- Codeforces Round #402 (Div. 2)B. Weird Rounding【暴力枚举】
- Codeforces Round #402 (Div. 2) B. Weird Rounding(爆搜)
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- Codeforces #402 (Div. 2) B. Weird Rounding
- (贪心)Codeforces Round #402 B. Weird Rounding
- 【codeforces 779B】Weird Rounding
- codeforces 779B Weird Rounding
- Codeforces 779B-Weird Rounding
- Codeforces 778B. Weird Rounding
- CodeForces 779B Weird Rounding
- codeforces 779B Weird Rounding
- Weird Rounding (Codeforces-779B)
- 【Codeforces-402B】-Weird Rounding(数学,思维)
- 【CodeForces 779B】Weird Rounding(模拟)
- Codeforces Round #451 (Div. 2) A. Rounding
- Codeforces Round #407 (Div. 1) B. Weird journey
- Leetcode 24. Swap Nodes in Pairs
- java文件读取的研究
- win10 64位JLink v8固件丢失修复总结
- 按正则表达式拷贝文件
- 《算法(第四版)》笔记(1)
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- 个人笔记:常用 Image 下载地址
- POJ3683 Priest John's Busiest Day(神父约翰的忙日)题解(2-SAT及布尔方程运用)
- 坚持#第143天~努力发展自己的天赋
- linux下配置jdk环境
- 欢迎使用CSDN-markdown编辑器
- Python笔记---setuptools 、feedparser、pydelicious
- 选课时间
- 第十三章:Java_反射机制