POJ1023 The Fun Number System

简介:
#include<iostream>
#include <string>
using namespace std;

string str;;
__int64 N;
int k;
int result[100];
int index = 0;

int main()
{
    int t,i;
    cin>>t;
    while(t--)
    {
        cin >> k;
        cin >> str;
        cin >> N;
        for(i = k-1;i >= 0; --i)
        {
            if((N%2==1) || (N%2 == -1))
            {//奇数
                
                if(str[i] == 'p')
                {//减1
                    N-=1;
                    N/=2;
                }
                else if (str[i] == 'n')
                {//加1
                    N+=1;
                    N/=2;
                }
                result[index++] = 1;
            }
            else
            {//偶数
                N/=2;
                result[index++] = 0;
            }
        }
        if(N != 0)
        {
            cout<<"Impossible";
        }
        else
        {
            for(i = k-1; i >= 0; --i)
            {
                cout << result[i];
            }
        }
        cout << endl;
        index = 0;
    }
    return 0;
}
复制代码


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2009/09/13/1565702.html,如需转载请自行联系原作者
目录
相关文章
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
121 1
遍历字符串,String line = xxx for(int i = 0;i<line.length();i++){system.out.println(line.chartAt(i)); 单个
遍历字符串,String line = xxx for(int i = 0;i<line.length();i++){system.out.println(line.chartAt(i)); 单个
VS2005 error C2864 only static const integral data members can be initialized within a class
VS2005 error C2864 only static const integral data members can be initialized within a class
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
初学算法之---pta fun with numbers
初学算法之---pta fun with numbers
|
存储
LeetCode 313. Super Ugly Number
编写一段程序来查找第 n 个超级丑数。 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数。
118 0
LeetCode 313. Super Ugly Number
|
Linux
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
212 0
HDOJ(HDU) 1708 Fibonacci String
HDOJ(HDU) 1708 Fibonacci String
106 0
|
JSON Go 数据格式
解决:interface conversion: interface {} is float64, not int
今天遇到一个小坑,但是自己陷进去好久,说起来有些不好意思,但是感觉还是应该拿出来晒一晒,希望大家别再被类似的问题耽误了。
1131 0
hdu 3336 Count the string
点击打开链接hdu 3336 思路:kmp+next数组的应用 分析: 1 题目要求的是给定一个字符串s,求字符串s的所有的前缀在s的匹配的次数之和mod10007. 2 很明显n1,为什么要从n开始而不是1开始呢,这里因为是要求前缀的匹配数而不是后缀; 4 求sum的时候注意每一步都有可能超过范围,所以就要求一次sum同时取模一次。
861 1