若
我們逐位討論,每一次將數字分為
以
最後不要忘記還有
同樣逐位討論,每一次將數字分為
以
// just for 0
ll countDigit(ll n){
if(n<1) return 1;
ll high=n,tp=0,cur=0,low=0,base=1,ret=0;
while(high>0){
high=n/(10*base);
tp=n%(10*base);
cur=tp/base;
low=tp%base;
if(cur==0){
ret+=(high-1)*base+low+1;
}else{
ret+=high*base;
}
base*=10;
}
return ret+1;
}
// only for 1~9
ll countDigit(ll n,ll k){
if(k==0) return countDigit(n);
ll high=n,tp=0,cur=0,low=0,base=1,ret=0;
while(high>0){
high=n/(10*base);
tp=n%(10*base);
cur=tp/base;
low=tp%base;
if(cur==k){
ret+=high*base+low+1;
}else if(cur<k){
ret+=high*base;
}else{
ret+=(high+1)*base;
}
base*=10;
}
return ret;
}
程式設計