Number Converter
时间限制: 1秒 内存限制: 64M
Problem Description
As we know 3 = 11 if we think about the binary representation . Now let’s make a number converter. Given you two integers n (0 <= n <=10^9) and k (2 <= k <= 36), please output the representation of the n in the radix specified by k. And in the representation, you should use A, B, … to represent 10, 11, …
Input
This problem contains multiple test cases.
For each test case, there are two integers n and k.
Output
For each test case, please output the representation of the n in the radix specified by k.
Sample Input
3 2
4 3
15 16
Sample Output
11
11
F
一开始交上去之后就出现了re,找了半天才发现是二进制的问题,忘记了二进制数字非常长的特点
方法很简单,短除取余,仅此而已
- #include<stdio.h>
- #include<string.h>
- //#define input (freopen("text.txt","r",stdin));
- int main()
- {
- //input;
- char ch[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0";//最后的0是由于误认为是数组下标越界造成re而加上的,应该没用
- char ans[60];//(10^9)2 == 111011100110101100101000000000,共30位
- int i, n, k;
- while ( scanf("%d%d",&n,&k) != -1 )
- {
- memset(ans,0,sizeof(ans));
- i = 54;
- do{
- ans[i] = ch[n%k];
- n /= k;
- i --;
- }while( n );
- printf("%s\n",(ans + i + 1));
- }
- return 0;
- }