#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll count(ll n,ll x){//找1-n中x的个数,时间复杂度log(10,n)
ll cnt=0,k;
for (ll i = 1; k = n/i;i*=10){
ll high=k/10;
if(x==0){
if(high) high--;
else break;
}
cnt+=high*i;
ll cur=k%10;
if(cur>x) cnt+=i;
else if(cur==x) cnt+=n-k*i+1;
}
return cnt;
}
int main(){
return 0;
}
本文介绍了一种使用C++实现的高效算法,用于计算1到n之间特定数字x出现的次数,通过log(10,n)的时间复杂度优化了传统方法。该算法适用于大数据量的快速处理,为数字统计提供了高效的解决方案。

被折叠的 条评论
为什么被折叠?



