Hexadecimal conversion (9-degree oj -- big Integer Division)

Source: Internet
Author: User

Foreword alas, I always feel that I can only do my best to help my teachers. In the past year, students are still reliable, and their educational qualifications are highly reliable, so they cannot reflect all problems, but it can reflect most of the problems. At least I am a master's who often learns algorithms or other computer things late at night. Question [html] Description: converts a decimal non-negative integer with a maximum length of 30 digits into a binary number. Input: multiple groups of data. Each row contains a decimal non-negative integer of no more than 30 digits. (Note that there may be 30 decimal numbers, rather than an integer of 30bits) Output: the binary number corresponding to each row. Sample input: 0 1 3 8 sample output: 0 1 11 1000 the idea is obvious. A 30-bit integer cannot be expressed in the long int type, therefore, we use the data structure of char str [30] to store the big integer represented by this big integer string and convert it to another hexadecimal number step www.2cto.com. By adding each digit of the divisor, we can determine whether the value is greater 0 is used as the termination condition of the outer loop. The inner layer is divided by bits, it is stored in an array of characters. Assume that after the outer loop of str2 ends, the str2 array needs to be in reverse order (the principle is equivalent to using the stack data structure for hexadecimal conversion) the process of derivation on the calculation paper (0: 10 still learning Code cannot afford to hurt): AC code [cpp] # include <stdio. h> # include <string. h> # include <stdlib. h> # define DATA 40 # define MAX 200 void tenToBin (char * str); char bin [MAX]; int Main () {char str [DATA]; while (scanf ("% s", str )! = EOF) {tenToBin (str); puts (bin);} return 0;}/*** Description: converts a string integer to a binary string */void tenToBin (char * str) {int I, j, k, len, sum, d; char temp; // initialization parameter sum = 1; len = strlen (str); k = 0; memset (bin, 0, sizeof (bin); while (sum) {sum = 0; for (I = 0; I <len; I ++) {d = (str [I]-'0')/2; sum + = d; if (I = len-1) {bin [k ++] = (str [I]-'0') % 2 + '0 ';} else {str [I + 1] + = (str [I]-'0') % 2*10;} str [I] = d + '0 ';}} // reverse order for (I = 0, j = k-1; I <j; I ++, j --) {temp = bin [j]; bin [j] = bin [I]; bin [I] = temp ;}} /*************************************** * *********************** Problem: 1138 User: wangzhengyi Language: C Result: Accepted Time: 90 MS Memory: 908 kb ************************************** **************************/

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.