码迷,mamicode.com
首页 > 其他好文 > 详细

计算1的个数

时间:2019-05-19 10:14:01      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:html   mod   views   article   one   sdn   href   long   pow   

__int64 CountOne(__int64 n)
{
    __int64 count =0;
    if (n ==0)
        count =0;
    else if (n >1&& n <10)
    count =1;
    else
    {
        __int64 highest = n;
        __int64 bit =0;
        while (highest >=10)
        {
            highest = highest /10;
            bit++;
        }

        __int64 weight = (__int64)pow(10, bit);
        if (highest ==1)
        {
            count = CountOne(weight -1)+ CountOne(n - weight)+ n - weight +1;
        }
        else
        {
            count = highest * CountOne(weight -1)+ CountOne(n - highest * weight) + weight;
        }
    }
    return count;
}
<pre name="code" class="cpp">publiclong CountOne2(long n)
{
    long count =0;
    long i =1;
    long current =0,after =0,before =0;
    while((n / i) !=0)
    {
        current = (n / i) %10;
        before = n / (i *10);
        after = n - (n / i) * i;
        if (current >1)
            count = count + (before +1) * i;
        elseif (current ==0)
        count = count + before * i;
        elseif(current ==1)
        count = count + before * i + after +1;

        i = i *10;
    }
    return count;

}




计算1的个数

标签:html   mod   views   article   one   sdn   href   long   pow   

原文地址:https://www.cnblogs.com/mqxnongmin/p/10888070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!