[Blue Bridge Cup] BASIC-12 from hexadecimal octal

Subject description:

Given a positive integer n hexadecimal, octal output thereof corresponds.

[Note] first convert a hexadecimal number to a decimal number, and then converted by a binary number to octal.

Enter a description:

The first act input a positive integer n (1 <= n <= 10). Next n lines, each line of a character string from 0 to 9, capital letters A ~ F composition indicates hexadecimal to convert a positive integer, each hexadecimal number length does not exceed 100,000.

Output Description:

N output lines, each act input octal positive integer corresponding.

[Note] enter the hexadecimal number does not have leading 0, such as 012A. Octal number also can not have a leading zero.

Sample input:

2
39
123ABC

Sample output :

71
4435274

Problem-solving ideas:

This is not a water problem wailing, Blue Bridge cup of data on nm ridiculously large. This problem is not binary conversions? First converter 10 into a hexadecimal number converted to decimal octal. I started with a long long of 64 should be enough to think of it, a single test sample after wave, click submit directly to WA, then I looked at the evaluation information, find the biggest test data is 100,000 (I try to The sample posted but CSDN article prompted exceed the maximum word limit). . . . Nothing wdnmd (I'll take you to play) were divided into short short binary conversion and output.

AC Code: WA Code:

WA data without error code testing within the scope of long long, only the data entered will be ridiculously large error.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll XToDec(string s,int x)
{
    ll ans = 0;
    for(int i = 0; i < s.length(); i++)
    {
        ans = ans*x + (isdigit(s[i]) ? (s[i]-'0') : (s[i]-'A'+10));
    }
    return ans;
}

string DecToX(ll n,int x)
{
    string ans = "";
    do{
        int t = n%x;
        n /= x;
        ans += ((t>=0 && t<=9) ? (t+'0') : (t-10+'A'));
    }while(n != 0);
    reverse(ans.begin(),ans.end());
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll n;
    cin >> n;
    while(n--)
    {
        string s;
        cin >> s;
        cout << DecToX(XToDec(s,16),8) << endl;
    }
    return 0;
}

AC Code:

#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
const int maxn = 100001;

void putans(char *s)
{
    int ans = 0;
    Up(i,0,2)
    {
        ans = ans*16 + (isdigit(s[i]) ? (s[i]-'0') : (s[i]-'A'+10));
    }
    printf("%04o",ans);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    while(n--)
    {
        char s[maxn];
        cin >> s;
        int len = strlen(s);
        int flen = (len-1)%3;
        int ans = 0;
        Up(i,0,flen)
        {
            ans = ans*16 + (isdigit(s[i]) ? (s[i]-'0') : (s[i]-'A'+10));
        }
        printf("%o",ans);
        for(int i = flen+1; i < len; i += 3)
        {
            putans(s+i);
        }
        printf("\n");
    }
    return 0;
}

 

Published 415 original articles · won praise 118 · Views 400,000 +

Guess you like

Origin blog.csdn.net/weixin_42449444/article/details/102945576