結果
問題 | No.2385 Parse Integer with Radix |
ユーザー |
![]() |
提出日時 | 2024-01-31 08:37:38 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,162 bytes |
コンパイル時間 | 3,039 ms |
コンパイル使用メモリ | 246,616 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-28 10:20:14 |
合計ジャッジ時間 | 3,721 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 11 |
ソースコード
#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 << 60template <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) {// decimalif (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: // decimaloutput(s);break;case 1: // binaryoutput(calc_binary(s));break;case 2: // octaloutput(calc_octal(s));break;case 3: // hexoutput(calc_hex(s));break;default:throw runtime_error(":^)");}}}