博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
F - The Fun Number System(第二季水)
阅读量:5776 次
发布时间:2019-06-18

本文共 2951 字,大约阅读时间需要 9 分钟。

Description

In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight of the most significant bit (i.e., in position k-1), is -2^(k-1), and the weight of a bit in any position i (0 ≤ i < k-1) is 2^i. For example, a 3 bit number 101 is -2^2 + 0 + 2^0 = -3. A negatively weighted bit is called a negabit (such as the most significant bit in a 2's complement number), and a positively weighted bit is called a posibit.         A Fun number system is a positional binary number system, where each bit can be either a negabit, or a posibit. For example consider a 3-bit fun number system Fun3, where bits in positions 0, and 2 are posibits, and the bit in position 1 is a negabit. (110)Fun3 is evaluated as 2^2-2^1 + 0 = 3. Now you are going to have fun with the Fun number systems! You are given the description of a k-bit Fun number system Funk, and an integer N (possibly negative. You should determine the k bits of a representation of N in Funk, or report that it is not possible to represent the given N in the given Funk. For example, a representation of -1 in the Fun3 number system (defined above), is 011 (evaluated as 0 - 2^1 + 2^0), and representing 6 in Fun3 is impossible.      

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case is given in three consecutive lines. In the first line there is a positive integer k (1 ≤ k ≤ 64). In the second line of a test data there is a string of length k, composed only of letters n, and p, describing the Fun number system for that test data, where each n (p) indicates that the bit in that position is a negabit (posibit).         The third line of each test data contains an integer N (-2^63 ≤ N < 2^63), the number to be represented in the Funk number system by your program.      

Output

For each test data, you should print one line containing either a k-bit string representing the given number N in the Funk number system, or the word Impossible, when it is impossible to represent the given number.      

Sample Input

23pnp64ppnn10

Sample Output

Impossible1110 这个题~  我这么笨 一定想不到简单的方法!!!  只有各种复杂办法  基本是不可行的 所以 必然需要求助!! 思路 1.n为奇数    最后一位一定是1,因为只有2的0次方可产生奇数,其他都为偶数。        最后一位若为p(正)  则n=(n-1)/2               若为n(负)  则n=(n+1)/2    n为偶数   最后一位一定是0,n=n/2   2.接下来判断倒数第二位  将其重复步骤1,直至判断完这个字符串系统   最后n若为0  则可表示出   n不为0  则结果为impossible 代码是这样
#include
#include
using namespace std;int main() { int t,k; cin>>t; while(t--){ __int64 n; string str; cin>>k>>str>>n; int j=0; int *p=new int[k]; for(int i=k-1;i>=0;i--){ if(n%2==1||n%2==-1){ if(str[i]=='p')n=(n-1)/2; else n=(n+1)/2; p[j++]=1; } else { n/=2; p[j++]=0; } } if(n)cout<<"Impossible"<
=0;i--)cout<

 

 

转载地址:http://mehux.baihongyu.com/

你可能感兴趣的文章
MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)
查看>>
【leetcode】Longest Common Prefix
查看>>
前端优化及相关要点总结
查看>>
Vue 列表渲染
查看>>
struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)
查看>>
25 个精美的手机网站模板
查看>>
C#反射实例应用--------获取程序集信息和通过类名创建类实例
查看>>
VC中实现文字竖排的简单方法
查看>>
会话标识未更新
查看>>
【设计模式】数据访问对象模式
查看>>
Tomcat8 配置Oracle11g数据源
查看>>
【PHP面向对象(OOP)编程入门教程】8.构造方法__construct()与析构方法__destruct()
查看>>
ThinkPHP常用配置路径
查看>>
阿里架构师:程序员必须掌握的几项核心技术能力
查看>>
程序员常用的六大技术博客类
查看>>
vue中动画的实现的几种方式
查看>>
Iceworks 2.8.0 发布,自定义你的 React 模板
查看>>
胖哥学SpringMVC:请求方式转换过滤器配置
查看>>
Kotlin 更加优雅的 Builder - 理解 with
查看>>
前端日拱一卒D6——字符编码与浏览器解析
查看>>