unsigned int fcountnew(unsigned int num)
{
unsigned int count=0,low=0,high=0,temp,times=0,time=0;
temp=num;
int i=1;
while(temp!=0)//任何一位上的1的个数由高位和低位组成,如果低位大于2*倍数 则为倍数个数,否则。。高位个数则为高位*倍数
{
temp/=10;
times=(int)pow(double(10),i);
time=(int)pow(double(10),i-1);
low=num%times;
high=num/times;
if(low<10)//这里是个位的特殊情况
{
if(low>=2*time)
count=count+high*time+1;
else
count=count+high*time;
}
else
{
if(low>= 2*time)
count=count+high*time+time;
else
count=count+high*time+(low%time)+1;
}
i++;
}
return count;
}
1到正整数N中所有整数中1的个数
最新推荐文章于 2023-05-01 18:01:04 发布