題目1113:二叉樹

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   sp   for   strong   on   

題目描述:

 

 

 

    如上所示,由正整數1,2,3……組成了一顆特殊二叉樹。我們已知這個二叉樹的最後一個結點是n。現在的問題是,結點m所在的子樹中一共包括多少個結點。

    比如,n = 12,m = 3那麼中的結點13,14,15以及後面的結點都是不存在的,結點m所在子樹中包括的結點有3,6,7,12,因此結點m的所在子樹中共有4個結點。

輸入:

    輸入資料包括多行,每行給出一組測試資料,包括兩個整數m,n (1 <= m <= n <= 1000000000)。最後一組測試資料中包括兩個0,表示輸入的結束,這組資料不用處理。

輸出:

    對於每一組測試資料,輸出一行,該行包含一個整數,給出結點m所在子樹中包括的結點的數目。

範例輸入:
3 120 0
範例輸出:
4

BFS: Memory Limit Exceed
Code:
#include <cstdio>#include <queue> using namespace std; int main(){    int n,m;    queue<int> Q;    while(scanf("%d%d",&n,&m)!=EOF){        if(n==0&&m==0)            break;        while(Q.empty()==false){            Q.pop();        }        Q.push(n);        int ans=0;        while(Q.empty()==false){            int now=Q.front();            Q.pop();            ++ans;            if(now*2<=m)                Q.push(now*2);            if(now*2+1<=m)                Q.push(now*2+1);        }        printf("%d\n",ans);    }    return 0;} /**************************************************************    Problem: 1113    User: lcyvino    Language: C++    Result: Memory Limit Exceed****************************************************************/

 

改用遞迴: Time Limit Exceed

Code:
#include <cstdio> using namespace std; long long countNode(long long x,long long m){    if(x>m)        return 0;    else{        x=x<<1;        return 1+countNode(x,m)+countNode(x+1,m);    }} int main(){    long long n,m;    while(scanf("%lld%lld",&n,&m)!=EOF){        if(n==0&&m==0)            break;        printf("%lld\n",countNode(n,m));    }    return 0;} /**************************************************************    Problem: 1113    User: lcyvino    Language: C++    Result: Time Limit Exceed****************************************************************/

 

只好用公式:     

Code:
#include <cstdio>#include <math.h> using namespace std; int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF){        if(n==0&&m==0)            break;        int depth_n=(int)(log2(n)+1);        int depth_m=(int)(log2(m)+1);        int sum=0;        int num=1;        for(int index=0;index<depth_m-depth_n;++index){            sum+=num;            num*=2;        }        long long leftNode=n,rightNode=n;        for(int index=0;index<depth_m-depth_n;++index){            leftNode=2*leftNode;            rightNode=2*rightNode+1;        }        if(m>=rightNode){            sum+=rightNode-leftNode+1;        }else{            if(m>=leftNode)                sum+=m-leftNode+1;        }        printf("%d\n",sum);    }    return 0;} /**************************************************************    Problem: 1113    User: lcyvino    Language: C++    Result: Accepted    Time:0 ms    Memory:1032 kb****************************************************************/

 

題目1113:二叉樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.