程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3252 Round Numbers

POJ 3252 Round Numbers

編輯:C++入門知識

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.2cto.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)                   {                       j=l1/2;                   }else                   {                       j=l1/2+1;                   }                   for(j=j-1-sum;j<=i; j++)                   {                       if(j>=0)                       {                           s-=f(j,i);                       }                   }               }else               {                   sum++;               }           }           if(l1%2==0)           {               weishu=l1/2;           }else           {               weishu=l1/2+1;           }           if(base[0]==1)           {               if(sum>=weishu)               {                   s-=2;               }else               {                   if((weishu-sum)==1)                   {                       s--;                   }               }           }else           {               if((sum+1)>=weishu)               {                   s--;               }           }      }      printf("%lld\n",s);      return 0;   }   long long int f(int m,int n)   {       int i,j;       long long int res;       double s;       if(m<0||m>n)       {           return 0;       }       if(m==0)       {           return 1;       }       s=1.0;       for(i=n,j=m;i>=n-m+1;i--,j--)       {           s=s*(double)i/(double)j;       }       res=(long long int)(s+0.01);       return res;   }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved