NOJ 1012 hexadecimal conversion (decimal to any hexadecimal), noj1012
Question:
Hexadecimal conversionTime limit (Common/Java):1000 MS/3000 MS running memory limit: 65536 KByte
Total submissions: 1819 pass the test: 525
Description
Converts a decimal number N to an R-base number output, 2 ≤ R ≤ 16, R = 10.
Input
Multiple rows. The first line indicates the total number of groups of data in the following table. Each subsequent row contains two integers, N and R, separated by spaces.-100000 ≤ N ≤ 100000,2 ≤ R ≤ 16, R = 10.
Output
Multiple rows. Each row provides the converted R base number.
Sample Input
3
7 2
23 12
-4 3
Sample output
111
1B
-11
Prompt
Question Source
GUOJ
Question Analysis:
Simple question. Use itoa to convert decimal to any base ..
The Code is as follows:
/**. Cpp ** Created on: March 31, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <cstdlib> using namespace std; const int maxn = 32; char numbers [maxn]; void toUpper (char str []) {int I = 0; while (str [I]! = '\ 0') {if (str [I]> = 'A' & str [I] <= 'Z') {str [I]-= 32 ;} I ++ ;}} int main () {int t; scanf ("% d", & t); while (t --) {int n; int base; scanf ("% d", & n, & base); bool flag = false; if (n <0) {n =-n; flag = true ;} itoa (n, numbers, base); toUpper (numbers); if (flag = true) {printf ("-");} printf ("% s \ n ", numbers);} return 0 ;}