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

来源:互联网 发布:山东招宝万金网络 编辑:程序博客网 时间:2024/06/01 03:59

The Moronic Cowmpouter
Time Limit: 1000MS Memory Limit: 65536KTotal 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: 10000KTotal 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

862 2 abcdefghiz10 16 123456789012345678901234567890123456789016 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 23 333YMHOUE8JPLT7OX6K9FYCQ8A23 49 946B9AA02MI37E3D3MMJ4G7BL2F0549 61 1VbDkSIMJL3JjRgAdlUfcaWj61 5 dl9MDSWqwHjDnToKcsWE1S5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz2 1101110000010001011111001001011001111100100110001101001000110 123456789012345678901234567890123456789016 3A0C92075C0DBF3B8ACBC5F96CE3F0AD216 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 333YMHOUE8JPLT7OX6K9FYCQ8A35 333YMHOUE8JPLT7OX6K9FYCQ8A23 946B9AA02MI37E3D3MMJ4G7BL2F0523 946B9AA02MI37E3D3MMJ4G7BL2F0549 1VbDkSIMJL3JjRgAdlUfcaWj49 1VbDkSIMJL3JjRgAdlUfcaWj61 dl9MDSWqwHjDnToKcsWE1S61 dl9MDSWqwHjDnToKcsWE1S5 421044444410014144012213024022012333403111042120221330305 4210444444100141440122130240220123334031110421202213303010 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;elsec[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;elseb[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: 10000KTotal 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

862 2 abcdefghiz10 16 123456789012345678901234567890123456789016 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 23 333YMHOUE8JPLT7OX6K9FYCQ8A23 49 946B9AA02MI37E3D3MMJ4G7BL2F0549 61 1VbDkSIMJL3JjRgAdlUfcaWj61 5 dl9MDSWqwHjDnToKcsWE1S5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz2 1101110000010001011111001001011001111100100110001101001000110 123456789012345678901234567890123456789016 3A0C92075C0DBF3B8ACBC5F96CE3F0AD216 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 333YMHOUE8JPLT7OX6K9FYCQ8A35 333YMHOUE8JPLT7OX6K9FYCQ8A23 946B9AA02MI37E3D3MMJ4G7BL2F0523 946B9AA02MI37E3D3MMJ4G7BL2F0549 1VbDkSIMJL3JjRgAdlUfcaWj49 1VbDkSIMJL3JjRgAdlUfcaWj61 dl9MDSWqwHjDnToKcsWE1S61 dl9MDSWqwHjDnToKcsWE1S5 421044444410014144012213024022012333403111042120221330305 4210444444100141440122130240220123334031110421202213303010 1234567890123456789012345678901234567890
0 0
原创粉丝点击