进制转换(水题)

进制转换

时间限制: 1 Sec  内存限制: 128 MB
提交: 66  解决: 27
[提交][状态][讨论版]

题目描述

请你编一程序实现两种不同进制之间的数据转换。

输入

输 入数据共有三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用大写字母A~F表示数码 10~15,并且该n进制数对应的十进制的值不超过1000000000,第三行也是一个正整数,表示转换之后的数的进制m(2≤m≤16)。

输出

输出仅一行,包含一个正整数,表示转换之后的m进制数。

样例输入

16
FF
2

样例输出

11111111
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=805;
const int M=15005;
ll sum=0;
int n,m;
char s[M];
int change(char ch)
{
    if(ch>='0'&&ch<='9')return (ch-'0');
    else return (ch-55);
}
char change2(int x)
{
    if(x>=0&&x<=9)return x+'0';
    else return x+55;
}
int main()
{
   scanf("%d",&n);
   scanf("%s",s);
   scanf("%d",&m);int j=0;
   for(int i=strlen(s)-1;i>=0;i--){
 
    sum+=change(s[i])*pow(n,j);j++;
   }
   char ans[M];
   int cnt=0;
   //printf("%lld
",sum);
   while(1){
      int y=sum%m;
      ans[cnt++]=change2(y);
      sum/=m;
      if(sum==0)break;
   }
   for(int i=cnt-1;i>=0;i--){
    printf("%c",ans[i]);
   }printf("
");
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jianrenfang/p/5751790.html