cf 779B Weird Rounding
2017-03-06 22:47
363 查看
题目链接:http://codeforces.com/problemset/problem/779/B
B. Weird Rounding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
Output
Input
Output
Input
Output
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.
水题,比赛时没多想就交了,wa了两次,都是情况没想全
代码:
#define _CRT_SBCURE_MO_DEPRECATE
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
const int maxn = 100000000 + 10;
const int INF = 0x3f3f3f3f;
int n;
char a[maxn];
int k;
int main() {
while (scanf("%s %d", a, &k) != EOF) {
int len = strlen(a);
int sum1 = 0, sum2 = 0;//1为0的个数 2为非0个数
bool m = false;
if (k > len) {
for (int i = 0; i < len; i++) {
if (a[i] == '0') m = true;
}
if (m)printf("%d\n", len - 1);
else printf("%d\n", len);
}
else {
for (int i = len - 1; i >= 0; i--) {
if (sum1 < k) {
if (a[i] == '0')sum1++;
else sum2++;
}
}
if(sum1>=k) printf("%d\n", sum2);
else {
if (sum1 > 0) printf("%d\n", len - 1);
else printf("%d\n", len);
}
}
}
return 0;
}
B. Weird Rounding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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.
水题,比赛时没多想就交了,wa了两次,都是情况没想全
代码:
#define _CRT_SBCURE_MO_DEPRECATE
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
const int maxn = 100000000 + 10;
const int INF = 0x3f3f3f3f;
int n;
char a[maxn];
int k;
int main() {
while (scanf("%s %d", a, &k) != EOF) {
int len = strlen(a);
int sum1 = 0, sum2 = 0;//1为0的个数 2为非0个数
bool m = false;
if (k > len) {
for (int i = 0; i < len; i++) {
if (a[i] == '0') m = true;
}
if (m)printf("%d\n", len - 1);
else printf("%d\n", len);
}
else {
for (int i = len - 1; i >= 0; i--) {
if (sum1 < k) {
if (a[i] == '0')sum1++;
else sum2++;
}
}
if(sum1>=k) printf("%d\n", sum2);
else {
if (sum1 > 0) printf("%d\n", len - 1);
else printf("%d\n", len);
}
}
}
return 0;
}
相关文章推荐
- cf664C. International Olympiad
- ★CF--552C
- [__NSCFString ma_MD5String]: unrecognized selector sent to instance 0x7ffeb34aaa80'
- CF 159C String Manipulation 1.0
- cf 675 B
- CF 675A 计算历程点
- CF 4A Watermelon(水??!!)
- Win7下玩CF等游戏不能宽屏的解决办法
- cf 687B Remainders Game (剩余定理)
- 【打CF,学算法——二星级】CodeForces 282C XOR and OR (观察)
- RMQ+二分 - CF 689D Friends and Subsequences
- CF——Mike and Shortcuts(BFS)
- cf 337 D(dfs)
- 协同过滤(CF)算法
- CF-697B Barnicle与691C Exponential notation
- CF 699D Fix a Tree 并查集
- CF 219D. Choosing Capital for Treeland 树形DP
- 第一场cf
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
- CF 245H. Queries for Number of Palindromes 区间dp