当前位置:编程学习 > C/C++ >>

POJ 3252 Round Numbers

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6553 Accepted: 2230
Description
 
The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets to be milked first. They can't even flip a coin because it's so hard to toss using hooves.
 
They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,
otherwise the second cow wins.
 
A positive integer N is said to be a "round number" if the binary representation of N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones, it is not a round number.
 
Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.
 
Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤ Start < Finish ≤ 2,000,000,000).
 
Input
 
Line 1: Two space-separated integers, respectively Start and Finish.
Output
 
Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish
Sample Input
 
2 12
Sample Output
 
6
Source
 
USACO 2006 November Silver
这题让我说什么好呢,处理不好会有很多bug的,我查了一早上的代码,在吃饭回来的路上突然想到我早n种选0种的时候没有算,其实那是该算的,回来改了改给A了,哎,真是不想做这种题啊
 
没有a的话,测一下数据吧。
[cpp] 
#include <stdio.h>  
#include <string.h>  
#include <math.h>  
int base[50],top[50];  
int main()  
{  
    long long int f(int m,int n);  
    int i,j,l1,l2,sum,weishu,t;  
    long long int n,m,s;  
    scanf("%lld %lld",&n,&m);  
    l1=l2=0;  
    while(n!=0)  
    {  
        base[l1++]=n%2;  
        n=n/2;  
    }  
    while(m!=0)  
    {  
        top[l2++]=m%2;  
        m=m/2;  
    }  
    s=0;  
    for(i=l1;i<=l2-1;i++)  
    {  
        if(i%2==0)  
        {  
            j=i/2;  
        }else if(i%2!=0)  
        {  
            j=i/2+1;  
        }  
        for(;j<=i-1;j++)  
        {  www.zzzyk.com
            s+=f(j,i-1);  
        }  
    }  
    for(i=l2-2,sum=0;i>=1;i--)  
    {  
        if(top[i]==1)  
        {  
            if(l2%2==0)  
            {  
                j=l2/2;  
            }else  
            {  
                j=l2/2+1;  
            }  
            for(j=j-1-sum;j<=i; j++)  
            {  
                if(j>=0)  
                {  
                    s+=f(j,i);  
                }  
            }  
        }else  
        {  
            sum++;  
        }  
    }  
    if(l2%2==0)  
    {  
        weishu=l2/2;  
    }else  
    {  
        weishu=l2/2+1;  
    }  
    if(top[0]==1)  
    {  
        if(sum>=weishu)  
        {  
            s+=2;  
        }else  
        {  
            if((weishu-sum)==1)  
            {  
                s++;  
            }  
        }  
    }else  
    {  
        if((sum+1)>=weishu)  
        {  
            s++;  
        }  
    }  
   base[0]-=1; i=0;  
   while(base[i]<0)  
   {  
       base[i]+=2;  
       base[i+1]-=1;  
       i++;  
   }  
   t=l1;  
   while(base[l1-1]==0)  
   {  
       l1-=1;  
   }  
   if(l1==t)  
   {  
        for(i=l1-2,sum=0;i>=1;i--)  
        {  
            if(base[i]==1)  
            {  
                if(l1%2==0)  
                { &
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,