結果

問題 No.2385 Parse Integer with Radix
ユーザー yansi819yansi819
提出日時 2024-03-18 11:42:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 4,572 ms
コンパイル使用メモリ 271,304 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-18 11:42:26
合計ジャッジ時間 5,507 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 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>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;

int main() {
  int Q; cin >> Q;
  while (Q--) {
    string S; cin >> S;
    int N = S.size();
    if (N > 2 && S.substr(0, 2) == "0b") {
      string S1 = S.substr(2);
      reverse(S1.begin(), S1.end());
      ll p = 1, ans = 0;
      for (int i = 0; i < S1.size(); i++) {
        ans += p * (S1[i] - '0');
        p *= 2;
      }
      cout << ans << endl;
    } else if (N > 2 && S.substr(0, 2) == "0o") {
      string S1 = S.substr(2);
      reverse(S1.begin(), S1.end());
      ll p = 1, ans = 0;
      for (int i = 0; i < S1.size(); i++) {
        ans += p * (S1[i] - '0');
        p *= 8;
      }
      cout << ans << endl;
    } else if (N > 2 && S.substr(0, 2) == "0x") {
      map<char, int> mp = { {'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5}, {'6', 6}, {'7', 7}, 
        {'8', 8}, {'9', 9}, {'a', 10}, {'b', 11}, {'c', 12}, {'d', 13}, {'e', 14}, {'f', 15}};
      string S1 = S.substr(2);
      reverse(S1.begin(), S1.end());
      ll p = 1, ans = 0;
      for (int i = 0; i < S1.size(); i++) {
        ans += p * mp[S1[i]];
        p *= 16;
      }
      cout << ans << endl;
    } else {
      cout << S << endl;
    }
  }
  return 0;
}
0