工 具 软 件 工具软件 工具软件
题目
大家都知道任何数据、符号在计算机中都是以二进制的形式表示的。小明是某软件公司的工程师,公司应客户的需要,要开发一个小工具程序,用来实现任意进制之间的转换。可以将给定的m进制数x转化为n进制的P(所有数数据均能在长整型范围内处理)。现在也将这个任务交给你,请你来完成这个程序
输入
从文件 t o o l . i n tool.in tool.in中读入数据,文件中只有一行,共有三个数 x , m , n ( 2 < = m , n < = 10 ) x,m,n(2<=m,n<=10) x,m,n(2<=m,n<=10)
输出
将结果输出到 t o o l . o u t tool.out tool.out中,输出文件中只有一个数,那就是转换后的结果 P P P
输入样例
tool.in
48 10 8
输出样例
tool.out
60
解题思路
这题我们直接模拟,然后先把当前的数转换为十进制后再转化即可
程序如下
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int x, m, n, ans, r, sum;
void cc(int xx)
{
if (xx == 0) return;
cc(xx / n);
ans = xx % n;
printf("%d", ans);
}
int c(int xx)
{
r = 1;
while(xx > 0)
{
sum += (xx % 10) * r;
r *= m;
xx /= 10;
}
return sum;
}
int main()
{
freopen("tool.in","r",stdin);
freopen("tool.out","w",stdout);
scanf("%d%d%d",&x,&m,&n);
if(m != 10) x = c(x);
cc(x);
return 0;
}