进制转换-acm
进制转换
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R != 10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Example Input
7 2
23 12
-4 3
Example Output
111
1B
-11
实验代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
int m, i, op;
char s[320];
long long int f;
while(scanf("%d%d",&n,&m)!=EOF)
{
op=0;
if(n==0)
printf("0");
if(n<0)
{
printf("-");
n=-n;
}
while(n>0)
{
f=n%m;
if(f>=10)
{
s[op++]=f-10+'A';
}
else
{
s[op++]=f+'0';
}
n-=f;
n/=m;
}
for(i=op-1; i>=0; i--)
{
printf("%c",s[i]);
}
printf("\n");
}
return 0;
}
注意事项
1.
s[op++]=f+’0’;
s[op++]=f-10+’A’;
使用要注意数字和字母的存在形式;
2.
if(n==0)
printf("0");
if(n<0)
{
printf("-");
n=-n;
}
for(i=op-1; i>=0; i--)
{
printf("%c",s[i]);
}
printf("\n");
注意细节和顺序;
3.虽然10不存在,但也要考虑,取模为10的情况。
http://blog.leanote.com/post/freewalk/Markdown-%E8%AF%AD%E6%B3%95%E6%89%8B%E5%86%8C#title