結果

問題 No.2385 Parse Integer with Radix
ユーザー Today03Today03
提出日時 2023-07-21 21:27:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 920 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 203,544 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 21:16:16
合計ジャッジ時間 2,326 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

long long change(string s, int b) {
  long long ret=0;
  int n=s.size();
  reverse(s.begin(),s.end());
  long long base=1;
  for (int i=0;i<n;i++) {
    ret+=base*(s[i]-'0');
    base*=b;
  }
  return ret;
}

long long change16(string s) {
  long long ret=0;
  int n=s.size();
  reverse(s.begin(),s.end());
  long long base=1;
  for (int i=0;i<n;i++) {
    ret+=base*('0'<=s[i]&&s[i]<='9'?(s[i]-'0'):(10+(s[i]-'a')));
    base*=16;
  }
  return ret;
}

int main() {
  int q;
  cin>>q;
  while (q--) {
    string s;
    cin>>s;
    if (s.size()==1) {
      cout<<s<<'\n';
    }
    else if (s[1]=='b') {
      s=s.substr(2);
      cout<<change(s,2)<<'\n';
    }
    else if (s[1]=='o') {
      s=s.substr(2);
      cout<<change(s,8)<<'\n';
    }
    else if (s[1]=='x') {
      s=s.substr(2);
      cout<<change16(s)<<'\n';
    }
    else {
      cout<<s<<'\n';
    }
  }
}
0