您的位置:首页 > 其它

九度OnlineJudge题目1118:数制转换

2014-03-20 10:04 204 查看
题目链接:http://ac.jobdu.com/problem.php?pid=1118

题目描述:
求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。

不同进制的表示符号为(0,1,...,9,a,b,...,f)或者(0,1,...,9,A,B,...,F)。

输入:
输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。a,b是十进制整数,2 =< a,b <= 16。

数据可能存在包含前导零的情况。

输出:
可能有多组测试数据,对于每组数据,输出包含一行,该行有一个整数为转换后的b进制数。输出时字母符号全部用大写表示,即(0,1,...,9,A,B,...,F)。

样例输入:
15 Aab3 7


样例输出:
210306


AC代码:

#include<stdio.h>
#include<string.h>

int main()
{
int a,b;
char str[100];
while(scanf("%d%s%d",&a,str,&b)!=EOF)
{
int temp=0;
int weight=1;
for(int i=strlen(str)-1;i>=0;i--)
{
if('0'<=str[i]&&str[i]<='9')
{
temp+=(str[i]-'0')*weight;
}
else if('A'<=str[i]&&str[i]<='F')
{
temp+=(str[i]-'A'+10)*weight;
}
else
{
temp+=(str[i]-'a'+10)*weight;
}
weight*=a;
}
char buf[100];
int size=0;
do
{
int x=temp%b;
buf[size++]=(x<10)?x+'0':x-10+'A';
temp/=b;
}while(temp);
while(size-->0)
{
printf("%c",buf[size]);
}
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: