AC日记——Weird Rounding Codeforces 779b

本文介绍了一个程序设计问题:给定一个整数n和一个正整数k,需要找到从n中至少删除多少位数字才能使剩余数字能被10^k整除。文章通过递归深度优先搜索的方法解决该问题,并给出完整的C++代码实现。
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, ifk = 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 by10k. 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.

 

 思路:

  dfs~~~~;

 

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int n,k,len,ans=0x7fffffff,p;

char ch[15];

bool if_[15];

bool check()
{
    int bzero=0;
    bool zero=true;
    long long int pos=0;
    for(int i=1;i<=len;i++)
    {
        if(if_[i]) continue;
        if(zero)
        {
            if(ch[i]=='0') bzero++;
            else
            {
                zero=false;
                pos=pos*10+ch[i]-'0';
            }
        }
        else pos=pos*10+ch[i]-'0';
    }
    if(bzero==1&&pos==0) return true;
    if(bzero>1) return false;
    if(pos%p==0) return true;
    return false;
}

void dfs(int step,int ci)
{
    if(step>=ans) return ;
    if(check())
    {
        ans=step;
        return ;
    }
    for(int i=ci+1;i<=len;i++)
    {
        if(!if_[i])
        {
            if_[i]=true;
            dfs(step+1,i);
            if_[i]=false;
        }
    }
}

int main()
{
    cin>>ch+1;
    cin>>k;
    p=1;
    for(int i=1;i<=k;i++) p=p*10;
    len=strlen(ch+1);
    dfs(0,0);
    cout<<ans;
    return 0;
}

 

转载于:https://www.cnblogs.com/IUUUUUUUskyyy/p/6556008.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值