Hexadecimal conversion
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 23830 accepted submission (s): 13344
Problem description input a decimal number N and convert it to the r hexadecimal number output.
Input data contains multiple test instances. Each test instance contains two integers, n (32-bit integer) and R (2 <= r <= 16, r <> 10 ).
Output is the number of converted outputs for each test instance. Each output occupies one row. If the R value is greater than 10, the corresponding digital rule is in hexadecimal notation (for example, 10 is represented by a, and so on ).
Sample Input
7 223 12-4 3
Sample output
1111B-11
Question ..
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int main(){ int n, m, s, i; char c, a[1000]; while(scanf("%d%d",&n,&m)!=EOF) { i=0; if(n<0) printf("-"); while(n) { s=abs(n%m); if(s>=0 && s<=9 || s<0 && s>=-9) a[i]='0'+s; if(s>9) { c='A'+s-10; a[i]=c; } n=n/m; i++; } for(int j=i-1; j>=0; j--) printf("%c", a[j]); printf("\n"); } return 0;}