(HDUStep 1.2.6) decimal system (any hexadecimal to decimal), hdustep1.2.6

Source: Internet
Author: User
Tags binary to decimal decimal to binary

(HDUStep 1.2.6) decimal system (any hexadecimal to decimal), hdustep1.2.6



Decimal system
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3622 Accepted Submission (s): 1419
Problem DescriptionAs we know, we always use the decimal system in our common life, even using the computer. if we want to calculate the value that 3 plus 9, we just import 3 and 9. after calculation of computer, we will get the result of 12.
But after learning <The Principle Of Computer>, we know that the computer will do the calculation as the following steps:
1 computer change the 3 into binary formality like 11;
2 computer change the 9 into binary formality like 1001;
3 computer plus the two number and get the result 1100;
4 computer change the result into decimal formality like 12;
5 computer export the result;

In the computer system there are other formalities to deal with the number such as hexadecimal. now I will give several number with a kind of change method, for example, if I give you 1011 (2), it means 1011 is a number in the binary system, and 123 (10) means 123 if a number in the decimal system. now I will give you some numbers with any kind of system, you guys shoshould tell me the sum of the number in the decimal system.
InputThere will be several cases. the first line of each case contains one integers N, and N means there will be N numbers to import, then there will be N numbers at the next N lines, each line contains a number with such form: X1 .... Xn. (Y), and 0 <= Xi <Y, 1 <Y <= 10. I promise you that the sum will not exceed the 100000000, and there will be at most 100 cases and the 0 <N <= 1000.
OutputThere is only one line output case for each input case, which is the sum of all the number. The sum must be expressed using the decimal system.
Sample Input
Sample Output
SourceHDU 2007-6 Programming Contest

Question Analysis:

It is actually a question of any hexadecimal or decimal conversion. There are also questions about the conversion of decimal to any meaning in hexadecimal notation. To do this, you only need to write a function toTen () that can be converted to any decimal. Because the decimal person and C ++ have the itoa () function. Note that if you want to convert a string to a number, you can use the atoi () function.

Analysis on the principle of conversion from any base to decimal:

1. Convert binary, octal, and hexadecimal to decimal.
There is a formula: binary number, octal number, hexadecimal number of the numbers respectively to their respective base (N-1) to the power, and the sum of the sum is the corresponding decimal number. Single digit, N = 1; ten digits, N = 2... example:
110B = 1*2 to the power of 2 + 1*2 to the power of 1 + 0*2 to the power of 0 = 0 + 4 + 2 + 0 = 6D
110Q = 1X8 power 2 + 1X8 power 1 + 0x8 power 0 = 64 + 8 + 0 = 72D
110 H = 1*16 power 2 + 1*16 power 1 + 0*16 power 0 = 256 + 16 + 0 = 272D
2. Convert decimal to binary, octal, and hexadecimal
The method is the same, that is, the integer part uses the Division-based remainder algorithm, and the fractional part uses the multiplication-based integer method, then, the integer and the decimal part are combined into a number as the final result of the conversion.
For example, refer to page 16 of Level 4 Guidance.
3. Convert binary data to other data types
3-1 binary to octal: Starting from the decimal point, the integer part is left, And the decimal part is right. Each three decimal digits are represented by a group of octal digits, and less than three digits are supplemented by 0,
Is a representation of the corresponding Octal numbers.
010110.001100B = 26.14Q
Octal to binary, and vice versa.
3-2 binary to decimal: see 1
3-3 binary to hexadecimal: Starting from the decimal point, the integer part is left, And the decimal part is right. each four-digit binary is represented by a group of hexadecimal digits,
If the number of digits is less than four, the value 0 indicates a hexadecimal number.
00100110.00010100B = 26.14 H
Convert decimal to hexadecimal
To convert the decimal to the hexadecimal notation, you only need to divide the weights of the hexadecimal notation to obtain the remaining number. The first remainder is the single digit, and the second remainder is the ten digits, the maximum number of deldeters is the maximum number of digits until the deldeviation value is smaller than the weight value.
I. Convert decimal to binary
For example, convert 55 to binary
2 | 55
27-1 bit
13-1 second place
6-1, third place
3-0 fourth digit
1-1 fifth place
The last divisor 1 is the seventh digit, that is, 110111
Ii. Convert decimal to octal
For example, convert 5621 to octal
8 | 5621
702-5 first (single)
87-6 second place
10−7, third place
1-2 fourth place
The last eight digits: 127658
Iii. hexadecimal notation
For example, convert 76521 to hexadecimal.
16 | 76521
4726-5 first (single)
295-6 second place
18-6 third place
1-2 fourth place
The last 1276516
Relationship between binary and hexadecimal
Binary 0000 0001 0010 0011 0100 0101 0110 0111
Hexadecimal 0 1 2 3 4 5 6 7
Binary 1000 1001 1010 1011 1100 1101 1110 1111
Hexadecimal 8 9 a (10) B (11) c (12) d (13) e (14) f (15)
The binary number of four digits can be used to represent a hexadecimal system. For example, 3A16 is converted to binary:
3 is 0011, A is 1010, and merged to 00111010. You can remove the leftmost 0 to 1110102.
To convert the binary to hexadecimal, you only need to separate the digits of the binary from the right to the left, and compare the units with the hexadecimal values.
Relationship between binary and octal
Binary 000 001 010 011 100 101 110 111
Octal 0 1 2 3 4 5 6 7
The relationship between binary and octal is similar to the relationship between binary and hexadecimal. The values are represented by three binary numbers, ranging from 0 to 7. To convert 51028 to binary, 5 is 1010010000102 to, 0 is, 2 is 010, And the binary of these numbers is merged into, that is, the binary value.
To convert binary data into octal values, separate the digits of the binary data from the right to the left, and compare the units to the octal values.

The code is implemented as follows:

# Include <iostream> # include <cstdio> # include <string> # include <cstdlib> # include <stdlib. h> # include <stdio. h> using namespace std;/*** arbitrary hexadecimal conversion method */long toTen (char a [], int bit) {int length = strlen (a); int I, B = 1, sum = 0; // I to subscript array, note that the start and end ranges from (I = length-1; I> = 0; I --) {if (a [I]> = 'A ') {sum + = (a [I]-'A' + 10) * B; B * = bit;} else {sum + = (A [I]-'0 ') * B; B * = bit;} return sum;} int main () {Int n; while (scanf ("% d", & n )! = EOF) {int I; int sum = 0; for (I = 0; I <n; ++ I) {int a, B; char left, right; cin> a> left> B> right; char number [1003]; itoa (a, number, 10); // converts decimal to any hexadecimal format, here it is mainly applicable to converting a into a string. note that C ++ sum + = toTen (number, B);} printf ("% d \ n", sum);} return 0 ;}

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.