题目来源: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. dubbo monitor simple 监控原理分析

    监控机制: 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心.监控中心负责统计各服务调用次数,调用时间等. 监控元数据存储目录结构: --dubbo.jetty ...

  2. 树套树Day1线段树套平衡树bzoj3196

    您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)5.查 ...

  3. 第K大子集-LH

    题解:搜索+二分 对于每个数有选与不选两种情况.然后我们先搜前一半的状态,每个数选还是不选. 有2^17种,然后我将每种状态拍一个序先存着.然后我再搜后一半的状态,2^18种. 假设后一半某一种情况的 ...

  4. 使用Sed抽取MySQL安装文档的目录及行号

    sed -nr  -e '/^2.|^shell/=' -e '/^2.|^shell/p' INSTALL-SOURCE |awk '{if (NR%2==1) x=$1; else printf ...

  5. JasperReports项目中的应用

    转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html . 2.业务处理 //返回报表查询结果 List<ReportEl ...

  6. zookeeper相关知识的总结:

    一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...

  7. C++经典题目:约瑟夫环问题

    问题描述: 有n个人围成一圈,顺序排号.从第一个人开始报数(1~3报数),凡报到3的人退出圈子,问最后留下的人原来排在第几号. 分析: 首先由用户输入人数n,然后对这n个人进行编号[因为如果不编号的话 ...

  8. 6.7 使用IDEA导入工程

    打开IDEA->File->new -> Project from existing ..->选择你的工程,导入: 请注意,在130或者40上面的项目并不是最新的,sunny也 ...

  9. location.assign()、location.href、location.replace(url)的不同

    window.location.assign(url) : 加载 URL 指定的新的 HTML 文档. 就相当于一个链接,跳转到指定的url,当前页面会转为新页面内容,可以点击后退返回上一个页面. w ...

  10. Remove all the html Tag in String

    在用Umbraco开发项目的过程中,由于在Umbraco Back office 中有用到 rich text editor, 而它返回的值是HtmlString类型,也就是说是包含Html Tag的 ...