正文

pku(1767)2005-09-08 04:49:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ddtme/4506.html

分享到:

#include <iostream>
#include <string>
#include <stack>
using namespace std;

long NextTreeIdentify(string t)
{
    string s,t2,t001("001");
    s = t;
    long res;
    int node,i,posof0,posof001;
    bool end = true;
    for(i=0;i<=s.size()-3;i++)
    {
        t2 = s.substr(i,3);
        if(t2.compare(t001)==0)
        {
            posof001 = i;
            break;
        }
    }
    end = true;
    for(i=posof001+3;i<s.size();i++)
    {
        if(s[i] != '1')
        {
            end = false;
            break;
        }
    }

    if(!end)
    {
        /*
        s.erase(posof001+1,2);
        posof0 = s.find_first_of("0",posof001+1);
        s.insert(posof0+1,"1");
        s.insert(posof0+1,"0");*/
        s.replace(posof001,3,"0");
        posof0 = s.find_first_of("0",posof001+1);
        s.replace(posof0,1,"001");
    }
    else
    {
        s[0] = '0';
        for(i=1;i<s.size();i++)
        {
            if(i%2 == 0)
                s[i] = '1';
            else
                s[i] = '0';
        }
    }
    i = s.size()-1;
    res = 0;
    while(i>=2)
    {
        node = s[i]-'0';
        if(node == 1)
            res +=  node<<i;
        i--;
    }
    return res;
}
void doRun()
{
    long res,n,t;
    int node;
    stack<int> tree;
    string s;
    scanf("%ld",&n);
    if(n == 0||n == 4)
    {
        res = n;
    }
    else
    {
        t = n;
        while(1)
        {
            node = t % 2;
            tree.push(node);
            if (t<=1)break;
            t>>=1;
        }
        if(tree.size()>30)
            return;
        while(!tree.empty())
        {
            s.insert(0,(char*)(tree.top()+'0'));
            tree.pop();
        }
        res = NextTreeIdentify(s);
    }
    printf("%ld\n",res);
}


int main()
{
    //while(1)
    doRun();
    return 0;
}

阅读(4190) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册