题目
Problem Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample Input
7 2
23 12
-4 3
Sample Output
111
1B
-11
题目意思比较明确,可能区别就在存放转化后的每位数字上吧;用数组或者链表都能轻松解决。理论上其他大部分的存储结构都能使用。。。。
AC代码(数组)
#include<stdio.h>
int main()
{
char w[6]= {'A','B','C','D','E','F'};
char a[1000];
int n,r,i,z;
while(~scanf("%d %d",&n,&r))
{
i=0,z=0;
if(n<0)
{
n=-n;
z=1;
}
while(n)
{
a[i++]=n%r;
n/=r;
}
if(z==1)
printf("-");
for(int j=i-1; j>=0; j--)
{
if(a[j]>9)
printf("%c",w[a[j]-10]);
else
printf("%d",a[j]);
}
putchar('\n');
}
return 0;
}
AC代码(链表)
#include<cstdio>
#include<iostream>
#include<list>
using namespace std;
int main()
{
char w[6]= {'A','B','C','D','E','F'};
int n,r,z;
while(cin>>n>>r)
{
list<int> a;
z=0;
if(n<0)
{
n=-n;
z=1;
}
while(n)
{
a.push_front(n%r);
n/=r;
}
if(z==1)
cout<<"-";
list<int>::iterator it;
for(it=a.begin();it!=a.end();it++)
{
if(*it>9)
cout<<w[*it-10];
else
cout<<*it;
}
cout<<endl;
}
return 0;
}