1075 数制转换

来源:互联网 发布:莳绘钢笔淘宝 编辑:程序博客网 时间:2024/06/05 16:22
 

描述

设有一个字符串A$的结构为: A$=’m(n)p’ 其中m为数字串(长度<=20),而n,p均为1或2位的数字串(其中所表达的内容在2-10之间)。 程序要求:从键盘上读入A$后(不用正确性检查),将A$中的数字串m(n进制),以p进制的形式输出。 例如:A$=’48(10)8’ 其意义为:将10进制数48,转换成8进制数输出。 输出结果为:48(10)=60(8)

输入

包含多组测试数据 一个字符串A,中间以一个(n)隔开,(n)之前是一个整数m,n表示n进制,(n)之后是一个整数p,表示要将n转换成p进制 每个字符串占一行

输出

每个输出占一行 按以下格式输出 m(n)=q(p) 表示n进制的m和p进制的q相等

样例输入

48(10)8

样例输出

48(10)=60(8)

解题思路:
先把每一位抽取出来,然后按照进制进行还原,反正最后都是按照十进制来做。接下来再按照新的进纸进行位抽取即可。

 

#include <stdio.h>#include <math.h>main(){long long  m;    int n,p;long  long result;int  a[200];int up;int i;long long  t;    long long  xx; while (scanf("%lld(%d)%d",&m,&n,&p)!=EOF) { t=m;    up=0;  //  printf("%d\n",m);   while(m!=0)   {   a[up]=m%10;   up++;   m=m/10;     //  printf("%d\n",a[up-1]);   }   result=0;   xx = 1;   for(i=0;i<up;i++)   {     result=result+xx*a[i];     xx =xx * n;   }  //   printf("%lld\n",result);  //  printf("%d\n",p);   up=0;   while(result!=0)   {   a[up]=result%p;   up++;   result=result/p;   }   result=0;   xx = 1;   for(i=0;i<up;i++)   {   result=result + xx*a[i];   xx = xx * 10;    }   printf("%lld(%d)=%lld(%d)\n",t,n,result,p); }}


 


原创粉丝点击