POJ1023 The Fun Number System
题目来源: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的更多相关文章
- 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 ...
- Moduli number system
A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be p ...
- 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 ...
- The Stern-Brocot Number System(排序二进制)
The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree i ...
- 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 ...
- 为什么实数系里不存在最小正数?(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 ...
- lightOJ 1172 Krypton Number System(矩阵+DP)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...
- 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& ...
- UVa 11651 Krypton Number System DP + 矩阵快速幂
题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...
随机推荐
- 将session存入数据库,memcache的方法
//存入数据库 <?phpif(!$con = mysql_connect('localhost','root','123456')){ die('连接数据库失败');}$link = m ...
- Git 部署 Web 网站
/*************************************************************************** * Git 部署 Web 网站 * 说明: * ...
- freeMarker(十六)——FAQ
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.JSP 和 FreeMarker ? 我们比较 FreeMarke ...
- bzoj 4504: K个串 可持久化线段树+堆
题目: Description 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一 个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计一次). 兔子们想 ...
- bzoj 2084: Antisymmetry 回文自动机
题目: Description 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作"反对称"字符串.比如00001111和010101就是反对称的 ...
- pytorch--cpu与gpu load时相互转化
pytorch------cpu与gpu load时相互转化 torch.load(map_location=)学习 将gpu改为cpu时,遇到一个报错:RuntimeError: Attemptin ...
- POJ3784:Running Median
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...
- docker 部署服务时,node(结点)显示no such image
1. 问题描述 ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 9cn5x84lnmga getstartedlab_web.1 ...
- jquery获取设置服务器控件的方法
jquery获取设置服务器控件的方法 获取TextBox,HiddenField,Label的值.例如: <asp:TextBox ID="txtBoxID" runat=& ...
- 问题:oracle ROW_NUMBER()over;结果: ORACLE 中的 ROW_NUMBER() OVER() 分析函数的用法
ORACLE 中的 ROW_NUMBER() OVER() 分析函数的用法 ROW_NUMBER() OVER(partition by col1 order by col2) 表示根据col1分组, ...