tsinsen_a1014. Binary Conversion 5

Source: Internet
Author: User

The problem describes the given n hexadecimal positive integers, outputting their corresponding octal numbers. Enter the first behavior of the input format as a positive integer n (1<=n<=10).
Next n rows, each line a string of 0~9, uppercase letters A~F, representing the hexadecimal positive integer to be converted, each hexadecimal number is not more than 100000. The output format outputs n rows, and each behavior enters a corresponding octal positive integer. Note that the hexadecimal number entered does not have a leading 0, such as 012A.
The octal number of the output cannot have a leading 0. Sample Input 2
39
123ABC Sample Output 71
4435274 hints

The hexadecimal number is converted to a number of decimal digits, and then a binary number is converted into octal.


#include <stdio.h>//#include <iostream> #include <string.h>//using namespace std;//comment section was used to debug the const int MAXN = 400000 + 5;const int maxn2 = 100000 + 5;char s[maxn2];int D[MAXN], b[4], _count;int flag;void init () {Me Mset (S, ' n ', sizeof (s)), memset (d, 0, sizeof (d)), memset (b, 0, sizeof (b)); _count = 0;} int c_d (int ch) {if (ch >= ' a ' && ch <= ' F ') return (CH-' a ' +10); else return (ch-48);}   void Chu (int x, int* b, int cs) {int BCS = x, sz, cnt = 4;//memset (b, 0, sizeof (b));  Why this initialization does not work b[0] = b[1] = b[2] = b[3] = 0; This initialization succeeds while (SZ = (Bcs/cs)) {b[--cnt] = Bcs%cs;bcs = sz;//cout << "___" << cnt << "____" << Endl;}        B[--CNT] = Bcs%cs; The problem is it's out here. If the number of digits that are calculated does not meet 4 digits, you should deal with it.//int BZ = 3-cnt; BZ indicates that the insufficient number of digits is just the CNT value//cout << "insufficient digits" << BZ << Endl; /*for (int i = bz-1, j = 3; I >=0 && J >= BZ && cnt! = 0; I--, j--) {//shift number B[J-BZ] = b[cnt];cnt--; After the}//shift is over, fill all remaining slots with 0 for (inti = 3; I >= 4-bz; i--) B[i] = 0;*///This does not need to shift the operation,,, is their own thinking is not clear}int BQ (int x) {return 3-x%3;} int main () {int N, sum;while (scanf ("%d", &n)! = EOF) {for (int i = 0; i < N; i++) {init (); scanf ('%s ', s); for (int j = Strlen (s)-1; J >= 0; j--) {//memset (d, 0, sizeof (d)), flag = 0;chu (C_d (S[j]), B, 2),//cout << s[j] << "-----" << c_d (S[j]) &lt ;< "B[0"--> B[3] "<< b[0] <<" "<< b[1] <<" "<< b[2] <<" "<< b [3] << endl;for (int k = 3; k >= 0; k--)//Note I j K variables in these for loops {//cout << d[_count] << "* * * *"     ;d [_count++] = b[k]; cout << "<< b[i]" << b[i] << "_count" << _count << "" << D[_cou Nt-1] << ".  Above.. "<< Endl;}}  _count = _count + BQ (_count); Count the number of array elements required//cout << _count << "----___----" << endl;//for (int i = 0; i < _count; i++) cout << "<< d[i" << "";//cout &Lt;< Endl;int _cs = _count/3;for (int j = 0; J < _cs; J + +) {sum = d[_count-1]*4 + d[_count-2]*2 + d[_count-3];_count-   = 3;    printf ("%d", sum); Note that you cannot have a leading 0 or else you should use while to write a judgment to be determined! if (!flag &&!sum) continue;else {flag = 1; printf ("%d", sum);}} printf ("\ n");}} return 0;} My brother, I almost spit blood on this problem.

In the final analysis, I am too water ....

tsinsen_a1014. Binary Conversion 5

Related Article

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.