70. 十六进制

Commando
#include <bits/stdc++.h>

using namespace std;

unsigned int parse(const string& s, int& i, bool& result) {
    stack<char> stack;

    while (i < s.size() && isxdigit(s[i])) {
        stack.push(s[i++]);
    }

    if (stack.empty()) {
        result = false;
        return 0;
    }

    result = true;

    unsigned int res = 0, base = 1, value;
    while (!stack.empty()) {
        char ch = stack.top();
        stack.pop();

        if (isdigit(ch)) value = ch - '0';
        else value = ch - 'a' + 10;

        res += value * base;
        base *= 16;
    }
    return res;
}

int main() {
    string s;
    cin >> s;

    bool hasHexcode = false;
    for (int i = 1; i < s.size();) {
        if (s[i - 1] == '0' && s[i] == 'x') {
            bool result = false;
            unsigned int value = parse(s, ++i, result);
            if (result) {
                if (hasHexcode) cout << ' ';
                cout << value;
                hasHexcode = true;
            }
        }
        else ++i;
    }
    if (!hasHexcode) cout << -1;
    cout << endl;
}
LzQuarter
#include<stdio.h>
int main(){
    char a, b, c;
    int ctr = 0;
    do{
        a = getchar();
        if(a == EOF)break;
        b = getchar();
        if(b == EOF)break;
        c = getchar();
        if(c == EOF)break;
        if(a == '0' && b == 'x' && (c >= '0' && c <= '9' || c >= 'a' && c <= 'f')){
            ungetc(c, stdin);
            ungetc(b, stdin);
            ungetc(a, stdin);
            if(ctr++)printf(" ");
            unsigned int a;
            scanf("%x", &a);
            printf("%u", a);
        }
        else{
            ungetc(c, stdin);
            ungetc(b, stdin);
        }
    }while(1);
    if(!ctr)printf("-1");
    printf("\n");
    return 0;
}
你当前正在回复 博客/题目
存在问题!