zoukankan      html  css  js  c++  java
  • POJ 1023

    #include <iostream>
    #include <string>
    #include <cmath>
    #define MAXN 65
    int op[MAXN];
    int ans[MAXN];
    using namespace std;
    int k;
    void get_ans();
    long long num;
    int main()
    {
        //freopen("acm.acm","r",stdin);
        int test;
        string s;
        int i;
        cin>>test;
        while(test --)
        {
            cin>>k;
            cin>>s;
            int j = 0;
            for(i = s.length()-1; i >= 0; -- i)
            {
                if(s[i] == 'n')
                {
                    op[j ++] = -1;
                }
                else
                {
                    op[j ++] = 1;
                }
            }
            cin>>num;
            get_ans();
        }
    }
    
    void get_ans()
    {
        int i;
        int is_pos;
        if(num > 0)
        {
            is_pos = 1;
        }
        else
        {
            is_pos = -1;
        }
        memset(ans,0,sizeof(ans));
        for(i = 0; i < k; ++ i)
        {
            if(num == 0)
            {
                break;
            }
            if(num%2 ==0)
            {
                ans[i] = 0;
                num /= 2;
            }
            else
            {
                if(op[i]*is_pos < 0)
                {
                    ans[i] = 1;
                    if(num > 0)
                    {
                        num /= 2;
                        num += 1;
                    }
                    else
                    {
                        num /= 2;
                        num -= 1;
                    }
                }
                else
                {
                    ans[i] = 1;
                    num /= 2;
                }
            }
        }
        if(num == 0)
        {
            for(i = k-1;i >= 0; -- i)
            {
                cout<<ans[i];
            }
            cout<<endl;
        }
        else
        {
            cout<<"Impossible"<<endl;
        }
    }

    关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。 

    技术网站地址: vmfor.com

  • 相关阅读:
    1004. Counting Leaves (30)
    51Nod 1272 最大距离 (栈或贪心)
    D
    M
    N
    F
    E
    L
    A. Office Keys ( Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) )
    K
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4563219.html
Copyright © 2011-2022 走看看