题目来源: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. 【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】

    本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  2. 关于对H264码流的PS的封装的相关代码实现

    1.写在开始之前: 最近因为新工作要维护别人留下的GB模块代码,先熟悉了流程,然后也试着封装了下ps流,结果也能通过测试正常预览了,当然,其中开发读文档的头疼,预览花屏,卡帧的事情都有遇到,当时慢慢的 ...

  3. 使用 py2exe 打包 Python 程序

    上回在<使用 PyInstaller 打包 Python 程序>中,我们介绍了使用 PyInstaller 对 Python 程序进行打包,今天带大家认识一个新的工具:py2exe. 接下 ...

  4. jQuery DataTables 使用手册(精简版)

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/5182940.html 前排提醒,这个插件能不用就不用,那么多好的插件等着你,为什么要用它呢?就算用easyui的 ...

  5. 标准模板库(STL)学习指南之map映射

    转载自CSDN博客:http://blog.csdn.net/bat603/article/details/1456141 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关 ...

  6. bzoj 1941 [Sdoi2010]Hide and Seek——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1941 第二道KDtree! 枚举每个点,求出距离它的最远和最近距离.O( n * logn ...

  7. bzoj 2039 & 洛谷 P1791 人员雇佣 —— 二元关系最小割

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2039 https://www.luogu.org/problemnew/show/P1791 ...

  8. 电子商务网站SQL注入项目实战一例(转载)

    故事A段:发现整站SQL对外输出: 有个朋友的网站,由于是外包项目,深圳某公司开发的,某天我帮他检测了一下网站相关情况. 我查看了页面源代码,发现了个惊人的事情,竟然整站打印SQL到Html里,着实吓 ...

  9. Java中读取输入方式的性能比较

    程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值的现成函数!Java没有提供这样的函数也不代表遇到这 ...

  10. 类方法,实例方法,静态方法,@property的应用

    class test(object): h = 'hello' w = 'world' def demo(self): print("demo") def test_class(s ...