Topic
Topic Description:
Converts the number X of M into the number output of N.
Input:
The first line entered consists of two integers: M and N (2<=m,n<=36).
The following line enters a number of x,x that is M, and now requires that you convert the M-ary number x into N-ary output.
Output:
The number of n-ary representations of the output x.
Sample input:
16 10
F
Sample output:
15
Tips:
When entered, the letter portion is uppercase, the output is lowercase, and there is large data.
Ideas
Large integer multiplication into a 10-binary array
Large integer division into a specified number
AC Code
#include <stdio.h> #include <stdlib.h> #include <string.h> #define LEN Cha
R Str[len], Another[len];
int Ten[len];
int Switchtoten ();
void Switchtoanother (int k, int n);
int main () {int m, n, K;
while (scanf ("%d%d", &m, &n)!= EOF) {scanf ("%s", str);
k = Switchtoten (m);
Switchtoanother (k, n);
return 0;
int Switchtoten (int m) {int I, J, Len, K, C;
Initialize len = strlen (str);
K = 1;
memset (ten, 0, sizeof (ten));
Convert to 10 number for (i = 0; i < len i + +) {for (j = 0; J < K; J + +) {ten[j] *= m;
} if (Str[i] >= ' 0 ' && str[i] <= ' 9 ') {ten[0] + = str[i]-' 0 ';
}else if (Str[i] >= ' A ' && str[i] <= ' Z ') {ten[0] + = str[i]-' a ' + 10;
}else if (Str[i] >= ' A ' && str[i] <= ' z ') { Ten[0] + = str[i]-' a ' + 10;
for (j = c = 0; J < K; J + +) {Ten[j] + = C;
if (Ten[j] >=) {c = ten[j]/10;
TEN[J]%= 10;
}else {c = 0;
} while (c) {ten[k + +] = c% 10;
C/= 10;
}//flip array int temp;
for (i = 0, j = k-1 I < J; i + +, J-) {temp = Ten[i];
Ten[i] = Ten[j];
TEN[J] = temp;
} return k;
} void Switchtoanother (int k, int n) {int sum, I, R, T, D;
sum = 1;
R = 0;
memset (another, 0, sizeof (another));
while (sum) {sum = 0;
for (i = 0; i < K; i + +) {d = ten[i]/n;
sum = D;
if (i = = k-1) {t = ten[i]% n;
if (t >= 0 && T <= 9) {Another[r] = t + ' 0 '; }else {Another[r] = t-10 + 'A ';
} R + +;
}else {ten[i + 1] = = Ten[i]% n * 10;
} Ten[i] = D;
}//print is output for (i = r-1 i >= 0; I-) {printf ("%c", Another[i]);
printf ("\ n");
}
/**************************************************************
problem:1080
User:wangzhengyi
& nbsp; language:c
result:accepted
time:170 Ms
memory:920 kb
****************************************************************/
The following is attached a binary, octal, decimal, hexadecimal conversion of the C language Program (a bit long, well, look at the function n in the same portion of XD) ~
#include <stdio.h> #include <string.h> #include <stdlib.h> void d_b (int);
void D_o (int);
void d_x (int);
void b_d (int);
void b_o (int);
void b_x (int);
void O_b (int);
void o_d (int);
void o_x (int);
void X_b (char r[],int k);
void X_d (char r[],int k);
void X_o (char r[],int k);
void Main () {int i,j,k=0;
int q;
Char r[10];
printf ("+=============================================================+\n"); printf ("|
Welcome to use the |\n conversion program ");
printf ("+=============================================================+\n"); printf ("Note: This version only makes positive integer conversion!!")
");
do {q=0;
Fflush (stdin);
printf ("\ n Select the system you want to convert: \n0, exit \n1, binary \n2, octal \n3, decimal \n4, hexadecimal \ n");
scanf ("%d", &i);
switch (i) {case 1:printf ("\ n Select a converted system: \n0, Exit \n1, binary \n2, octal \n3, decimal \n4, hexadecimal \ n");
scanf ("%d", &j); Switch (j) {case 1:printf ("\ n) no conversion between the same system!"
\ n ");
Q=1;
Break
Case 2:printf ("Please enter the number you want to convert:");
scanf ("%d", &k);
B_o (k);
Q=1;
Break Case 3:printf ("\ nplease Enter the number you want to convert: ");
scanf ("%d", &k);
B_d (k);
Q=1;
Break
Case 4:printf ("Please enter the number you want to convert:");
scanf ("%d", &k);
B_x (k);
Q=1;
Break Case 0:printf ("Thank you for using!!")
");
} break;
Case 2:printf ("\ n Select the converted system: \n0, Exit \n1, binary \n2, octal \n3, decimal \n4, hexadecimal \ n");
scanf ("%d", &j); Switch (j) {case 2:printf ("\ n) no conversion between the same system!"
\ n ");
Q=1;
Break
Case 1:printf ("Please enter the number you want to convert:");
scanf ("%d", &k);
O_b (k);
Q=1;
Break
Case 3:printf ("Please enter the number you want to convert:");
scanf ("%d", &k);
O_d (k);
Q=1;
Break
Case 4:printf ("Please enter the number you want to convert:");
scanf ("%d", &k);
O_x (k);
Q=1;
Break Case 0:printf ("Thank you for using!!")
");
} break;
Case 3:printf ("\ n Select the converted system: \n0, Exit \n1, binary \n2, octal \n3, decimal \n4, hexadecimal \ n");
scanf ("%d", &j); Switch (j) {case 3:printf ("\ n) no conversion between the same system!"
\ n ");
Q=1;
Break
Case 1:printf ("Please enter the number you want to convert:");
scanf ("%d", &k);
D_b (k);
Q=1;
Break Case 2:printf("Please enter the number you want to convert:");
scanf ("%d", &k);
D_o (k);
Q=1;
Break
Case 4:printf ("Please enter the number you want to convert:");
scanf ("%d", &k);
D_x (k);
Q=1;
Break Case 0:printf ("Thank you for using!!")
");
} break;
Case 4:printf ("\ n Select the converted system: \n0, Exit \n1, binary \n2, octal \n3, decimal \n4, hexadecimal \ n");
scanf ("%d", &j); Switch (j) {case 4:printf ("\ n) no conversion between the same system!"
\ n ");
Q=1;
Break
Case 1:printf ("Please enter the number you want to convert:");
Fflush (stdin);
Gets (R); for (k=0;;
k++) {if (r[k]== ') break;
} x_b (R,k);
Q=1;
Break
Case 2:printf ("Please enter the number you want to convert:");
Fflush (stdin);
Gets (R); for (k=0;;
k++) {if (r[k]== ') break;
} x_o (R,k);
Q=1;
Break
Case 3:printf ("Please enter the number you want to convert:");
Fflush (stdin);
Gets (R); for (k=0;;
k++) {if (r[k]== ') break;
} x_d (R,k);
Q=1;
Break Case 0:printf ("Thank you for using!!")
");
} break; Case 0:printf ("\ n Thank you for using it!")
\ n ");
}}while (Q==1);
}The following are: binary conversion to decimal, octal, hexadecimal.
void B_d (int a) {int i,s=0;
int result=0;
for (i=1;a!=0;i*=2) {if (a%10>1) {S=1;
Break
else {result+= (a%10) *i;
A=A/10; } if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else printf ("\ n converted number:%d\n", result);
} void B_o (int a) {int i,j,k,s=0;
int p[30];
int result=0;
for (i=1;a!=0;i*=2) {if (a%10>1) {S=1;
Break
else {result+= (a%10) *i;
A=A/10;
} for (j=0;result!=0;j++) {p[j]=result%8;
RESULT=RESULT/8; if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else {printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%d", p[k]);
printf ("\ n");
} void b_x (int a) {int i,j,k,s=0;
Char p[30];
int result=0;
for (i=1;a!=0;i*=2) {if (a%10>1) {S=1;
Break
else {result+= (a%10) *i;
A=A/10;
} for (j=0;result!=0;j++) {p[j]=result%16;
RESULT=RESULT/16;
if (p[j]>10) {switch (P[j]) {case 10:p[j]= ' A ';
Break
Case 11:p[j]= ' B ';
Break
Case 12:p[j]= ' C '; Break;
Case 13:p[j]= ' D ';
Break
Case 14:p[j]= ' E ';
Break
Case 15:p[j]= ' F ';
Break
} else p[j]+=48; if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else {printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%c", P[k]);
printf ("\ n");
The following///////are: octal is converted to binary, decimal, hexadecimal.
void O_b (int a) {int i,j,k,s=0;
int result=0;
int p[30];
for (i=1;a!=0;i*=8) {if (a%10>7) {S=1;
Break
else {result+= (a%10) *i;
A=A/10;
} for (j=0;result!=0;j++) {p[j]=result%2;
RESULT=RESULT/2; if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else {printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%d", p[k]);
printf ("\ n");
} void O_d (int a) {int i,s=0;
int result=0;
for (i=1;a!=0;i*=8) {if (a%10>7) {S=1;
Break
else {result+= (a%10) *i;
A=A/10; } if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else {printf ("\ n converted number is:%d\n", result);
} void o_x (int a) {int i,j,k,s=0;
Char p[30];
int result=0; for (i=1;a!=0;i*=8) {if (a%10>7) {S=1;
Break
else {result+= (a%10) *i;
A=A/10;
} for (j=0;result!=0;j++) {p[j]=result%16;
RESULT=RESULT/16;
if (p[j]<10) p[j]+=48;
else {switch (P[j]) {case 10:p[j]= ' A ';
Break
Case 11:p[j]= ' B ';
Break
Case 12:p[j]= ' C ';
Break
Case 13:p[j]= ' D ';
Break
Case 14:p[j]= ' E ';
Break
Case 15:p[j]= ' F ';
Break }} if (s==1) printf ("Your input is incorrect!")
Please re-enter \ n ");
else {printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%c", P[k]);
printf ("\ n");
The following///////are: 16 Convert to decimal, binary, octal.
void X_d (char a[],int k) {int i,j,s=0;
int result=0;
int B[50];
for (i=0;i<k;i++) {if (a[i]<= ' 9 ' &&a[i]>= ' 1 ') {b[i]=a[i]-48;
else {switch (A[i]) {case ' a ': b[i]=10;
Break
Case ' B ': b[i]=11;
Break
Case ' C ': b[i]=12;
Break
Case ' D ': b[i]=13;
Break
Case ' E ': b[i]=14;
Break
Case ' F ': b[i]=15;
Break
Case ' a ': b[i]=10;
Break
Case ' B ': b[i]=11; Break;
Case ' C ': b[i]=12;
Break
Case ' d ': b[i]=13;
Break
Case ' E ': b[i]=14;
Break
Case ' F ': b[i]=15;
Break
Default:s=1;
{Result+=b[j]*i for (i=1,j=k-1;j>=0;j--, i*=16)}}} if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else {printf ("\ n Convert number is:%d", result);
} void X_b (char a[],int k) {int i,j,s=0;
int result=0;
int B[50];
int p[30];
for (i=0;i<k;i++) {if (a[i]<= ' 9 ' &&a[i]>= ' 1 ') b[i]=a[i]-48;
else {switch (A[i]) {case ' a ': b[i]=10;
Break
Case ' B ': b[i]=11;
Break
Case ' C ': b[i]=12;
Break
Case ' D ': b[i]=13;
Break
Case ' E ': b[i]=14;
Break
Case ' F ': b[i]=15;
Break
Case ' a ': b[i]=10;
Break
Case ' B ': b[i]=11;
Break
Case ' C ': b[i]=12;
Break
Case ' d ': b[i]=13;
Break
Case ' E ': b[i]=14;
Break
Case ' F ': b[i]=15;
Break
Default:s=1;
{Result+=b[j]*i for (j=k-1,i=1;j>=0;j--, i*=16)}}} for (j=0;result!=0;j++) {P[j]=result%2
RESULT=RESULT/2; if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else {printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%d", p[k]);
printf ("\ n");
} void X_o (char a[],int k) {int i,j,s=0;
int result=0;
int b[30];
int p[30];
for (i=0;i<k;i++) {if (a[i]<= ' 9 ' &&a[i]>= ' 1 ') b[i]=a[i]-48;
else {switch (A[i]) {case ' a ': b[i]=10;
Break
Case ' B ': b[i]=11;
Break
Case ' C ': b[i]=12;
Break
Case ' D ': b[i]=13;
Break
Case ' E ': b[i]=14;
Break
Case ' F ': b[i]=15;
Break
Case ' a ': b[i]=10;
Break
Case ' B ': b[i]=11;
Break
Case ' C ': b[i]=12;
Break
Case ' d ': b[i]=13;
Break
Case ' E ': b[i]=14;
Break
Case ' F ': b[i]=15;
Break
Default:s=1;
{Result+=b[j]*i for (j=k-1,i=1;j>=0;j--, i*=16)}}}
for (j=0;result!=0;j++) {p[j]=result%8;
RESULT=RESULT/8; if (s==1) printf ("Your input is wrong!")
Please re-enter \ n ");
else {printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%d", p[k]);printf ("\ n");
The following///////are: decimal conversion to binary, octal, hexadecimal.
void d_b (int a) {int j,k;
int p[30];
for (j=0;a!=0;j++) {p[j]=a%2;
A=A/2;
printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%d", p[k]);
printf ("\ n");
} void D_o (int a) {int j,k;
int p[30];
for (j=0;a!=0;j++) {p[j]=a%8;
A=A/8;
printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%d", p[k]);
printf ("\ n");
} void d_x (int a) {int j,k;
int p[30];
for (j=0;a!=0;j++) {p[j]=a%16;
A=A/16;
if (p[j]<10) p[j]+=48;
else {switch (P[j]) {case 10:p[j]= ' A ';
Break
Case 11:p[j]= ' B ';
Break
Case 12:p[j]= ' C ';
Break
Case 13:p[j]= ' D ';
Break
Case 14:p[j]= ' E ';
Break
Case 15:p[j]= ' F ';
Break
} printf ("\ n converted number is:");
for (k=j-1;k>=0;k--) {printf ("%c", P[k]);
printf ("\ n");
}