1501091239-Blue Bridge cup-basic practice hex to octal

Source: Internet
Author: User

Basic practice hex to octal time limit: 1.0s memory limit: 512.0MBThe 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 indicates that the hexadecimal number is converted to a binary number, and then converted to octal by a number of digits. Thinking of solving problems converting 16 into octal, you can now convert the 16 binary to binary, because a 16 binary can be represented by a binary four bit , and then the binary is converted to octal, because The three-digit binary can be represented by an octal . Example: Hex A1Binary 1010 0001 octal 010 000 001 (if the binary number is not 3 divisible, add 0 to the left)so 16-->2, you can follow, 2-->8 when you need to reverse, so enough to add 0 on the leftCode
#include <stdio.h> #include <math.h> #include <string.h>int temp[410000];char jz16[110000];int Answer[410000];int SwitchA (char a) {if (a>= ' 0 ' &&a<= ' 9 ')    return a-' 0 '; else    return a-' a ' +10;} int main () {int n;int len;int now;int i,j,k;int sum;scanf ("%d", &n), while (n--) {scanf ("%s", jz16); Len=strlen (JZ16); for (i=0;i<len;i++) {Now=switcha (Jz16[i]), for (j= (i+1) *4-1;j>=i*4;j--) {temp[j]=now%2;now/=2;}} A hexadecimal becomes 4 binary followed by memset (answer,0,sizeof (answer)); for (i=len*4-1,sum=0;i>=0;i-=3,sum++)//Three binary into an octal inverse For     (j=i,k=0;j>i-3&&j>=0;j--, k++)        answer[sum]+=temp[j]* (int) pow (2,k), for (i=sum;; i--)    if (answer[i]!=0)        break;//Note Do not output the preceding redundant 0 for (i=i;i>=0;i--)    printf ("%d", Answer[i]);p rintf ("\ n");} return 0;}


1501091239-Blue Bridge cup-basic practice hex to octal

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.