poj1220(多种进制转换) poj3191(负数进制)

57 篇文章 0 订阅

The Moronic Cowmpouter
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3767 Accepted: 1962

Description

Inexperienced in the digital arts, the cows tried to build a calculating engine (yes, it's a cowmpouter) using binary numbers (base 2) but instead built one based on base negative 2! They were quite pleased since numbers expressed in base −2 do not have a sign bit. 

You know number bases have place values that start at 1 (base to the 0 power) and proceed right-to-left to base^1, base^2, and so on. In base −2, the place values are 1, −2, 4, −8, 16, −32, ... (reading from right to left). Thus, counting from 1 goes like this: 1, 110, 111, 100, 101, 11010, 11011, 11000, 11001, and so on. 

Eerily, negative numbers are also represented with 1's and 0's but no sign. Consider counting from −1 downward: 11, 10, 1101, 1100, 1111, and so on. 

Please help the cows convert ordinary decimal integers (range -2,000,000,000..2,000,000,000) to their counterpart representation in base −2.

Input

Line 1: A single integer to be converted to base −2

Output

Line 1: A single integer with no leading zeroes that is the input integer converted to base −2. The value 0 is expressed as 0, with exactly one 0.

Sample Input

-13

Sample Output

110111

Hint

Explanation of the sample: 

Reading from right-to-left:
1*1 + 1*-2 + 1*4 + 0*-8 +1*16 + 1*-32 = -13




poj3191  就是是把十进制转换为二进制:找到最小的非负整数x,使a/x==0成立;再就是a/=x;


注意  0



#include<stdio.h>
#include<string.h>

int main()
{
	int a;
	int s[1005];
	int i=0;
	scanf("%d",&a);
		if(a==0)
		{
			printf("0\n");
			return 0;
		}
		memset(s,0,sizeof(s));
		while(a)
		{
			s[i]=a%2;
			if(s[i]<0)
				s[i]=-s[i];
			a-=s[i];
			a/=(-2);
			i++;
		}
		for(int j=i-1;j>=0;j--)
			printf("%d",s[j]);
	return 0;
}








Language:
NUMBER BASE CONVERSION
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5195 Accepted: 2362

Description

Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: 
{ 0-9,A-Z,a-z } 
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number. 

Input

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings). 

Output

The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank. 

Sample Input

8
62 2 abcdefghiz
10 16 1234567890123456789012345678901234567890
16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
49 61 1VbDkSIMJL3JjRgAdlUfcaWj
61 5 dl9MDSWqwHjDnToKcsWE1S
5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz
2 11011100000100010111110010010110011111001001100011010010001

10 1234567890123456789012345678901234567890
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2

16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 333YMHOUE8JPLT7OX6K9FYCQ8A

35 333YMHOUE8JPLT7OX6K9FYCQ8A
23 946B9AA02MI37E3D3MMJ4G7BL2F05

23 946B9AA02MI37E3D3MMJ4G7BL2F05
49 1VbDkSIMJL3JjRgAdlUfcaWj

49 1VbDkSIMJL3JjRgAdlUfcaWj
61 dl9MDSWqwHjDnToKcsWE1S

61 dl9MDSWqwHjDnToKcsWE1S
5 42104444441001414401221302402201233340311104212022133030

5 42104444441001414401221302402201233340311104212022133030
10 1234567890123456789012345678901234567890



多种进制之间的转化:注意事项
1~~ :注意初始化
2~~ :注意字符,而不是数字
3~~:注意是格式调理清晰化
4~~:大数处理就是要逐个细心处理
5~~:不要怕浪费时间,其实是在进步




#include<stdio.h>
#include<string.h>

int n,m;
char a[555],b[555];
int c[555],d[555];
int anss[555];

int main()
{
	int num;
	int ans;
	int pos;
	int i;
	scanf("%d",&num);
	while(num--)
	{

		scanf("%d%d",&n,&m);
		scanf("%s",a);

		if(a[0]=='0')
		{
			printf("%d %s\n%d ",n,a,m);
			char l='0';
			printf("%c",l);
			puts("");
			puts("");
			continue;
		}

		memset(c,'\0',sizeof(c));
		for(i=0;i<strlen(a);i++)
		{
			if(a[i]>='A'&&a[i]<='Z')
				c[i]=a[i]-'A'+10;
			else if(a[i]>='a'&&a[i]<='z')
				c[i]=a[i]-'a'+36;
			else
				c[i]=a[i]-'0';
		}

		pos=0;
		int k=0;
		memset(anss,'0',sizeof(anss));

		while(pos<555)
		{
			ans=0;
			for(k=0;k<i;k++)
			{
				ans=ans*n+c[k];
				c[k]=ans/m;
				ans=ans%m;
			}
			anss[pos++]=ans;
		}
		anss[pos]='\0';

		memset(b,'\0',sizeof(b));
		int poss=0;
		for(int j=0;j<pos;j++)
		{
			if(anss[j]>=10&&anss[j]<=35)
				b[pos-j-1]=anss[j]+'A'-10;
			else if(anss[j]>=36&&anss[j]<=61)
				b[pos-j-1]=anss[j]+'a'-36;
			else
				b[pos-j-1]=anss[j]+'0';
		}
		printf("%d %s\n%d ",n,a,m);
		int f=1;
		for(int p=0;p<pos;p++)
		{
			if(b[p]=='0'&&f)
				continue;
			if(b[p]!='0')
				f=0;
			printf("%c",b[p]);
		}
		puts("");
		puts("");
	}
	return 0;
}


Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
   Register
Language:
NUMBER BASE CONVERSION
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5195 Accepted: 2362

Description

Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: 
{ 0-9,A-Z,a-z } 
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number. 

Input

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings). 

Output

The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank. 

Sample Input

8
62 2 abcdefghiz
10 16 1234567890123456789012345678901234567890
16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
49 61 1VbDkSIMJL3JjRgAdlUfcaWj
61 5 dl9MDSWqwHjDnToKcsWE1S
5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz
2 11011100000100010111110010010110011111001001100011010010001

10 1234567890123456789012345678901234567890
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2

16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 333YMHOUE8JPLT7OX6K9FYCQ8A

35 333YMHOUE8JPLT7OX6K9FYCQ8A
23 946B9AA02MI37E3D3MMJ4G7BL2F05

23 946B9AA02MI37E3D3MMJ4G7BL2F05
49 1VbDkSIMJL3JjRgAdlUfcaWj

49 1VbDkSIMJL3JjRgAdlUfcaWj
61 dl9MDSWqwHjDnToKcsWE1S

61 dl9MDSWqwHjDnToKcsWE1S
5 42104444441001414401221302402201233340311104212022133030

5 42104444441001414401221302402201233340311104212022133030
10 1234567890123456789012345678901234567890
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值