M进制转换成N进制

#include <iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int N = 1010;
char a[N];
char b[N];
int m;
int n;
int num =0;
void transf(char a[],char b[],int m,int n){
    int temp;
    int sum = 0;
    char c;
    int len=strlen(a);
    for(int i=0;i<len;i++){
        if(a[i]>='0'&&a[i]<='9')
            temp = a[i]-'0';
        else
            temp = a[i]-'A'+10;
        sum = sum*m+temp;
    }
    cout<<sum<<endl;
    int i=0;
    while(1){
        temp = sum %n;
        if(temp<=9)
            b[i] = temp+'0';
        else
            b[i] = temp+'A'-10;
        sum/=n;
        if(sum ==0)
            break;
        i++;
    }
}
int main()
{
    gets(a);
    cin>>m;
    cin>>n;
    transf(a,b,m,n);
    int len = strlen(b);
    for(int i=len-1;i>=0;i--)
        cout<<b[i];
    return 0;

}

  

posted @ 2018-08-01 09:12  左岸繁华右岸殇  阅读(614)  评论(0编辑  收藏  举报