题目来源: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. Metasploit的使用下篇——漏洞攻击

    一.上文总结 上篇当中主要通过开启的端口对目标主机进行攻击,最后成功做到了连接不过却不能查看到telnet的登陆用户名和密码,目前来看有两个原因:第一.我对于靶机telnet的设置有问题,没有设置相应 ...

  2. 用VBA计算两个日期之间的工作日(去掉周末两天)

    最近公司HR和Finance想算员工的工作天数,想让我帮忙写些VBA,自己从网上找了下代码,自己再改改,以下来自网络. 计算两个日期之间的工作日,用VBA,因量大,最好用数组做 Sub kk() Di ...

  3. 洛谷【P1480】A/B Problem

    题目传送门:https://www.luogu.org/problemnew/show/P1480 高精除低精板子题,灵性地回忆一下小学时期列竖式的草稿纸即可. 时间复杂度:\(O(len)\) 空间 ...

  4. SQL中replace函数

    string sql1 = "select price from dbo.eazy_farm where REPLACE(title,' ','')='" + cainame + ...

  5. css中的特殊居中

    大图居中: 先看一下普通的居中: 代码为: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  6. wpf 样式继承

    当定义的wpf多个样式,其样式内容(属性.触发器等)有较多的重复时,可以考虑将其抽象成父样式,来提升样式代码的可维护性以及减少代码冗余. wpf 进行样式继承时,需要使用style的BasedOn属性 ...

  7. mongodb插入时间

    插入时间: db.test.insert({time:new Date()}) 给mongodb插入日期格式的数据时发现,日期时间相差8个小时,原来存储在mongodb中的时间是标准时间UTC +0: ...

  8. SpringSecurity01 SpringSecurity环境搭建

    版本说明: JDK -> java version "1.8.0_101" MAVEN -> Apache Maven 3.5.0 IDEA -> 2017.2. ...

  9. Struts2 资源配置文件国际化

    Struts2 资源配置文件国际化 Struts2资源文件的命名规范:basename_language_country.properties Struts2国际化如果系统同时存在资源文件.类文件,系 ...

  10. Python短小精悍的Orator基本使用技巧

    基本使用 配置 设置数据库配置参数,创建一个DatabaseManager实例. from orator import DatabaseManager config = { 'mysql': { 'd ...