问题 A: 又一版 A+B
时间限制: 1 Sec 内存限制: 32 MB
提交: 1691 解决: 456
[提交][状态][讨论版][命题人:外部导入]
题目描述
输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。
输入
输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。
输出
输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
样例输入
<span style="color:#333333">2 4 5
8 123 456
0</span>
样例输出
<span style="color:#333333">1001
1103</span>
提示
注意输入的两个数相加后的结果可能会超过int和long的范围。
#include<stdio.h>
#include<string.h>
#include<math.h>
int transferletter(char c)
{
int n;
if(c=='A'||c=='a')
{
n=10;
}
if(c=='B'||c=='b')
{
n=11;
}
if(c=='C'||c=='c')
{
n=12;
}
if(c=='D'||c=='d')
{
n=13;
}
if(c=='E'||c=='e')
{
n=14;
}
if(c=='F'||c=='f')
{
n=15;
}
return n;
}
long long int transferToten(int n,char str[])
{
int len=strlen(str),product=1,t;
long int temp=0;
for(int i=len-1;i>=0;i--)
{
if(str[i]>='0'&&str[i]<='9')
{
temp+=(str[i]-'0') * product;
}
else
{
t=transferletter(str[i]);
temp+=t*product;
}
product=product*n;
}
return temp;
}
int main()
{
long long int a,b,c;
char n[10010];
while(scanf("%d %s %d",&a,n,&b)!=EOF)
{
c=transferToten(a,n);
char z[100010],num=0;
do
{
if(c%b<10)
{
z[num++]=c%b+'0';
}
else
{
switch(c%b)
{
case 10: z[num++]='A';break;
case 11: z[num++]='B';break;
case 12: z[num++]='C';break;
case 13: z[num++]='D';break;
case 14: z[num++]='E';break;
case 15: z[num++]='F';break;
}
}
c=c/b;
}while(c);
for(int i=num-1;i>=0;i--)
{
printf("%c",z[i]);
}
printf("\n");
}
return 0;
}