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

HDU 4588 Count The Carries(找规律,模拟)

时间:2014-07-08 22:58:23      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   art   

题目

 

大意:

求二进制的a加到b的进位数。

思路:

列出前几个2进制,找规律模拟。

 

 

bubuko.com,布布扣
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <vector>

using namespace std;

int main()
{
    int a,b,id;
    __int64 ans,temp,len[63],aa,bb,cc,cc1,bb1;
    len[0]=1;
    for(int i=1;i<63;i++)
        len[i]=len[i-1]*2;
    while(scanf("%d%d",&a,&b)!=EOF)
    {        
        id=0;
        b++;a++;
        ans=0;
        temp=1;
        while(temp)
        {
            temp=temp/2;
            ans+=temp;
            id++;
            aa= b/len[id]*len[id-1] - a/len[id]*len[id-1];
            bb=b%len[id], bb1 = a%len[id];
            cc=len[id-1], cc1 = len[id-1];
            if(bb<cc) bb=0,cc=0;
            if(bb1<cc1) bb1=0,cc1=0;
            temp=temp+aa+bb-cc-bb1+cc1;//原来是这里出了岔子,,,,唉
            if(  (a-1 ) & len[id-1] )
                temp++;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
View Code

 

HDU 4588 Count The Carries(找规律,模拟),布布扣,bubuko.com

HDU 4588 Count The Carries(找规律,模拟)

标签:style   blog   http   color   os   art   

原文地址:http://www.cnblogs.com/laiba2004/p/3830451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!