Weird Rounding
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Weird Rounding
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            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).Example
Input 
 30020 3 
 Output 
 1 
 Input 
 100 9 
 Output 
 2 
 Input 
 10203049 2 
 Output 
 3
題目大意
給一個數字n,從中移除一些位上的數,使其能夠被10^k整除,要求移除次數最小,題目保證有解
解題思路
若是n中0的個數小于k,那么就需要把其他數字都移除,只剩下一個0,否則就從后往前掃描,若不是0,++ans,移除,是0,設置一個計數器i來統計0的個數,當0的個數達到k時,此時就剛好可以被10^k整除,輸出ans
代碼
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int Maxn = 1e3;int main() {int ans = 0;char a[11];memset(a, 0, sizeof a);int k;scanf("%s%d",a,&k);int len = strlen(a);int num = 0;for(int i = 0; i < len; ++i)if(a[i] == '0')++num;if(num < k)ans = len - 1;else{int i = 0, j = len - 1;while(i < k){if(a[j] == '0')++i;else++ans;--j; } }printf("%d\n",ans);return 0; }總結
以上是生活随笔為你收集整理的Weird Rounding的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 大兔子和小兔子
 - 下一篇: 视频图像去模糊常用处理方法