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

poj 3252 RoundNumber

編輯:C++入門知識

 題意是求把十進制數轉化成二進制數,0的個數大於等於1 的數,給定一個閉區間求出區間的這樣的數有多少個。

 

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
這是一個找規律的模擬數學題,雖然WA了三次不過經過不懈的努力還是AC了。(*^__^*) 嘻嘻……

一定要注意細心,耐心。

代碼:

[cpp] 
<span style="font-family:FangSong_GB2312;font-size:18px;">#include<iostream> 
using namespace std; 
int num[35],sum[35],numa[35],numb[35]; 
int dp[35][35]; 
int main() 

    int a,b,i,j,la,lb,len,suma,sumb,cnt,k; 
    dp[0][0]=1; 
    for(i=1;i<=34;i++){dp[i][0]=1;dp[0][i]=0;} 
    for (i=1;i<34;++i) 
        for (j=1;j<=i;++j) 
            dp[i][j]=dp[i-1][j-1]+dp[i-1][j]; 
    memset(sum,0,sizeof(sum)); 
    for(i=2;i<=34;i++) 
    { 
      for(j=0;j<=i/2-1;j++) 
         sum[i]+=dp[i-1][j]; 
    } 
    while(cin>>a>>b) 
    { 
      la=0; 
      int p=a; 
      while(p) 
      { 
        numa[la++]=p%2; 
        p=p/2; 
      }     
      suma=0; 
      k=0; 
      for(i=0;i<=la-1;i++) suma+=sum[i]; 
      for(i=la-2;i>=0;i--) 
      { 
         if(numa[i]==1) 
         { 
           for(j=(la+1)/2-k-1;j<=i;j++) 
            suma+=dp[i][j]; 
         } 
         else k++; 
      } 
      p=b+1;lb=0; 
      while(p) 
      { 
        numb[lb++]=p%2; 
        p=p/2; 
      } 
      sumb=0;k=0; 
      for(i=0;i<=lb-1;i++) sumb+=sum[i]; 
      for(i=lb-2;i>=0;i--) 
      { 
          if(numb[i]==1) 
          { 
             for(j=(lb+1)/2-k-1;j<=i;j++) 
             { 
              sumb+=dp[i][j]; 
             } 
          } 
          else k++; 
      } 
      cout<<sumb-suma<<endl; 
    } 
    return 0; 

       
       
       
</span> 

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