zoukankan      html  css  js  c++  java
  • AC日记——Weird Rounding Codeforces 779b

    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;
    }
  • 相关阅读:
    Java 位运算(移位、位与、或、异或、非)
    负数的二进制表示方法(正数:原码、负数:补码)
    MacOS X终端里SSH会话管理
    Mac软件分享:上小巧实用的GIF格式录屏软件 LICEcap
    问题追踪:ImageView执行缩放动画ScaleAnimation之后,图像显示不全的问题。
    自定义res/anim加载类,加载自定义Interpolator
    原文翻译 Android_Develop_API Guides_Animation Resources(动画资源)
    OAuth2.0详解
    Grails框架使用指南
    Groovy语言学习汇总
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6556008.html
Copyright © 2011-2022 走看看