Blue Bridge Cup Basics practice hex to octal

Source: Internet
Author: User

Basic practice hex to octal time limit: 1.0s memory limit: 512.0MB Problem Description
Given n hexadecimal positive integers, output their corresponding octal numbers.

Input Format
The first behavior of the input is 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.

output Format
Outputs n rows, each of which enters a corresponding octal positive integer.

   "Note
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

   the prompt
The hexadecimal number is converted to a number of decimal digits, and then a binary number is converted into octal. The hexadecimal number is converted to a binary number, and then the binary number is converted to octal. I just started to define a char character array that has been timed out, see the string defined by string on the net does not time out, depressed timeout code
#include <stdio.h> #include <string.h> #include <string> #include <math.h> #include < Algorithm> #define LL Long long#define PI atan (1.0) *4#define DD doublea#define MAX 100100#define mod 10007using Namespac    E Std;char a[max];int p[max*10];int main () {int n,m,j,i,len,sum;    scanf ("%d", &n);    while (n--) {scanf ("%s", &a);    Char s[max*10]= "";    Len=strlen (a);        for (i=0;i<len;i++)//conversion to binary {switch (A[i]) {case ' 0 ': strcat (S, "0000");        Case ' 1 ': strcat (S, "0001");        Case ' 2 ': strcat (S, "0010");        Case ' 3 ': strcat (S, "0011");        Case ' 4 ': strcat (S, "0100");        Case ' 5 ': strcat (S, "0101");        Case ' 6 ': strcat (S, "0110");        Case ' 7 ': strcat (S, "0111");        Case ' 8 ': strcat (S, "n");        Case ' 9 ': strcat (S, "1001");        Case ' A ': strcat (S, "1010");        Case ' B ': strcat (S, "1011");        Case ' C ': strcat (S, "1100"); CaSe ' D ': strcat (S, "1101");        Case ' E ': strcat (S, "1110");        Case ' F ': strcat (S, "1111");        Default:break;    }}//puts (s);    Strrev (s);    int Len1=strlen (s);    if (len1%3==1)//Because the binary to octal, is a three-bit binary into an octal {//So to complement, in front of 0 so that the length can be divisible by 3 strcat (S, "00");    len1+=2;    } else if (len1%3==2) {strcat (S, "0");    Len1+=1;    } int op=1;    for (i=0;i<len1-2;i+=3)//binary turn octal {p[op++]= (s[i]-' 0 ') *1+ (s[i+1]-' 0 ') *2+ (s[i+2]-' 0 ');    } int flag=1;    for (i=op;i>0;i--) {if (p[i]!=0) flag=0;    if (P[i]==0&&flag) continue;    printf ("%d", p[i]);    } printf ("\ n");  }return 0;}

AC Code:

#include <stdio.h> #include <string.h> #include <string> #include <math.h> #include < Algorithm> #define LL Long long#define PI atan (1.0) *4#define DD doublea#define MAX 100100#define mod 10007using Namespac    E std;int p[max*10];int Main () {int n,m,j,i,len,sum;    scanf ("%d", &n);    while (n--) {string a,s; A.resize (MAX);    Requires pre-allocated space scanf ("%s", &a[0]);    S= "";        Len=a.length ();//String length for (i=0;i<len;i++) {switch (A[i]) {case ' 0 ': s+= "0000";        Case ' 1 ': s+= "0001";        Case ' 2 ': s+= "0010";        Case ' 3 ': s+= "0011";        Case ' 4 ': s+= "0100";        Case ' 5 ': s+= "0101";        Case ' 6 ': s+= "0110";        Case ' 7 ': s+= "0111";        Case ' 8 ': s+= "n";        Case ' 9 ': s+= "1001";        Case ' A ': s+= "1010";        Case ' B ': s+= "1011";        Case ' C ': s+= "1100";        Case ' D ': s+= "1101";        Case ' E ': s+= "1110"; Case ' F ':s+= "1111";        Default:break;    }}//puts (s);    Strrev (s);    int Len1=s.length ();  if (len1%3==1) {s= "xx" +s;    00+s is to add 00 to the string before s+00 is to add 00 to the string after len1+=2;    } else if (len1%3==2) {s= "0" +s;    Len1+=1;    } int op=1;    for (i=0;i<len1-2;i+=3) {p[op++]= (s[i]-' 0 ') *4+ (s[i+1]-' 0 ') *2+ (s[i+2]-' 0 ') * *;    } int flag=1;    for (i=0;i<op;i++) {if (p[i]!=0) flag=0;    if (P[i]==0&&flag) continue;    printf ("%d", p[i]);    } printf ("\ n");  }return 0;}

  

Blue Bridge Cup Basics 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.