POJ_1023 The Fun Number System解题报告

The Fun Number System
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8173 Accepted: 2640

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

2
3
pnp
6
4
ppnn
10

Sample Output

Impossible
1110

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
算法类型:数论
解题思路:这道题的思路是从最后一位开始判断,如果N是奇数,那么说明最后一位肯定是1。因为只有最后一位才是0次方,才有可能得到唯一的奇数1。这时如果最后那位是+时,就把N-1再除以2,得到前面的k-1位数;如果最后那位是-时,就把N+1再除以2,得到前面的k-1位数。如果N是偶数,可以断定最后一位是0,那么直接除以2得到前面k-1位得到的数。依次循环至剩下0位即可。如果循环完毕N没有等于0,可以判断为impossible,因为可以存在负数,上述考虑都是排除负数的情况,有负数也没关系,判断N%2==-1就行了。
算法实现:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
__int64 N;    
char P[64];
int Er[64];
int d;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&d);
		memset(P,0,sizeof(P));
		memset(Er,0,sizeof(Er));
		getchar();
		gets(P);
		scanf("%I64d",&N);
		int i=d-1;
		for(i;i>=0;i--)
		{
			if(N%2!=0)
			{
				Er[i]=1;
			    if(P[i]=='n')
				{N=N+1;
				}
				else
				{N=N-1;}
			    N=N/2;
			}
			else
			{
				Er[i]=0;
				N=N/2;
			}
		}
		if(N==0)
		{
			for(int j=0;j<d;j++)
				printf("%d",Er[j]);
			printf("\n");
		}
		else
		{
			printf("Impossible\n");
		}
	}
	return 0;
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
主要特色:自由定制的双风格,分桢讨论区,内含GVBBS4.1版升级包<br> 4.2新增功能<br> 1,论坛进站画面增加双风格显示(完整型/简约型)后台基本信息可以订制<br> 2,主题属性增设了积分贴,用户发帖的时候可以自己订制浏览该贴的分数(主要防止论坛太多潜水员)<br> 3,论坛属性之外增设浏览论坛的用户等级(主要防止论坛太多潜水员),这个可以在论坛后台编辑版面的时候设定。<br> 4,论坛分版内增加了本版在线名单,后台可以订制默认是否打开。<br> 5,论坛前台增加了斑竹禁言功能,后台可以订制是否开发此功能!<br> 6,论坛分类,论坛版面后台新增默认风格属性,自由定制用户首次进入的显示风格。<br> 7,去掉了首次进入本站的cookies支持提示,但是用户登陆仍然需要cookies支持才能登陆。<br> 8,用户等级新增多个属性,管理员定制更加灵活。 <br> 9,后台两级管理员,不限量增加,总管理员可在asp文件内定普通管理员权限<br> 10,此外修复一些Bug<br> 4.3新增功能:<br> 1,社区增加个人媒体库,这样大家既可以灵活的上传、 删除自己的文件,又能节约社区有限的空间。<br> 当您的媒体库满了,你可以自行删除老的文件释放空间,也可以向管理员申请给自己大一些上传空间。<br> 2,后台用户管理增加复杂查询,由于功能强大,查询结构复杂请管理员使用仔细看旁边的注释。<br> 3,后台短信管理增加复杂查询,由于功能强大,查询结构复杂请管理员使用仔细看旁边的注释。<br> 4,后台配色模版新增一个色彩变量,就是主题文章背景色。<br> 5,前台配色我们变动了几个变量位置,这样能更加灵活的发挥您的想象力,使配色方案变化更多,新增了三个配色方案<br> 6,新增帮助文件。<br> 7,修复n多bug<br> 4.3.1新增功能:<br> 1,增加页面预载动画<br> 2,修复前台管理权限混乱<br> 3,修复findpass.asp页面错误处理请求<br> 4,过滤不安全字符<br> 5,修复编辑状态上传无法关闭问题<br> 6,过滤大容量写入or编辑操作<br> 7,论坛用户权限设定(后台)<br> 8,修复其它n个bug<br> 默认管理员 gamvan 默认登陆密码 gamvan

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值