Blue Bridge Cup Basics practice hex to octal "string processing"

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
Tips the hexadecimal number is converted to a number of decimal digits, and then a binary number is converted into octal.

Idea: Convert first to binary and then into octal

Code:

 #include < stdio.h> #include <string.h> #define M 100005char S[m*4];char str[m];int ans[150005];int main () {int N;//char TEMP[6];SCANF ("%d", &n), while (n--) {scanf ("%s", str+1), memset (s, 0, sizeof (s)), int i, j, len = strlen (str+1) +1, temp; printf ("%d.. \ n ", Len); for (i = 1; i < Len; ++i) {if (Str[i] >= ' 0 ' &&str[i] <= ' 9 ') {temp = str[i]-' 0 ';} else temp = 10+str[i]-' A '; j = I*4;while (temp) {S[j] = temp%2+ ' 0 '; j--;temp/= 2;} while (J > (i-1)) {s[j--] = ' 0 ';}} printf ("%s", &s[1]), memset (ans, 0, sizeof (ANS)), int tot = 0;for (i = (len-1), i-3 >= 0; I-= 3) {ans[tot++] = (s[ i]-' 0 ') + (s[i-1]-' 0 ') *2+ (s[i-2]-' 0 ');} temp = 1;if (s[i]! = ' + ') {++tot;while (i > 0&&s[i]!= ') {ans[tot-1] + = (s[i]-' 0 ') *temp;temp *= 2;--i;}} while (tot>0&&ans[tot] = = 0)--tot;for (i = tot; I >= 0;--i) printf ("%d", Ans[i]);p UTS ("");////for (i = ())}r Eturn 0;} 


Blue Bridge Cup Basics practice hex to octal "string processing"

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.