Number conversion (9 degree oj Question 44)

Source: Internet
Author: User

Question [html] Question Description: Calculate the conversion of any two non-negative integers in different hexadecimal notation (2-decimal ~ Hexadecimal), the given integer is within the range that long can express. Different hexadecimal notation is ,..., 9, a, B ,..., f) or (0, 1 ,..., 9, A, B ,..., F ). Input: enter only one row, which contains three integers a, n, and B. A indicates that the n after it is an a-hexadecimal integer, and B indicates that you want to convert a-hexadecimal integer n to a-hexadecimal integer. A and B are decimal integers, 2 = <a, B <= 16. Data may contain leading zeros. Output: There may be multiple groups of test data. For each group of data, the output contains one row, and the row has an integer that is converted to the hexadecimal number. Uppercase letters are all used for output, that is, (0, 1,..., 9, A, B,..., F ). Sample input: 15 Aab3 7 sample output: 210306 tip: Different hexadecimal integers can be expressed using strings. The concept of this question is a digital conversion problem, examining the conversion algorithms between different hexadecimal systems. The conversion between different hexadecimal formats can be converted to decimal by using decimal as the intermediary R: multiplied by the Weight Sum Method: decimal to R: divided by the base to obtain the remainder method, the sequence stack is used as the data structure to help pay attention to the leading 0 problem. Therefore, strings cannot be output directly because a = B, the AC code [cpp] # include <stdio. h> # include <stdlib. h> # include <string. h> # define MAX 200 struct stack {char data [MAX]; int top ;}; long int switchToTen (int a, char * s); int main () {long int a, B, n; char str [MAX]; while (scanf ("% ld % s % ld", & a, str, & B )! = EOF) {n = switchToTen (a, str); if (n = 0) {printf ("0");} else {int temp; struct stack * s = (struct stack *) malloc (sizeof (struct stack); s-> top = 0; while (n) {temp = n % B; if (temp> = 0 & temp <= 9) {s-> data [s-> top ++] = temp + '0 ';} else {s-> data [s-> top ++] = temp-10 + 'a';} n/= B;} while (s-> top) {printf ("% c", s-> data [-- s-> top]) ;}} printf ("\ n");} return 0 ;} long int switchToTen (int a, char * s) {int I, B; long int n, c; for (n = 0, c = 1, I = strlen (s) -1; I> = 0; I --) {if (s [I]> = 'A' & s [I] <= 'Z ') B = s [I]-55; else if (s [I]> = 'A' & s [I] <= 'Z ') B = s [I]-87; else B = s [I]-'0'; n + = B * c; c * = a;} return n ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.