結果

問題 No.2385 Parse Integer with Radix
ユーザー weakenweaken
提出日時 2024-01-31 08:37:38
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,162 bytes
コンパイル時間 3,469 ms
コンパイル使用メモリ 247,852 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-01-31 08:37:43
合計ジャッジ時間 4,645 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef DEBUG_
#include <compe/debug.hpp>
#else
#define dump(...)
#endif

#define FastIO cin.tie(nullptr), ios_base::sync_with_stdio(false);
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define output(msg) cout << (msg) << '\n'
#define die(msg)         \
  do {                   \
    cout << msg << endl; \
    exit(0);             \
  } while (0)
#define all(k) k.begin(), k.end()
#define INFi 1 << 30
#define INFll 1LL << 60

template <typename T> bool chmax(T& a, const T& b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> bool chmin(T& a, const T& b) {
  return ((a > b) ? (a = b, true) : false);
}

using llint = long long int;
using ullint = unsigned long long int;

int checkType(string s) {
  // decimal
  if (s.size() <= 2) {
    return 0;
  }

  string type = s.substr(0, 2);
  if (type == "0b") {
    return 1;  // binary
  } else if (type == "0o") {
    return 2;  // octal
  } else if (type == "0x") {
    return 3;  // hex
  } else {
    return 0;
  }
}

ullint calc_binary(const string& s) {
  ullint res{};
  for (auto ch : s) {
    res *= 2;
    if (ch == '1') {
      res += 1;
    }
  }
  return res;
}

ullint calc_octal(const string& s) {
  ullint res{};
  for (auto ch : s.substr(2)) {
    res *= 8;
    res += ch - '0';
  }
  return res;
}

ullint ch2hex(char ch) {
  if ('0' <= ch && ch <= '9') return ch - '0';
  if (ch == 'a') return 10;
  if (ch == 'b') return 11;
  if (ch == 'c') return 12;
  if (ch == 'd') return 13;
  if (ch == 'e') return 14;
  return 15;
}
ullint calc_hex(const string& s) {
  ullint res{};
  for (auto ch : s.substr(2)) {
    res *= 16;
    res += ch2hex(ch);
  }

  return res;
}

int main() {
  FastIO;
  int q;
  cin >> q;

  rep(_, q) {
    string s;
    cin >> s;

    switch (checkType(s)) {
      case 0:  // decimal
        output(s);
        break;
      case 1:  // binary
        output(calc_binary(s));
        break;
      case 2:  // octal
        output(calc_octal(s));
        break;
      case 3:  // hex
        output(calc_hex(s));
        break;
      default:
        throw runtime_error(":^)");
    }
  }
}
0