m进制转十进制
#include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> char a[10000]; using namespace std; int main() { int n,m; int f=0; scanf ("%s%d",a,&m); for (int i=0;i<strlen(a);i++) { f*=m; if (a[i]=='A'||a[i]=='B'||a[i]=='C'||a[i]=='D'||a[i]=='E'||a[i]=='F') { f=f+(a[i]-'A'+10); } else { f=f+(a[i]-'0'); } } printf ("%d",f); return 0; }
十进制转k进制
#include<cstdio> #include<iostream> #include<ctime> char a[1000]; using namespace std; int main() { int y=0,k,n,x; char z='A'; scanf ("%d %d",&n,&x); while (n!=0) { y++; a[y]=n%x; n=n/x; if (a[y]>9) a[y]=z+(a[y]-10); else a[y]=a[y]+'0'; } for (int i=y;i>0;i--) printf ("%c",a[i]); return 0; }