题目来源:http://poj.org/problem?id=1023

题目大意:

  有一种有趣的数字系统。类似于我们熟知的二进制,区别是每一位的权重有正有负。(低位至高位编号0->k,第i位的权值为2^i 或-2^i)

输入:第一行给定测试用例数。每个用例的第一行为一个正整数k(1<=k<=64),说明该二进制编码的位数长度。接下来一行是一个长度为k的字符串,仅有两种字母p和n,描述了该种编码格式中每一位的权重为正(p)还是为负(n).第三行为一个正整数,N(-2^23<=N<=2^23)。

输出:对于N若能够用前述的编码方式编码,则输出其编码结果,若不能输出Impossible。


Sample Input

2
3
pnp
6
4
ppnn
10

Sample Output

Impossible
1110

按10进制转化为2进制的方法计算每位为0还是为1,若求完最后一位后,剩下的数字为0,则说明可以按该规则编码成功,否则不能。

 //////////////////////////////////////////////////////////////////////////
// POJ1023 The Fun Number System
// Memory: 304K Time: 0MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <cmath> using namespace std; int main() {
int ncase;
cin >> ncase;
for (int caseNo = ; caseNo <= ncase; ++caseNo) {
int k;
cin >> k;
char s[];
int v[];
for (int i = ; i < k; ++i) {
cin >> s[i];
}
long long n;
cin >> n;
for (int t = ; t < k; ++t) {
long long base = (long long)pow(2.0, t+1.0);
if (n % base == ) {
v[t] = ;
} else {
v[t] = ;
n -= (s[k - - t] == 'p' ? ((long long)pow(2.0, t+0.0)) : - * ((long long)pow(2.0, t+0.0)));
}
}
if (n == ) {
for (int t = k - ; t >= ; --t) {
cout << v[t];
}
cout << endl;
} else {
cout << "Impossible" << endl;
} }
system("pause");
return ;
}

POJ1023 The Fun Number System的更多相关文章

  1. Find n‘th number in a number system with only 3 and 4

    这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...

  2. Moduli number system

    A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be p ...

  3. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  4. The Stern-Brocot Number System(排序二进制)

    The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree i ...

  5. POJ 1023 The Fun Number System

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  6. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

  7. lightOJ 1172 Krypton Number System(矩阵+DP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...

  8. uva 10077 - The Stern-Brocot Number System

    想法: 初始化三個數L=0/1, M=1/1, R=1/0,設輸入的分數為a: 如果a<M,那麼要往左邊走,    R = M;    M = (L分子+M分子)/(L分母+M分母); 如果a& ...

  9. UVa 11651 Krypton Number System DP + 矩阵快速幂

    题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...

随机推荐

  1. OpenCV——旋转模糊 (二)

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  2. 霍夫变换Hough

    http://blog.csdn.net/sudohello/article/details/51335237 霍夫变换Hough 霍夫变换(Hough)是一个非常重要的检测间断点边界形状的方法.它通 ...

  3. Parallel Programming-Task Result && Continuation Task

    本文主要介绍带有返回值的Task和Continuation Task 带返回值的Task Continuation Task ContinueWhenAll即多任务延续 一.带返回值的Task 1.1 ...

  4. BZOJ1657:[USACO2006MAR]Mooo

    浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?id ...

  5. Java的native关键字

    一. 什么是Native Method   简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由非j ...

  6. GrayCode for state machine

    How & Why use Gray Code A gray counter is a binary counter where only one bit changes at a time. ...

  7. MySQL的分页技术总结

    利用子查询示例: SELECT * FROM your_table WHERE id <= (SELECT id FROM your_table ORDER BY id desc LIMIT ( ...

  8. 优化Java堆大小的5个技巧

    本文作者Pierre是一名有10多年经验的高级系统架构师,他的主要专业领域是Java EE.中间件和JVM技术.根据他多年的工作实践经验,他发现许多性能问题都是由Java堆容量不足和调优引起的.下面他 ...

  9. Code:目录

    ylbtech-Code:目录 1.返回顶部 1. https://github.com/   2.返回顶部 1. https://gitee.com 2. 3.返回顶部   4.返回顶部   5.返 ...

  10. 杂项:Webpack

    ylbtech-杂项:Webpack 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地 ...